Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Disable federation when using SQLite by default #5078

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c58538d
Add config for disabling/enabling federation when using SQLite
babolivier Apr 18, 2019
6a72d7a
Deny federation traffic if config says so
babolivier Apr 18, 2019
8050fa4
Merge branch 'develop' into babolivier/sqlite_federation
babolivier Apr 18, 2019
56ac502
fix
babolivier Apr 18, 2019
415a3fe
Changelog
babolivier Apr 18, 2019
051e8b5
fix
babolivier Apr 18, 2019
7fd865e
Sample config
babolivier Apr 18, 2019
6629da6
Fix unit tests
babolivier Apr 18, 2019
50c2dd6
Lint
babolivier Apr 18, 2019
e751bb0
pop instead of get
babolivier Apr 18, 2019
7911334
i give up
babolivier Apr 18, 2019
e44b275
sample conf
babolivier Apr 18, 2019
25de64f
fix
babolivier Apr 18, 2019
48985f9
Update synapse/config/database.py
aaronraimist Apr 18, 2019
4138b06
Update synapse/config/database.py
aaronraimist Apr 18, 2019
288b555
arrrrgh scaaary
babolivier Apr 18, 2019
131df58
sample conf
babolivier Apr 18, 2019
c5956d4
Update synapse/config/database.py
richvdh Apr 25, 2019
35e7b60
Merge branch 'develop' into babolivier/sqlite_federation
babolivier Apr 25, 2019
66fee87
review
babolivier Apr 25, 2019
94c4bd3
Merge branch 'babolivier/sqlite_federation' of github.com:matrix-org/…
babolivier Apr 25, 2019
0a8cd10
Merge branch 'develop' into babolivier/sqlite_federation
babolivier Apr 25, 2019
2e04f2c
o no
babolivier Apr 25, 2019
a32cd95
config
babolivier Apr 25, 2019
97e27a5
Reverting undeeded change
babolivier Apr 26, 2019
209c777
Fix test config
babolivier Apr 26, 2019
637e82c
Update doc
babolivier Apr 26, 2019
8216106
Add test case
babolivier Apr 26, 2019
72f80c0
Update other doc
babolivier Apr 26, 2019
235136a
lint
babolivier Apr 26, 2019
3a36244
Update synapse/config/server.py
richvdh May 1, 2019
1aef88d
Incorporate review
babolivier May 1, 2019
d1482a8
Merge branch 'babolivier/sqlite_federation' of github.com:matrix-org/…
babolivier May 1, 2019
841b644
Use a more sensible way to get database settings
babolivier May 1, 2019
972b54a
Sample config
babolivier May 3, 2019
13b853a
Merge branch 'develop' into babolivier/sqlite_federation
babolivier May 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5078.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disable federation when using SQLite by default.
14 changes: 14 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ listeners:
# Used by phonehome stats to group together related servers.
#server_context: context

# Using SQLite with Synapse induces a severe degradation in performances
# compared to using PostgreSQL. SQLite should only be used for personal
# testing. Production instances should use PostgreSQL instead. You can
# find documentation on setting up PostgreSQL with Synapse here:
# https://github.com/matrix-org/synapse/blob/master/docs/postgres.rst
#
# If you want to use federation with SQLite regardless, you can
# uncomment the line below, but be aware that it can make Synapse
# malfunction and be very laggy, especially when joining large rooms.
# This option defaults to 'false' when using SQLite and 'true'
# otherwise.
#
#enable_federation_can_cause_bad_perfs_with_sqlite = true


## TLS ##

Expand Down
10 changes: 6 additions & 4 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,19 @@ class RegistrationError(SynapseError):


class FederationDeniedError(SynapseError):
"""An error raised when the server tries to federate with a server which
is not on its federation whitelist.
"""An error raised when the server tries to federate with a server which is
not on its federation whitelist or when federation is disabled in the
configuration.

Attributes:
destination (str): The destination which has been denied
"""

def __init__(self, destination):
"""Raised by federation client or server to indicate that we are
are deliberately not attempting to contact a given server because it is
not on our federation whitelist.
deliberately not attempting to contact a given server because it is not
on our federation whitelist or federation is disabled in the
configuration.

Args:
destination (str): the domain in question
Expand Down
24 changes: 24 additions & 0 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def read_config(self, config):
# "disable" federation
self.send_federation = config.get("send_federation", True)

try:
self.enable_federation = config[
"enable_federation_can_cause_bad_perfs_with_sqlite"
]
except KeyError:
if config.get("database", {"name": "sqlite3"}).get("name") == "sqlite3":
self.enable_federation = False
else:
self.enable_federation = True

# Whether to enable user presence.
self.use_presence = config.get("use_presence", True)

Expand Down Expand Up @@ -490,6 +500,20 @@ def default_config(self, server_name, data_dir_path, **kwargs):

# Used by phonehome stats to group together related servers.
#server_context: context

# Using SQLite with Synapse induces a severe degradation in performances
babolivier marked this conversation as resolved.
Show resolved Hide resolved
# compared to using PostgreSQL. SQLite should only be used for personal
# testing. Production instances should use PostgreSQL instead. You can
# find documentation on setting up PostgreSQL with Synapse here:
# https://github.com/matrix-org/synapse/blob/master/docs/postgres.rst
#
# If you want to use federation with SQLite regardless, you can
# uncomment the line below, but be aware that it can make Synapse
babolivier marked this conversation as resolved.
Show resolved Hide resolved
# malfunction and be very laggy, especially when joining large rooms.
# This option defaults to 'false' when using SQLite and 'true'
# otherwise.
#
#enable_federation_can_cause_bad_perfs_with_sqlite = true
babolivier marked this conversation as resolved.
Show resolved Hide resolved
""" % locals()

def read_arguments(self, args):
Expand Down
3 changes: 2 additions & 1 deletion synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(self, hs):
self.server_name = hs.hostname
self.store = hs.get_datastore()
self.federation_domain_whitelist = hs.config.federation_domain_whitelist
self.enable_federation = hs.config.enable_federation

# A method just so we can pass 'self' as the authenticator to the Servlets
@defer.inlineCallbacks
Expand Down Expand Up @@ -130,7 +131,7 @@ def authenticate_request(self, request, content):
if (
self.federation_domain_whitelist is not None and
origin not in self.federation_domain_whitelist
):
) or not self.enable_federation:
raise FederationDeniedError(origin)

if not json_request["signatures"]:
Expand Down
20 changes: 13 additions & 7 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def _send_request(
NotRetryingDestination: If we are not yet ready to retry this
server.
FederationDeniedError: If this destination is not on our
federation whitelist
federation whitelist or federation is disabled in the server's
configuration.
RequestSendFailed: If there were problems connecting to the
remote, due to e.g. DNS failures, connection timeouts etc.
"""
Expand All @@ -286,7 +287,7 @@ def _send_request(
if (
self.hs.config.federation_domain_whitelist is not None and
request.destination not in self.hs.config.federation_domain_whitelist
):
) or not self.hs.config.enable_federation:
raise FederationDeniedError(request.destination)

limiter = yield synapse.util.retryutils.get_retry_limiter(
Expand Down Expand Up @@ -563,7 +564,8 @@ def put_json(self, destination, path, args={}, data={},
NotRetryingDestination: If we are not yet ready to retry this
server.
FederationDeniedError: If this destination is not on our
federation whitelist
federation whitelist or federation is disabled in the server's
configuration.
RequestSendFailed: If there were problems connecting to the
remote, due to e.g. DNS failures, connection timeouts etc.
"""
Expand Down Expand Up @@ -619,7 +621,8 @@ def post_json(self, destination, path, data={}, long_retries=False,
NotRetryingDestination: If we are not yet ready to retry this
server.
FederationDeniedError: If this destination is not on our
federation whitelist
federation whitelist or federation is disabled in the server's
configuration.
RequestSendFailed: If there were problems connecting to the
remote, due to e.g. DNS failures, connection timeouts etc.
"""
Expand Down Expand Up @@ -679,7 +682,8 @@ def get_json(self, destination, path, args=None, retry_on_dns_fail=True,
NotRetryingDestination: If we are not yet ready to retry this
server.
FederationDeniedError: If this destination is not on our
federation whitelist
federation whitelist or federation is disabled in the server's
configuration.
RequestSendFailed: If there were problems connecting to the
remote, due to e.g. DNS failures, connection timeouts etc.
"""
Expand Down Expand Up @@ -734,7 +738,8 @@ def delete_json(self, destination, path, long_retries=False,
NotRetryingDestination: If we are not yet ready to retry this
server.
FederationDeniedError: If this destination is not on our
federation whitelist
federation whitelist or federation is disabled in the server's
configuration.
RequestSendFailed: If there were problems connecting to the
remote, due to e.g. DNS failures, connection timeouts etc.
"""
Expand Down Expand Up @@ -780,7 +785,8 @@ def get_file(self, destination, path, output_stream, args={},
NotRetryingDestination: If we are not yet ready to retry this
server.
FederationDeniedError: If this destination is not on our
federation whitelist
federation whitelist or federation is disabled in the server's
configuration.
RequestSendFailed: If there were problems connecting to the
remote, due to e.g. DNS failures, connection timeouts etc.
"""
Expand Down
5 changes: 3 additions & 2 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, hs):
self.recently_accessed_locals = set()

self.federation_domain_whitelist = hs.config.federation_domain_whitelist
self.enable_federation = hs.config.enable_federation

# List of StorageProviders where we should search for media and
# potentially upload to.
Expand Down Expand Up @@ -234,7 +235,7 @@ def get_remote_media(self, request, server_name, media_id, name):
if (
self.federation_domain_whitelist is not None and
server_name not in self.federation_domain_whitelist
):
) or not self.enable_federation:
raise FederationDeniedError(server_name)

self.mark_recently_accessed(server_name, media_id)
Expand Down Expand Up @@ -274,7 +275,7 @@ def get_remote_media_info(self, server_name, media_id):
if (
self.federation_domain_whitelist is not None and
server_name not in self.federation_domain_whitelist
):
) or not self.enable_federation:
raise FederationDeniedError(server_name)

# We linearize here to ensure that we don't try and download remote
Expand Down
21 changes: 20 additions & 1 deletion tests/http/test_fedclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from twisted.web.client import ResponseNeverReceived
from twisted.web.http import HTTPChannel

from synapse.api.errors import RequestSendFailed
from synapse.api.errors import FederationDeniedError, RequestSendFailed
from synapse.http.matrixfederationclient import (
MatrixFederationHttpClient,
MatrixFederationRequest,
Expand Down Expand Up @@ -428,3 +428,22 @@ def test_closes_connection(self):
self.pump(120)

self.assertTrue(conn.disconnecting)


class FederationDisabledClientTests(HomeserverTestCase):

def make_homeserver(self, reactor, clock):
config = self.default_config()
config.enable_federation = False
hs = self.setup_test_homeserver(config=config)
return hs

def prepare(self, reactor, clock, homeserver):
self.cl = MatrixFederationHttpClient(self.hs, None)
self.reactor.lookups["testserv"] = "1.2.3.4"

def test_federation_denied(self):
self.assertFailure(
self.cl.get_json("testserv:8008", "foo/bar"),
FederationDeniedError,
)
2 changes: 2 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def default_config(name):
# background, which upsets the test runner.
config.update_user_directory = False

config.enable_federation = True
babolivier marked this conversation as resolved.
Show resolved Hide resolved

return config


Expand Down