Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Propagate errors sensibly from proxied IS requests #2147

Merged
merged 12 commits into from May 3, 2017

Conversation

Projects
None yet
3 participants
Member

dbkr commented Apr 21, 2017

When we're proxying Matrix endpoints, parse out Matrix error
responses and turn them into SynapseErrors so they can be
propagated sensibly upstream.

This will allow clients to give sensible error messages if the ID server rejects their request, for example vector-im/riot-web#3542

dbkr added some commits Apr 21, 2017

Propagate errors sensibly from proxied IS requests
When we're proxying Matrix endpoints, parse out Matrix error
responses and turn them into SynapseErrors so they can be
propagated sensibly upstream.
synapse/http/client.py
@@ -145,6 +145,9 @@ def post_json_get_json(self, uri, post_json):
body = yield preserve_context_over_fn(readBody, response)
+ if response.code / 100 != 2:
+ raise CodeMessageException(response.code, body)
@ara4n

ara4n Apr 21, 2017

Owner

do we really want to raise an exception on 300s?

@dbkr

dbkr Apr 21, 2017

Member

Probably not, yeah

synapse/http/client.py
+class MatrixProxyClient(object):
+ """
+ An HTTP client that proxies other Matrix endpoints, ie. if the remote endpoint
+ returns Matrix-style error response, this will raise the appropriate SynapseError
@ara4n

ara4n Apr 21, 2017

Owner

I'm a bit confused - why can't we just pass the Matrix-style error response through to the client?

@dbkr

dbkr Apr 21, 2017

Member

Basically we just want to indicate that an error has occurred, and since post_json_get_json just returns the body, previously we were masking the error status code and behaving as if the endpoint returned 2xx which confused things. The main thing is that we raise an exception on error responses, and if we turn it into a SynapseException then it just works without having to handle it specifically in all the different places.

Owner

ara4n commented Apr 21, 2017

looks plausible beyond the fact i think i'm entirely missing how this works :D might need slightly more of an explanation as to why turning a matrix error into a synapse error is actually the soln.

@dbkr dbkr assigned ara4n and unassigned dbkr Apr 21, 2017

dbkr added some commits Apr 21, 2017

@dbkr dbkr referenced this pull request in matrix-org/sydent Apr 21, 2017

Merged

Add support for rejecting sms reqs to countries #43

Owner

erikjohnston commented Apr 25, 2017

What's the rationale here for not making the simple http client look for synapse style error messages?

Member

dbkr commented Apr 25, 2017 edited

It seems pretty terrible for the HTTP client to look for any error responses that happen to have 'error' and 'errcode' keys in the response and interpret them without necessarily knowing what that means at the app protocol level.

Owner

erikjohnston commented Apr 25, 2017

Having yet another HTTP client is going to be a bit confusing, I would have thought.

There are two choices for doing this in SimpleHttpClient, the first is to add a parameter to the functions to specify whether it should try and parse synapse style error messages. This would be something like is_remote_a_matrix_server or something.

The other option, that i think is probably nicer, is to add a subclass to CodeMessageException that is thrown when we managed to parse the error message as a synapse one. Thus callers can either ignore the new exception if don't care, or do something like the following if they do:

try:
    result = client.get_json(...)
    ...
except MatrixCodeMessageException as e:
    raise e.as_synapse_error()  # Converts the exception to SynapseError
Member

dbkr commented Apr 25, 2017

OK - I'd like to understand a bit better why more clients is going to be more confusing, but happy to change it back if you prefer.

Use CodeMessageException subclass instead
Parse json errors from get_json client methods and throw special
errors.

@dbkr dbkr assigned erikjohnston and unassigned ara4n Apr 25, 2017

Member

dbkr commented Apr 25, 2017

Done - ptal

dbkr added some commits Apr 26, 2017

synapse/http/client.py
+ errcode = jsonBody['errcode']
+ error = jsonBody['error']
+ return MatrixCodeMessageException(response.code, error, errcode)
+ except:
@erikjohnston

erikjohnston May 2, 2017

Owner

Generally we prefer to list the types of exceptions, as otherwise its too easy to catch a typo or something. Its probably fine for me.

lgtm

dbkr added some commits May 3, 2017

@dbkr dbkr merged commit 60833c8 into develop May 3, 2017

5 of 8 checks passed

Sytest Dendron (Commit) Build #2059 origin/dbkr/http_request_propagate_error failed in 6 min 11 sec
Details
Sytest SQLite (Commit) Build #2960 origin/dbkr/http_request_propagate_error failed in 6 min 59 sec
Details
Sytest Dendron (Merged PR) Build finished.
Details
Sytest Postgres (Commit) Build #2892 origin/dbkr/http_request_propagate_error succeeded in 6 min 33 sec
Details
Sytest Postgres (Merged PR) Build finished.
Details
Sytest SQLite (Merged PR) Build finished.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

psaavedra added a commit to psaavedra/synapse that referenced this pull request May 19, 2017

Merge tag 'v0.21.0' into v0.21.0_no_federate_by_default
Changes in synapse v0.21.0 (2017-05-18)
=======================================

No changes since v0.21.0-rc3

Changes in synapse v0.21.0-rc3 (2017-05-17)
===========================================

Features:

* Add per user rate-limiting overrides (PR #2208)
* Add config option to limit maximum number of events requested by ``/sync``
  and ``/messages`` (PR #2221) Thanks to @psaavedra!

Changes:

* Various small performance fixes (PR #2201, #2202, #2224, #2226, #2227, #2228,
  #2229)
* Update username availability checker API (PR #2209, #2213)
* When purging, don't de-delta state groups we're about to delete (PR #2214)
* Documentation to check synapse version (PR #2215) Thanks to @hamber-dick!
* Add an index to event_search to speed up purge history API (PR #2218)

Bug fixes:

* Fix API to allow clients to upload one-time-keys with new sigs (PR #2206)

Changes in synapse v0.21.0-rc2 (2017-05-08)
===========================================

Changes:

* Always mark remotes as up if we receive a signed request from them (PR #2190)

Bug fixes:

* Fix bug where users got pushed for rooms they had muted (PR #2200)

Changes in synapse v0.21.0-rc1 (2017-05-08)
===========================================

Features:

* Add username availability checker API (PR #2183)
* Add read marker API (PR #2120)

Changes:

* Enable guest access for the 3pl/3pid APIs (PR #1986)
* Add setting to support TURN for guests (PR #2011)
* Various performance improvements (PR #2075, #2076, #2080, #2083, #2108,
  #2158, #2176, #2185)
* Make synctl a bit more user friendly (PR #2078, #2127) Thanks @APwhitehat!
* Replace HTTP replication with TCP replication (PR #2082, #2097, #2098,
  #2099, #2103, #2014, #2016, #2115, #2116, #2117)
* Support authenticated SMTP (PR #2102) Thanks @DanielDent!
* Add a counter metric for successfully-sent transactions (PR #2121)
* Propagate errors sensibly from proxied IS requests (PR #2147)
* Add more granular event send metrics (PR #2178)

Bug fixes:

* Fix nuke-room script to work with current schema (PR #1927) Thanks
  @zuckschwerdt!
* Fix db port script to not assume postgres tables are in the public schema
  (PR #2024) Thanks @jerrykan!
* Fix getting latest device IP for user with no devices (PR #2118)
* Fix rejection of invites to unreachable servers (PR #2145)
* Fix code for reporting old verify keys in synapse (PR #2156)
* Fix invite state to always include all events (PR #2163)
* Fix bug where synapse would always fetch state for any missing event (PR #2170)
* Fix a leak with timed out HTTP connections (PR #2180)
* Fix bug where we didn't time out HTTP requests to ASes  (PR #2192)

Docs:

* Clarify doc for SQLite to PostgreSQL port (PR #1961) Thanks @benhylau!
* Fix typo in synctl help (PR #2107) Thanks @HarHarLinks!
* ``web_client_location`` documentation fix (PR #2131) Thanks @matthewjwolff!
* Update README.rst with FreeBSD changes (PR #2132) Thanks @feld!
* Clarify setting up metrics (PR #2149) Thanks @encks!

@dbkr dbkr deleted the dbkr/http_request_propagate_error branch Oct 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment