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

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into erikj/redactions_eiah
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jan 28, 2019
2 parents 33a6ecf + f1a0446 commit e65aca4
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 76 deletions.
1 change: 1 addition & 0 deletions changelog.d/4482.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add infrastructure to support different event formats
1 change: 1 addition & 0 deletions changelog.d/4492.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Synapse can now automatically provision TLS certificates via ACME (the protocol used by CAs like Let's Encrypt).
14 changes: 10 additions & 4 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, hs):
register_cache("cache", "token_cache", self.token_cache)

@defer.inlineCallbacks
def check_from_context(self, event, context, do_sig_check=True):
def check_from_context(self, room_version, event, context, do_sig_check=True):
prev_state_ids = yield context.get_prev_state_ids(self.store)
auth_events_ids = yield self.compute_auth_events(
event, prev_state_ids, for_verification=True,
Expand All @@ -74,12 +74,16 @@ def check_from_context(self, event, context, do_sig_check=True):
auth_events = {
(e.type, e.state_key): e for e in itervalues(auth_events)
}
self.check(event, auth_events=auth_events, do_sig_check=do_sig_check)
self.check(
room_version, event,
auth_events=auth_events, do_sig_check=do_sig_check,
)

def check(self, event, auth_events, do_sig_check=True):
def check(self, room_version, event, auth_events, do_sig_check=True):
""" Checks if this event is correctly authed.
Args:
room_version (str): version of the room
event: the event being checked.
auth_events (dict: event-key -> event): the existing room state.
Expand All @@ -88,7 +92,9 @@ def check(self, event, auth_events, do_sig_check=True):
True if the auth checks pass.
"""
with Measure(self.clock, "auth.check"):
event_auth.check(event, auth_events, do_sig_check=do_sig_check)
event_auth.check(
room_version, event, auth_events, do_sig_check=do_sig_check
)

@defer.inlineCallbacks
def check_joined_room(self, room_id, user_id, current_state=None):
Expand Down
12 changes: 6 additions & 6 deletions synapse/app/client_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,23 @@ def start(config_options):

database_engine = create_engine(config.database_config)

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ss = ClientReaderServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ss.setup()
ss.start_listening(config.worker_listeners)

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
Expand Down
12 changes: 6 additions & 6 deletions synapse/app/event_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,23 @@ def start(config_options):

database_engine = create_engine(config.database_config)

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ss = EventCreatorServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ss.setup()
ss.start_listening(config.worker_listeners)

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
Expand Down
12 changes: 6 additions & 6 deletions synapse/app/federation_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,23 @@ def start(config_options):

database_engine = create_engine(config.database_config)

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ss = FederationReaderServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ss.setup()
ss.start_listening(config.worker_listeners)

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
Expand Down
18 changes: 9 additions & 9 deletions synapse/app/federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,24 @@ def start(config_options):
# Force the pushers to start since they will be disabled in the main config
config.send_federation = True

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ps = FederationSenderServer(
ss = FederationSenderServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ps.setup()
ps.start_listening(config.worker_listeners)
ss.setup()

def start():
ps.get_datastore().start_profiling()
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
_base.start_worker_reactor("synapse-federation-sender", config)
Expand Down
12 changes: 6 additions & 6 deletions synapse/app/frontend_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,23 @@ def start(config_options):

database_engine = create_engine(config.database_config)

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ss = FrontendProxyServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ss.setup()
ss.start_listening(config.worker_listeners)

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
Expand Down
12 changes: 6 additions & 6 deletions synapse/app/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,23 @@ def start(config_options):

database_engine = create_engine(config.database_config)

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ss = MediaRepositoryServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ss.setup()
ss.start_listening(config.worker_listeners)

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
Expand Down
18 changes: 9 additions & 9 deletions synapse/app/user_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,24 @@ def start(config_options):
# Force the pushers to start since they will be disabled in the main config
config.update_user_directory = True

tls_server_context_factory = context_factory.ServerContextFactory(config)
tls_client_options_factory = context_factory.ClientTLSOptionsFactory(config)

ps = UserDirectoryServer(
ss = UserDirectoryServer(
config.server_name,
db_config=config.database_config,
tls_server_context_factory=tls_server_context_factory,
tls_client_options_factory=tls_client_options_factory,
config=config,
version_string="Synapse/" + get_version_string(synapse),
database_engine=database_engine,
)

ps.setup()
ps.start_listening(config.worker_listeners)
ss.setup()

def start():
ps.get_datastore().start_profiling()
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)

Expand Down
3 changes: 2 additions & 1 deletion synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
logger = logging.getLogger(__name__)


def check(event, auth_events, do_sig_check=True, do_size_check=True):
def check(room_version, event, auth_events, do_sig_check=True, do_size_check=True):
""" Checks if this event is correctly authed.
Args:
room_version (str): the version of the room
event: the event being checked.
auth_events (dict: event-key -> event): the existing room state.
Expand Down
20 changes: 12 additions & 8 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,9 @@ def on_make_join_request(self, room_id, user_id):

# The remote hasn't signed it yet, obviously. We'll do the full checks
# when we get the event back in `on_send_join_request`
yield self.auth.check_from_context(event, context, do_sig_check=False)
yield self.auth.check_from_context(
room_version, event, context, do_sig_check=False,
)

defer.returnValue(event)

Expand Down Expand Up @@ -1388,7 +1390,9 @@ def on_make_leave_request(self, room_id, user_id):
try:
# The remote hasn't signed it yet, obviously. We'll do the full checks
# when we get the event back in `on_send_leave_request`
yield self.auth.check_from_context(event, context, do_sig_check=False)
yield self.auth.check_from_context(
room_version, event, context, do_sig_check=False,
)
except AuthError as e:
logger.warn("Failed to create new leave %r because %s", event, e)
raise e
Expand Down Expand Up @@ -1683,7 +1687,7 @@ def _persist_auth_tree(self, origin, auth_events, state, event):
auth_for_e[(EventTypes.Create, "")] = create_event

try:
self.auth.check(e, auth_events=auth_for_e)
self.auth.check(room_version, e, auth_events=auth_for_e)
except SynapseError as err:
# we may get SynapseErrors here as well as AuthErrors. For
# instance, there are a couple of (ancient) events in some
Expand Down Expand Up @@ -1927,6 +1931,8 @@ def do_auth(self, origin, event, context, auth_events):
current_state = set(e.event_id for e in auth_events.values())
different_auth = event_auth_events - current_state

room_version = yield self.store.get_room_version(event.room_id)

if different_auth and not event.internal_metadata.is_outlier():
# Do auth conflict res.
logger.info("Different auth: %s", different_auth)
Expand All @@ -1951,8 +1957,6 @@ def do_auth(self, origin, event, context, auth_events):
(d.type, d.state_key): d for d in different_events if d
})

room_version = yield self.store.get_room_version(event.room_id)

new_state = yield self.state_handler.resolve_events(
room_version,
[list(local_view.values()), list(remote_view.values())],
Expand Down Expand Up @@ -2052,7 +2056,7 @@ def do_auth(self, origin, event, context, auth_events):
)

try:
self.auth.check(event, auth_events=auth_events)
self.auth.check(room_version, event, auth_events=auth_events)
except AuthError as e:
logger.warn("Failed auth resolution for %r because %s", event, e)
raise e
Expand Down Expand Up @@ -2290,7 +2294,7 @@ def exchange_third_party_invite(
EventValidator().validate_new(event)

try:
yield self.auth.check_from_context(event, context)
yield self.auth.check_from_context(room_version, event, context)
except AuthError as e:
logger.warn("Denying new third party invite %r because %s", event, e)
raise e
Expand Down Expand Up @@ -2332,7 +2336,7 @@ def on_exchange_third_party_invite_request(self, origin, room_id, event_dict):
)

try:
self.auth.check_from_context(event, context)
self.auth.check_from_context(room_version, event, context)
except AuthError as e:
logger.warn("Denying third party invite %r because %s", event, e)
raise e
Expand Down
9 changes: 7 additions & 2 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from twisted.internet import defer
from twisted.internet.defer import succeed

from synapse.api.constants import EventTypes, Membership
from synapse.api.constants import EventTypes, Membership, RoomVersions
from synapse.api.errors import (
AuthError,
Codes,
Expand Down Expand Up @@ -589,8 +589,13 @@ def handle_new_client_event(
extra_users (list(UserID)): Any extra users to notify about event
"""

if event.is_state() and (event.type, event.state_key) == (EventTypes.Create, ""):
room_version = event.content.get("room_version", RoomVersions.V1)
else:
room_version = yield self.store.get_room_version(event.room_id)

try:
yield self.auth.check_from_context(event, context)
yield self.auth.check_from_context(room_version, event, context)
except AuthError as err:
logger.warn("Denying new event %r because %s", event, err)
raise err
Expand Down
5 changes: 4 additions & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ def upgrade_room(self, requester, old_room_id, new_version):
token_id=requester.access_token_id,
)
)
yield self.auth.check_from_context(tombstone_event, tombstone_context)
old_room_version = yield self.store.get_room_version(old_room_id)
yield self.auth.check_from_context(
old_room_version, tombstone_event, tombstone_context,
)

yield self.clone_existing_room(
requester,
Expand Down
2 changes: 1 addition & 1 deletion synapse/state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def resolve_events_with_store(room_version, state_sets, event_map, state_res_sto
RoomVersions.VDH_TEST, RoomVersions.STATE_V2_TEST, RoomVersions.V2,
):
return v2.resolve_events_with_store(
state_sets, event_map, state_res_store,
room_version, state_sets, event_map, state_res_store,
)
else:
# This should only happen if we added a version but forgot to add it to
Expand Down
Loading

0 comments on commit e65aca4

Please sign in to comment.