test_fedclient.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from netaddr import IPSet
  17. from twisted.internet import defer
  18. from twisted.internet.defer import TimeoutError
  19. from twisted.internet.error import ConnectingCancelledError, DNSLookupError
  20. from twisted.test.proto_helpers import StringTransport
  21. from twisted.web.client import ResponseNeverReceived
  22. from twisted.web.http import HTTPChannel
  23. from synapse.api.errors import RequestSendFailed
  24. from synapse.http.matrixfederationclient import (
  25. MatrixFederationHttpClient,
  26. MatrixFederationRequest,
  27. )
  28. from synapse.logging.context import SENTINEL_CONTEXT, LoggingContext, current_context
  29. from tests.server import FakeTransport
  30. from tests.unittest import HomeserverTestCase
  31. def check_logcontext(context):
  32. current = current_context()
  33. if current is not context:
  34. raise AssertionError("Expected logcontext %s but was %s" % (context, current))
  35. class FederationClientTests(HomeserverTestCase):
  36. def make_homeserver(self, reactor, clock):
  37. hs = self.setup_test_homeserver(reactor=reactor, clock=clock)
  38. return hs
  39. def prepare(self, reactor, clock, homeserver):
  40. self.cl = MatrixFederationHttpClient(self.hs, None)
  41. self.reactor.lookups["testserv"] = "1.2.3.4"
  42. def test_client_get(self):
  43. """
  44. happy-path test of a GET request
  45. """
  46. @defer.inlineCallbacks
  47. def do_request():
  48. with LoggingContext("one") as context:
  49. fetch_d = defer.ensureDeferred(
  50. self.cl.get_json("testserv:8008", "foo/bar")
  51. )
  52. # Nothing happened yet
  53. self.assertNoResult(fetch_d)
  54. # should have reset logcontext to the sentinel
  55. check_logcontext(SENTINEL_CONTEXT)
  56. try:
  57. fetch_res = yield fetch_d
  58. return fetch_res
  59. finally:
  60. check_logcontext(context)
  61. test_d = do_request()
  62. self.pump()
  63. # Nothing happened yet
  64. self.assertNoResult(test_d)
  65. # Make sure treq is trying to connect
  66. clients = self.reactor.tcpClients
  67. self.assertEqual(len(clients), 1)
  68. (host, port, factory, _timeout, _bindAddress) = clients[0]
  69. self.assertEqual(host, "1.2.3.4")
  70. self.assertEqual(port, 8008)
  71. # complete the connection and wire it up to a fake transport
  72. protocol = factory.buildProtocol(None)
  73. transport = StringTransport()
  74. protocol.makeConnection(transport)
  75. # that should have made it send the request to the transport
  76. self.assertRegex(transport.value(), b"^GET /foo/bar")
  77. self.assertRegex(transport.value(), b"Host: testserv:8008")
  78. # Deferred is still without a result
  79. self.assertNoResult(test_d)
  80. # Send it the HTTP response
  81. res_json = '{ "a": 1 }'.encode("ascii")
  82. protocol.dataReceived(
  83. b"HTTP/1.1 200 OK\r\n"
  84. b"Server: Fake\r\n"
  85. b"Content-Type: application/json\r\n"
  86. b"Content-Length: %i\r\n"
  87. b"\r\n"
  88. b"%s" % (len(res_json), res_json)
  89. )
  90. self.pump()
  91. res = self.successResultOf(test_d)
  92. # check the response is as expected
  93. self.assertEqual(res, {"a": 1})
  94. def test_dns_error(self):
  95. """
  96. If the DNS lookup returns an error, it will bubble up.
  97. """
  98. d = defer.ensureDeferred(
  99. self.cl.get_json("testserv2:8008", "foo/bar", timeout=10000)
  100. )
  101. self.pump()
  102. f = self.failureResultOf(d)
  103. self.assertIsInstance(f.value, RequestSendFailed)
  104. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  105. def test_client_connection_refused(self):
  106. d = defer.ensureDeferred(
  107. self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
  108. )
  109. self.pump()
  110. # Nothing happened yet
  111. self.assertNoResult(d)
  112. clients = self.reactor.tcpClients
  113. self.assertEqual(len(clients), 1)
  114. (host, port, factory, _timeout, _bindAddress) = clients[0]
  115. self.assertEqual(host, "1.2.3.4")
  116. self.assertEqual(port, 8008)
  117. e = Exception("go away")
  118. factory.clientConnectionFailed(None, e)
  119. self.pump(0.5)
  120. f = self.failureResultOf(d)
  121. self.assertIsInstance(f.value, RequestSendFailed)
  122. self.assertIs(f.value.inner_exception, e)
  123. def test_client_never_connect(self):
  124. """
  125. If the HTTP request is not connected and is timed out, it'll give a
  126. ConnectingCancelledError or TimeoutError.
  127. """
  128. d = defer.ensureDeferred(
  129. self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
  130. )
  131. self.pump()
  132. # Nothing happened yet
  133. self.assertNoResult(d)
  134. # Make sure treq is trying to connect
  135. clients = self.reactor.tcpClients
  136. self.assertEqual(len(clients), 1)
  137. self.assertEqual(clients[0][0], "1.2.3.4")
  138. self.assertEqual(clients[0][1], 8008)
  139. # Deferred is still without a result
  140. self.assertNoResult(d)
  141. # Push by enough to time it out
  142. self.reactor.advance(10.5)
  143. f = self.failureResultOf(d)
  144. self.assertIsInstance(f.value, RequestSendFailed)
  145. self.assertIsInstance(
  146. f.value.inner_exception, (ConnectingCancelledError, TimeoutError)
  147. )
  148. def test_client_connect_no_response(self):
  149. """
  150. If the HTTP request is connected, but gets no response before being
  151. timed out, it'll give a ResponseNeverReceived.
  152. """
  153. d = defer.ensureDeferred(
  154. self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
  155. )
  156. self.pump()
  157. # Nothing happened yet
  158. self.assertNoResult(d)
  159. # Make sure treq is trying to connect
  160. clients = self.reactor.tcpClients
  161. self.assertEqual(len(clients), 1)
  162. self.assertEqual(clients[0][0], "1.2.3.4")
  163. self.assertEqual(clients[0][1], 8008)
  164. conn = Mock()
  165. client = clients[0][2].buildProtocol(None)
  166. client.makeConnection(conn)
  167. # Deferred is still without a result
  168. self.assertNoResult(d)
  169. # Push by enough to time it out
  170. self.reactor.advance(10.5)
  171. f = self.failureResultOf(d)
  172. self.assertIsInstance(f.value, RequestSendFailed)
  173. self.assertIsInstance(f.value.inner_exception, ResponseNeverReceived)
  174. def test_client_ip_range_blacklist(self):
  175. """Ensure that Synapse does not try to connect to blacklisted IPs"""
  176. # Set up the ip_range blacklist
  177. self.hs.config.federation_ip_range_blacklist = IPSet(
  178. ["127.0.0.0/8", "fe80::/64"]
  179. )
  180. self.reactor.lookups["internal"] = "127.0.0.1"
  181. self.reactor.lookups["internalv6"] = "fe80:0:0:0:0:8a2e:370:7337"
  182. self.reactor.lookups["fine"] = "10.20.30.40"
  183. cl = MatrixFederationHttpClient(self.hs, None)
  184. # Try making a GET request to a blacklisted IPv4 address
  185. # ------------------------------------------------------
  186. # Make the request
  187. d = defer.ensureDeferred(cl.get_json("internal:8008", "foo/bar", timeout=10000))
  188. # Nothing happened yet
  189. self.assertNoResult(d)
  190. self.pump(1)
  191. # Check that it was unable to resolve the address
  192. clients = self.reactor.tcpClients
  193. self.assertEqual(len(clients), 0)
  194. f = self.failureResultOf(d)
  195. self.assertIsInstance(f.value, RequestSendFailed)
  196. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  197. # Try making a POST request to a blacklisted IPv6 address
  198. # -------------------------------------------------------
  199. # Make the request
  200. d = defer.ensureDeferred(
  201. cl.post_json("internalv6:8008", "foo/bar", timeout=10000)
  202. )
  203. # Nothing has happened yet
  204. self.assertNoResult(d)
  205. # Move the reactor forwards
  206. self.pump(1)
  207. # Check that it was unable to resolve the address
  208. clients = self.reactor.tcpClients
  209. self.assertEqual(len(clients), 0)
  210. # Check that it was due to a blacklisted DNS lookup
  211. f = self.failureResultOf(d, RequestSendFailed)
  212. self.assertIsInstance(f.value.inner_exception, DNSLookupError)
  213. # Try making a GET request to a non-blacklisted IPv4 address
  214. # ----------------------------------------------------------
  215. # Make the request
  216. d = defer.ensureDeferred(cl.post_json("fine:8008", "foo/bar", timeout=10000))
  217. # Nothing has happened yet
  218. self.assertNoResult(d)
  219. # Move the reactor forwards
  220. self.pump(1)
  221. # Check that it was able to resolve the address
  222. clients = self.reactor.tcpClients
  223. self.assertNotEqual(len(clients), 0)
  224. # Connection will still fail as this IP address does not resolve to anything
  225. f = self.failureResultOf(d, RequestSendFailed)
  226. self.assertIsInstance(f.value.inner_exception, ConnectingCancelledError)
  227. def test_client_gets_headers(self):
  228. """
  229. Once the client gets the headers, _request returns successfully.
  230. """
  231. request = MatrixFederationRequest(
  232. method="GET", destination="testserv:8008", path="foo/bar"
  233. )
  234. d = defer.ensureDeferred(self.cl._send_request(request, timeout=10000))
  235. self.pump()
  236. conn = Mock()
  237. clients = self.reactor.tcpClients
  238. client = clients[0][2].buildProtocol(None)
  239. client.makeConnection(conn)
  240. # Deferred does not have a result
  241. self.assertNoResult(d)
  242. # Send it the HTTP response
  243. client.dataReceived(b"HTTP/1.1 200 OK\r\nServer: Fake\r\n\r\n")
  244. # We should get a successful response
  245. r = self.successResultOf(d)
  246. self.assertEqual(r.code, 200)
  247. def test_client_headers_no_body(self):
  248. """
  249. If the HTTP request is connected, but gets no response before being
  250. timed out, it'll give a ResponseNeverReceived.
  251. """
  252. d = defer.ensureDeferred(
  253. self.cl.post_json("testserv:8008", "foo/bar", timeout=10000)
  254. )
  255. self.pump()
  256. conn = Mock()
  257. clients = self.reactor.tcpClients
  258. client = clients[0][2].buildProtocol(None)
  259. client.makeConnection(conn)
  260. # Deferred does not have a result
  261. self.assertNoResult(d)
  262. # Send it the HTTP response
  263. client.dataReceived(
  264. (
  265. b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
  266. b"Server: Fake\r\n\r\n"
  267. )
  268. )
  269. # Push by enough to time it out
  270. self.reactor.advance(10.5)
  271. f = self.failureResultOf(d)
  272. self.assertIsInstance(f.value, TimeoutError)
  273. def test_client_requires_trailing_slashes(self):
  274. """
  275. If a connection is made to a client but the client rejects it due to
  276. requiring a trailing slash. We need to retry the request with a
  277. trailing slash. Workaround for Synapse <= v0.99.3, explained in #3622.
  278. """
  279. d = defer.ensureDeferred(
  280. self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
  281. )
  282. # Send the request
  283. self.pump()
  284. # there should have been a call to connectTCP
  285. clients = self.reactor.tcpClients
  286. self.assertEqual(len(clients), 1)
  287. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  288. # complete the connection and wire it up to a fake transport
  289. client = factory.buildProtocol(None)
  290. conn = StringTransport()
  291. client.makeConnection(conn)
  292. # that should have made it send the request to the connection
  293. self.assertRegex(conn.value(), b"^GET /foo/bar")
  294. # Clear the original request data before sending a response
  295. conn.clear()
  296. # Send the HTTP response
  297. client.dataReceived(
  298. b"HTTP/1.1 400 Bad Request\r\n"
  299. b"Content-Type: application/json\r\n"
  300. b"Content-Length: 59\r\n"
  301. b"\r\n"
  302. b'{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}'
  303. )
  304. # We should get another request with a trailing slash
  305. self.assertRegex(conn.value(), b"^GET /foo/bar/")
  306. # Send a happy response this time
  307. client.dataReceived(
  308. b"HTTP/1.1 200 OK\r\n"
  309. b"Content-Type: application/json\r\n"
  310. b"Content-Length: 2\r\n"
  311. b"\r\n"
  312. b"{}"
  313. )
  314. # We should get a successful response
  315. r = self.successResultOf(d)
  316. self.assertEqual(r, {})
  317. def test_client_does_not_retry_on_400_plus(self):
  318. """
  319. Another test for trailing slashes but now test that we don't retry on
  320. trailing slashes on a non-400/M_UNRECOGNIZED response.
  321. See test_client_requires_trailing_slashes() for context.
  322. """
  323. d = defer.ensureDeferred(
  324. self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
  325. )
  326. # Send the request
  327. self.pump()
  328. # there should have been a call to connectTCP
  329. clients = self.reactor.tcpClients
  330. self.assertEqual(len(clients), 1)
  331. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  332. # complete the connection and wire it up to a fake transport
  333. client = factory.buildProtocol(None)
  334. conn = StringTransport()
  335. client.makeConnection(conn)
  336. # that should have made it send the request to the connection
  337. self.assertRegex(conn.value(), b"^GET /foo/bar")
  338. # Clear the original request data before sending a response
  339. conn.clear()
  340. # Send the HTTP response
  341. client.dataReceived(
  342. b"HTTP/1.1 404 Not Found\r\n"
  343. b"Content-Type: application/json\r\n"
  344. b"Content-Length: 2\r\n"
  345. b"\r\n"
  346. b"{}"
  347. )
  348. # We should not get another request
  349. self.assertEqual(conn.value(), b"")
  350. # We should get a 404 failure response
  351. self.failureResultOf(d)
  352. def test_client_sends_body(self):
  353. defer.ensureDeferred(
  354. self.cl.post_json(
  355. "testserv:8008", "foo/bar", timeout=10000, data={"a": "b"}
  356. )
  357. )
  358. self.pump()
  359. clients = self.reactor.tcpClients
  360. self.assertEqual(len(clients), 1)
  361. client = clients[0][2].buildProtocol(None)
  362. server = HTTPChannel()
  363. client.makeConnection(FakeTransport(server, self.reactor))
  364. server.makeConnection(FakeTransport(client, self.reactor))
  365. self.pump(0.1)
  366. self.assertEqual(len(server.requests), 1)
  367. request = server.requests[0]
  368. content = request.content.read()
  369. self.assertEqual(content, b'{"a":"b"}')
  370. def test_closes_connection(self):
  371. """Check that the client closes unused HTTP connections"""
  372. d = defer.ensureDeferred(self.cl.get_json("testserv:8008", "foo/bar"))
  373. self.pump()
  374. # there should have been a call to connectTCP
  375. clients = self.reactor.tcpClients
  376. self.assertEqual(len(clients), 1)
  377. (_host, _port, factory, _timeout, _bindAddress) = clients[0]
  378. # complete the connection and wire it up to a fake transport
  379. client = factory.buildProtocol(None)
  380. conn = StringTransport()
  381. client.makeConnection(conn)
  382. # that should have made it send the request to the connection
  383. self.assertRegex(conn.value(), b"^GET /foo/bar")
  384. # Send the HTTP response
  385. client.dataReceived(
  386. b"HTTP/1.1 200 OK\r\n"
  387. b"Content-Type: application/json\r\n"
  388. b"Content-Length: 2\r\n"
  389. b"\r\n"
  390. b"{}"
  391. )
  392. # We should get a successful response
  393. r = self.successResultOf(d)
  394. self.assertEqual(r, {})
  395. self.assertFalse(conn.disconnecting)
  396. # wait for a while
  397. self.pump(120)
  398. self.assertTrue(conn.disconnecting)