test_auth.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. """
  68. channel = self._test_register()
  69. self.assertEqual(channel.code, 200)
  70. # We're given a registered user.
  71. self.assertEqual(channel.json_body["user_id"], "@user:test")
  72. @override_config({"main_replication_secret": "my-secret"})
  73. def test_missing_auth(self):
  74. """If the main process expects a secret that is not provided, an error results.
  75. """
  76. channel = self._test_register()
  77. self.assertEqual(channel.code, 500)
  78. @override_config(
  79. {
  80. "main_replication_secret": "my-secret",
  81. "worker_replication_secret": "wrong-secret",
  82. }
  83. )
  84. def test_unauthorized(self):
  85. """If the main process receives the wrong secret, an error results.
  86. """
  87. channel = self._test_register()
  88. self.assertEqual(channel.code, 500)
  89. @override_config({"worker_replication_secret": "my-secret"})
  90. def test_authorized(self):
  91. """The request should finish when the worker provides the authentication header.
  92. """
  93. channel = self._test_register()
  94. self.assertEqual(channel.code, 200)
  95. # We're given a registered user.
  96. self.assertEqual(channel.json_body["user_id"], "@user:test")