register_new_matrix_user: Support multiple config files (#18784)

Co-authored-by: Andrew Morgan <andrew@amorgan.xyz>
This commit is contained in:
V02460 2025-11-10 17:52:57 +01:00 committed by GitHub
parent a50923b6bf
commit dc7f01f334
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -0,0 +1 @@
Support multiple config files in `register_new_matrix_user`.

View File

@ -26,7 +26,7 @@ import hashlib
import hmac import hmac
import logging import logging
import sys import sys
from typing import Any, Callable from typing import Any, Callable, Iterable, TextIO
import requests import requests
import yaml import yaml
@ -244,6 +244,7 @@ def main() -> None:
group.add_argument( group.add_argument(
"-c", "-c",
"--config", "--config",
action="append",
type=argparse.FileType("r"), type=argparse.FileType("r"),
help="Path to server config file. Used to read in shared secret.", help="Path to server config file. Used to read in shared secret.",
) )
@ -264,7 +265,7 @@ def main() -> None:
config: dict[str, Any] | None = None config: dict[str, Any] | None = None
if "config" in args and args.config: if "config" in args and args.config:
config = yaml.safe_load(args.config) config = _read_config_files(args.config)
if args.shared_secret: if args.shared_secret:
secret = args.shared_secret secret = args.shared_secret
@ -326,6 +327,33 @@ def main() -> None:
) )
# Adapted from synapse.config._base.
def _read_config_files(config_files: Iterable[TextIO]) -> dict[str, Any]:
"""Read the config files and shallowly merge them into a dict.
Successive configurations are shallowly merged into ones provided earlier,
i.e., entirely replacing top-level sections of the configuration.
Args:
config_files: A list of the config files to read
Returns:
The configuration dictionary.
"""
specified_config = {}
for config_file in config_files:
yaml_config = yaml.safe_load(config_file)
if not isinstance(yaml_config, dict):
err = "File %r is empty or doesn't parse into a key-value map. IGNORING."
print(err % (config_file,))
continue
specified_config.update(yaml_config)
return specified_config
def _read_file(file_path: Any, config_path: str) -> str: def _read_file(file_path: Any, config_path: str) -> str:
"""Check the given file exists, and read it into a string """Check the given file exists, and read it into a string