def setKeyring(): try: #HACK set keyring backend explicitly if platform.system().startswith("Windows"): import keyring # @Reimport keyring.set_keyring(keyring.backends.Windows.WinVaultKeyring() ) # @UndefinedVariable elif platform.system() == 'Darwin': import keyring # @Reimport keyring.set_keyring(keyring.backends.OS_X.Keyring()) else: # Linux try: import keyring # @Reimport config.logger.debug("controller: keyring.get_keyring() %s", keyring.get_keyring()) # test if secretstorage dbus is working import secretstorage # @Reimport @UnresolvedImport bus = secretstorage.dbus_init() _ = list(secretstorage.get_all_collections(bus)) # if yes, import it import keyring.backends.SecretService # @Reimport ss_keyring = keyring.backends.SecretService.Keyring() if ss_keyring.priority: import keyring # @Reimport # if priority is not 0, we set it as keyring system keyring.set_keyring(ss_keyring) except Exception as e: pass #config.logger.error("controller: Linux keyring Exception %s",e) #config.logger.debug("keyring: %s",str(keyring.get_keyring())) except Exception as e: config.logger.error("controller: keyring Exception %s", e)
def ready(self): try: # this will throw an exception of all the dependencies didn't get setup correctly import secretstorage bus = secretstorage.dbus_init() list(secretstorage.get_all_collections(bus)) return True except Exception: return False
def get_gnome_keyrings(): connection = secretstorage.dbus_init() keyrings = {} for collection in secretstorage.get_all_collections(connection): keyring_name = collection.collection_path keyrings[keyring_name] = [ get_item_info(i) for i in list(collection.get_all_items()) ] return keyrings
def hackng(): bus = secretstorage.dbus_init() for keyring in secretstorage.get_all_collections(bus): for item in keyring.get_all_items(): attr = item.get_attributes() if attr and 'username_value' in attr: print('[%s] %s: %s = %s' % (keyring.get_label(), item.get_label(), attr['username_value'], item.get_secret())) else: print( '[%s] %s = %s' % (keyring.get_label(), item.get_label(), item.get_secret()))
def priority(cls): with ExceptionRaisedContext() as exc: secretstorage.__name__ if exc: raise RuntimeError("SecretStorage required") if not hasattr(secretstorage, 'get_default_collection'): raise RuntimeError("SecretStorage 1.0 or newer required") try: bus = secretstorage.dbus_init() list(secretstorage.get_all_collections(bus)) except exceptions.SecretStorageException as e: raise RuntimeError("Unable to initialize SecretService: %s" % e) return 5
def priority(cls): with ExceptionRaisedContext() as exc: secretstorage.__name__ if exc: raise RuntimeError("SecretStorage required") if secretstorage.__version_tuple__[0] < 3: raise RuntimeError("SecretStorage 3.0 or newer required") try: with closing(secretstorage.dbus_init()) as connection: list(secretstorage.get_all_collections(connection)) except exceptions.SecretStorageException as e: raise RuntimeError("Unable to initialize SecretService: %s" % e) return 5
def get_collection(name): connection = secretstorage.dbus_init() collections = secretstorage.get_all_collections(connection) collection = next((collection for collection in collections if collection.get_label() == name), None) if collection == None: print('keyring "%s" not found, creating...' % name) collection = secretstorage.create_collection(connection, name) collection.unlock() return collection
def priority(cls): with ExceptionRaisedContext() as exc: secretstorage.__name__ if exc: raise RuntimeError("SecretStorage required") if not hasattr(secretstorage, 'get_default_collection'): raise RuntimeError("SecretStorage 1.0 or newer required") try: bus = secretstorage.dbus_init() list(secretstorage.get_all_collections(bus)) except exceptions.SecretServiceNotAvailableException as e: raise RuntimeError( "Unable to initialize SecretService: %s" % e) return 5
def read_secret_storage(): bus = secretstorage.dbus_init() data = [] for keyring in secretstorage.get_all_collections(bus): for item in keyring.get_all_items(): if item.is_locked(): item.unlock() attr = item.get_attributes() if attr and 'username_value' in attr: data.append((keyring.get_label(), item.get_label() + ": " + attr['username_value'], item.get_secret().decode("utf-8"))) else: data.append((keyring.get_label(), item.get_label(), item.get_secret().decode("utf-8"))) return data
def hackng(): bus = secretstorage.dbus_init() for keyring in secretstorage.get_all_collections(bus): for item in keyring.get_all_items(): attr = item.get_attributes() if attr and 'username_value' in attr: print('[%s] %s: %s = %s' % ( keyring.get_label(), item.get_label(), attr['username_value'], item.get_secret() )) else: print('[%s] %s = %s' % ( keyring.get_label(), item.get_label(), item.get_secret() ))
def get_collections(unlock=False, connection=None): "get all collections, unlock them if requested" collections = [] if not connection: connection = dbus_init() count = 0 for count, col in enumerate(get_all_collections(connection)): if col.is_locked(): if not unlock: continue if col.unlock(): # failed to unlock log_secret.warning('Failed to unlock %s', col.collection_path) continue collections.append(col) if not connection: connection.close() log_secret.debug("No dbus connection for getting collections") else: log_secret.debug('Found %d collections', count) return collections
def get_secret_key(keyring, key): bus = secretstorage.dbus_init() keyrings = [ collection for collection in secretstorage.get_all_collections(bus) if collection.get_label() == keyring ] if len(keyrings) != 1: raise RuntimeError("Expect 1 keyring for the given label {}," " found {} keyring: {}".format( keyring, len(keyrings), [k.get_label() for k in keyrings])) keyrings[0].ensure_not_locked() secrets = [ item for item in keyrings[0].get_all_items() if item.get_label() == key ] if len(secrets) != 1: raise RuntimeError("Expect 1 key for the given key {} in keyring {}," " found {} items: {}".format( key, keyrings[0].get_label(), len(secrets), [i.get_label() for i in secrets])) return secrets[0].get_secret()
def parse(self): """Direct import from the Gnome keyring using Dbus.""" if not SECRETSTORAGE: raise ImportError(name='secretstorage') keys = self.invkeys() connection = secretstorage.dbus_init() for collection in secretstorage.get_all_collections(connection): group = collection.get_label() if self.prefix not in ('', group): continue collection.unlock() for item in collection.get_all_items(): entry = {} entry['group'] = group entry['title'] = item.get_label() entry['password'] = item.get_secret().decode('utf-8') entry['modified'] = item.get_modified() entry['created'] = item.get_created() for key, value in item.get_attributes().items(): entry[keys.get(key, key)] = value self.data.append(entry)
def test_all_collections(self): labels = map(Collection.get_label, get_all_collections(self.bus)) self.assertIn(self.collection.get_label(), labels)
def run(self): items = [] visited = set() try: import dbus import secretstorage import datetime except ImportError as e: self.error('libsecret: {0}'.format(e)) return [] for uid, session in homes.sessions(): try: # List bus connection names bus = dbus.bus.BusConnection(session) if 'org.freedesktop.secrets' not in [ str(x) for x in bus.list_names() ]: continue except Exception: self.error(traceback.format_exc()) continue collections = None try: # Python 2.7 collections = list( secretstorage.collection.get_all_collections(bus)) except Exception: pass if not collections: try: # Python 3 from jeepney.integrate.blocking import connect_and_authenticate make_auth_external.uid = uid bus = connect_and_authenticate(session) collections = secretstorage.get_all_collections(bus) except Exception: self.error(traceback.format_exc()) continue for collection in collections: if collection.is_locked(): continue label = collection.get_label() if label in visited: continue visited.add(label) try: storage = collection.get_all_items() except Exception: self.error(traceback.format_exc()) continue for item in storage: values = { 'created': str(datetime.datetime.fromtimestamp( item.get_created())), 'modified': str( datetime.datetime.fromtimestamp( item.get_modified())), 'content-type': item.get_secret_content_type(), 'label': item.get_label(), 'Password': item.get_secret().decode('utf8'), 'collection': label, } # for k, v in item.get_attributes().iteritems(): # values[unicode(k)] = unicode(v) items.append(values) try: bus.flush() bus.close() except Exception: pass return items
def test_all_collections(self) -> None: labels = map(Collection.get_label, get_all_collections(self.connection)) self.assertIn(self.collection.get_label(), labels)
def run(self): items = [] visited = set() try: import dbus import secretstorage import datetime except ImportError as e: self.error('libsecret: {0}'.format(e)) return [] for uid, session in homes.sessions(): try: # List bus connection names bus = dbus.bus.BusConnection(session) if 'org.freedesktop.secrets' not in [str(x) for x in bus.list_names()]: continue except Exception: self.error(traceback.format_exc()) continue collections = None try: # Python 2.7 collections = list(secretstorage.collection.get_all_collections(bus)) except Exception: pass if not collections: try: # Python 3 from jeepney.integrate.blocking import connect_and_authenticate make_auth_external.uid = uid bus = connect_and_authenticate(session) collections = secretstorage.get_all_collections(bus) except Exception: self.error(traceback.format_exc()) continue for collection in collections: if collection.is_locked(): continue label = collection.get_label() if label in visited: continue visited.add(label) try: storage = collection.get_all_items() except Exception: self.error(traceback.format_exc()) continue for item in storage: values = { 'created': str(datetime.datetime.fromtimestamp(item.get_created())), 'modified': str(datetime.datetime.fromtimestamp(item.get_modified())), 'content-type': item.get_secret_content_type(), 'label': item.get_label(), 'Password': item.get_secret().decode('utf8'), 'collection': label, } # for k, v in item.get_attributes().iteritems(): # values[unicode(k)] = unicode(v) items.append(values) try: bus.flush() bus.close() except Exception: pass return items