From 1e2dda9e10a27220f988dc939e92dfe39b1e7201 Mon Sep 17 00:00:00 2001 From: drebs Date: Tue, 20 Sep 2016 20:47:15 -0300 Subject: [bug] only generate key if soledad has ever synced --- src/leap/bitmask/core/mail_services.py | 60 +++++++++++++++++++++++---------- src/leap/bitmask/keymanager/__init__.py | 7 ++++ 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/leap/bitmask/core/mail_services.py b/src/leap/bitmask/core/mail_services.py index d1cfbd9..b288f42 100644 --- a/src/leap/bitmask/core/mail_services.py +++ b/src/leap/bitmask/core/mail_services.py @@ -235,43 +235,67 @@ class KeymanagerContainer(Container): def _get_or_generate_keys(self, keymanager, userid): - def if_not_found_generate(failure): - # TODO -------------- should ONLY generate if INITIAL_SYNC_DONE. - # ie: put callback on_soledad_first_sync_ready ----------------- - # -------------------------------------------------------------- + def _get_key(_): + log.msg("looking up private key for %s" % userid) + return keymanager.get_key(userid, private=True, fetch_remote=False) + + def _found_key(key): + log.msg("found key: %r" % key) + + def _if_not_found_generate(failure): failure.trap(KeyNotFound) - log.msg("Core: Key not found. Generating key for %s" % (userid,)) + log.msg("key not found, generating key for %s" % (userid,)) d = keymanager.gen_key() - d.addCallbacks(send_key, log_key_error("generating")) + d.addCallbacks(_send_key, _log_key_error("generating")) return d - def send_key(ignored): + def _send_key(ignored): # ---------------------------------------------------------------- # It might be the case that we have generated a key-pair # but this hasn't been successfully uploaded. How do we know that? # XXX Should this be a method of bonafide instead? # ----------------------------------------------------------------- - log.msg("Key generated succesfully for %s" % userid) - if not self.get_instance(userid).token: - log.msg("Token not available yet, " - "wait before attempting to send key...") - return task.deferLater(reactor, 5, send_key, None) - log.msg("Sending key to server.") + log.msg("key generated for %s" % userid) + + if not keymanager.token: + log.msg("token not available, scheduling new send attempt...") + return task.deferLater(reactor, 5, _send_key, None) + + log.msg("sending public key to server") d = keymanager.send_key() d.addCallbacks( - lambda _: log.msg("Key sent to server."), - log_key_error("sending")) + lambda _: log.msg("key sent to server"), + _log_key_error("sending")) return d - def log_key_error(step): + def _log_key_error(step): def log_error(failure): log.err("Error while %s key!" % step) log.err(failure) return failure return log_error - d = keymanager.get_key(userid, private=True, fetch_remote=False) - d.addErrback(if_not_found_generate) + def _maybe_sync(ever_synced): + if ever_synced: + log.msg("soledad has synced in the past") + return defer.succeed(None) + + log.msg("soledad has never synced") + + if not keymanager.token: + log.msg("no token to sync now, scheduling a new check") + d = task.deferLater(reactor, 5, keymanager.ever_synced) + d.addCallback(_maybe_sync) + return d + + log.msg("syncing soledad for the first time...") + return keymanager._soledad.sync() + + log.msg("checking if soledad has ever synced...") + d = keymanager.ever_synced() + d.addCallback(_maybe_sync) + d.addCallback(_get_key) + d.addCallbacks(_found_key, _if_not_found_generate) d.addCallback(lambda _: keymanager) return d diff --git a/src/leap/bitmask/keymanager/__init__.py b/src/leap/bitmask/keymanager/__init__.py index b603fc5..c3ece05 100644 --- a/src/leap/bitmask/keymanager/__init__.py +++ b/src/leap/bitmask/keymanager/__init__.py @@ -783,6 +783,13 @@ class KeyManager(object): pubkey.validation = validation yield self.put_key(pubkey) + def ever_synced(self): + # TODO: provide this method in soledad api, avoid using a private + # attribute here + d = self._soledad._dbpool.runQuery('SELECT * FROM sync_log') + d.addCallback(lambda result: bool(result)) + return d + def _split_email(address): """ -- cgit v1.2.3