summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
Diffstat (limited to 'synapse')
-rw-r--r--synapse/handlers/e2e_room_keys.py35
-rw-r--r--synapse/rest/client/v2_alpha/room_keys.py6
-rw-r--r--synapse/storage/e2e_room_keys.py3
3 files changed, 36 insertions, 8 deletions
diff --git a/synapse/handlers/e2e_room_keys.py b/synapse/handlers/e2e_room_keys.py
index f08d80da3e..09c2888db6 100644
--- a/synapse/handlers/e2e_room_keys.py
+++ b/synapse/handlers/e2e_room_keys.py
@@ -58,6 +58,10 @@ class E2eRoomKeysHandler(object):
             results = yield self.store.get_e2e_room_keys(
                 user_id, version, room_id, session_id
             )
+
+            if results['rooms'] == {}:
+                raise SynapseError(404, "No room_keys found")
+
             defer.returnValue(results)
 
     @defer.inlineCallbacks
@@ -109,9 +113,10 @@ class E2eRoomKeysHandler(object):
 
         # XXX: perhaps we should use a finer grained lock here?
         with (yield self._upload_linearizer.queue(user_id)):
+
             # Check that the version we're trying to upload is the current version
             try:
-                version_info = yield self.get_version_info(user_id)
+                version_info = yield self._get_version_info_unlocked(user_id)
             except StoreError as e:
                 if e.code == 404:
                     raise SynapseError(404, "Version '%s' not found" % (version,))
@@ -119,16 +124,23 @@ class E2eRoomKeysHandler(object):
                     raise e
 
             if version_info['version'] != version:
-                raise RoomKeysVersionError(current_version=version_info.version)
+                # Check that the version we're trying to upload actually exists
+                try:
+                    version_info = yield self._get_version_info_unlocked(user_id, version)
+                    # if we get this far, the version must exist
+                    raise RoomKeysVersionError(current_version=version_info['version'])
+                except StoreError as e:
+                    if e.code == 404:
+                        raise SynapseError(404, "Version '%s' not found" % (version,))
+                    else:
+                        raise e
 
             # go through the room_keys.
             # XXX: this should/could be done concurrently, given we're in a lock.
             for room_id, room in room_keys['rooms'].iteritems():
                 for session_id, session in room['sessions'].iteritems():
-                    room_key = session[session_id]
-
                     yield self._upload_room_key(
-                        user_id, version, room_id, session_id, room_key
+                        user_id, version, room_id, session_id, session
                     )
 
     @defer.inlineCallbacks
@@ -242,8 +254,17 @@ class E2eRoomKeysHandler(object):
         """
 
         with (yield self._upload_linearizer.queue(user_id)):
-            results = yield self.store.get_e2e_room_keys_version_info(user_id)
-            defer.returnValue(results)
+            res = yield self._get_version_info_unlocked(user_id, version)
+            defer.returnValue(res)
+
+    @defer.inlineCallbacks
+    def _get_version_info_unlocked(self, user_id, version=None):
+        """Get the info about a given version of the user's backup
+        without obtaining the upload_linearizer lock.  For params see get_version_info
+        """
+
+        results = yield self.store.get_e2e_room_keys_version_info(user_id, version)
+        defer.returnValue(results)
 
     @defer.inlineCallbacks
     def delete_version(self, user_id, version):
diff --git a/synapse/rest/client/v2_alpha/room_keys.py b/synapse/rest/client/v2_alpha/room_keys.py
index ca69ced1e3..8f10e4e1cd 100644
--- a/synapse/rest/client/v2_alpha/room_keys.py
+++ b/synapse/rest/client/v2_alpha/room_keys.py
@@ -204,6 +204,12 @@ class RoomKeysServlet(RestServlet):
         room_keys = yield self.e2e_room_keys_handler.get_room_keys(
             user_id, version, room_id, session_id
         )
+
+        if session_id:
+            room_keys = room_keys['rooms'][room_id]['sessions'][session_id]
+        elif room_id:
+            room_keys = room_keys['rooms'][room_id]
+
         defer.returnValue((200, room_keys))
 
     @defer.inlineCallbacks
diff --git a/synapse/storage/e2e_room_keys.py b/synapse/storage/e2e_room_keys.py
index e4d56b7c37..8e8e4e457c 100644
--- a/synapse/storage/e2e_room_keys.py
+++ b/synapse/storage/e2e_room_keys.py
@@ -58,6 +58,7 @@ class EndToEndRoomKeyStore(SQLBaseStore):
 
         defer.returnValue(row)
 
+    @defer.inlineCallbacks
     def set_e2e_room_key(self, user_id, version, room_id, session_id, room_key):
         """Replaces or inserts the encrypted E2E room key for a given session in
         a given backup
@@ -135,7 +136,7 @@ class EndToEndRoomKeyStore(SQLBaseStore):
             desc="get_e2e_room_keys",
         )
 
-        sessions = {}
+        sessions = { 'rooms': {} }
         for row in rows:
             room_entry = sessions['rooms'].setdefault(row['room_id'], {"sessions": {}})
             room_entry['sessions'][row['session_id']] = {