test_auth.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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. import logging
  16. from synapse.rest.client.v2_alpha import register
  17. from tests.replication._base import BaseMultiWorkerStreamTestCase
  18. from tests.server import FakeChannel, make_request
  19. from tests.unittest import override_config
  20. logger = logging.getLogger(__name__)
  21. class WorkerAuthenticationTestCase(BaseMultiWorkerStreamTestCase):
  22. """Test the authentication of HTTP calls between workers."""
  23. servlets = [register.register_servlets]
  24. def make_homeserver(self, reactor, clock):
  25. config = self.default_config()
  26. # This isn't a real configuration option but is used to provide the main
  27. # homeserver and worker homeserver different options.
  28. main_replication_secret = config.pop("main_replication_secret", None)
  29. if main_replication_secret:
  30. config["worker_replication_secret"] = main_replication_secret
  31. return self.setup_test_homeserver(config=config)
  32. def _get_worker_hs_config(self) -> dict:
  33. config = self.default_config()
  34. config["worker_app"] = "synapse.app.client_reader"
  35. config["worker_replication_host"] = "testserv"
  36. config["worker_replication_http_port"] = "8765"
  37. return config
  38. def _test_register(self) -> FakeChannel:
  39. """Run the actual test:
  40. 1. Create a worker homeserver.
  41. 2. Start registration by providing a user/password.
  42. 3. Complete registration by providing dummy auth (this hits the main synapse).
  43. 4. Return the final request.
  44. """
  45. worker_hs = self.make_worker_hs("synapse.app.client_reader")
  46. site = self._hs_to_site[worker_hs]
  47. channel_1 = make_request(
  48. self.reactor,
  49. site,
  50. "POST",
  51. "register",
  52. {"username": "user", "type": "m.login.password", "password": "bar"},
  53. )
  54. self.assertEqual(channel_1.code, 401)
  55. # Grab the session
  56. session = channel_1.json_body["session"]
  57. # also complete the dummy auth
  58. return make_request(
  59. self.reactor,
  60. site,
  61. "POST",
  62. "register",
  63. {"auth": {"session": session, "type": "m.login.dummy"}},
  64. )
  65. def test_no_auth(self):
  66. """With no authentication the request should finish."""
  67. channel = self._test_register()
  68. self.assertEqual(channel.code, 200)
  69. # We're given a registered user.
  70. self.assertEqual(channel.json_body["user_id"], "@user:test")
  71. @override_config({"main_replication_secret": "my-secret"})
  72. def test_missing_auth(self):
  73. """If the main process expects a secret that is not provided, an error results."""
  74. channel = self._test_register()
  75. self.assertEqual(channel.code, 500)
  76. @override_config(
  77. {
  78. "main_replication_secret": "my-secret",
  79. "worker_replication_secret": "wrong-secret",
  80. }
  81. )
  82. def test_unauthorized(self):
  83. """If the main process receives the wrong secret, an error results."""
  84. channel = self._test_register()
  85. self.assertEqual(channel.code, 500)
  86. @override_config({"worker_replication_secret": "my-secret"})
  87. def test_authorized(self):
  88. """The request should finish when the worker provides the authentication header."""
  89. channel = self._test_register()
  90. self.assertEqual(channel.code, 200)
  91. # We're given a registered user.
  92. self.assertEqual(channel.json_body["user_id"], "@user:test")