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

Commit

Permalink
Share SSL options for well-known requests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jul 30, 2019
1 parent 62a2d60 commit accf957
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions synapse/crypto/context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
platformTrust,
)
from twisted.python.failure import Failure
from twisted.web.iweb import IPolicyForHTTPS

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,6 +75,7 @@ def getContext(self):
return self._context


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

def creatorForNetloc(self, hostname, port):
"""Implements the IPolicyForHTTPS interace so that this can be passed
directly to agents.
"""
return self.get_options(hostname)


@implementer(IOpenSSLClientConnectionCreator)
class SSLClientConnectionCreator(object):
Expand Down
3 changes: 3 additions & 0 deletions synapse/http/federation/matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __init__(
# the param is called 'contextFactory', but actually passing a
# contextfactory is deprecated, and it expects an IPolicyForHTTPS.
agent_args["contextFactory"] = _well_known_tls_policy
else:
agent_args["contextFactory"] = tls_client_options_factory

_well_known_agent = RedirectAgent(
Agent(self._reactor, pool=self._pool, **agent_args)
)
Expand Down
5 changes: 3 additions & 2 deletions tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def setUp(self):

config_dict = default_config("test", parse=False)
config_dict["federation_custom_ca_list"] = [get_test_ca_cert_file()]
# config_dict["trusted_key_servers"] = []

self._config = config = HomeServerConfig()
config.parse_config_dict(config_dict, "", "")
Expand Down Expand Up @@ -698,9 +697,11 @@ def test_get_well_known_unsigned_cert(self):
self.mock_resolver.resolve_service.side_effect = lambda _: []
self.reactor.lookups["testserv"] = "1.2.3.4"

config = default_config("test", parse=True)

agent = MatrixFederationAgent(
reactor=self.reactor,
tls_client_options_factory=ClientTLSOptionsFactory(self._config),
tls_client_options_factory=ClientTLSOptionsFactory(config),
_srv_resolver=self.mock_resolver,
_well_known_cache=self.well_known_cache,
)
Expand Down

0 comments on commit accf957

Please sign in to comment.