def __init__(self, provider, soledad, email_address, token, uuid): nicknym_url = provider._discover_nicknym_server() self._email = email_address self.keymanager = KeyManager( self._email, nicknym_url, soledad, token=token, ca_cert_path=provider.provider_api_cert, api_uri=provider.api_uri, api_version=provider.api_version, uid=uuid, gpgbinary=leap_config.gpg_binary, combined_ca_bundle=provider.combined_cerfificates_path)
def _create_keymanager_instance(self, userid, token, uuid, soledad): user, provider = userid.split('@') nickserver_uri = self._get_nicknym_uri(provider) cert_path = _get_ca_cert_path(self._basedir, provider) api_uri = self._get_api_uri(provider) if not token: token = self.service.tokens.get(userid) km_args = (userid, nickserver_uri, soledad) # TODO use the method in # services.soledadbootstrapper._get_gpg_bin_path. # That should probably live in keymanager package. km_kwargs = { "token": token, "uid": uuid, "api_uri": api_uri, "api_version": "1", "ca_cert_path": cert_path, "gpgbinary": "/usr/bin/gpg" } keymanager = KeyManager(*km_args, **km_kwargs) return keymanager
def _key_manager(self, user=ADDRESS, url='', token=None, ca_cert_path=None): return KeyManager(user, url, self._soledad, token=token, gpgbinary=self.gpg_binary_path, ca_cert_path=ca_cert_path)
def _create_keymanager_instance(self, userid, token, uuid, soledad): user, provider = userid.split('@') nickserver_uri = self._get_nicknym_uri(provider) cert_path = _get_ca_cert_path(self._basedir, provider) api_uri = self._get_api_uri(provider) if not token: token = self.service.tokens.get(userid) km_args = (userid, nickserver_uri, soledad) km_kwargs = { "token": token, "uid": uuid, "api_uri": api_uri, "api_version": "1", "ca_cert_path": cert_path, "gpgbinary": get_gpg_bin_path() } keymanager = KeyManager(*km_args, **km_kwargs) return keymanager
class Keymanager(object): def __init__(self, provider, soledad, email_address, token, uuid): nicknym_url = provider._discover_nicknym_server() self._email = email_address self.keymanager = KeyManager( self._email, nicknym_url, soledad, token=token, ca_cert_path=provider.provider_api_cert, api_uri=provider.api_uri, api_version=provider.api_version, uid=uuid, gpgbinary=leap_config.gpg_binary, combined_ca_bundle=provider.combined_cerfificates_path) @defer.inlineCallbacks def generate_openpgp_key(self): current_key = yield self._key_exists(self._email) if not current_key: current_key = yield self._generate_key_and_send_to_leap() elif current_key.needs_renewal(DEFAULT_EXTENSION_THRESHOLD): current_key = yield self._regenerate_key_and_send_to_leap() self._synchronize_remote_key(current_key) logger.debug("Current key for {}: {}".format(self._email, current_key.fingerprint)) @defer.inlineCallbacks def _synchronize_remote_key(self, current_key): if not self._is_key_synchronized_with_server(current_key): try: yield self.keymanager.send_key() except Exception as e: raise UploadKeyError(e.message) @defer.inlineCallbacks def _is_key_synchronized_with_server(self, current_key): remote_key = yield self.get_key(self._email, private=False, fetch_remote=True) defer.returnValue(remote_key.fingerprint == current_key.fingerprint) @defer.inlineCallbacks def _regenerate_key_and_send_to_leap(self): logger.info("Regenerating keys - this could take a while...") key = yield self.keymanager.regenerate_key() try: yield self.keymanager.send_key() defer.returnValue(key) except Exception as e: raise UploadKeyError(e.message) @defer.inlineCallbacks def _generate_key_and_send_to_leap(self): logger.info("Generating keys - this could take a while...") key = yield self.keymanager.gen_key() try: yield self.keymanager.send_key() defer.returnValue(key) except Exception as e: yield self.delete_key_pair() raise UploadKeyError(e.message) @defer.inlineCallbacks def _key_exists(self, email): try: current_key = yield self.get_key(email, private=True, fetch_remote=False) defer.returnValue(current_key) except KeyNotFound: defer.returnValue(None) @defer.inlineCallbacks def get_key(self, email, private=False, fetch_remote=True): key = yield self.keymanager.get_key(email, private=private, fetch_remote=fetch_remote) defer.returnValue(key) @defer.inlineCallbacks def delete_key_pair(self): private_key = yield self.get_key(self._email, private=True, fetch_remote=False) public_key = yield self.get_key(self._email, private=False, fetch_remote=False) self.keymanager.delete_key(private_key) self.keymanager.delete_key(public_key)