diff options
author | Tomás Touceda <chiiph@leap.se> | 2013-05-29 09:50:38 -0300 |
---|---|---|
committer | Tomás Touceda <chiiph@leap.se> | 2013-05-29 09:50:38 -0300 |
commit | cec2baa24f454c9f9168b5faed1a465e9943f591 (patch) | |
tree | f5428d3c2a0a02e484a69f24063a11f1d9327dda /src/leap/common/keymanager/keys.py | |
parent | 205fcd6c6bb8712a115f3dfe738c490f5427e09b (diff) | |
parent | 7fc904d797cb3c07f593157df1126b4179fe48d8 (diff) |
Merge remote-tracking branch 'drebs/feature/2341-use-indexes-to-store-and-fetch-keys' into develop
Conflicts:
src/leap/common/keymanager/keys.py
Diffstat (limited to 'src/leap/common/keymanager/keys.py')
-rw-r--r-- | src/leap/common/keymanager/keys.py | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/src/leap/common/keymanager/keys.py b/src/leap/common/keymanager/keys.py index f2c1beb..a3c8537 100644 --- a/src/leap/common/keymanager/keys.py +++ b/src/leap/common/keymanager/keys.py @@ -28,7 +28,6 @@ except ImportError: import re -from hashlib import sha256 from abc import ABCMeta, abstractmethod from leap.common.check import leap_assert @@ -57,6 +56,26 @@ KEY_TAGS_KEY = 'tags' KEYMANAGER_KEY_TAG = 'keymanager-key' + +# +# key indexing constants. +# + +TAGS_PRIVATE_INDEX = 'by-tags-private' +TAGS_ADDRESS_PRIVATE_INDEX = 'by-tags-address-private' +INDEXES = { + TAGS_PRIVATE_INDEX: [ + KEY_TAGS_KEY, + 'bool(%s)' % KEY_PRIVATE_KEY, + ], + TAGS_ADDRESS_PRIVATE_INDEX: [ + KEY_TAGS_KEY, + KEY_ADDRESS_KEY, + 'bool(%s)' % KEY_PRIVATE_KEY, + ] +} + + # # Key handling utilities # @@ -100,28 +119,6 @@ def build_key_from_dict(kClass, address, kdict): validation=kdict[KEY_VALIDATION_KEY], # TODO: verify for validation. ) - -def keymanager_doc_id(ktype, address, private=False): - """ - Return the document id for the document containing a key for - C{address}. - - :param address: The type of the key. - :type address: KeyType - :param address: The address bound to the key. - :type address: str - :param private: Whether the key is private or not. - :type private: bool - :return: The document id for the document that stores a key bound to - C{address}. - :rtype: str - """ - leap_assert(is_address(address), "Wrong address format: %s" % address) - ktype = str(ktype) - visibility = KEY_PRIVATE_KEY if private else 'public' - return sha256('keymanager-'+address+'-'+ktype+'-'+visibility).hexdigest() - - # # Abstraction for encryption keys # @@ -215,6 +212,27 @@ class EncryptionScheme(object): :type soledad: leap.soledad.Soledad """ self._soledad = soledad + self._init_indexes() + + def _init_indexes(self): + """ + Initialize the database indexes. + """ + # Ask the database for currently existing indexes. + db_indexes = dict(self._soledad.list_indexes()) + # Loop through the indexes we expect to find. + for name, expression in INDEXES.items(): + if name not in db_indexes: + # The index does not yet exist. + self._soledad.create_index(name, *expression) + continue + if expression == db_indexes[name]: + # The index exists and is up to date. + continue + # The index exists but the definition is not what expected, so we + # delete it and add the proper index expression. + self._soledad.delete_index(name) + self._soledad.create_index(name, *expression) @abstractmethod def get_key(self, address, private=False): |