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

support federation queries through http connect proxy #9306

Closed
wants to merge 4 commits into from

Conversation

Bubu
Copy link
Contributor

@Bubu Bubu commented Feb 2, 2021

Can be specified by HTTPS_PROXY env var.

pass unfiltered reactor to federation agent for proxy support

Signed-off-by: Marcus Hoffmann bubu@bubu1.eu

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
  • Pull request includes a sign off
  • Code style is correct (run the linters)

Fixes #8660

@Bubu Bubu marked this pull request as draft February 2, 2021 22:35
@Bubu
Copy link
Contributor Author

Bubu commented Feb 2, 2021

@clokep I'd like your feedback here, some notes:

  • This has been running in production on an instance of synapse that only supports outgoing connections via a proxy. So I'm somewhat certain that in general it does the correct thing.

  • It obviously still needs tests. I didn't actually look into that yet.

  • this doesn't attempt to tackle the fact that the blacklisting should probably be only happening at the proxy instead of in synapse. I think this can be solved independently though.

  • From the will not be used for list from Support for routing outbound HTTP requests via a proxy #6239 this only adds fed traffic as there was no immediate need for the other things on that list (in fact adding some of those would need Support for proxy exceptions (e.g. NO_PROXY env var) #9088 to be solved)

  • This might be solving Support proxy server for outbound federation #8859, though the reasoning in that issue is currently not clear to me (ssl offloading vs. us needed that just for connectivity to other servers)

  • The thing I'm unsure about is how to best introduce this: It changes existing functionality if you use a proxy server for anything... but on the other hand introducing more (nonstandard) env vars or config options for this doesn't seem satisfactory either. Maybe add this together with Support for proxy exceptions (e.g. NO_PROXY env var) #9088 and pointing it out in the upgrading notes would be enough?

@clokep clokep requested a review from a team February 3, 2021 12:29
@clokep
Copy link
Member

clokep commented Feb 3, 2021

I didn't look in detail yet, but a quick look makes it look reasonable. Is this only HTTPS proxy support? (No HTTP proxy support?)

I've set the review flag so that someone can provide feedback!

@Bubu
Copy link
Contributor Author

Bubu commented Feb 3, 2021

I didn't look in detail yet, but a quick look makes it look reasonable. Is this only HTTPS proxy support? (No HTTP proxy support?)

This is https only (target server is on https, not the proxy server like #9090) because I was under the impression that federation traffic requires a tls connection anyway. (I should be easy enough to pass through the http_proxy as well, if that is necessary for some reason.)

Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

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

  • This has been running in production on an instance of synapse that only supports outgoing connections via a proxy. So I'm somewhat certain that in general it does the correct thing.

Awesome news! I know that there's interest by others in running with this too!

  • It obviously still needs tests. I didn't actually look into that yet.

Yeah...it is rather unfortunate that the federation client is separate from the other HTTP client, but I don't think fixing that is trivial.

  • this doesn't attempt to tackle the fact that the blacklisting should probably be only happening at the proxy instead of in synapse. I think this can be solved independently though.

I think non-federation requests somewhat have the same issue right now, so I don't see this as a problem to solve here.

Ideally we would eventually add proxy support for all of these locations, I think. But not 100% sure. This fixes #8660, might be worth filing an issue with the remaining places we don't use a proxy?

This solves #8660, which I've added to the description (#8859 was marked as duplicate of #8660).

  • The thing I'm unsure about is how to best introduce this: It changes existing functionality if you use a proxy server for anything... but on the other hand introducing more (nonstandard) env vars or config options for this doesn't seem satisfactory either. Maybe add this together with Support for proxy exceptions (e.g. NO_PROXY env var) #9088 and pointing it out in the upgrading notes would be enough?

For #6239 it seems we just turned it on, I don't think we would do something different here. I suspect documenting it in the upgrade notes should be enough.

synapse/server.py Outdated Show resolved Hide resolved
@tzyl
Copy link
Contributor

tzyl commented Feb 9, 2021

Thanks for starting this @Bubu! This would also be really useful for me and I'm running a similar patch to support a setup where outbound traffic flows through an egress proxy host. A follow up that I would also want would be to support the NO_PROXY convention environment variable (as urllib and other non-python software does) which I'm happy to contribute following this.

@clokep
Copy link
Member

clokep commented Feb 9, 2021

@Bubu based on the conversation above...do you think this is ready for more feedback or what's the next step?

anoadragon453 pushed a commit that referenced this pull request Feb 26, 2021
### Changes proposed in this PR

- Add support for the `no_proxy` and `NO_PROXY` environment variables
  - Internally rely on urllib's [`proxy_bypass_environment`](https://github.com/python/cpython/blob/bdb941be423bde8b02a5695ccf51c303d6204bed/Lib/urllib/request.py#L2519)
- Extract env variables using urllib's `getproxies`/[`getproxies_environment`](https://github.com/python/cpython/blob/bdb941be423bde8b02a5695ccf51c303d6204bed/Lib/urllib/request.py#L2488) which supports lowercase + uppercase, preferring lowercase, except for `HTTP_PROXY` in a CGI environment

This does contain behaviour changes for consumers so making sure these are called out:
- `no_proxy`/`NO_PROXY` is now respected
- lowercase `https_proxy` is now allowed and taken over `HTTPS_PROXY`

Related to #9306 which also uses `ProxyAgent`

Signed-off-by: Timothy Leung tim95@hotmail.co.uk
@Bubu Bubu force-pushed the federation_through_proxy branch from 1fb936c to 960e8a9 Compare March 12, 2021 11:49
@richvdh
Copy link
Member

richvdh commented Apr 14, 2021

@Bubu is this ready for review, or are you still working on it? It looks like a couple of conflicts have cropped up.

@f1-outsourcing
Copy link

I also have a problem with the federation traffic not going through the proxy. Can anyone point me to a patch that I can apply to 1.31.0 ?

Bubu added 3 commits April 27, 2021 18:33
Can be specified by HTTPS_PROXY env var.

pass unfiltered reactor to federation agent for proxy support

Signed-off-by: Marcus Hoffmann <bubu@bubu1.eu>
This is used for proxy authentication, we just continue our "solution"
of copying code from proxyagent -> matrix_federation_agent.
@Bubu Bubu force-pushed the federation_through_proxy branch from 17d183c to ef8792a Compare April 27, 2021 16:50
@Bubu
Copy link
Contributor Author

Bubu commented Apr 27, 2021

@richvdh I resolved the conflicts. In general review is appreciated though it still lacks functioning tests.

(I currently don't have time to look into writing those, though potentially @chrisguida wanted to look at that.)

@richvdh richvdh requested a review from a team April 29, 2021 12:25
@chrisguida
Copy link

@richvdh I resolved the conflicts. In general review is appreciated though it still lacks functioning tests.

(I currently don't have time to look into writing those, though potentially @chrisguida wanted to look at that.)

If anyone else wants to take on writing the test so that this can be merged, please go ahead. I've run this code and it works, but figuring out how to write the test properly will most likely take me a while since I'm not too familiar with the testing framework. Otherwise I'll get to it when time permits.

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.

ok, I don't really understand what's going on in this code. Please update the docstrings to help me follow it.

@@ -72,6 +76,7 @@ def __init__(
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
user_agent: bytes,
ip_blacklist: IPSet,
proxy_reactor: Optional[ISynapseReactor] = None,
Copy link
Member

Choose a reason for hiding this comment

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

please add new params to the docstring (on the class, in this case). As well as making the code maintainable in future, it's important to help with review.

@@ -279,6 +279,7 @@ def __init__(self, hs, tls_client_options_factory):
tls_client_options_factory,
user_agent,
hs.config.federation_ip_range_blacklist,
proxy_reactor=hs.get_reactor(),
Copy link
Member

Choose a reason for hiding this comment

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

given that we already pass the ip blacklist into MatrixFederationAgent, why not move the construction of BlacklistingReactorWrapper down to it, rather than having to pass in two reactors?

@erikjohnston erikjohnston added the X-Awaiting-Changes A contributed PR which needs changes and re-review before it can be merged label May 20, 2021
@dklimpel dklimpel mentioned this pull request Jul 7, 2021
4 tasks
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# -*- coding: utf-8 -*-

This is no longer needed. See #9786

@dklimpel
Copy link
Contributor

(I currently don't have time to look into writing those, though potentially @chrisguida wanted to look at that.)

If anyone else wants to take on writing the test so that this can be merged, please go ahead. I've run this code and it works, but figuring out how to write the test properly will most likely take me a while since I'm not too familiar with the testing framework. Otherwise I'll get to it when time permits.

I will work on it after #10360 and #9119 is merged.
Some of this code and tests is needed to keep it simpler and avoid conflicts.

@ShadowJonathan
Copy link
Contributor

ShadowJonathan commented Jul 11, 2021

I'm seeing uses of urllib here, is it possible to use urllib3 for this?

My main gripe with it is that urllib3 is newer, but also that urllib produces bytes instead of str, which speaks of python 2 behaviour. Those bytes may not be UTF8, or properly parsed from the environment, so for compatibility i'd suggest trying to see if you can get urllib3 to work with this.

@dklimpel
Copy link
Contributor

I'm seeing uses of urllib here, is it possible to use urllib3 for this?

My main gripe with it is that urllib3 is newer, but also that urllib produces bytes instead of str, which speaks of python 2 behaviour. Those bytes may not be UTF8, or properly parsed from the environment, so for compatibility i'd suggest trying to see if you can get urllib3 to work with this.

I am not sure if it is useful to use the library only here.
If would have to check them certainly in whole Synapse.

There is also a discussion about it there: #9119 (comment)

@clokep
Copy link
Member

clokep commented Aug 5, 2021

I'm going to close this in lieu of #10475 where @dklimpel picked up this work. Hopefully that's OK! 🎉

@clokep clokep closed this Aug 5, 2021
@Bubu
Copy link
Contributor Author

Bubu commented Aug 5, 2021

Sure, sorry for not having the bandwidth to continue here :-(.

And thanks for picking this up @dklimpel.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
X-Awaiting-Changes A contributed PR which needs changes and re-review before it can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for HTTPS proxy for federation requests
10 participants