Signal events from keymanager
[keymanager.git] / src / leap / keymanager / __init__.py
index d24e08e..20e44fd 100644 (file)
 Key Manager is a Nicknym agent for LEAP client.
 """
 
+import logging
 import requests
 
 from leap.common.check import leap_assert, leap_assert_type
-from leap.keymanager.errors import (
-    KeyNotFound,
-    NoPasswordGiven,
-)
+from leap.common.events import signal
+from leap.common.events import events_pb2 as proto
+
+from leap.keymanager.errors import KeyNotFound
+
 from leap.keymanager.keys import (
     EncryptionKey,
     build_key_from_dict,
@@ -38,6 +40,8 @@ from leap.keymanager.openpgp import (
     OpenPGPScheme,
 )
 
+logger = logging.getLogger(__name__)
+
 
 #
 # The Key Manager
@@ -170,12 +174,18 @@ class KeyManager(object):
         @raise KeyNotFound: If the key was not found on nickserver.
         """
         # request keys from the nickserver
-        server_keys = self._get(
-            self._nickserver_uri, {'address': address}).json()
-        # insert keys in local database
-        if self.OPENPGP_KEY in server_keys:
-            self._wrapper_map[OpenPGPKey].put_ascii_key(
-                server_keys['openpgp'])
+        res = None
+        try:
+            res = self._get(self._nickserver_uri, {'address': address})
+            server_keys = res.json()
+            # insert keys in local database
+            if self.OPENPGP_KEY in server_keys:
+                self._wrapper_map[OpenPGPKey].put_ascii_key(
+                    server_keys['openpgp'])
+        except Exception as e:
+            logger.warning("Error retrieving the keys: %r" % (e,))
+            if res:
+                logger.warning("%s" % (res.content,))
 
     #
     # key management
@@ -213,6 +223,7 @@ class KeyManager(object):
             self._api_version,
             self._uid)
         self._put(uri, data)
+        signal(proto.KEYMANAGER_DONE_UPLOADING_KEYS, self._address)
 
     def get_key(self, address, ktype, private=False, fetch_remote=True):
         """
@@ -237,15 +248,26 @@ class KeyManager(object):
             ktype in self._wrapper_map,
             'Unkown key type: %s.' % str(ktype))
         try:
+            signal(proto.KEYMANAGER_LOOKING_FOR_KEY, address)
             # return key if it exists in local database
-            return self._wrapper_map[ktype].get_key(address, private=private)
+            key = self._wrapper_map[ktype].get_key(address, private=private)
+            signal(proto.KEYMANAGER_KEY_FOUND, address)
+
+            return key
         except KeyNotFound:
+            signal(proto.KEYMANAGER_KEY_NOT_FOUND, address)
+
             # we will only try to fetch a key from nickserver if fetch_remote
             # is True and the key is not private.
             if fetch_remote is False or private is True:
                 raise
+
+            signal(proto.KEYMANAGER_LOOKING_FOR_KEY, address)
             self._fetch_keys_from_server(address)
-            return self._wrapper_map[ktype].get_key(address, private=False)
+            key = self._wrapper_map[ktype].get_key(address, private=False)
+            signal(proto.KEYMANAGER_KEY_FOUND, address)
+
+            return key
 
     def get_all_keys_in_local_db(self, private=False):
         """
@@ -287,7 +309,11 @@ class KeyManager(object):
         :return: The generated key.
         :rtype: EncryptionKey
         """
-        return self._wrapper_map[ktype].gen_key(self._address)
+        signal(proto.KEYMANAGER_STARTED_KEY_GENERATION, self._address)
+        key = self._wrapper_map[ktype].gen_key(self._address)
+        signal(proto.KEYMANAGER_FINISHED_KEY_GENERATION, self._address)
+
+        return key
 
     #
     # Setters/getters