test_auth.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from synapse.rest.client import register
  16. from tests.replication._base import BaseMultiWorkerStreamTestCase
  17. from tests.server import FakeChannel, make_request
  18. from tests.unittest import override_config
  19. logger = logging.getLogger(__name__)
  20. class WorkerAuthenticationTestCase(BaseMultiWorkerStreamTestCase):
  21. """Test the authentication of HTTP calls between workers."""
  22. servlets = [register.register_servlets]
  23. def make_homeserver(self, reactor, clock):
  24. config = self.default_config()
  25. # This isn't a real configuration option but is used to provide the main
  26. # homeserver and worker homeserver different options.
  27. main_replication_secret = config.pop("main_replication_secret", None)
  28. if main_replication_secret:
  29. config["worker_replication_secret"] = main_replication_secret
  30. return self.setup_test_homeserver(config=config)
  31. def _get_worker_hs_config(self) -> dict:
  32. config = self.default_config()
  33. config["worker_app"] = "synapse.app.client_reader"
  34. config["worker_replication_host"] = "testserv"
  35. config["worker_replication_http_port"] = "8765"
  36. return config
  37. def _test_register(self) -> FakeChannel:
  38. """Run the actual test:
  39. 1. Create a worker homeserver.
  40. 2. Start registration by providing a user/password.
  41. 3. Complete registration by providing dummy auth (this hits the main synapse).
  42. 4. Return the final request.
  43. """
  44. worker_hs = self.make_worker_hs("synapse.app.client_reader")
  45. site = self._hs_to_site[worker_hs]
  46. channel_1 = make_request(
  47. self.reactor,
  48. site,
  49. "POST",
  50. "register",
  51. {"username": "user", "type": "m.login.password", "password": "bar"},
  52. )
  53. self.assertEqual(channel_1.code, 401)
  54. # Grab the session
  55. session = channel_1.json_body["session"]
  56. # also complete the dummy auth
  57. return make_request(
  58. self.reactor,
  59. site,
  60. "POST",
  61. "register",
  62. {"auth": {"session": session, "type": "m.login.dummy"}},
  63. )
  64. def test_no_auth(self):
  65. """With no authentication the request should finish."""
  66. channel = self._test_register()
  67. self.assertEqual(channel.code, 200)
  68. # We're given a registered user.
  69. self.assertEqual(channel.json_body["user_id"], "@user:test")
  70. @override_config({"main_replication_secret": "my-secret"})
  71. def test_missing_auth(self):
  72. """If the main process expects a secret that is not provided, an error results."""
  73. channel = self._test_register()
  74. self.assertEqual(channel.code, 500)
  75. @override_config(
  76. {
  77. "main_replication_secret": "my-secret",
  78. "worker_replication_secret": "wrong-secret",
  79. }
  80. )
  81. def test_unauthorized(self):
  82. """If the main process receives the wrong secret, an error results."""
  83. channel = self._test_register()
  84. self.assertEqual(channel.code, 500)
  85. @override_config({"worker_replication_secret": "my-secret"})
  86. def test_authorized(self):
  87. """The request should finish when the worker provides the authentication header."""
  88. channel = self._test_register()
  89. self.assertEqual(channel.code, 200)
  90. # We're given a registered user.
  91. self.assertEqual(channel.json_body["user_id"], "@user:test")