Skip to content

Commit 12dc6b1

Browse files
Add support to proxy outbound requests from Synapse in tests (#18158)
Adds new environment variables that can be used with the Docker image (`SYNAPSE_HTTP_PROXY`/`SYNAPSE_HTTPS_PROXY`/`SYNAPSE_NO_PROXY`) Useful for things like the [Secure Border Gateway](https://element.io/server-suite/secure-border-gateways) ### Why is this necessary? You can already configure the `HTTP_PROXY`/`HTTPS_PROXY` environment variables to proxy outbound requests but setting this globally in the Docker image affects all processes which isn't always desirable or workable in the case where the proxy is running in the Docker image itself (because the Debian packages will fail to download because the proxy isn't up and running yet) . Adding Synapse specific environment variables (`SYNAPSE_HTTP_PROXY`/`SYNAPSE_HTTPS_PROXY`/`SYNAPSE_NO_PROXY`) makes things much more targetable.
1 parent 0c31783 commit 12dc6b1

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

changelog.d/18158.docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `SYNAPSE_HTTP_PROXY`/`SYNAPSE_HTTPS_PROXY`/`SYNAPSE_NO_PROXY` environment variables to pass through specifically to the Synapse process (instead of needing to apply [`http_proxy`/`https_proxy`/`no_proxy`](https://element-hq.github.io/synapse/latest/setup/forward_proxy.html) globally).

docker/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ The following environment variables are supported in `run` mode:
114114
is set via `docker run --user`, defaults to `991`, `991`. Note that this user
115115
must have permission to read the config files, and write to the data directories.
116116
* `TZ`: the [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) the container will run with. Defaults to `UTC`.
117+
* `SYNAPSE_HTTP_PROXY`: Passed through to the Synapse process as the `http_proxy` environment variable.
118+
* `SYNAPSE_HTTPS_PROXY`: Passed through to the Synapse process as the `https_proxy` environment variable.
119+
* `SYNAPSE_NO_PROXY`: Passed through to the Synapse process as `no_proxy` environment variable.
117120

118121
For more complex setups (e.g. for workers) you can also pass your args directly to synapse using `run` mode. For example like this:
119122

docker/conf-workers/synapse.supervisord.conf.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% if use_forking_launcher %}
22
[program:synapse_fork]
3+
environment=http_proxy="%(ENV_SYNAPSE_HTTP_PROXY)s",https_proxy="%(ENV_SYNAPSE_HTTPS_PROXY)s",no_proxy="%(ENV_SYNAPSE_NO_PROXY)s"
34
command=/usr/local/bin/python -m synapse.app.complement_fork_starter
45
{{ main_config_path }}
56
synapse.app.homeserver
@@ -20,6 +21,7 @@ exitcodes=0
2021

2122
{% else %}
2223
[program:synapse_main]
24+
environment=http_proxy="%(ENV_SYNAPSE_HTTP_PROXY)s",https_proxy="%(ENV_SYNAPSE_HTTPS_PROXY)s",no_proxy="%(ENV_SYNAPSE_NO_PROXY)s"
2325
command=/usr/local/bin/prefix-log /usr/local/bin/python -m synapse.app.homeserver
2426
--config-path="{{ main_config_path }}"
2527
--config-path=/conf/workers/shared.yaml
@@ -36,6 +38,7 @@ exitcodes=0
3638

3739
{% for worker in workers %}
3840
[program:synapse_{{ worker.name }}]
41+
environment=http_proxy="%(ENV_SYNAPSE_HTTP_PROXY)s",https_proxy="%(ENV_SYNAPSE_HTTPS_PROXY)s",no_proxy="%(ENV_SYNAPSE_NO_PROXY)s"
3942
command=/usr/local/bin/prefix-log /usr/local/bin/python -m {{ worker.app }}
4043
--config-path="{{ main_config_path }}"
4144
--config-path=/conf/workers/shared.yaml

docker/configure_workers_and_start.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,13 @@ def main(args: List[str], environ: MutableMapping[str, str]) -> None:
10991099
else:
11001100
log("Could not find %s, will not use" % (jemallocpath,))
11011101

1102+
# Empty strings are falsy in Python so this default is fine. We just can't have these
1103+
# be undefined because supervisord will complain about our
1104+
# `%(ENV_SYNAPSE_HTTP_PROXY)s` usage.
1105+
environ.setdefault("SYNAPSE_HTTP_PROXY", "")
1106+
environ.setdefault("SYNAPSE_HTTPS_PROXY", "")
1107+
environ.setdefault("SYNAPSE_NO_PROXY", "")
1108+
11021109
# Start supervisord, which will start Synapse, all of the configured worker
11031110
# processes, redis, nginx etc. according to the config we created above.
11041111
log("Starting supervisord")

synapse/http/proxyagent.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ def __init__(
150150
http_proxy = proxies["http"].encode() if "http" in proxies else None
151151
https_proxy = proxies["https"].encode() if "https" in proxies else None
152152
no_proxy = proxies["no"] if "no" in proxies else None
153+
logger.debug(
154+
"Using proxy settings: http_proxy=%s, https_proxy=%s, no_proxy=%s",
155+
http_proxy,
156+
https_proxy,
157+
no_proxy,
158+
)
153159

154160
self.http_proxy_endpoint, self.http_proxy_creds = http_proxy_endpoint(
155161
http_proxy, self.proxy_reactor, contextFactory, **self._endpoint_kwargs

0 commit comments

Comments
 (0)