From 6eba069fdb566fd006fa48b8b9c5d5b44085f524 Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Fri, 25 Sep 2015 17:30:00 +0200 Subject: Add repair to pixelated maintenance - Issue #468 - Call with: pixelated-maintenance repair --- .../adapter/mailstore/maintenance/__init__.py | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'service/pixelated/adapter/mailstore/maintenance/__init__.py') diff --git a/service/pixelated/adapter/mailstore/maintenance/__init__.py b/service/pixelated/adapter/mailstore/maintenance/__init__.py index eaac5814..edc442c2 100644 --- a/service/pixelated/adapter/mailstore/maintenance/__init__.py +++ b/service/pixelated/adapter/mailstore/maintenance/__init__.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . from leap.keymanager.keys import KEY_TYPE_KEY, KEY_PRIVATE_KEY, KEY_ID_KEY, KEY_ADDRESS_KEY +from leap.keymanager.openpgp import OpenPGPKey from twisted.internet import defer import logging @@ -35,6 +36,14 @@ def _is_private_key_doc(doc): return _is_key_doc(doc) and doc.content.get(KEY_PRIVATE_KEY, False) +def _is_active_key_doc(doc): + return _is_key_doc(doc) and doc.content.get(KEY_TYPE_KEY, None) == TYPE_OPENPGP_ACTIVE + + +def _is_public_key(doc): + return _is_key_doc(doc) and not doc.content.get(KEY_PRIVATE_KEY, False) + + def _key_id(doc): return doc.content.get(KEY_ID_KEY, None) @@ -58,5 +67,33 @@ class SoledadMaintenance(object): logger.warn('Deleting doc %s for key %s of <%s>' % (doc.doc_id, _key_id(doc), _address(doc))) yield self._soledad.delete_doc(doc) + yield self._repair_missing_active_docs(docs, private_key_ids) + + @defer.inlineCallbacks + def _repair_missing_active_docs(self, docs, private_key_ids): + missing = self._missing_active_docs(docs, private_key_ids) + for key_id in missing: + emails = self._emails_for_key_id(docs, key_id) + for email in emails: + logger.warn('Re-creating active doc for key %s, email %s' % (key_id, email)) + yield self._soledad.create_doc_from_json(OpenPGPKey(email, key_id=key_id, private=False).get_active_json(email)) + def _key_ids_with_private_key(self, docs): return [doc.content[KEY_ID_KEY] for doc in docs if _is_private_key_doc(doc)] + + def _missing_active_docs(self, docs, private_key_ids): + active_doc_ids = self._active_docs_for_key_id(docs) + + return set([private_key_id for private_key_id in private_key_ids if private_key_id not in active_doc_ids]) + + def _emails_for_key_id(self, docs, key_id): + for doc in docs: + if _is_private_key_doc(doc) and _key_id(doc) == key_id: + email = _address(doc) + if isinstance(email, list): + return email + else: + return [email] + + def _active_docs_for_key_id(self, docs): + return [doc.content[KEY_ID_KEY] for doc in docs if _is_active_key_doc(doc) and _is_public_key(doc)] -- cgit v1.2.3