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

Commit f4b1a9a

Browse files
authored
Require direct references to configuration variables. (#10985)
This removes the magic allowing accessing configurable variables directly from the config object. It is now required that a specific configuration class is used (e.g. `config.foo` must be replaced with `config.server.foo`).
1 parent 829f2a8 commit f4b1a9a

31 files changed

+124
-160
lines changed

changelog.d/10985.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use direct references to config flags.

scripts/synapse_port_db

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class MockHomeserver:
215215
def __init__(self, config):
216216
self.clock = Clock(reactor)
217217
self.config = config
218-
self.hostname = config.server_name
218+
self.hostname = config.server.server_name
219219
self.version_string = "Synapse/" + get_version_string(synapse)
220220

221221
def get_clock(self):
@@ -583,7 +583,7 @@ class Porter(object):
583583
return
584584

585585
self.postgres_store = self.build_db_store(
586-
self.hs_config.get_single_database()
586+
self.hs_config.database.get_single_database()
587587
)
588588

589589
await self.run_background_updates_on_postgres()

scripts/update_synapse_database

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MockHomeserver(HomeServer):
3636

3737
def __init__(self, config, **kwargs):
3838
super(MockHomeserver, self).__init__(
39-
config.server_name, reactor=reactor, config=config, **kwargs
39+
config.server.server_name, reactor=reactor, config=config, **kwargs
4040
)
4141

4242
self.version_string = "Synapse/" + get_version_string(synapse)

synapse/app/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def refresh_certificate(hs):
301301
if not hs.config.server.has_tls_listener():
302302
return
303303

304-
hs.config.read_certificate_from_disk()
304+
hs.config.tls.read_certificate_from_disk()
305305
hs.tls_server_context_factory = context_factory.ServerContextFactory(hs.config)
306306

307307
if hs._listening_services:

synapse/app/admin_cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ def start(config_options):
197197
# Explicitly disable background processes
198198
config.server.update_user_directory = False
199199
config.worker.run_background_tasks = False
200-
config.start_pushers = False
200+
config.worker.start_pushers = False
201201
config.pusher_shard_config.instances = []
202-
config.send_federation = False
202+
config.worker.send_federation = False
203203
config.federation_shard_config.instances = []
204204

205205
synapse.events.USE_FROZEN_DICTS = config.server.use_frozen_dicts

synapse/app/homeserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def _configure_named_resource(self, name, compress=False):
234234
)
235235

236236
if name in ["media", "federation", "client"]:
237-
if self.config.media.enable_media_repo:
237+
if self.config.server.enable_media_repo:
238238
media_repo = self.get_media_repository_resource()
239239
resources.update(
240240
{MEDIA_PREFIX: media_repo, LEGACY_MEDIA_PREFIX: media_repo}

synapse/config/_base.py

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,6 @@ def __init__(self, root_config=None):
118118
"synapse", "res/templates"
119119
)
120120

121-
def __getattr__(self, item: str) -> Any:
122-
"""
123-
Try and fetch a configuration option that does not exist on this class.
124-
125-
This is so that existing configs that rely on `self.value`, where value
126-
is actually from a different config section, continue to work.
127-
"""
128-
if item in ["generate_config_section", "read_config"]:
129-
raise AttributeError(item)
130-
131-
if self.root is None:
132-
raise AttributeError(item)
133-
else:
134-
return self.root._get_unclassed_config(self.section, item)
135-
136121
@staticmethod
137122
def parse_size(value):
138123
if isinstance(value, int):
@@ -289,7 +274,9 @@ def read_templates(
289274
env.filters.update(
290275
{
291276
"format_ts": _format_ts_filter,
292-
"mxc_to_http": _create_mxc_to_http_filter(self.public_baseurl),
277+
"mxc_to_http": _create_mxc_to_http_filter(
278+
self.root.server.public_baseurl
279+
),
293280
}
294281
)
295282

@@ -311,8 +298,6 @@ class RootConfig:
311298
config_classes = []
312299

313300
def __init__(self):
314-
self._configs = OrderedDict()
315-
316301
for config_class in self.config_classes:
317302
if config_class.section is None:
318303
raise ValueError("%r requires a section name" % (config_class,))
@@ -321,42 +306,7 @@ def __init__(self):
321306
conf = config_class(self)
322307
except Exception as e:
323308
raise Exception("Failed making %s: %r" % (config_class.section, e))
324-
self._configs[config_class.section] = conf
325-
326-
def __getattr__(self, item: str) -> Any:
327-
"""
328-
Redirect lookups on this object either to config objects, or values on
329-
config objects, so that `config.tls.blah` works, as well as legacy uses
330-
of things like `config.server.server_name`. It will first look up the config
331-
section name, and then values on those config classes.
332-
"""
333-
if item in self._configs.keys():
334-
return self._configs[item]
335-
336-
return self._get_unclassed_config(None, item)
337-
338-
def _get_unclassed_config(self, asking_section: Optional[str], item: str):
339-
"""
340-
Fetch a config value from one of the instantiated config classes that
341-
has not been fetched directly.
342-
343-
Args:
344-
asking_section: If this check is coming from a Config child, which
345-
one? This section will not be asked if it has the value.
346-
item: The configuration value key.
347-
348-
Raises:
349-
AttributeError if no config classes have the config key. The body
350-
will contain what sections were checked.
351-
"""
352-
for key, val in self._configs.items():
353-
if key == asking_section:
354-
continue
355-
356-
if item in dir(val):
357-
return getattr(val, item)
358-
359-
raise AttributeError(item, "not found in %s" % (list(self._configs.keys()),))
309+
setattr(self, config_class.section, conf)
360310

361311
def invoke_all(self, func_name: str, *args, **kwargs) -> MutableMapping[str, Any]:
362312
"""
@@ -373,9 +323,11 @@ def invoke_all(self, func_name: str, *args, **kwargs) -> MutableMapping[str, Any
373323
"""
374324
res = OrderedDict()
375325

376-
for name, config in self._configs.items():
326+
for config_class in self.config_classes:
327+
config = getattr(self, config_class.section)
328+
377329
if hasattr(config, func_name):
378-
res[name] = getattr(config, func_name)(*args, **kwargs)
330+
res[config_class.section] = getattr(config, func_name)(*args, **kwargs)
379331

380332
return res
381333

synapse/config/account_validity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def read_config(self, config, **kwargs):
7676
)
7777

7878
if self.account_validity_renew_by_email_enabled:
79-
if not self.public_baseurl:
79+
if not self.root.server.public_baseurl:
8080
raise ConfigError("Can't send renewal emails without 'public_baseurl'")
8181

8282
# Load account validity templates.

synapse/config/cas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def read_config(self, config, **kwargs):
3737

3838
# The public baseurl is required because it is used by the redirect
3939
# template.
40-
public_baseurl = self.public_baseurl
40+
public_baseurl = self.root.server.public_baseurl
4141
if not public_baseurl:
4242
raise ConfigError("cas_config requires a public_baseurl to be set")
4343

synapse/config/emailconfig.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import logging
2020
import os
2121
from enum import Enum
22-
from typing import Optional
2322

2423
import attr
2524

@@ -135,7 +134,7 @@ def read_config(self, config, **kwargs):
135134
# msisdn is currently always remote while Synapse does not support any method of
136135
# sending SMS messages
137136
ThreepidBehaviour.REMOTE
138-
if self.account_threepid_delegate_email
137+
if self.root.registration.account_threepid_delegate_email
139138
else ThreepidBehaviour.LOCAL
140139
)
141140
# Prior to Synapse v1.4.0, there was another option that defined whether Synapse would
@@ -144,7 +143,7 @@ def read_config(self, config, **kwargs):
144143
# identity server in the process.
145144
self.using_identity_server_from_trusted_list = False
146145
if (
147-
not self.account_threepid_delegate_email
146+
not self.root.registration.account_threepid_delegate_email
148147
and config.get("trust_identity_server_for_password_resets", False) is True
149148
):
150149
# Use the first entry in self.trusted_third_party_id_servers instead
@@ -156,7 +155,7 @@ def read_config(self, config, **kwargs):
156155

157156
# trusted_third_party_id_servers does not contain a scheme whereas
158157
# account_threepid_delegate_email is expected to. Presume https
159-
self.account_threepid_delegate_email: Optional[str] = (
158+
self.root.registration.account_threepid_delegate_email = (
160159
"https://" + first_trusted_identity_server
161160
)
162161
self.using_identity_server_from_trusted_list = True
@@ -335,7 +334,7 @@ def read_config(self, config, **kwargs):
335334
"client_base_url", email_config.get("riot_base_url", None)
336335
)
337336

338-
if self.account_validity_renew_by_email_enabled:
337+
if self.root.account_validity.account_validity_renew_by_email_enabled:
339338
expiry_template_html = email_config.get(
340339
"expiry_template_html", "notice_expiry.html"
341340
)

synapse/config/key.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ def read_config(self, config, config_dir_path, **kwargs):
145145

146146
# list of TrustedKeyServer objects
147147
self.key_servers = list(
148-
_parse_key_servers(key_servers, self.federation_verify_certificates)
148+
_parse_key_servers(
149+
key_servers, self.root.tls.federation_verify_certificates
150+
)
149151
)
150152

151153
self.macaroon_secret_key = config.get(
152-
"macaroon_secret_key", self.registration_shared_secret
154+
"macaroon_secret_key", self.root.registration.registration_shared_secret
153155
)
154156

155157
if not self.macaroon_secret_key:

synapse/config/oidc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def read_config(self, config, **kwargs):
5858
"Multiple OIDC providers have the idp_id %r." % idp_id
5959
)
6060

61-
public_baseurl = self.public_baseurl
61+
public_baseurl = self.root.server.public_baseurl
6262
if public_baseurl is None:
6363
raise ConfigError("oidc_config requires a public_baseurl to be set")
6464
self.oidc_callback_url = public_baseurl + "_synapse/client/oidc/callback"

synapse/config/registration.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def read_config(self, config, **kwargs):
4545
account_threepid_delegates = config.get("account_threepid_delegates") or {}
4646
self.account_threepid_delegate_email = account_threepid_delegates.get("email")
4747
self.account_threepid_delegate_msisdn = account_threepid_delegates.get("msisdn")
48-
if self.account_threepid_delegate_msisdn and not self.public_baseurl:
48+
if (
49+
self.account_threepid_delegate_msisdn
50+
and not self.root.server.public_baseurl
51+
):
4952
raise ConfigError(
5053
"The configuration option `public_baseurl` is required if "
5154
"`account_threepid_delegate.msisdn` is set, such that "
@@ -85,7 +88,7 @@ def read_config(self, config, **kwargs):
8588
if mxid_localpart:
8689
# Convert the localpart to a full mxid.
8790
self.auto_join_user_id = UserID(
88-
mxid_localpart, self.server_name
91+
mxid_localpart, self.root.server.server_name
8992
).to_string()
9093

9194
if self.autocreate_auto_join_rooms:

synapse/config/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def read_config(self, config, **kwargs):
9494
# Only enable the media repo if either the media repo is enabled or the
9595
# current worker app is the media repo.
9696
if (
97-
self.enable_media_repo is False
97+
self.root.server.enable_media_repo is False
9898
and config.get("worker_app") != "synapse.app.media_repository"
9999
):
100100
self.can_load_media_repo = False

synapse/config/saml2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def _default_saml_config_dict(
199199
"""
200200
import saml2
201201

202-
public_baseurl = self.public_baseurl
202+
public_baseurl = self.root.server.public_baseurl
203203
if public_baseurl is None:
204204
raise ConfigError("saml2_config requires a public_baseurl to be set")
205205

synapse/config/server_notices.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def read_config(self, config, **kwargs):
7373
return
7474

7575
mxid_localpart = c["system_mxid_localpart"]
76-
self.server_notices_mxid = UserID(mxid_localpart, self.server_name).to_string()
76+
self.server_notices_mxid = UserID(
77+
mxid_localpart, self.root.server.server_name
78+
).to_string()
7779
self.server_notices_mxid_display_name = c.get("system_mxid_display_name", None)
7880
self.server_notices_mxid_avatar_url = c.get("system_mxid_avatar_url", None)
7981
# todo: i18n

synapse/config/sso.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ def read_config(self, config, **kwargs):
103103
# the client's.
104104
# public_baseurl is an optional setting, so we only add the fallback's URL to the
105105
# list if it's provided (because we can't figure out what that URL is otherwise).
106-
if self.public_baseurl:
107-
login_fallback_url = self.public_baseurl + "_matrix/static/client/login"
106+
if self.root.server.public_baseurl:
107+
login_fallback_url = (
108+
self.root.server.public_baseurl + "_matrix/static/client/login"
109+
)
108110
self.sso_client_whitelist.append(login_fallback_url)
109111

110112
def generate_config_section(self, **kwargs):

synapse/handlers/account_validity.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ def __init__(self, hs: "HomeServer"):
6767
and self._account_validity_renew_by_email_enabled
6868
):
6969
# Don't do email-specific configuration if renewal by email is disabled.
70-
self._template_html = (
71-
hs.config.account_validity.account_validity_template_html
72-
)
73-
self._template_text = (
74-
hs.config.account_validity.account_validity_template_text
75-
)
70+
self._template_html = hs.config.email.account_validity_template_html
71+
self._template_text = hs.config.email.account_validity_template_text
7672
self._renew_email_subject = (
7773
hs.config.account_validity.account_validity_renew_email_subject
7874
)

synapse/handlers/room_member.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,8 +1499,11 @@ async def _remote_join(
14991499
if len(remote_room_hosts) == 0:
15001500
raise SynapseError(404, "No known servers")
15011501

1502-
check_complexity = self.hs.config.limit_remote_rooms.enabled
1503-
if check_complexity and self.hs.config.limit_remote_rooms.admins_can_join:
1502+
check_complexity = self.hs.config.server.limit_remote_rooms.enabled
1503+
if (
1504+
check_complexity
1505+
and self.hs.config.server.limit_remote_rooms.admins_can_join
1506+
):
15041507
check_complexity = not await self.auth.is_server_admin(user)
15051508

15061509
if check_complexity:

synapse/replication/tcp/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __init__(self, hs: "HomeServer"):
117117
self._instance_name = hs.get_instance_name()
118118
self._typing_handler = hs.get_typing_handler()
119119

120-
self._notify_pushers = hs.config.start_pushers
120+
self._notify_pushers = hs.config.worker.start_pushers
121121
self._pusher_pool = hs.get_pusherpool()
122122
self._presence_handler = hs.get_presence_handler()
123123

synapse/replication/tcp/handler.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ def __init__(self, hs: "HomeServer"):
171171
if hs.config.worker.worker_app is not None:
172172
continue
173173

174-
if stream.NAME == FederationStream.NAME and hs.config.send_federation:
174+
if (
175+
stream.NAME == FederationStream.NAME
176+
and hs.config.worker.send_federation
177+
):
175178
# We only support federation stream if federation sending
176179
# has been disabled on the master.
177180
continue
@@ -225,7 +228,7 @@ def __init__(self, hs: "HomeServer"):
225228
self._is_master = hs.config.worker.worker_app is None
226229

227230
self._federation_sender = None
228-
if self._is_master and not hs.config.send_federation:
231+
if self._is_master and not hs.config.worker.send_federation:
229232
self._federation_sender = hs.get_federation_sender()
230233

231234
self._server_notices_sender = None

synapse/rest/client/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, hs: "HomeServer"):
4848
self.auth_handler = hs.get_auth_handler()
4949
self.registration_handler = hs.get_registration_handler()
5050
self.recaptcha_template = hs.config.captcha.recaptcha_template
51-
self.terms_template = hs.config.terms_template
51+
self.terms_template = hs.config.consent.terms_template
5252
self.registration_token_template = (
5353
hs.config.registration.registration_token_template
5454
)

synapse/rest/client/push_rule.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def __init__(self, hs: "HomeServer"):
6161
self.notifier = hs.get_notifier()
6262
self._is_worker = hs.config.worker.worker_app is not None
6363

64-
self._users_new_default_push_rules = hs.config.users_new_default_push_rules
64+
self._users_new_default_push_rules = (
65+
hs.config.server.users_new_default_push_rules
66+
)
6567

6668
async def on_PUT(self, request: SynapseRequest, path: str) -> Tuple[int, JsonDict]:
6769
if self._is_worker:

synapse/storage/databases/main/push_rule.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def __init__(self, database: DatabasePool, db_conn, hs):
101101
prefilled_cache=push_rules_prefill,
102102
)
103103

104-
self._users_new_default_push_rules = hs.config.users_new_default_push_rules
104+
self._users_new_default_push_rules = (
105+
hs.config.server.users_new_default_push_rules
106+
)
105107

106108
@abc.abstractmethod
107109
def get_max_push_rules_stream_id(self):

0 commit comments

Comments
 (0)