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

Commit d3e927b

Browse files
committed
Merge pull request #5794 from matrix-org/erikj/share_ssl_options_for_well_known
2 parents f04902a + 8fde611 commit d3e927b

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

changelog.d/5794.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance when making `.well-known` requests by sharing the SSL options between requests.

synapse/crypto/context_factory.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
platformTrust,
3232
)
3333
from twisted.python.failure import Failure
34+
from twisted.web.iweb import IPolicyForHTTPS
3435

3536
logger = logging.getLogger(__name__)
3637

@@ -74,6 +75,7 @@ def getContext(self):
7475
return self._context
7576

7677

78+
@implementer(IPolicyForHTTPS)
7779
class ClientTLSOptionsFactory(object):
7880
"""Factory for Twisted SSLClientConnectionCreators that are used to make connections
7981
to remote servers for federation.
@@ -146,6 +148,12 @@ def _context_info_cb(ssl_connection, where, ret):
146148
f = Failure()
147149
tls_protocol.failVerification(f)
148150

151+
def creatorForNetloc(self, hostname, port):
152+
"""Implements the IPolicyForHTTPS interace so that this can be passed
153+
directly to agents.
154+
"""
155+
return self.get_options(hostname)
156+
149157

150158
@implementer(IOpenSSLClientConnectionCreator)
151159
class SSLClientConnectionCreator(object):

synapse/http/federation/matrix_federation_agent.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ class MatrixFederationAgent(object):
6464
tls_client_options_factory (ClientTLSOptionsFactory|None):
6565
factory to use for fetching client tls options, or none to disable TLS.
6666
67-
_well_known_tls_policy (IPolicyForHTTPS|None):
68-
TLS policy to use for fetching .well-known files. None to use a default
69-
(browser-like) implementation.
70-
7167
_srv_resolver (SrvResolver|None):
7268
SRVResolver impl to use for looking up SRV records. None to use a default
7369
implementation.
@@ -81,7 +77,6 @@ def __init__(
8177
self,
8278
reactor,
8379
tls_client_options_factory,
84-
_well_known_tls_policy=None,
8580
_srv_resolver=None,
8681
_well_known_cache=well_known_cache,
8782
):
@@ -98,13 +93,12 @@ def __init__(
9893
self._pool.maxPersistentPerHost = 5
9994
self._pool.cachedConnectionTimeout = 2 * 60
10095

101-
agent_args = {}
102-
if _well_known_tls_policy is not None:
103-
# the param is called 'contextFactory', but actually passing a
104-
# contextfactory is deprecated, and it expects an IPolicyForHTTPS.
105-
agent_args["contextFactory"] = _well_known_tls_policy
10696
_well_known_agent = RedirectAgent(
107-
Agent(self._reactor, pool=self._pool, **agent_args)
97+
Agent(
98+
self._reactor,
99+
pool=self._pool,
100+
contextFactory=tls_client_options_factory,
101+
)
108102
)
109103
self._well_known_agent = _well_known_agent
110104

tests/http/federation/test_matrix_federation_agent.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ def setUp(self):
7575

7676
config_dict = default_config("test", parse=False)
7777
config_dict["federation_custom_ca_list"] = [get_test_ca_cert_file()]
78-
# config_dict["trusted_key_servers"] = []
7978

8079
self._config = config = HomeServerConfig()
8180
config.parse_config_dict(config_dict, "", "")
8281

8382
self.agent = MatrixFederationAgent(
8483
reactor=self.reactor,
8584
tls_client_options_factory=ClientTLSOptionsFactory(config),
86-
_well_known_tls_policy=TrustingTLSPolicyForHTTPS(),
8785
_srv_resolver=self.mock_resolver,
8886
_well_known_cache=self.well_known_cache,
8987
)
@@ -696,16 +694,18 @@ def test_get_well_known_unsigned_cert(self):
696694
not signed by a CA
697695
"""
698696

699-
# we use the same test server as the other tests, but use an agent
700-
# with _well_known_tls_policy left to the default, which will not
701-
# trust it (since the presented cert is signed by a test CA)
697+
# we use the same test server as the other tests, but use an agent with
698+
# the config left to the default, which will not trust it (since the
699+
# presented cert is signed by a test CA)
702700

703701
self.mock_resolver.resolve_service.side_effect = lambda _: []
704702
self.reactor.lookups["testserv"] = "1.2.3.4"
705703

704+
config = default_config("test", parse=True)
705+
706706
agent = MatrixFederationAgent(
707707
reactor=self.reactor,
708-
tls_client_options_factory=ClientTLSOptionsFactory(self._config),
708+
tls_client_options_factory=ClientTLSOptionsFactory(config),
709709
_srv_resolver=self.mock_resolver,
710710
_well_known_cache=self.well_known_cache,
711711
)

0 commit comments

Comments
 (0)