2019-01-22 23:28:48 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
import treq
|
2019-06-10 18:06:25 +03:00
|
|
|
from service_identity import VerificationError
|
2019-01-29 16:53:02 +03:00
|
|
|
from zope.interface import implementer
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
2019-01-29 16:53:02 +03:00
|
|
|
from twisted.internet._sslverify import ClientTLSOptions, OpenSSLCertificateOptions
|
2019-01-22 23:28:48 +03:00
|
|
|
from twisted.internet.protocol import Factory
|
|
|
|
from twisted.protocols.tls import TLSMemoryBIOFactory
|
2019-06-10 18:06:25 +03:00
|
|
|
from twisted.web._newclient import ResponseNeverReceived
|
2019-08-07 17:36:38 +03:00
|
|
|
from twisted.web.client import Agent
|
2019-01-22 23:28:48 +03:00
|
|
|
from twisted.web.http import HTTPChannel
|
2019-01-30 13:55:25 +03:00
|
|
|
from twisted.web.http_headers import Headers
|
2019-01-29 16:53:02 +03:00
|
|
|
from twisted.web.iweb import IPolicyForHTTPS
|
2019-01-22 23:28:48 +03:00
|
|
|
|
2019-06-05 16:16:07 +03:00
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
2019-01-22 23:28:48 +03:00
|
|
|
from synapse.crypto.context_factory import ClientTLSOptionsFactory
|
2019-08-07 17:36:38 +03:00
|
|
|
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
|
|
|
|
from synapse.http.federation.srv_resolver import Server
|
|
|
|
from synapse.http.federation.well_known_resolver import (
|
|
|
|
WellKnownResolver,
|
2019-01-30 13:55:25 +03:00
|
|
|
_cache_period_from_headers,
|
|
|
|
)
|
2019-07-03 17:07:04 +03:00
|
|
|
from synapse.logging.context import LoggingContext
|
2019-01-30 13:55:25 +03:00
|
|
|
from synapse.util.caches.ttlcache import TTLCache
|
2019-01-22 23:28:48 +03:00
|
|
|
|
2019-06-10 18:06:25 +03:00
|
|
|
from tests.http import TestServerTLSConnectionFactory, get_test_ca_cert_file
|
2019-01-22 23:28:48 +03:00
|
|
|
from tests.server import FakeTransport, ThreadedMemoryReactorClock
|
|
|
|
from tests.unittest import TestCase
|
2019-04-25 16:22:49 +03:00
|
|
|
from tests.utils import default_config
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-06-10 18:06:25 +03:00
|
|
|
test_server_connection_factory = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_connection_factory():
|
|
|
|
# this needs to happen once, but not until we are ready to run the first test
|
|
|
|
global test_server_connection_factory
|
|
|
|
if test_server_connection_factory is None:
|
2019-06-20 12:32:02 +03:00
|
|
|
test_server_connection_factory = TestServerTLSConnectionFactory(
|
|
|
|
sanlist=[
|
|
|
|
b"DNS:testserv",
|
|
|
|
b"DNS:target-server",
|
|
|
|
b"DNS:xn--bcher-kva.com",
|
|
|
|
b"IP:1.2.3.4",
|
|
|
|
b"IP:::1",
|
|
|
|
]
|
|
|
|
)
|
2019-06-10 18:06:25 +03:00
|
|
|
return test_server_connection_factory
|
|
|
|
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
class MatrixFederationAgentTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
|
|
|
|
self.mock_resolver = Mock()
|
|
|
|
|
2019-06-05 16:16:07 +03:00
|
|
|
config_dict = default_config("test", parse=False)
|
2019-06-10 18:06:25 +03:00
|
|
|
config_dict["federation_custom_ca_list"] = [get_test_ca_cert_file()]
|
|
|
|
|
|
|
|
self._config = config = HomeServerConfig()
|
2019-06-24 13:34:45 +03:00
|
|
|
config.parse_config_dict(config_dict, "", "")
|
2019-06-05 16:16:07 +03:00
|
|
|
|
2019-08-07 17:36:38 +03:00
|
|
|
self.tls_factory = ClientTLSOptionsFactory(config)
|
2019-08-13 20:04:46 +03:00
|
|
|
|
|
|
|
self.well_known_cache = TTLCache("test_cache", timer=self.reactor.seconds)
|
|
|
|
self.had_well_known_cache = TTLCache("test_cache", timer=self.reactor.seconds)
|
|
|
|
self.well_known_resolver = WellKnownResolver(
|
|
|
|
self.reactor,
|
|
|
|
Agent(self.reactor, contextFactory=self.tls_factory),
|
|
|
|
well_known_cache=self.well_known_cache,
|
|
|
|
had_well_known_cache=self.had_well_known_cache,
|
|
|
|
)
|
|
|
|
|
2019-01-22 23:28:48 +03:00
|
|
|
self.agent = MatrixFederationAgent(
|
|
|
|
reactor=self.reactor,
|
2019-08-07 17:36:38 +03:00
|
|
|
tls_client_options_factory=self.tls_factory,
|
2019-01-22 23:28:48 +03:00
|
|
|
_srv_resolver=self.mock_resolver,
|
2019-08-13 20:04:46 +03:00
|
|
|
_well_known_resolver=self.well_known_resolver,
|
2019-01-22 23:28:48 +03:00
|
|
|
)
|
|
|
|
|
2019-01-24 12:34:44 +03:00
|
|
|
def _make_connection(self, client_factory, expected_sni):
|
2019-01-22 23:28:48 +03:00
|
|
|
"""Builds a test server, and completes the outgoing client connection
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
HTTPChannel: the test server
|
|
|
|
"""
|
|
|
|
|
|
|
|
# build the test server
|
2019-06-10 18:06:25 +03:00
|
|
|
server_tls_protocol = _build_test_server(get_connection_factory())
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
# now, tell the client protocol factory to build the client protocol (it will be a
|
|
|
|
# _WrappingProtocol, around a TLSMemoryBIOProtocol, around an
|
|
|
|
# HTTP11ClientProtocol) and wire the output of said protocol up to the server via
|
|
|
|
# a FakeTransport.
|
|
|
|
#
|
|
|
|
# Normally this would be done by the TCP socket code in Twisted, but we are
|
|
|
|
# stubbing that out here.
|
|
|
|
client_protocol = client_factory.buildProtocol(None)
|
2019-01-29 16:53:02 +03:00
|
|
|
client_protocol.makeConnection(
|
2019-05-10 08:12:11 +03:00
|
|
|
FakeTransport(server_tls_protocol, self.reactor, client_protocol)
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
# tell the server tls protocol to send its stuff back to the client, too
|
2019-01-29 16:53:02 +03:00
|
|
|
server_tls_protocol.makeConnection(
|
2019-05-10 08:12:11 +03:00
|
|
|
FakeTransport(client_protocol, self.reactor, server_tls_protocol)
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
2019-01-22 23:28:48 +03:00
|
|
|
|
2019-01-24 12:34:44 +03:00
|
|
|
# give the reactor a pump to get the TLS juices flowing.
|
2019-01-22 23:28:48 +03:00
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
2019-01-24 12:34:44 +03:00
|
|
|
# check the SNI
|
|
|
|
server_name = server_tls_protocol._tlsConnection.get_servername()
|
|
|
|
self.assertEqual(
|
|
|
|
server_name,
|
|
|
|
expected_sni,
|
|
|
|
"Expected SNI %s but got %s" % (expected_sni, server_name),
|
|
|
|
)
|
|
|
|
|
2019-01-22 23:28:48 +03:00
|
|
|
# fish the test server back out of the server-side TLS protocol.
|
|
|
|
return server_tls_protocol.wrappedProtocol
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _make_get_request(self, uri):
|
|
|
|
"""
|
|
|
|
Sends a simple GET request via the agent, and checks its logcontext management
|
|
|
|
"""
|
|
|
|
with LoggingContext("one") as context:
|
2019-06-20 12:32:02 +03:00
|
|
|
fetch_d = self.agent.request(b"GET", uri)
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(fetch_d)
|
|
|
|
|
|
|
|
# should have reset logcontext to the sentinel
|
|
|
|
_check_logcontext(LoggingContext.sentinel)
|
|
|
|
|
|
|
|
try:
|
|
|
|
fetch_res = yield fetch_d
|
2019-07-23 16:00:55 +03:00
|
|
|
return fetch_res
|
2019-01-29 16:53:02 +03:00
|
|
|
except Exception as e:
|
|
|
|
logger.info("Fetch of %s failed: %s", uri.decode("ascii"), e)
|
|
|
|
raise
|
2019-01-22 23:28:48 +03:00
|
|
|
finally:
|
|
|
|
_check_logcontext(context)
|
|
|
|
|
2019-01-30 13:55:25 +03:00
|
|
|
def _handle_well_known_connection(
|
2019-05-10 08:12:11 +03:00
|
|
|
self, client_factory, expected_sni, content, response_headers={}
|
2019-01-30 13:55:25 +03:00
|
|
|
):
|
2019-01-29 16:53:02 +03:00
|
|
|
"""Handle an outgoing HTTPs connection: wire it up to a server, check that the
|
|
|
|
request is for a .well-known, and send the response.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
client_factory (IProtocolFactory): outgoing connection
|
|
|
|
expected_sni (bytes): SNI that we expect the outgoing connection to send
|
2019-02-01 14:33:48 +03:00
|
|
|
content (bytes): content to send back as the .well-known
|
2019-01-30 13:55:25 +03:00
|
|
|
Returns:
|
|
|
|
HTTPChannel: server impl
|
2019-01-29 16:53:02 +03:00
|
|
|
"""
|
|
|
|
# make the connection for .well-known
|
|
|
|
well_known_server = self._make_connection(
|
2019-05-10 08:12:11 +03:00
|
|
|
client_factory, expected_sni=expected_sni
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
# check the .well-known request and send a response
|
|
|
|
self.assertEqual(len(well_known_server.requests), 1)
|
|
|
|
request = well_known_server.requests[0]
|
2019-02-01 14:33:48 +03:00
|
|
|
self._send_well_known_response(request, content, headers=response_headers)
|
2019-01-30 13:55:25 +03:00
|
|
|
return well_known_server
|
2019-01-29 16:53:02 +03:00
|
|
|
|
2019-02-01 14:33:48 +03:00
|
|
|
def _send_well_known_response(self, request, content, headers={}):
|
2019-01-29 16:53:02 +03:00
|
|
|
"""Check that an incoming request looks like a valid .well-known request, and
|
|
|
|
send back the response.
|
|
|
|
"""
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/.well-known/matrix/server")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-01-29 16:53:02 +03:00
|
|
|
# send back a response
|
2019-01-30 13:55:25 +03:00
|
|
|
for k, v in headers.items():
|
|
|
|
request.setHeader(k, v)
|
2019-02-01 14:33:48 +03:00
|
|
|
request.write(content)
|
2019-01-29 16:53:02 +03:00
|
|
|
request.finish()
|
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.reactor.pump((0.1,))
|
2019-01-29 16:53:02 +03:00
|
|
|
|
2019-01-22 23:28:48 +03:00
|
|
|
def test_get(self):
|
|
|
|
"""
|
2019-01-24 16:28:07 +03:00
|
|
|
happy-path test of a GET request with an explicit port
|
2019-01-22 23:28:48 +03:00
|
|
|
"""
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv:8448/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-22 23:28:48 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 08:12:11 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-25 15:38:16 +03:00
|
|
|
self.assertEqual(
|
2019-06-20 12:32:02 +03:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"testserv:8448"]
|
2019-01-25 15:38:16 +03:00
|
|
|
)
|
2019-01-22 23:28:48 +03:00
|
|
|
content = request.content.read()
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(content, b"")
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
# Deferred is still without a result
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# send the headers
|
2019-06-20 12:32:02 +03:00
|
|
|
request.responseHeaders.setRawHeaders(b"Content-Type", [b"application/json"])
|
|
|
|
request.write("")
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
|
|
|
response = self.successResultOf(test_d)
|
|
|
|
|
|
|
|
# that should give us a Response object
|
|
|
|
self.assertEqual(response.code, 200)
|
|
|
|
|
|
|
|
# Send the body
|
2019-06-20 12:32:02 +03:00
|
|
|
request.write('{ "a": 1 }'.encode("ascii"))
|
2019-01-22 23:28:48 +03:00
|
|
|
request.finish()
|
|
|
|
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
|
|
|
|
# check it can be read
|
|
|
|
json = self.successResultOf(treq.json_content(response))
|
|
|
|
self.assertEqual(json, {"a": 1})
|
|
|
|
|
2019-01-24 12:34:44 +03:00
|
|
|
def test_get_ip_address(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IP (with no port)
|
|
|
|
"""
|
2019-01-27 00:48:50 +03:00
|
|
|
# there will be a getaddrinfo on the IP
|
2019-01-24 12:34:44 +03:00
|
|
|
self.reactor.lookups["1.2.3.4"] = "1.2.3.4"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://1.2.3.4/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-24 12:34:44 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 08:12:11 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
2019-01-24 12:34:44 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"1.2.3.4"])
|
2019-01-24 16:28:07 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-28 12:56:59 +03:00
|
|
|
def test_get_ipv6_address(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IPv6 address
|
|
|
|
(with no port)
|
|
|
|
"""
|
|
|
|
|
2019-01-27 00:48:50 +03:00
|
|
|
# there will be a getaddrinfo on the IP
|
2019-01-28 12:56:59 +03:00
|
|
|
self.reactor.lookups["::1"] = "::1"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://[::1]/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "::1")
|
2019-01-28 12:56:59 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 08:12:11 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"[::1]"])
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
|
|
|
def test_get_ipv6_address_with_port(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IPv6 address
|
|
|
|
(with explicit port)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# there will be a getaddrinfo on the IP
|
|
|
|
self.reactor.lookups["::1"] = "::1"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://[::1]:80/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "::1")
|
2019-01-28 12:56:59 +03:00
|
|
|
self.assertEqual(port, 80)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-05-10 08:12:11 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"[::1]:80"])
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-06-10 18:06:25 +03:00
|
|
|
def test_get_hostname_bad_cert(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when the certificate on the server doesn't match the hostname
|
|
|
|
"""
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
self.reactor.lookups["testserv1"] = "1.2.3.4"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv1/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-06-10 18:06:25 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
# fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# attemptdelay on the hostnameendpoint is 0.3, so takes that long before the
|
|
|
|
# .well-known request fails.
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
|
|
|
# now there should be a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
|
|
|
b"_matrix._tcp.testserv1"
|
|
|
|
)
|
|
|
|
|
|
|
|
# we should fall back to a direct connection
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-06-10 18:06:25 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-06-20 12:32:02 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv1")
|
2019-06-10 18:06:25 +03:00
|
|
|
|
|
|
|
# there should be no requests
|
|
|
|
self.assertEqual(len(http_server.requests), 0)
|
|
|
|
|
|
|
|
# ... and the request should have failed
|
|
|
|
e = self.failureResultOf(test_d, ResponseNeverReceived)
|
|
|
|
failure_reason = e.value.reasons[0]
|
|
|
|
self.assertIsInstance(failure_reason.value, VerificationError)
|
|
|
|
|
|
|
|
def test_get_ip_address_bad_cert(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when the server name contains an explicit IP, but
|
|
|
|
the server cert doesn't cover it
|
|
|
|
"""
|
|
|
|
# there will be a getaddrinfo on the IP
|
|
|
|
self.reactor.lookups["1.2.3.5"] = "1.2.3.5"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://1.2.3.5/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.5")
|
2019-06-10 18:06:25 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(client_factory, expected_sni=None)
|
|
|
|
|
|
|
|
# there should be no requests
|
|
|
|
self.assertEqual(len(http_server.requests), 0)
|
|
|
|
|
|
|
|
# ... and the request should have failed
|
|
|
|
e = self.failureResultOf(test_d, ResponseNeverReceived)
|
|
|
|
failure_reason = e.value.reasons[0]
|
|
|
|
self.assertIsInstance(failure_reason.value, VerificationError)
|
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
def test_get_no_srv_no_well_known(self):
|
2019-01-24 16:28:07 +03:00
|
|
|
"""
|
2019-01-29 16:53:02 +03:00
|
|
|
Test the behaviour when the server name has no port, no SRV, and no well-known
|
2019-01-24 16:28:07 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
2019-01-24 16:28:07 +03:00
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
2019-01-24 16:28:07 +03:00
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
# fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# attemptdelay on the hostnameendpoint is 0.3, so takes that long before the
|
|
|
|
# .well-known request fails.
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# now there should be a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.testserv"
|
2019-02-01 02:13:44 +03:00
|
|
|
)
|
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
# we should fall back to a direct connection
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-24 16:28:07 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-06-20 12:32:02 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-01-24 16:28:07 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-01-24 16:28:07 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
def test_get_well_known(self):
|
2019-02-01 14:33:48 +03:00
|
|
|
"""Test the behaviour when the .well-known delegates elsewhere
|
2019-01-29 16:53:02 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["target-server"] = "1::f"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
2019-05-10 08:12:11 +03:00
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-02-01 14:33:48 +03:00
|
|
|
content=b'{ "m.server": "target-server" }',
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# there should be a SRV lookup
|
2019-01-29 16:53:02 +03:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.target-server"
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# now we should get a connection to the target server
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1::f")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-06-20 12:32:02 +03:00
|
|
|
client_factory, expected_sni=b"target-server"
|
2019-01-30 14:43:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-30 14:43:33 +03:00
|
|
|
self.assertEqual(
|
2019-06-20 12:32:02 +03:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"target-server"]
|
2019-01-30 14:43:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
|
|
|
self.assertEqual(self.well_known_cache[b"testserv"], b"target-server")
|
|
|
|
|
|
|
|
# check the cache expires
|
2019-08-20 13:46:00 +03:00
|
|
|
self.reactor.pump((48 * 3600,))
|
2019-01-30 14:43:33 +03:00
|
|
|
self.well_known_cache.expire()
|
|
|
|
self.assertNotIn(b"testserv", self.well_known_cache)
|
|
|
|
|
|
|
|
def test_get_well_known_redirect(self):
|
|
|
|
"""Test the behaviour when the server name has no port and no SRV record, but
|
|
|
|
the .well-known has a 300 redirect
|
|
|
|
"""
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["target-server"] = "1::f"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-30 14:43:33 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
redirect_server = self._make_connection(
|
2019-05-10 08:12:11 +03:00
|
|
|
client_factory, expected_sni=b"testserv"
|
2019-01-30 14:43:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# send a 302 redirect
|
|
|
|
self.assertEqual(len(redirect_server.requests), 1)
|
|
|
|
request = redirect_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
request.redirect(b"https://testserv/even_better_known")
|
2019-01-30 14:43:33 +03:00
|
|
|
request.finish()
|
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.reactor.pump((0.1,))
|
2019-01-30 14:43:33 +03:00
|
|
|
|
|
|
|
# now there should be another connection
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-30 14:43:33 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
well_known_server = self._make_connection(
|
2019-05-10 08:12:11 +03:00
|
|
|
client_factory, expected_sni=b"testserv"
|
2019-01-30 14:43:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(well_known_server.requests), 1, "No request after 302")
|
|
|
|
request = well_known_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/even_better_known")
|
2019-01-30 14:43:33 +03:00
|
|
|
request.write(b'{ "m.server": "target-server" }')
|
|
|
|
request.finish()
|
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.reactor.pump((0.1,))
|
2019-01-30 14:43:33 +03:00
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# there should be a SRV lookup
|
2019-01-30 14:43:33 +03:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.target-server"
|
2019-01-30 14:43:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# now we should get a connection to the target server
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1::f")
|
2019-01-30 14:43:33 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-06-20 12:32:02 +03:00
|
|
|
client_factory, expected_sni=b"target-server"
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(
|
2019-06-20 12:32:02 +03:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"target-server"]
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-30 13:55:25 +03:00
|
|
|
self.assertEqual(self.well_known_cache[b"testserv"], b"target-server")
|
|
|
|
|
|
|
|
# check the cache expires
|
2019-08-20 13:46:00 +03:00
|
|
|
self.reactor.pump((48 * 3600,))
|
2019-01-30 13:55:25 +03:00
|
|
|
self.well_known_cache.expire()
|
|
|
|
self.assertNotIn(b"testserv", self.well_known_cache)
|
|
|
|
|
2019-02-01 14:33:48 +03:00
|
|
|
def test_get_invalid_well_known(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when the server name has an *invalid* well-known (and no SRV)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-02-01 14:33:48 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
2019-06-20 12:32:02 +03:00
|
|
|
client_factory, expected_sni=b"testserv", content=b"NOT JSON"
|
2019-02-01 14:33:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# now there should be a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.testserv"
|
2019-02-01 14:33:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# we should fall back to a direct connection
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop()
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-02-01 14:33:48 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-06-20 12:32:02 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-02-01 14:33:48 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-02-01 14:33:48 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-06-10 18:06:25 +03:00
|
|
|
def test_get_well_known_unsigned_cert(self):
|
|
|
|
"""Test the behaviour when the .well-known server presents a cert
|
|
|
|
not signed by a CA
|
|
|
|
"""
|
|
|
|
|
2019-07-30 19:42:56 +03:00
|
|
|
# we use the same test server as the other tests, but use an agent with
|
|
|
|
# the config left to the default, which will not trust it (since the
|
|
|
|
# presented cert is signed by a test CA)
|
2019-06-10 18:06:25 +03:00
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2019-07-30 19:42:56 +03:00
|
|
|
config = default_config("test", parse=True)
|
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
# Build a new agent and WellKnownResolver with a different tls factory
|
|
|
|
tls_factory = ClientTLSOptionsFactory(config)
|
2019-06-10 18:06:25 +03:00
|
|
|
agent = MatrixFederationAgent(
|
|
|
|
reactor=self.reactor,
|
2019-08-13 20:04:46 +03:00
|
|
|
tls_client_options_factory=tls_factory,
|
2019-06-10 18:06:25 +03:00
|
|
|
_srv_resolver=self.mock_resolver,
|
2019-08-13 20:04:46 +03:00
|
|
|
_well_known_resolver=WellKnownResolver(
|
|
|
|
self.reactor,
|
|
|
|
Agent(self.reactor, contextFactory=tls_factory),
|
|
|
|
well_known_cache=self.well_known_cache,
|
|
|
|
had_well_known_cache=self.had_well_known_cache,
|
|
|
|
),
|
2019-06-10 18:06:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
test_d = agent.request(b"GET", b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-06-10 18:06:25 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
2019-06-20 12:32:02 +03:00
|
|
|
http_proto = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-06-10 18:06:25 +03:00
|
|
|
|
|
|
|
# there should be no requests
|
|
|
|
self.assertEqual(len(http_proto.requests), 0)
|
|
|
|
|
|
|
|
# and there should be a SRV lookup instead
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
|
|
|
b"_matrix._tcp.testserv"
|
|
|
|
)
|
|
|
|
|
2019-01-24 16:28:07 +03:00
|
|
|
def test_get_hostname_srv(self):
|
|
|
|
"""
|
|
|
|
Test the behaviour when there is a single SRV record
|
|
|
|
"""
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: [
|
2019-01-28 12:56:59 +03:00
|
|
|
Server(host=b"srvtarget", port=8443)
|
2019-01-24 16:28:07 +03:00
|
|
|
]
|
|
|
|
self.reactor.lookups["srvtarget"] = "1.2.3.4"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# the request for a .well-known will have failed with a DNS lookup error.
|
2019-01-24 16:29:33 +03:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.testserv"
|
2019-01-24 16:29:33 +03:00
|
|
|
)
|
2019-01-24 16:28:07 +03:00
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-24 16:28:07 +03:00
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
2019-06-20 12:32:02 +03:00
|
|
|
http_server = self._make_connection(client_factory, expected_sni=b"testserv")
|
2019-01-24 16:28:07 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
|
|
|
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"])
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
def test_get_well_known_srv(self):
|
2019-02-01 02:13:44 +03:00
|
|
|
"""Test the behaviour when the .well-known redirects to a place where there
|
|
|
|
is a SRV.
|
2019-01-29 16:53:02 +03:00
|
|
|
"""
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
self.reactor.lookups["srvtarget"] = "5.6.7.8"
|
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://testserv/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: [
|
2019-05-10 08:12:11 +03:00
|
|
|
Server(host=b"srvtarget", port=8443)
|
2019-01-29 16:53:02 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
2019-05-10 08:12:11 +03:00
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-02-01 14:33:48 +03:00
|
|
|
content=b'{ "m.server": "target-server" }',
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# there should be a SRV lookup
|
2019-01-29 16:53:02 +03:00
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.target-server"
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# now we should get a connection to the target of the SRV record
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "5.6.7.8")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-06-20 12:32:02 +03:00
|
|
|
client_factory, expected_sni=b"target-server"
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(
|
2019-06-20 12:32:02 +03:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"target-server"]
|
2019-01-29 16:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-28 12:56:59 +03:00
|
|
|
def test_idna_servername(self):
|
|
|
|
"""test the behaviour when the server name has idna chars in"""
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: []
|
|
|
|
|
2019-01-29 12:38:29 +03:00
|
|
|
# the resolver is always called with the IDNA hostname as a native string.
|
|
|
|
self.reactor.lookups["xn--bcher-kva.com"] = "1.2.3.4"
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
# this is idna for bücher.com
|
|
|
|
test_d = self._make_get_request(b"matrix://xn--bcher-kva.com/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# No SRV record lookup yet
|
|
|
|
self.mock_resolver.resolve_service.assert_not_called()
|
2019-01-28 12:56:59 +03:00
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
2019-01-28 12:56:59 +03:00
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-29 16:53:02 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
# fonx the connection
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# attemptdelay on the hostnameendpoint is 0.3, so takes that long before the
|
|
|
|
# .well-known request fails.
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
2019-02-01 02:13:44 +03:00
|
|
|
# now there should have been a SRV lookup
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.xn--bcher-kva.com"
|
2019-02-01 02:13:44 +03:00
|
|
|
)
|
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
# We should fall back to port 8448
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 2)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[1]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-28 12:56:59 +03:00
|
|
|
self.assertEqual(port, 8448)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-06-20 12:32:02 +03:00
|
|
|
client_factory, expected_sni=b"xn--bcher-kva.com"
|
2019-01-28 12:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-28 12:56:59 +03:00
|
|
|
self.assertEqual(
|
2019-06-20 12:32:02 +03:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"xn--bcher-kva.com"]
|
2019-01-28 12:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
|
|
|
def test_idna_srv_target(self):
|
|
|
|
"""test the behaviour when the target of a SRV record has idna chars"""
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.side_effect = lambda _: [
|
|
|
|
Server(host=b"xn--trget-3qa.com", port=8443) # târget.com
|
|
|
|
]
|
2019-01-29 12:38:29 +03:00
|
|
|
self.reactor.lookups["xn--trget-3qa.com"] = "1.2.3.4"
|
2019-01-28 12:56:59 +03:00
|
|
|
|
|
|
|
test_d = self._make_get_request(b"matrix://xn--bcher-kva.com/foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
self.mock_resolver.resolve_service.assert_called_once_with(
|
2019-05-10 08:12:11 +03:00
|
|
|
b"_matrix._tcp.xn--bcher-kva.com"
|
2019-01-28 12:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-28 12:56:59 +03:00
|
|
|
self.assertEqual(port, 8443)
|
|
|
|
|
|
|
|
# make a test server, and wire up the client
|
|
|
|
http_server = self._make_connection(
|
2019-06-20 12:32:02 +03:00
|
|
|
client_factory, expected_sni=b"xn--bcher-kva.com"
|
2019-01-28 12:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(http_server.requests), 1)
|
|
|
|
request = http_server.requests[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(request.method, b"GET")
|
|
|
|
self.assertEqual(request.path, b"/foo/bar")
|
2019-01-28 12:56:59 +03:00
|
|
|
self.assertEqual(
|
2019-06-20 12:32:02 +03:00
|
|
|
request.requestHeaders.getRawHeaders(b"host"), [b"xn--bcher-kva.com"]
|
2019-01-25 15:38:16 +03:00
|
|
|
)
|
2019-01-24 12:34:44 +03:00
|
|
|
|
|
|
|
# finish the request
|
|
|
|
request.finish()
|
|
|
|
self.reactor.pump((0.1,))
|
|
|
|
self.successResultOf(test_d)
|
|
|
|
|
2019-01-30 13:55:25 +03:00
|
|
|
def test_well_known_cache(self):
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
fetch_d = self.well_known_resolver.get_well_known(b"testserv")
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-30 13:55:25 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
well_known_server = self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-08-06 18:27:46 +03:00
|
|
|
response_headers={b"Cache-Control": b"max-age=1000"},
|
2019-02-01 14:33:48 +03:00
|
|
|
content=b'{ "m.server": "target-server" }',
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
2019-08-07 17:36:38 +03:00
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
# close the tcp connection
|
|
|
|
well_known_server.loseConnection()
|
|
|
|
|
|
|
|
# repeat the request: it should hit the cache
|
2019-08-13 20:04:46 +03:00
|
|
|
fetch_d = self.well_known_resolver.get_well_known(b"testserv")
|
2019-01-30 13:55:25 +03:00
|
|
|
r = self.successResultOf(fetch_d)
|
2019-08-07 17:36:38 +03:00
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
# expire the cache
|
2019-08-06 18:27:46 +03:00
|
|
|
self.reactor.pump((1000.0,))
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
# now it should connect again
|
2019-08-13 20:04:46 +03:00
|
|
|
fetch_d = self.well_known_resolver.get_well_known(b"testserv")
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-30 13:55:25 +03:00
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
2019-02-01 14:33:48 +03:00
|
|
|
content=b'{ "m.server": "other-server" }',
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
2019-08-07 17:36:38 +03:00
|
|
|
self.assertEqual(r.delegated_server, b"other-server")
|
2019-01-30 13:55:25 +03:00
|
|
|
|
2019-08-12 17:39:14 +03:00
|
|
|
def test_well_known_cache_with_temp_failure(self):
|
|
|
|
"""Test that we refetch well-known before the cache expires, and that
|
|
|
|
it ignores transient errors.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
fetch_d = self.well_known_resolver.get_well_known(b"testserv")
|
2019-08-12 17:39:14 +03:00
|
|
|
|
|
|
|
# there should be an attempt to connect on port 443 for the .well-known
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
self.assertEqual(host, "1.2.3.4")
|
|
|
|
self.assertEqual(port, 443)
|
|
|
|
|
|
|
|
well_known_server = self._handle_well_known_connection(
|
|
|
|
client_factory,
|
|
|
|
expected_sni=b"testserv",
|
|
|
|
response_headers={b"Cache-Control": b"max-age=1000"},
|
|
|
|
content=b'{ "m.server": "target-server" }',
|
|
|
|
)
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
|
|
|
|
|
|
|
# close the tcp connection
|
|
|
|
well_known_server.loseConnection()
|
|
|
|
|
|
|
|
# Get close to the cache expiry, this will cause the resolver to do
|
|
|
|
# another lookup.
|
|
|
|
self.reactor.pump((900.0,))
|
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
fetch_d = self.well_known_resolver.get_well_known(b"testserv")
|
2019-08-12 17:39:14 +03:00
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
# The resolver may retry a few times, so fonx all requests that come along
|
|
|
|
attempts = 0
|
|
|
|
while self.reactor.tcpClients:
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
2019-08-12 17:39:14 +03:00
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
# fonx the connection attempt, this will be treated as a temporary
|
|
|
|
# failure.
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
|
|
|
|
# There's a few sleeps involved, so we have to pump the reactor a
|
|
|
|
# bit.
|
|
|
|
self.reactor.pump((1.0, 1.0))
|
|
|
|
|
|
|
|
# We expect to see more than one attempt as there was previously a valid
|
|
|
|
# well known.
|
|
|
|
self.assertGreater(attempts, 1)
|
2019-08-12 17:39:14 +03:00
|
|
|
|
|
|
|
# Resolver should return cached value, despite the lookup failing.
|
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertEqual(r.delegated_server, b"target-server")
|
|
|
|
|
2019-08-13 20:04:46 +03:00
|
|
|
# Expire both caches and repeat the request
|
|
|
|
self.reactor.pump((10000.0,))
|
2019-08-12 17:39:14 +03:00
|
|
|
|
|
|
|
# Repated the request, this time it should fail if the lookup fails.
|
2019-08-13 20:04:46 +03:00
|
|
|
fetch_d = self.well_known_resolver.get_well_known(b"testserv")
|
2019-08-12 17:39:14 +03:00
|
|
|
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
|
|
|
|
client_factory.clientConnectionFailed(None, Exception("nope"))
|
|
|
|
self.reactor.pump((0.4,))
|
|
|
|
|
|
|
|
r = self.successResultOf(fetch_d)
|
|
|
|
self.assertEqual(r.delegated_server, None)
|
|
|
|
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
class TestCachePeriodFromHeaders(TestCase):
|
|
|
|
def test_cache_control(self):
|
|
|
|
# uppercase
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-06-20 12:32:02 +03:00
|
|
|
Headers({b"Cache-Control": [b"foo, Max-Age = 100, bar"]})
|
2019-05-10 08:12:11 +03:00
|
|
|
),
|
|
|
|
100,
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# missing value
|
2019-05-10 08:12:11 +03:00
|
|
|
self.assertIsNone(
|
2019-06-20 12:32:02 +03:00
|
|
|
_cache_period_from_headers(Headers({b"Cache-Control": [b"max-age=, bar"]}))
|
2019-05-10 08:12:11 +03:00
|
|
|
)
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
# hackernews: bogus due to semicolon
|
2019-05-10 08:12:11 +03:00
|
|
|
self.assertIsNone(
|
|
|
|
_cache_period_from_headers(
|
2019-06-20 12:32:02 +03:00
|
|
|
Headers({b"Cache-Control": [b"private; max-age=0"]})
|
2019-05-10 08:12:11 +03:00
|
|
|
)
|
|
|
|
)
|
2019-01-30 13:55:25 +03:00
|
|
|
|
|
|
|
# github
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-06-20 12:32:02 +03:00
|
|
|
Headers({b"Cache-Control": [b"max-age=0, private, must-revalidate"]})
|
2019-05-10 08:12:11 +03:00
|
|
|
),
|
|
|
|
0,
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# google
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-06-20 12:32:02 +03:00
|
|
|
Headers({b"cache-control": [b"private, max-age=0"]})
|
2019-05-10 08:12:11 +03:00
|
|
|
),
|
|
|
|
0,
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_expires(self):
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-06-20 12:32:02 +03:00
|
|
|
Headers({b"Expires": [b"Wed, 30 Jan 2019 07:35:33 GMT"]}),
|
2019-05-10 08:12:11 +03:00
|
|
|
time_now=lambda: 1548833700,
|
|
|
|
),
|
|
|
|
33,
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# cache-control overrides expires
|
|
|
|
self.assertEqual(
|
|
|
|
_cache_period_from_headers(
|
2019-05-10 08:12:11 +03:00
|
|
|
Headers(
|
|
|
|
{
|
2019-06-20 12:32:02 +03:00
|
|
|
b"cache-control": [b"max-age=10"],
|
|
|
|
b"Expires": [b"Wed, 30 Jan 2019 07:35:33 GMT"],
|
2019-05-10 08:12:11 +03:00
|
|
|
}
|
|
|
|
),
|
|
|
|
time_now=lambda: 1548833700,
|
|
|
|
),
|
|
|
|
10,
|
2019-01-30 13:55:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# invalid expires means immediate expiry
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(_cache_period_from_headers(Headers({b"Expires": [b"0"]})), 0)
|
2019-01-30 13:55:25 +03:00
|
|
|
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
def _check_logcontext(context):
|
|
|
|
current = LoggingContext.current_context()
|
|
|
|
if current is not context:
|
2019-05-10 08:12:11 +03:00
|
|
|
raise AssertionError("Expected logcontext %s but was %s" % (context, current))
|
2019-01-22 23:28:48 +03:00
|
|
|
|
|
|
|
|
2019-06-10 18:06:25 +03:00
|
|
|
def _build_test_server(connection_creator):
|
2019-01-22 23:28:48 +03:00
|
|
|
"""Construct a test server
|
|
|
|
|
|
|
|
This builds an HTTP channel, wrapped with a TLSMemoryBIOProtocol
|
|
|
|
|
2019-06-10 18:06:25 +03:00
|
|
|
Args:
|
|
|
|
connection_creator (IOpenSSLServerConnectionCreator): thing to build
|
|
|
|
SSL connections
|
|
|
|
sanlist (list[bytes]): list of the SAN entries for the cert returned
|
|
|
|
by the server
|
|
|
|
|
2019-01-22 23:28:48 +03:00
|
|
|
Returns:
|
|
|
|
TLSMemoryBIOProtocol
|
|
|
|
"""
|
|
|
|
server_factory = Factory.forProtocol(HTTPChannel)
|
|
|
|
# Request.finish expects the factory to have a 'log' method.
|
|
|
|
server_factory.log = _log_request
|
|
|
|
|
|
|
|
server_tls_factory = TLSMemoryBIOFactory(
|
2019-06-10 18:06:25 +03:00
|
|
|
connection_creator, isClient=False, wrappedFactory=server_factory
|
2019-01-22 23:28:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return server_tls_factory.buildProtocol(None)
|
|
|
|
|
|
|
|
|
|
|
|
def _log_request(request):
|
|
|
|
"""Implements Factory.log, which is expected by Request.finish"""
|
|
|
|
logger.info("Completed request %s", request)
|
2019-01-29 16:53:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
@implementer(IPolicyForHTTPS)
|
|
|
|
class TrustingTLSPolicyForHTTPS(object):
|
2019-06-10 18:06:25 +03:00
|
|
|
"""An IPolicyForHTTPS which checks that the certificate belongs to the
|
|
|
|
right server, but doesn't check the certificate chain."""
|
2019-05-10 08:12:11 +03:00
|
|
|
|
2019-01-29 16:53:02 +03:00
|
|
|
def creatorForNetloc(self, hostname, port):
|
|
|
|
certificateOptions = OpenSSLCertificateOptions()
|
|
|
|
return ClientTLSOptions(hostname, certificateOptions.getContext())
|