test_auth.py 4.2 KB

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