Update keyring Measure

This commit is contained in:
Erik Johnston 2016-08-19 17:56:44 +01:00
parent 39b900b316
commit 04fc8bbcb0

View File

@ -116,43 +116,42 @@ class Keyring(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def handle_key_deferred(verify_request): def handle_key_deferred(verify_request):
with Measure(self.clock, "handle_key_deferred"): server_name = verify_request.server_name
server_name = verify_request.server_name try:
try: _, key_id, verify_key = yield verify_request.deferred
_, key_id, verify_key = yield verify_request.deferred except IOError as e:
except IOError as e: logger.warn(
logger.warn( "Got IOError when downloading keys for %s: %s %s",
"Got IOError when downloading keys for %s: %s %s", server_name, type(e).__name__, str(e.message),
server_name, type(e).__name__, str(e.message), )
) raise SynapseError(
raise SynapseError( 502,
502, "Error downloading keys for %s" % (server_name,),
"Error downloading keys for %s" % (server_name,), Codes.UNAUTHORIZED,
Codes.UNAUTHORIZED, )
) except Exception as e:
except Exception as e: logger.exception(
logger.exception( "Got Exception when downloading keys for %s: %s %s",
"Got Exception when downloading keys for %s: %s %s", server_name, type(e).__name__, str(e.message),
server_name, type(e).__name__, str(e.message), )
) raise SynapseError(
raise SynapseError( 401,
401, "No key for %s with id %s" % (server_name, key_ids),
"No key for %s with id %s" % (server_name, key_ids), Codes.UNAUTHORIZED,
Codes.UNAUTHORIZED, )
)
json_object = verify_request.json_object json_object = verify_request.json_object
try: try:
verify_signed_json(json_object, server_name, verify_key) verify_signed_json(json_object, server_name, verify_key)
except: except:
raise SynapseError( raise SynapseError(
401, 401,
"Invalid signature for server %s with key %s:%s" % ( "Invalid signature for server %s with key %s:%s" % (
server_name, verify_key.alg, verify_key.version server_name, verify_key.alg, verify_key.version
), ),
Codes.UNAUTHORIZED, Codes.UNAUTHORIZED,
) )
server_to_deferred = { server_to_deferred = {
server_name: defer.Deferred() server_name: defer.Deferred()
@ -245,59 +244,60 @@ class Keyring(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def do_iterations(): def do_iterations():
merged_results = {} with Measure(self.clock, "get_server_verify_keys"):
merged_results = {}
missing_keys = {}
for verify_request in verify_requests:
missing_keys.setdefault(verify_request.server_name, set()).update(
verify_request.key_ids
)
for fn in key_fetch_fns:
results = yield fn(missing_keys.items())
merged_results.update(results)
# We now need to figure out which verify requests we have keys
# for and which we don't
missing_keys = {} missing_keys = {}
requests_missing_keys = []
for verify_request in verify_requests: for verify_request in verify_requests:
server_name = verify_request.server_name missing_keys.setdefault(verify_request.server_name, set()).update(
result_keys = merged_results[server_name] verify_request.key_ids
)
if verify_request.deferred.called: for fn in key_fetch_fns:
# We've already called this deferred, which probably results = yield fn(missing_keys.items())
# means that we've already found a key for it. merged_results.update(results)
continue
for key_id in verify_request.key_ids: # We now need to figure out which verify requests we have keys
if key_id in result_keys: # for and which we don't
with PreserveLoggingContext(): missing_keys = {}
verify_request.deferred.callback(( requests_missing_keys = []
server_name, for verify_request in verify_requests:
key_id, server_name = verify_request.server_name
result_keys[key_id], result_keys = merged_results[server_name]
))
break
else:
# The else block is only reached if the loop above
# doesn't break.
missing_keys.setdefault(server_name, set()).update(
verify_request.key_ids
)
requests_missing_keys.append(verify_request)
if not missing_keys: if verify_request.deferred.called:
break # We've already called this deferred, which probably
# means that we've already found a key for it.
continue
for verify_request in requests_missing_keys.values(): for key_id in verify_request.key_ids:
verify_request.deferred.errback(SynapseError( if key_id in result_keys:
401, with PreserveLoggingContext():
"No key for %s with id %s" % ( verify_request.deferred.callback((
verify_request.server_name, verify_request.key_ids, server_name,
), key_id,
Codes.UNAUTHORIZED, result_keys[key_id],
)) ))
break
else:
# The else block is only reached if the loop above
# doesn't break.
missing_keys.setdefault(server_name, set()).update(
verify_request.key_ids
)
requests_missing_keys.append(verify_request)
if not missing_keys:
break
for verify_request in requests_missing_keys.values():
verify_request.deferred.errback(SynapseError(
401,
"No key for %s with id %s" % (
verify_request.server_name, verify_request.key_ids,
),
Codes.UNAUTHORIZED,
))
def on_err(err): def on_err(err):
for verify_request in verify_requests: for verify_request in verify_requests: