Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Return timeout error to user for identity server calls (#6073)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 authored and richvdh committed Sep 23, 2019
1 parent 0660f8e commit 885a472
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.d/6073.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return a clearer error message when a timeout occurs when attempting to contact an identity server.
16 changes: 14 additions & 2 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from canonicaljson import json

from twisted.internet import defer
from twisted.internet.error import TimeoutError

from synapse.api.errors import (
CodeMessageException,
Expand Down Expand Up @@ -108,7 +109,10 @@ def threepid_from_creds(self, id_server, creds):

url = id_server + "/_matrix/identity/api/v1/3pid/getValidated3pid"

data = yield self.http_client.get_json(url, query_params)
try:
data = yield self.http_client.get_json(url, query_params)
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
return data if "medium" in data else None

@defer.inlineCallbacks
Expand Down Expand Up @@ -171,6 +175,8 @@ def bind_threepid(self, creds, mxid, use_v2=True):
if e.code != 404 or not use_v2:
logger.error("3PID bind failed with Matrix error: %r", e)
raise e.to_synapse_error()
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
except CodeMessageException as e:
data = json.loads(e.msg) # XXX WAT?
return data
Expand Down Expand Up @@ -261,7 +267,9 @@ def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
logger.warn("Received %d response while unbinding threepid", e.code)
else:
logger.error("Failed to unbind threepid on identity server: %s", e)
raise SynapseError(502, "Failed to contact identity server")
raise SynapseError(500, "Failed to contact identity server")
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")

yield self.store.remove_user_bound_threepid(
user_id=mxid,
Expand Down Expand Up @@ -394,6 +402,8 @@ def requestEmailToken(
except HttpResponseException as e:
logger.info("Proxied requestToken failed: %r", e)
raise e.to_synapse_error()
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")

@defer.inlineCallbacks
def requestMsisdnToken(
Expand Down Expand Up @@ -446,6 +456,8 @@ def requestMsisdnToken(
except HttpResponseException as e:
logger.info("Proxied requestToken failed: %r", e)
raise e.to_synapse_error()
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")


def create_id_access_token_header(id_access_token):
Expand Down
32 changes: 23 additions & 9 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from unpaddedbase64 import decode_base64

from twisted.internet import defer
from twisted.internet.error import TimeoutError

from synapse import types
from synapse.api.constants import EventTypes, Membership
Expand Down Expand Up @@ -756,7 +757,8 @@ def _lookup_3pid_v1(self, id_server, medium, address):
raise AuthError(401, "No signatures on 3pid binding")
yield self._verify_any_signature(data, id_server)
return data["mxid"]

except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
except IOError as e:
logger.warning("Error from v1 identity server lookup: %s" % (e,))

Expand All @@ -777,10 +779,13 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
Deferred[str|None]: the matrix ID of the 3pid, or None if it is not recognised.
"""
# Check what hashing details are supported by this identity server
hash_details = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme, id_server),
{"access_token": id_access_token},
)
try:
hash_details = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme, id_server),
{"access_token": id_access_token},
)
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")

if not isinstance(hash_details, dict):
logger.warning(
Expand Down Expand Up @@ -851,6 +856,8 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
},
headers=headers,
)
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
except Exception as e:
logger.warning("Error when performing a v2 3pid lookup: %s", e)
raise SynapseError(
Expand All @@ -873,10 +880,13 @@ def _verify_any_signature(self, data, server_hostname):
if server_hostname not in data["signatures"]:
raise AuthError(401, "No signature from server %s" % (server_hostname,))
for key_name, signature in data["signatures"][server_hostname].items():
key_data = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/api/v1/pubkey/%s"
% (id_server_scheme, server_hostname, key_name)
)
try:
key_data = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/api/v1/pubkey/%s"
% (id_server_scheme, server_hostname, key_name)
)
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
if "public_key" not in key_data:
raise AuthError(
401, "No public key named %s from %s" % (key_name, server_hostname)
Expand Down Expand Up @@ -1051,6 +1061,8 @@ def _ask_id_server_for_third_party_invite(
invite_config,
{"Authorization": create_id_access_token_header(id_access_token)},
)
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
except HttpResponseException as e:
if e.code != 404:
logger.info("Failed to POST %s with JSON: %s", url, e)
Expand All @@ -1067,6 +1079,8 @@ def _ask_id_server_for_third_party_invite(
data = yield self.simple_http_client.post_json_get_json(
url, invite_config
)
except TimeoutError:
raise SynapseError(500, "Timed out contacting identity server")
except HttpResponseException as e:
logger.warning(
"Error trying to call /store-invite on %s%s: %s",
Expand Down

0 comments on commit 885a472

Please sign in to comment.