Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the configuration file handling code into a separate module #385

Merged
merged 28 commits into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cf17843
Add SydentConfig class and use when calling Sydent constructor
Sep 7, 2021
2985b37
Move database config handling over to SydentConfig
Sep 7, 2021
3a1a400
Move crypto config handling over to SydentConfig
Sep 7, 2021
91fdc6b
Move sms config handling over to SydentConfig
Sep 7, 2021
4b0a900
Move deprecated email template config over to SydentConfig
Sep 7, 2021
21b030a
Move email config handled by synapse.py over to SynapseConfig
Sep 7, 2021
11f8711
Move rest of email config over to SydenConfig
Sep 7, 2021
f09779b
Move deprecated http template config over to SydentConfig
Sep 7, 2021
25aac48
Move rest of http config handling over to SynapseConfig
Sep 7, 2021
1492c1c
Move server name config handling to SydentConfig
Sep 7, 2021
bb02656
Move 'general' template config handling over to SydentConfig
Sep 7, 2021
2d460ee
Move rest of 'general' config handling over to SydentConfig
Sep 7, 2021
4b0eb67
Remove deprecated template argument from get_branded_template
Sep 8, 2021
37b928b
Remove old cfg argument from Sydent constructor
Sep 8, 2021
a9415f4
Add changelog
Sep 8, 2021
c0b7b03
Merge remote-tracking branch 'origin/main' into azren/move_config_cod…
Sep 9, 2021
cf58f3a
Add file that got lost while fixing merge conflicts
Sep 9, 2021
85bd376
Apply suggestions from code review
Sep 10, 2021
6cf0090
Run linters
Sep 10, 2021
b0ad7d0
Make initial read of internalapi.http.port a local variable
Sep 10, 2021
760963e
Standardize it being one empty line after licence
Sep 10, 2021
d2c24c3
Document more clearly that parse_config_file sets up logging
Sep 10, 2021
e4b0bcb
Merge remote-tracking branch 'origin/main' into azren/move_config_cod…
Sep 10, 2021
64d3d34
Readd lines between licence and code to make linters happy
Sep 10, 2021
e57fa59
Apply suggestions from code review
Sep 10, 2021
d12d283
Add missing file from last commit
Sep 10, 2021
e4ce136
Fix type issues in config code
Sep 13, 2021
98bc803
Merge remote-tracking branch 'origin/main' into azren/move_config_cod…
Sep 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions sydent/config/__init__.py
Expand Up @@ -14,6 +14,7 @@

from configparser import ConfigParser

from sydent.config.crypto import CryptoConfig
from sydent.config.database import DatabaseConfig


Expand All @@ -29,8 +30,12 @@ class SydentConfig:

def __init__(self):
self.database = DatabaseConfig()
self.crypto = CryptoConfig()

self.config_sections = [self.database]
self.config_sections = [
self.database,
self.crypto,
]

def _parse_config(self, cfg: ConfigParser) -> None:
Azrenbeth marked this conversation as resolved.
Show resolved Hide resolved
"""
Expand All @@ -42,10 +47,17 @@ def _parse_config(self, cfg: ConfigParser) -> None:
for section in self.config_sections:
section.parse_config(cfg)

def parse_from_config_parser(self, cfg: ConfigParser) -> None:
def parse_from_config_parser(self, cfg: ConfigParser) -> bool:
"""
Parse the configuration from a ConfigParser object

:param cfg: the configuration to be parsed
...
Azrenbeth marked this conversation as resolved.
Show resolved Hide resolved
:return: Whether or not cfg has been changed and needs saving
"""
self._parse_config(cfg)

# TODO: Don't alter config file when starting Sydent unless
# user has asked for this specifially (e.g. on first
# run only, or when specify --generate-config)
return self.crypto.save_key
Azrenbeth marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 29 additions & 22 deletions sydent/sign/ed25519.py → sydent/config/crypto.py
@@ -1,4 +1,4 @@
# Copyright 2014 OpenMarket Ltd
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,53 +12,60 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import logging
from typing import TYPE_CHECKING

import nacl.encoding
import nacl.exceptions
import nacl.signing
import nacl
import signedjson.key

if TYPE_CHECKING:
from configparser import ConfigParser

logger = logging.getLogger(__name__)


class SydentEd25519:
def __init__(self, syd):
self.sydent = syd
class CryptoConfig:
def parse_config(self, cfg: "ConfigParser") -> None:
"""
Parse the crypto section of the config
:param cfg: the configuration to be parsed
"""

save_key = False
signing_key_str = cfg.get("crypto", "ed25519.signingkey")
signing_key_parts = signing_key_str.split(" ")

sk_str = self.sydent.cfg.get("crypto", "ed25519.signingkey")
sk_parts = sk_str.split(" ")
self.save_key = False

if sk_str == "":
if signing_key_str == "":
logger.info(
"This server does not yet have an ed25519 signing key. "
+ "Creating one and saving it in the config file."
"Creating one and saving it in the config file."
)

self.signing_key = signedjson.key.generate_signing_key("0")
save_key = True
elif len(sk_parts) == 1:

self.save_key = True
elif len(signing_key_parts) == 1:
# old format key
logger.info("Updating signing key format: brace yourselves")

self.signing_key = nacl.signing.SigningKey(
sk_str, encoder=nacl.encoding.HexEncoder
signing_key_str, encoder=nacl.encoding.HexEncoder
)
self.signing_key.version = "0"
self.signing_key.alg = signedjson.key.NACL_ED25519

save_key = True
self.save_key = True
else:
self.signing_key = signedjson.key.decode_signing_key_base64(
sk_parts[0], sk_parts[1], sk_parts[2]
signing_key_parts[0], signing_key_parts[1], signing_key_parts[2]
)

if save_key:
sk_str = "%s %s %s" % (
if self.save_key:
signing_key_str = "%s %s %s" % (
self.signing_key.alg,
self.signing_key.version,
signedjson.key.encode_signing_key_base64(self.signing_key),
)
self.sydent.cfg.set("crypto", "ed25519.signingkey", sk_str)
self.sydent.save_config()
logger.info("Key saved")
cfg.set("crypto", "ed25519.signingkey", signing_key_str)
Empty file removed sydent/sign/__init__.py
Empty file.
9 changes: 6 additions & 3 deletions sydent/sydent.py
Expand Up @@ -69,7 +69,6 @@
from sydent.http.servlets.v1_servlet import V1Servlet
from sydent.http.servlets.v2_servlet import V2Servlet
from sydent.replication.pusher import Pusher
from sydent.sign.ed25519 import SydentEd25519
from sydent.threepid.bind import ThreepidBinder
from sydent.util.hash import sha256_and_url_safe_base64
from sydent.util.ip_range import DEFAULT_IP_RANGE_BLACKLIST, generate_ip_set
Expand Down Expand Up @@ -326,7 +325,7 @@ def __init__(
self.validators.msisdn = MsisdnValidator(self)

self.keyring = Keyring()
self.keyring.ed25519 = SydentEd25519(self).signing_key
self.keyring.ed25519 = self.config.crypto.signing_key
self.keyring.ed25519.alg = "ed25519"

self.sig_verifier = Verifier(self)
Expand Down Expand Up @@ -610,7 +609,11 @@ def run_gc():
setup_logging(cfg)

sydent_config = SydentConfig()
sydent_config.parse_from_config_parser(cfg)
cfg_needs_saving = sydent_config.parse_from_config_parser(cfg)

syd = Sydent(cfg, sydent_config=sydent_config)

if cfg_needs_saving:
syd.save_config()

syd.run()