mirror of
https://github.com/element-hq/synapse
synced 2024-09-12 12:55:11 +00:00
Allow configuring the set of workers to proxy outbound federation traffic through (`outbound_federation_restricted_to`). This is useful when you have a worker setup with `federation_sender` instances responsible for sending outbound federation requests and want to make sure *all* outbound federation traffic goes through those instances. Before this change, the generic workers would still contact federation themselves for things like profile lookups, backfill, etc. This PR allows you to set more strict access controls/firewall for all workers and only allow the `federation_sender`'s to contact the outside world.
134 lines
4.5 KiB
Python
134 lines
4.5 KiB
Python
# Copyright 2019 New Vector Ltd
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
from typing import List
|
|
from unittest.mock import Mock, patch
|
|
|
|
from parameterized import parameterized
|
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
from synapse.app.generic_worker import GenericWorkerServer
|
|
from synapse.app.homeserver import SynapseHomeServer
|
|
from synapse.config.server import parse_listener_def
|
|
from synapse.server import HomeServer
|
|
from synapse.types import JsonDict
|
|
from synapse.util import Clock
|
|
|
|
from tests.server import make_request
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
class FederationReaderOpenIDListenerTests(HomeserverTestCase):
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
|
hs = self.setup_test_homeserver(homeserver_to_use=GenericWorkerServer)
|
|
return hs
|
|
|
|
def default_config(self) -> JsonDict:
|
|
conf = super().default_config()
|
|
# we're using GenericWorkerServer, which uses a GenericWorkerStore, so we
|
|
# have to tell the FederationHandler not to try to access stuff that is only
|
|
# in the primary store.
|
|
conf["worker_app"] = "yes"
|
|
conf["instance_map"] = {"main": {"host": "127.0.0.1", "port": 0}}
|
|
|
|
return conf
|
|
|
|
@parameterized.expand(
|
|
[
|
|
(["federation"], "auth_fail"),
|
|
([], "no_resource"),
|
|
(["openid", "federation"], "auth_fail"),
|
|
(["openid"], "auth_fail"),
|
|
]
|
|
)
|
|
def test_openid_listener(self, names: List[str], expectation: str) -> None:
|
|
"""
|
|
Test different openid listener configurations.
|
|
|
|
401 is success here since it means we hit the handler and auth failed.
|
|
"""
|
|
config = {
|
|
"port": 8080,
|
|
"type": "http",
|
|
"bind_addresses": ["0.0.0.0"],
|
|
"resources": [{"names": names}],
|
|
}
|
|
|
|
# Listen with the config
|
|
hs = self.hs
|
|
assert isinstance(hs, GenericWorkerServer)
|
|
hs._listen_http(parse_listener_def(0, config))
|
|
|
|
# Grab the resource from the site that was told to listen
|
|
site = self.reactor.tcpServers[0][1]
|
|
try:
|
|
site.resource.children[b"_matrix"].children[b"federation"]
|
|
except KeyError:
|
|
if expectation == "no_resource":
|
|
return
|
|
raise
|
|
|
|
channel = make_request(
|
|
self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
|
|
)
|
|
|
|
self.assertEqual(channel.code, 401)
|
|
|
|
|
|
@patch("synapse.app.homeserver.KeyResource", new=Mock())
|
|
class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
|
hs = self.setup_test_homeserver(homeserver_to_use=SynapseHomeServer)
|
|
return hs
|
|
|
|
@parameterized.expand(
|
|
[
|
|
(["federation"], "auth_fail"),
|
|
([], "no_resource"),
|
|
(["openid", "federation"], "auth_fail"),
|
|
(["openid"], "auth_fail"),
|
|
]
|
|
)
|
|
def test_openid_listener(self, names: List[str], expectation: str) -> None:
|
|
"""
|
|
Test different openid listener configurations.
|
|
|
|
401 is success here since it means we hit the handler and auth failed.
|
|
"""
|
|
config = {
|
|
"port": 8080,
|
|
"type": "http",
|
|
"bind_addresses": ["0.0.0.0"],
|
|
"resources": [{"names": names}],
|
|
}
|
|
|
|
# Listen with the config
|
|
hs = self.hs
|
|
assert isinstance(hs, SynapseHomeServer)
|
|
hs._listener_http(self.hs.config, parse_listener_def(0, config))
|
|
|
|
# Grab the resource from the site that was told to listen
|
|
site = self.reactor.tcpServers[0][1]
|
|
try:
|
|
site.resource.children[b"_matrix"].children[b"federation"]
|
|
except KeyError:
|
|
if expectation == "no_resource":
|
|
return
|
|
raise
|
|
|
|
channel = make_request(
|
|
self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
|
|
)
|
|
|
|
self.assertEqual(channel.code, 401)
|