Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MatrixFederationAgent for binds to Synapse #157

Merged
merged 5 commits into from
May 10, 2019

Conversation

anoadragon453
Copy link
Member

Stop using the InsecureInterceptableContextFactory and instead use MatrixFederationAgent which can handle .well-known server files.

@anoadragon453 anoadragon453 requested a review from a team May 8, 2019 23:51
richvdh
richvdh previously requested changes May 9, 2019
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks like the right direction, but needs a few tweaks.

@@ -101,29 +102,27 @@ def _notify(self, assoc, attempt):
mxid = assoc["mxid"]
domain = mxid.split(":")[-1]
server = yield self._pickServer(domain)
callbackUrl = "https://%s/_matrix/federation/v1/3pid/onbind" % (

post_url = "https://%s/_matrix/federation/v1/3pid/onbind" % (
server,
)

logger.info("Making bind callback to: %s", callbackUrl)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callbackUrl doesn't seem to be defined here any more

@@ -101,29 +102,27 @@ def _notify(self, assoc, attempt):
mxid = assoc["mxid"]
domain = mxid.split(":")[-1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the wrong way to extract a domain from an mxid, ftr... but that's probably a problem for another day.

# Make a POST to the chosen Synapse server
http_client = FederationHttpClient()
response = yield http_client.post_json_get_nothing(post_url, assoc, {
'headers': {
"Content-Type": ["application/json"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is set by post_json_get_nothing

# Make a POST to the chosen Synapse server
http_client = FederationHttpClient()
response = yield http_client.post_json_get_nothing(post_url, assoc, {
'headers': {
"Content-Type": ["application/json"],
"User-Agent": ["Sydent"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if this is worth doing, I think it should be done by the http client. Relatedly: I don't think it's worth doing.

sydent/threepid/bind.py Show resolved Hide resolved
)
# If the request failed, try again with exponential backoff
if response.code != 200:
self._notifyErrback(assoc, attempt, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err is not defined here.

Headers({

# Make a POST to the chosen Synapse server
http_client = FederationHttpClient()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FederationHttpClient takes a parameter.


# Make a POST to the chosen Synapse server
http_client = FederationHttpClient()
response = yield http_client.post_json_get_nothing(post_url, assoc, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docs on Agent (which also apply to FederationAgent, since they share an interface) have this to say:

An important thing to keep in mind is that the body will only be read from the connection after Response.deliverBody is called. This also means that the connection will remain open until this is done (and the body read). So, in general, any response with a body must have that body read using deliverBody . If the application is not interested in the body, it should issue a HEAD request or use a protocol which immediately calls stopProducing on its transport.

In other words, we're going to leak HTTP connections because we fail to read the response.

If you look at things like post_json_get_json in the synapse you'll notice that it is careful to always call readBody (which in turn calls deliverBody). I'd suggest you cargo-cult that function into the sydent codebase.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm otherwise!

else:
logger.info("Successfully notified on bind for %s" % (mxid,))
except Exception as e:
self._notifyErrback(assoc, attempt, e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will get called if the first _notifyErrback raises, which is admittedly unlikely, but it would be nice to make it right. How about:

        try:
            response = yield http_client.post_json_get_nothing(post_url, assoc, {})
        except Exception as e:
            self._notifyErrback(assoc, attempt, e)
            return

        # now we can process response

@richvdh
Copy link
Member

richvdh commented May 10, 2019

(feel free to fix and merge)

@anoadragon453 anoadragon453 merged commit bb346c4 into master May 10, 2019
@anoadragon453 anoadragon453 deleted the anoa/use_federation_agent_for_binds branch May 10, 2019 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants