-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Initial APIs for storing E2E room keys #2731
Changes from 6 commits
824b339
e558f21
7983840
602b647
552934e
124efee
3e98a00
356829c
59aa35b
ace298f
a658f4e
dee6849
a22968d
98fcffd
fecfa63
1aa87fc
68e9cfe
eb1a429
ef75daf
3a8d9ab
7ab77f6
843ce19
4fdd02f
10cec6f
ebfaeac
1976540
4cd381d
e3bc9f2
c199970
f27afe1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2017 New Vector Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
|
||
from twisted.internet import defer | ||
|
||
from synapse.api.errors import StoreError, SynapseError, RoomKeysVersionError | ||
from synapse.util.async import Linearizer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class E2eRoomKeysHandler(object): | ||
def __init__(self, hs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to have a quick comment on the handler to say what its responsible for. A two line description would probably be good enough, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
self.store = hs.get_datastore() | ||
self._upload_linearizer = Linearizer("upload_room_keys_lock") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth commenting on what this protects There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
@defer.inlineCallbacks | ||
def get_room_keys(self, user_id, version, room_id, session_id): | ||
# we deliberately take the lock to get keys so that changing the version | ||
# works atomically | ||
with (yield self._upload_linearizer.queue(user_id)): | ||
results = yield self.store.get_e2e_room_keys( | ||
user_id, version, room_id, session_id | ||
) | ||
defer.returnValue(results) | ||
|
||
@defer.inlineCallbacks | ||
def delete_room_keys(self, user_id, version, room_id, session_id): | ||
yield self.store.delete_e2e_room_keys(user_id, version, room_id, session_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No lock? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops; added for consistency with uploads now |
||
|
||
@defer.inlineCallbacks | ||
def upload_room_keys(self, user_id, version, room_keys): | ||
|
||
# TODO: Validate the JSON to make sure it has the right keys. | ||
|
||
# Check that the version we're trying to upload is the current version | ||
|
||
try: | ||
version_info = yield self.get_version_info(user_id, version) | ||
except StoreError as e: | ||
if e.code == 404: | ||
raise SynapseError(404, "Version '%s' not found" % (version,)) | ||
else: | ||
raise e | ||
|
||
if version_info['version'] != version: | ||
raise RoomKeysVersionError(current_version=version_info.version) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need to lock to ensure that we don't change version while doing the checks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm. fixed. |
||
|
||
# XXX: perhaps we should use a finer grained lock here? | ||
with (yield self._upload_linearizer.queue(user_id)): | ||
|
||
# go through the room_keys | ||
for room_id in room_keys['rooms']: | ||
for session_id in room_keys['rooms'][room_id]['sessions']: | ||
room_key = room_keys['rooms'][room_id]['sessions'][session_id] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd do the loop as something like: for room_id, entry in room_keys['rooms'].iteritems():
for session_id in entry['sessions']:
etc... So that we don't have to constantly look the key up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
yield self._upload_room_key( | ||
user_id, version, room_id, session_id, room_key | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be tempted to do some of the different rooms concurrently, given its in a lock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is preemie optimisation; we don't even have a flow yet where the bulk upload is used. so have added a comment instead. |
||
|
||
@defer.inlineCallbacks | ||
def _upload_room_key(self, user_id, version, room_id, session_id, room_key): | ||
# get the room_key for this particular row | ||
current_room_key = None | ||
try: | ||
current_room_key = yield self.store.get_e2e_room_key( | ||
user_id, version, room_id, session_id | ||
) | ||
except StoreError as e: | ||
if e.code == 404: | ||
pass | ||
else: | ||
raise e | ||
|
||
# check whether we merge or not. spelling it out with if/elifs rather | ||
# than lots of booleans for legibility. | ||
upsert = True | ||
if current_room_key: | ||
if room_key['is_verified'] and not current_room_key['is_verified']: | ||
pass | ||
elif ( | ||
room_key['first_message_index'] < | ||
current_room_key['first_message_index'] | ||
): | ||
pass | ||
elif room_key['forwarded_count'] < room_key['forwarded_count']: | ||
pass | ||
else: | ||
upsert = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be tempted to split this logic out into a freestanding function, as its business logic that could be easily tested. It also gives a chance to document whats going on a bit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
||
# if so, we set the new room_key | ||
if upsert: | ||
yield self.store.set_e2e_room_key( | ||
user_id, version, room_id, session_id, room_key | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def create_version(self, user_id, version_info): | ||
|
||
# TODO: Validate the JSON to make sure it has the right keys. | ||
|
||
# lock everyone out until we've switched version | ||
with (yield self._upload_linearizer.queue(user_id)): | ||
new_version = yield self.store.create_e2e_room_key_version( | ||
user_id, version_info | ||
) | ||
defer.returnValue(new_version) | ||
|
||
@defer.inlineCallbacks | ||
def get_version_info(self, user_id, version): | ||
with (yield self._upload_linearizer.queue(user_id)): | ||
results = yield self.store.get_e2e_room_key_version_info( | ||
user_id, version | ||
) | ||
defer.returnValue(results) | ||
|
||
@defer.inlineCallbacks | ||
def delete_version(self, user_id, version): | ||
with (yield self._upload_linearizer.queue(user_id)): | ||
yield self.store.delete_e2e_room_key_version(user_id, version) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want the
code
,msg
anderrcode
to be configurable?Do we not want to force the caller to include a
current_version
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have made
code
,msg
&errcode
unconfigurable, and de-optionalised current_version.