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

Commit

Permalink
Use direct references for some configuration variables (part 3) (#10885)
Browse files Browse the repository at this point in the history
This avoids the overhead of searching through the various
configuration classes by directly referencing the class that
the attributes are in.

It also improves type hints since mypy can now resolve the
types of the configuration variables.
  • Loading branch information
clokep authored Sep 23, 2021
1 parent aa2c027 commit e584534
Show file tree
Hide file tree
Showing 32 changed files with 137 additions and 119 deletions.
1 change: 1 addition & 0 deletions changelog.d/10885.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use direct references to config flags.
2 changes: 1 addition & 1 deletion synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _configure_named_resource(self, name, compress=False):
}
)

if self.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
if self.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
from synapse.rest.synapse.client.password_reset import (
PasswordResetSubmitTokenResource,
)
Expand Down
9 changes: 6 additions & 3 deletions synapse/config/consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from os import path
from typing import Optional

from synapse.config import ConfigError

Expand Down Expand Up @@ -78,8 +79,8 @@ class ConsentConfig(Config):
def __init__(self, *args):
super().__init__(*args)

self.user_consent_version = None
self.user_consent_template_dir = None
self.user_consent_version: Optional[str] = None
self.user_consent_template_dir: Optional[str] = None
self.user_consent_server_notice_content = None
self.user_consent_server_notice_to_guests = False
self.block_events_without_consent_error = None
Expand All @@ -94,7 +95,9 @@ def read_config(self, config, **kwargs):
return
self.user_consent_version = str(consent_config["version"])
self.user_consent_template_dir = self.abspath(consent_config["template_dir"])
if not path.isdir(self.user_consent_template_dir):
if not isinstance(self.user_consent_template_dir, str) or not path.isdir(
self.user_consent_template_dir
):
raise ConfigError(
"Could not find template directory '%s'"
% (self.user_consent_template_dir,)
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/account_validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, hs: "HomeServer"):
self.send_email_handler = self.hs.get_send_email_handler()
self.clock = self.hs.get_clock()

self._app_name = self.hs.config.email_app_name
self._app_name = self.hs.config.email.email_app_name

self._account_validity_enabled = (
hs.config.account_validity.account_validity_enabled
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, hs: "HomeServer"):
self.scheduler = hs.get_application_service_scheduler()
self.started_scheduler = False
self.clock = hs.get_clock()
self.notify_appservices = hs.config.notify_appservices
self.notify_appservices = hs.config.appservice.notify_appservices
self.event_sources = hs.get_event_sources()

self.current_max = 0
Expand Down
22 changes: 11 additions & 11 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ def __init__(self, hs: "HomeServer"):

self.password_providers = [
PasswordProvider.load(module, config, account_handler)
for module, config in hs.config.password_providers
for module, config in hs.config.authproviders.password_providers
]

logger.info("Extra password_providers: %s", self.password_providers)

self.hs = hs # FIXME better possibility to access registrationHandler later?
self.macaroon_gen = hs.get_macaroon_generator()
self._password_enabled = hs.config.password_enabled
self._password_localdb_enabled = hs.config.password_localdb_enabled
self._password_enabled = hs.config.auth.password_enabled
self._password_localdb_enabled = hs.config.auth.password_localdb_enabled

# start out by assuming PASSWORD is enabled; we will remove it later if not.
login_types = set()
Expand Down Expand Up @@ -250,7 +250,7 @@ def __init__(self, hs: "HomeServer"):
)

# The number of seconds to keep a UI auth session active.
self._ui_auth_session_timeout = hs.config.ui_auth_session_timeout
self._ui_auth_session_timeout = hs.config.auth.ui_auth_session_timeout

# Ratelimitier for failed /login attempts
self._failed_login_attempts_ratelimiter = Ratelimiter(
Expand Down Expand Up @@ -739,19 +739,19 @@ async def _check_auth_dict(
return canonical_id

def _get_params_recaptcha(self) -> dict:
return {"public_key": self.hs.config.recaptcha_public_key}
return {"public_key": self.hs.config.captcha.recaptcha_public_key}

def _get_params_terms(self) -> dict:
return {
"policies": {
"privacy_policy": {
"version": self.hs.config.user_consent_version,
"version": self.hs.config.consent.user_consent_version,
"en": {
"name": self.hs.config.user_consent_policy_name,
"name": self.hs.config.consent.user_consent_policy_name,
"url": "%s_matrix/consent?v=%s"
% (
self.hs.config.server.public_baseurl,
self.hs.config.user_consent_version,
self.hs.config.consent.user_consent_version,
),
},
}
Expand Down Expand Up @@ -1016,7 +1016,7 @@ async def _find_user_id_and_pwd_hash(
def can_change_password(self) -> bool:
"""Get whether users on this server are allowed to change or set a password.
Both `config.password_enabled` and `config.password_localdb_enabled` must be true.
Both `config.auth.password_enabled` and `config.auth.password_localdb_enabled` must be true.
Note that any account (even SSO accounts) are allowed to add passwords if the above
is true.
Expand Down Expand Up @@ -1486,7 +1486,7 @@ def _do_hash() -> str:
pw = unicodedata.normalize("NFKC", password)

return bcrypt.hashpw(
pw.encode("utf8") + self.hs.config.password_pepper.encode("utf8"),
pw.encode("utf8") + self.hs.config.auth.password_pepper.encode("utf8"),
bcrypt.gensalt(self.bcrypt_rounds),
).decode("ascii")

Expand All @@ -1510,7 +1510,7 @@ def _do_validate_hash(checked_hash: bytes) -> bool:
pw = unicodedata.normalize("NFKC", password)

return bcrypt.checkpw(
pw.encode("utf8") + self.hs.config.password_pepper.encode("utf8"),
pw.encode("utf8") + self.hs.config.auth.password_pepper.encode("utf8"),
checked_hash,
)

Expand Down
8 changes: 4 additions & 4 deletions synapse/handlers/cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def __init__(self, hs: "HomeServer"):
self._auth_handler = hs.get_auth_handler()
self._registration_handler = hs.get_registration_handler()

self._cas_server_url = hs.config.cas_server_url
self._cas_service_url = hs.config.cas_service_url
self._cas_displayname_attribute = hs.config.cas_displayname_attribute
self._cas_required_attributes = hs.config.cas_required_attributes
self._cas_server_url = hs.config.cas.cas_server_url
self._cas_service_url = hs.config.cas.cas_service_url
self._cas_displayname_attribute = hs.config.cas.cas_displayname_attribute
self._cas_required_attributes = hs.config.cas.cas_required_attributes

self._http_client = hs.get_proxied_http_client()

Expand Down
12 changes: 6 additions & 6 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, hs: "HomeServer"):
self.federation_http_client = hs.get_federation_http_client()
self.hs = hs

self._web_client_location = hs.config.invite_client_location
self._web_client_location = hs.config.email.invite_client_location

# Ratelimiters for `/requestToken` endpoints.
self._3pid_validation_ratelimiter_ip = Ratelimiter(
Expand Down Expand Up @@ -419,7 +419,7 @@ async def send_threepid_validation(

token_expires = (
self.hs.get_clock().time_msec()
+ self.hs.config.email_validation_token_lifetime
+ self.hs.config.email.email_validation_token_lifetime
)

await self.store.start_or_continue_validation_session(
Expand Down Expand Up @@ -465,7 +465,7 @@ async def requestEmailToken(
if next_link:
params["next_link"] = next_link

if self.hs.config.using_identity_server_from_trusted_list:
if self.hs.config.email.using_identity_server_from_trusted_list:
# Warn that a deprecated config option is in use
logger.warning(
'The config option "trust_identity_server_for_password_resets" '
Expand Down Expand Up @@ -518,7 +518,7 @@ async def requestMsisdnToken(
if next_link:
params["next_link"] = next_link

if self.hs.config.using_identity_server_from_trusted_list:
if self.hs.config.email.using_identity_server_from_trusted_list:
# Warn that a deprecated config option is in use
logger.warning(
'The config option "trust_identity_server_for_password_resets" '
Expand Down Expand Up @@ -572,12 +572,12 @@ async def validate_threepid_session(
validation_session = None

# Try to validate as email
if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
if self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
# Ask our delegated email identity server
validation_session = await self.threepid_from_creds(
self.hs.config.account_threepid_delegate_email, threepid_creds
)
elif self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
elif self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
# Get a validated session matching these details
validation_session = await self.store.get_threepid_validation_session(
"email", client_secret, sid=sid, validated=True
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def __init__(self, hs: "HomeServer"):
)

self._block_events_without_consent_error = (
self.config.block_events_without_consent_error
self.config.consent.block_events_without_consent_error
)

# we need to construct a ConsentURIBuilder here, as it checks that the necessary
Expand Down Expand Up @@ -744,7 +744,7 @@ async def assert_accepted_privacy_policy(self, requester: Requester) -> None:
if u["appservice_id"] is not None:
# users registered by an appservice are exempt
return
if u["consent_version"] == self.config.user_consent_version:
if u["consent_version"] == self.config.consent.user_consent_version:
return

consent_uri = self._consent_uri_builder.build_user_consent_uri(user.localpart)
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/password_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

class PasswordPolicyHandler:
def __init__(self, hs: "HomeServer"):
self.policy = hs.config.password_policy
self.enabled = hs.config.password_policy_enabled
self.policy = hs.config.auth.password_policy
self.enabled = hs.config.auth.password_policy_enabled

# Regexps for the spec'd policy parameters.
self.regexp_digit = re.compile("[0-9]")
Expand Down
11 changes: 7 additions & 4 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __init__(self, hs: "HomeServer"):
self.ratelimiter = hs.get_registration_ratelimiter()
self.macaroon_gen = hs.get_macaroon_generator()
self._account_validity_handler = hs.get_account_validity_handler()
self._user_consent_version = self.hs.config.consent.user_consent_version
self._server_notices_mxid = hs.config.server_notices_mxid
self._server_name = hs.hostname

Expand Down Expand Up @@ -339,7 +340,7 @@ async def register_user(
auth_provider=(auth_provider_id or ""),
).inc()

if not self.hs.config.user_consent_at_registration:
if not self.hs.config.consent.user_consent_at_registration:
if not self.hs.config.auto_join_rooms_for_guests and make_guest:
logger.info(
"Skipping auto-join for %s because auto-join for guests is disabled",
Expand Down Expand Up @@ -864,7 +865,9 @@ async def post_registration_actions(
await self._register_msisdn_threepid(user_id, threepid)

if auth_result and LoginType.TERMS in auth_result:
await self._on_user_consented(user_id, self.hs.config.user_consent_version)
# The terms type should only exist if consent is enabled.
assert self._user_consent_version is not None
await self._on_user_consented(user_id, self._user_consent_version)

async def _on_user_consented(self, user_id: str, consent_version: str) -> None:
"""A user consented to the terms on registration
Expand Down Expand Up @@ -910,8 +913,8 @@ async def _register_email_threepid(
# getting mail spam where they weren't before if email
# notifs are set up on a homeserver)
if (
self.hs.config.email_enable_notifs
and self.hs.config.email_notif_for_new_users
self.hs.config.email.email_enable_notifs
and self.hs.config.email.email_notif_for_new_users
and token
):
# Pull the ID of the access token back out of the db
Expand Down
17 changes: 11 additions & 6 deletions synapse/handlers/ui_auth/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ class RecaptchaAuthChecker(UserInteractiveAuthChecker):

def __init__(self, hs: "HomeServer"):
super().__init__(hs)
self._enabled = bool(hs.config.recaptcha_private_key)
self._enabled = bool(hs.config.captcha.recaptcha_private_key)
self._http_client = hs.get_proxied_http_client()
self._url = hs.config.recaptcha_siteverify_api
self._secret = hs.config.recaptcha_private_key
self._url = hs.config.captcha.recaptcha_siteverify_api
self._secret = hs.config.captcha.recaptcha_private_key

def is_enabled(self) -> bool:
return self._enabled
Expand Down Expand Up @@ -161,12 +161,17 @@ async def _check_threepid(self, medium: str, authdict: dict) -> dict:
self.hs.config.account_threepid_delegate_msisdn, threepid_creds
)
elif medium == "email":
if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
if (
self.hs.config.email.threepid_behaviour_email
== ThreepidBehaviour.REMOTE
):
assert self.hs.config.account_threepid_delegate_email
threepid = await identity_handler.threepid_from_creds(
self.hs.config.account_threepid_delegate_email, threepid_creds
)
elif self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
elif (
self.hs.config.email.threepid_behaviour_email == ThreepidBehaviour.LOCAL
):
threepid = None
row = await self.store.get_threepid_validation_session(
medium,
Expand Down Expand Up @@ -218,7 +223,7 @@ def __init__(self, hs: "HomeServer"):
_BaseThreepidAuthChecker.__init__(self, hs)

def is_enabled(self) -> bool:
return self.hs.config.threepid_behaviour_email in (
return self.hs.config.email.threepid_behaviour_email in (
ThreepidBehaviour.REMOTE,
ThreepidBehaviour.LOCAL,
)
Expand Down
8 changes: 5 additions & 3 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,16 @@ def __init__(self, hs: "HomeServer", auth_handler):
self.custom_template_dir = hs.config.server.custom_template_directory

try:
app_name = self._hs.config.email_app_name
app_name = self._hs.config.email.email_app_name

self._from_string = self._hs.config.email_notif_from % {"app": app_name}
self._from_string = self._hs.config.email.email_notif_from % {
"app": app_name
}
except (KeyError, TypeError):
# If substitution failed (which can happen if the string contains
# placeholders other than just "app", or if the type of the placeholder is
# not a string), fall back to the bare strings.
self._from_string = self._hs.config.email_notif_from
self._from_string = self._hs.config.email.email_notif_from

self._raw_from = email.utils.parseaddr(self._from_string)[1]

Expand Down
2 changes: 1 addition & 1 deletion synapse/push/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ def _app_name_from_pusherdict(self, pusher_config: PusherConfig) -> str:
if isinstance(brand, str):
return brand

return self.config.email_app_name
return self.config.email.email_app_name
4 changes: 2 additions & 2 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ async def on_PUT(
user_id, medium, address, current_time
)
if (
self.hs.config.email_enable_notifs
and self.hs.config.email_notif_for_new_users
self.hs.config.email.email_enable_notifs
and self.hs.config.email.email_notif_for_new_users
):
await self.pusher_pool.add_pusher(
user_id=user_id,
Expand Down
Loading

0 comments on commit e584534

Please sign in to comment.