You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

114 lines
4.1 KiB

  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. 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.generic_worker")
  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) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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")