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