test_fedclient.py 18 KB

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