mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Additional type hints for the config module, part 2. (#11480)
This commit is contained in:
parent
941ebe49ff
commit
0cc3bf97b4
1
changelog.d/11480.misc
Normal file
1
changelog.d/11480.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Add missing type hints to `synapse.config` module.
|
@ -16,12 +16,14 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict, Iterator, List, Optional
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import jsonschema
|
import jsonschema
|
||||||
from signedjson.key import (
|
from signedjson.key import (
|
||||||
NACL_ED25519,
|
NACL_ED25519,
|
||||||
|
SigningKey,
|
||||||
|
VerifyKey,
|
||||||
decode_signing_key_base64,
|
decode_signing_key_base64,
|
||||||
decode_verify_key_bytes,
|
decode_verify_key_bytes,
|
||||||
generate_signing_key,
|
generate_signing_key,
|
||||||
@ -31,6 +33,7 @@ from signedjson.key import (
|
|||||||
)
|
)
|
||||||
from unpaddedbase64 import decode_base64
|
from unpaddedbase64 import decode_base64
|
||||||
|
|
||||||
|
from synapse.types import JsonDict
|
||||||
from synapse.util.stringutils import random_string, random_string_with_symbols
|
from synapse.util.stringutils import random_string, random_string_with_symbols
|
||||||
|
|
||||||
from ._base import Config, ConfigError
|
from ._base import Config, ConfigError
|
||||||
@ -81,14 +84,13 @@ To suppress this warning and continue using 'matrix.org', admins should set
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s(slots=True, auto_attribs=True)
|
||||||
class TrustedKeyServer:
|
class TrustedKeyServer:
|
||||||
# string: name of the server.
|
# name of the server.
|
||||||
server_name = attr.ib()
|
server_name: str
|
||||||
|
|
||||||
# dict[str,VerifyKey]|None: map from key id to key object, or None to disable
|
# map from key id to key object, or None to disable signature verification.
|
||||||
# signature verification.
|
verify_keys: Optional[Dict[str, VerifyKey]] = None
|
||||||
verify_keys = attr.ib(default=None)
|
|
||||||
|
|
||||||
|
|
||||||
class KeyConfig(Config):
|
class KeyConfig(Config):
|
||||||
@ -279,15 +281,15 @@ class KeyConfig(Config):
|
|||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
|
|
||||||
def read_signing_keys(self, signing_key_path, name):
|
def read_signing_keys(self, signing_key_path: str, name: str) -> List[SigningKey]:
|
||||||
"""Read the signing keys in the given path.
|
"""Read the signing keys in the given path.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
signing_key_path (str)
|
signing_key_path
|
||||||
name (str): Associated config key name
|
name: Associated config key name
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[SigningKey]
|
The signing keys read from the given path.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
signing_keys = self.read_file(signing_key_path, name)
|
signing_keys = self.read_file(signing_key_path, name)
|
||||||
@ -296,7 +298,9 @@ class KeyConfig(Config):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ConfigError("Error reading %s: %s" % (name, str(e)))
|
raise ConfigError("Error reading %s: %s" % (name, str(e)))
|
||||||
|
|
||||||
def read_old_signing_keys(self, old_signing_keys):
|
def read_old_signing_keys(
|
||||||
|
self, old_signing_keys: Optional[JsonDict]
|
||||||
|
) -> Dict[str, VerifyKey]:
|
||||||
if old_signing_keys is None:
|
if old_signing_keys is None:
|
||||||
return {}
|
return {}
|
||||||
keys = {}
|
keys = {}
|
||||||
@ -340,7 +344,7 @@ class KeyConfig(Config):
|
|||||||
write_signing_keys(signing_key_file, (key,))
|
write_signing_keys(signing_key_file, (key,))
|
||||||
|
|
||||||
|
|
||||||
def _perspectives_to_key_servers(config):
|
def _perspectives_to_key_servers(config: JsonDict) -> Iterator[JsonDict]:
|
||||||
"""Convert old-style 'perspectives' configs into new-style 'trusted_key_servers'
|
"""Convert old-style 'perspectives' configs into new-style 'trusted_key_servers'
|
||||||
|
|
||||||
Returns an iterable of entries to add to trusted_key_servers.
|
Returns an iterable of entries to add to trusted_key_servers.
|
||||||
@ -402,7 +406,9 @@ TRUSTED_KEY_SERVERS_SCHEMA = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _parse_key_servers(key_servers, federation_verify_certificates):
|
def _parse_key_servers(
|
||||||
|
key_servers: List[Any], federation_verify_certificates: bool
|
||||||
|
) -> Iterator[TrustedKeyServer]:
|
||||||
try:
|
try:
|
||||||
jsonschema.validate(key_servers, TRUSTED_KEY_SERVERS_SCHEMA)
|
jsonschema.validate(key_servers, TRUSTED_KEY_SERVERS_SCHEMA)
|
||||||
except jsonschema.ValidationError as e:
|
except jsonschema.ValidationError as e:
|
||||||
@ -444,7 +450,7 @@ def _parse_key_servers(key_servers, federation_verify_certificates):
|
|||||||
yield result
|
yield result
|
||||||
|
|
||||||
|
|
||||||
def _assert_keyserver_has_verify_keys(trusted_key_server):
|
def _assert_keyserver_has_verify_keys(trusted_key_server: TrustedKeyServer) -> None:
|
||||||
if not trusted_key_server.verify_keys:
|
if not trusted_key_server.verify_keys:
|
||||||
raise ConfigError(INSECURE_NOTARY_ERROR)
|
raise ConfigError(INSECURE_NOTARY_ERROR)
|
||||||
|
|
||||||
|
@ -22,10 +22,12 @@ from ._base import Config, ConfigError
|
|||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class MetricsFlags:
|
class MetricsFlags:
|
||||||
known_servers = attr.ib(default=False, validator=attr.validators.instance_of(bool))
|
known_servers: bool = attr.ib(
|
||||||
|
default=False, validator=attr.validators.instance_of(bool)
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all_off(cls):
|
def all_off(cls) -> "MetricsFlags":
|
||||||
"""
|
"""
|
||||||
Instantiate the flags with all options set to off.
|
Instantiate the flags with all options set to off.
|
||||||
"""
|
"""
|
||||||
|
@ -1257,7 +1257,7 @@ class ServerConfig(Config):
|
|||||||
help="Turn on the twisted telnet manhole service on the given port.",
|
help="Turn on the twisted telnet manhole service on the given port.",
|
||||||
)
|
)
|
||||||
|
|
||||||
def read_gc_intervals(self, durations) -> Optional[Tuple[float, float, float]]:
|
def read_gc_intervals(self, durations: Any) -> Optional[Tuple[float, float, float]]:
|
||||||
"""Reads the three durations for the GC min interval option, returning seconds."""
|
"""Reads the three durations for the GC min interval option, returning seconds."""
|
||||||
if durations is None:
|
if durations is None:
|
||||||
return None
|
return None
|
||||||
|
@ -132,7 +132,7 @@ class TlsConfig(Config):
|
|||||||
self.tls_certificate: Optional[crypto.X509] = None
|
self.tls_certificate: Optional[crypto.X509] = None
|
||||||
self.tls_private_key: Optional[crypto.PKey] = None
|
self.tls_private_key: Optional[crypto.PKey] = None
|
||||||
|
|
||||||
def read_certificate_from_disk(self):
|
def read_certificate_from_disk(self) -> None:
|
||||||
"""
|
"""
|
||||||
Read the certificates and private key from disk.
|
Read the certificates and private key from disk.
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user