From 9d596feaaff72154ff94fe3a54840677f672ddb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Goinvic?= Date: Fri, 27 Feb 2026 11:43:25 +0100 Subject: [PATCH] migration: Add support for MAS config --- scripts/migration/__main__.py | 12 ++++ scripts/migration/engine.py | 24 ++++--- scripts/migration/inputs.py | 4 +- scripts/migration/tests/conftest.py | 30 +++++++++ scripts/migration/tests/test_main_e2e.py | 74 +++++++++++++++++++++ scripts/migration/tests/test_mas_secrets.py | 25 +++++++ 6 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 scripts/migration/tests/test_mas_secrets.py diff --git a/scripts/migration/__main__.py b/scripts/migration/__main__.py index 17a06429..6cd468fe 100644 --- a/scripts/migration/__main__.py +++ b/scripts/migration/__main__.py @@ -95,6 +95,12 @@ Examples: ), ) + parser.add_argument( + "--mas-config", + required=False, + help=("Path to Matrix Authentication Service config.yaml configuration file. "), + ) + parser.add_argument( "--output-dir", default="output", @@ -165,6 +171,12 @@ Examples: config_path=args.synapse_config, ) + if args.mas_config: + input_processor.load_migration_input( + name="matrixAuthenticationService", + config_path=args.mas_config, + ) + # Run migration reporter.report_step(MIGRATING_STEP) engine = MigrationEngine(input_processor=input_processor, pretty_logger=pretty_logger) diff --git a/scripts/migration/engine.py b/scripts/migration/engine.py index 0129ae38..64bbea10 100644 --- a/scripts/migration/engine.py +++ b/scripts/migration/engine.py @@ -40,18 +40,20 @@ class MigrationEngine: (SynapseMigration(), SynapseSecretDiscovery(), SynapseExtraFileDiscovery()), (MASMigration(), MASSecretDiscovery(), MASExtraFileDiscovery()), ]: - self.migrators.append( - MigrationService( - input=self.input_processor.input_for_component(migration.component_root_key), - ess_config=self.ess_config, - pretty_logger=self.pretty_logger, - migration=migration, - configmaps=self.configmaps, - secrets=self.secrets, - secret_discovery_strategy=secret_discovery_strategy, - extra_files_strategy=extra_file_strategy, + migration_input = self.input_processor.input_for_component(migration.component_root_key) + if migration_input: + self.migrators.append( + MigrationService( + input=migration_input, + ess_config=self.ess_config, + pretty_logger=self.pretty_logger, + migration=migration, + secrets=self.secrets, + secret_discovery_strategy=secret_discovery_strategy, + configmaps=self.configmaps, + extra_files_strategy=extra_file_strategy, + ) ) - ) def run_migration(self) -> dict[str, Any]: """ diff --git a/scripts/migration/inputs.py b/scripts/migration/inputs.py index 2519ea54..81c6af25 100644 --- a/scripts/migration/inputs.py +++ b/scripts/migration/inputs.py @@ -32,14 +32,14 @@ class InputProcessor: inputs: list[MigrationInput] = field(default_factory=list) - def input_for_component(self, component_root_key: str) -> MigrationInput: + def input_for_component(self, component_root_key: str) -> MigrationInput | None: """ Return the migration input for a component. """ for _input in self.inputs: if _input.name == component_root_key: return _input - raise MigrationError(f"Component {component_root_key} does not have an migration input service") + return None @staticmethod def load_yaml_file(path: str) -> dict[str, Any]: diff --git a/scripts/migration/tests/conftest.py b/scripts/migration/tests/conftest.py index 2ce15ad5..8115d5fb 100644 --- a/scripts/migration/tests/conftest.py +++ b/scripts/migration/tests/conftest.py @@ -105,3 +105,33 @@ def synapse_config_with_ca_federation_list(tmp_path, basic_synapse_config): ] return config + + +@pytest.fixture +def basic_mas_config(): + """Basic MAS configuration for testing.""" + return { + "http": {"public_base": "https://auth.example.com", "bind": {"address": "0.0.0.0", "port": 8080}}, + "database": {"uri": "postgresql://mas:mas_password@postgres:5432/mas?sslmode=prefer"}, + "secrets": { + "encryption": "my_encryption_key", + }, + "matrix": { + "homeserver": "test.example.com", + "secret": "synapse_shared_secret_abcdef", + "endpoint": "http://synapse:8008", + }, + } + + +@pytest.fixture +def write_mas_config(tmp_path): + """Helper fixture to write a MAS config file.""" + + def _write_config(config_data): + mas_config_file = tmp_path / "mas.yaml" + with open(mas_config_file, "w") as f: + yaml.dump(config_data, f) + return mas_config_file + + return _write_config diff --git a/scripts/migration/tests/test_main_e2e.py b/scripts/migration/tests/test_main_e2e.py index ec0306ab..c78a5b14 100644 --- a/scripts/migration/tests/test_main_e2e.py +++ b/scripts/migration/tests/test_main_e2e.py @@ -154,3 +154,77 @@ def test_main_e2e_synapse_only( "/etc/synapse/extra/ca-second.pem", "/etc/synapse/extra/another-ca.pem", ] + + +def test_main_e2e_synapse_with_mas( + tmp_path, synapse_config_with_signing_key, basic_mas_config, write_synapse_config, write_mas_config +): + """Test the complete end-to-end migration workflow with Synapse and MAS.""" + # Write configuration files + synapse_config_file = write_synapse_config(synapse_config_with_signing_key) + mas_config_file = write_mas_config(basic_mas_config) + + # Create output directory + output_dir = tmp_path / "output" + output_dir.mkdir() + + # Mock sys.argv to simulate CLI arguments + test_args = [ + "migration", + "--synapse-config", + str(synapse_config_file), + "--mas-config", + str(mas_config_file), + "--output-dir", + str(output_dir), + ] + + # Run the main function with mocked sys.argv + with patch.object(sys, "argv", test_args): + exit_code = __main__.main() + + # Verify successful execution + assert exit_code == 0 + + # Check that output files were created + values_file = output_dir / "values.yaml" + assert values_file.exists(), "values.yaml should be created" + + # Load and verify the generated values + with open(values_file) as f: + generated_values = yaml.safe_load(f) + + # Verify Synapse configuration was migrated + assert "synapse" in generated_values + synapse_config = generated_values["synapse"] + assert synapse_config["enabled"] is True + + # Verify MAS configuration was migrated + assert "matrixAuthenticationService" in generated_values + mas_config = generated_values["matrixAuthenticationService"] + assert mas_config["enabled"] is True + + # Verify MAS secrets were handled (using credential schema) + assert "synapseSharedSecret" in mas_config + + # Verify synapseSharedSecret uses credential schema + synapse_shared_secret_config = mas_config["synapseSharedSecret"] + assert "secret" in synapse_shared_secret_config + assert synapse_shared_secret_config["secret"] == "imported-matrix-authentication-service" + assert "secretKey" in synapse_shared_secret_config + assert synapse_shared_secret_config["secretKey"] == "matrixAuthenticationService.synapseSharedSecret" + + # Check for Secret files (should be created for both Synapse and MAS secrets) + secret_files = list(output_dir.glob("*secret.yaml")) + # Should have at least two secret files (one for Synapse, one for MAS) + assert len(secret_files) >= 2, "Secret files should be created for both Synapse and MAS secrets" + + # Verify the secret file content + for secret_file in secret_files: + with open(secret_file) as f: + secret_content = yaml.safe_load(f) + assert secret_content["apiVersion"] == "v1" + assert secret_content["kind"] == "Secret" + assert "metadata" in secret_content + assert "name" in secret_content["metadata"] + assert "data" in secret_content diff --git a/scripts/migration/tests/test_mas_secrets.py b/scripts/migration/tests/test_mas_secrets.py new file mode 100644 index 00000000..a9ee6fee --- /dev/null +++ b/scripts/migration/tests/test_mas_secrets.py @@ -0,0 +1,25 @@ +# Copyright 2026 Element Creations Ltd +# +# SPDX-License-Identifier: AGPL-3.0-only + +import logging + +from scripts.migration.secrets import SecretDiscovery + +from ..mas import MASSecretDiscovery + + +def test_discover_secrets_from_mas_config(basic_mas_config): + """Test MAS config with PostgreSQL database URI using transformer.""" + mas_config = basic_mas_config.copy() + # The basic_mas_config already has database.uri with password + + mas_secrets = MASSecretDiscovery() + discovery = SecretDiscovery(mas_secrets, logging.getLogger(), "mas.yaml") + discovery.discover_secrets(mas_config) + + # Should have discovered the password from database URI using transformer + assert "matrixAuthenticationService.postgres.password" in discovery.discovered_secrets + assert discovery.discovered_secrets["matrixAuthenticationService.postgres.password"].value == "mas_password" + assert "matrixAuthenticationService.encryptionSecret" in discovery.discovered_secrets + assert discovery.discovered_secrets["matrixAuthenticationService.encryptionSecret"].value == "my_encryption_key"