Files
Matt Van Horn 4f68a0ff2e Retry syn2mas guest registration on 429s
Synapse restarts to apply the guest access config change, but its
ingress was already ready, so wrap the guest registration in
async_retry_with_timeout and retry on 429s.
2026-11-12 09:31:07 -08:00

197 lines
8.1 KiB
Python

# Copyright 2025 New Vector Ltd
# Copyright 2025-2026 Element Creations Ltd
#
# SPDX-License-Identifier: AGPL-3.0-only
import aiohttp
import pyhelm3
import pytest
from lightkube import AsyncClient
from lightkube.resources.core_v1 import Pod
from .fixtures import ESSData, User
from .fixtures.users import create_mas_user, get_client_token
from .lib.helpers import deploy_with_values_patch, get_deployment_marker
from .lib.utils import aiohttp_get_json, aiohttp_post_json, async_retry_with_timeout, value_file_has
from .test_matrix_authentication_service import test_matrix_authentication_service_graphql_endpoint
@pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="Synapse not deployed")
@pytest.mark.skipif(
not value_file_has("matrixAuthenticationService.syn2mas")
or value_file_has("matrixAuthenticationService.syn2mas.enabled", False),
reason="Syn2Mas not deployed or the tested chart version does not support syn2mas",
)
@pytest.mark.parametrize("users", [(User(name="syn2mas-user"),)], indirect=True)
@pytest.mark.asyncio_cooperative
async def test_run_syn2mas_upgrade(
helm_client: pyhelm3.Client,
kube_client: AsyncClient,
users,
ingress_ready,
ssl_context,
generated_data: ESSData,
pytestconfig,
):
access_token = users[0].access_token
await ingress_ready("synapse")
assert await get_deployment_marker(kube_client, generated_data, "MATRIX_STACK_MSC3861") == "legacy_auth"
# Guest accounts produce preflight warnings, but must not block either migration mode.
# Register a guest before MAS is enabled, so syn2mas finds it on its first run.
revision, error = await deploy_with_values_patch(
generated_data,
helm_client,
{"synapse": {"additional": {"syn2mas-guests.yaml": {"config": "allow_guest_access: true"}}}},
)
assert error is None, error
assert revision.status == pyhelm3.ReleaseRevisionStatus.DEPLOYED
await ingress_ready("synapse")
# Synapse restarts to apply the config change, but its ingress was already ready,
# so retry on 429s until HAProxy sees the restarted backend
async def _register_guest():
return await aiohttp_post_json(
f"https://synapse.{generated_data.server_name}/_matrix/client/v3/register?kind=guest",
{},
{},
ssl_context,
)
guest = await async_retry_with_timeout(
_register_guest,
should_retry=lambda e: isinstance(e, aiohttp.ClientResponseError) and e.status == 429,
)
assert guest["user_id"]
assert guest["access_token"]
# After the base chart is setup, we enable MAS to run the syn2mas dry run job.
# Guest registration itself must be disabled for preflight checks to pass,
# and the guest warning only lets the migration proceed when acceptWarnings is set.
revision, error = await deploy_with_values_patch(
generated_data,
helm_client,
{
"matrixAuthenticationService": {"enabled": True, "syn2mas": {"acceptWarnings": True}},
"synapse": {"additional": {"syn2mas-guests.yaml": {"config": "allow_guest_access: false"}}},
},
)
assert error is None, error
assert revision.status == pyhelm3.ReleaseRevisionStatus.DEPLOYED
# We should still be able to reach synapse ingress
await ingress_ready("synapse")
# Auth metadata endpoint should not be reachable
with pytest.raises(aiohttp.ClientResponseError):
await aiohttp_get_json(
f"https://synapse.{generated_data.server_name}/_matrix/client/v1/auth_metadata",
{},
ssl_context,
)
# Syn2Mas is running in dryRun mode, so the state has not changed yet
assert await get_deployment_marker(kube_client, generated_data, "MATRIX_STACK_MSC3861") == "legacy_auth"
# MAS should be reachable through its ingress
await test_matrix_authentication_service_graphql_endpoint(ingress_ready, generated_data, ssl_context)
logs = ""
async for pod in kube_client.list(
Pod,
namespace=generated_data.ess_namespace,
labels={"job-name": f"{generated_data.release_name}-syn2mas"},
):
assert pod.metadata
assert pod.metadata.name
async for line in kube_client.log(
pod.metadata.name, namespace=generated_data.ess_namespace, container="syn2mas-migrate"
):
logs += line
assert "Synapse database contains 1 guests" in logs, logs
assert "Command failed with status: 11" in logs, logs
assert "syn2mas migrate --dry-run ended" in logs, logs
assert await get_deployment_marker(kube_client, generated_data, "MATRIX_STACK_MSC3861") == "legacy_auth"
await ingress_ready("synapse")
# After the base chart is setup, we enable MAS to run the syn2mas actual migration
revision, error = await deploy_with_values_patch(
generated_data, helm_client, {"matrixAuthenticationService": {"syn2mas": {"dryRun": False}}}
)
assert error is None, error
assert revision.status == pyhelm3.ReleaseRevisionStatus.DEPLOYED
# Syn2Mas is running in migrate mode, so the state must have changed
assert await get_deployment_marker(kube_client, generated_data, "MATRIX_STACK_MSC3861") == "syn2mas_migrated"
# Assert we can run syn2mas again (same values as last run)
revision, error = await deploy_with_values_patch(generated_data, helm_client, {})
assert error is None, error
assert revision.status == pyhelm3.ReleaseRevisionStatus.DEPLOYED
# Auth metadata endpoint should be reachable
response = await aiohttp_get_json(
f"https://synapse.{generated_data.server_name}/_matrix/client/v1/auth_metadata",
{},
ssl_context,
)
assert "issuer" in response
# MAS should be reachable through its ingress
await test_matrix_authentication_service_graphql_endpoint(ingress_ready, generated_data, ssl_context)
# The Synapse-issued tokens should have been migrated
sync_result = await aiohttp_post_json(
f"https://synapse.{generated_data.server_name}/_matrix/client/unstable/org.matrix.simplified_msc3575/sync",
{},
{"Authorization": f"Bearer {access_token}"},
ssl_context,
)
assert "pos" in sync_result, (
"The sync result failed, meaning the Synapse-issued tokens have probably not been migrated"
)
revision, error = await deploy_with_values_patch(
generated_data, helm_client, {"matrixAuthenticationService": {"syn2mas": {"enabled": False}}}
)
assert error is None, error
assert revision.status == pyhelm3.ReleaseRevisionStatus.DEPLOYED
# MAS should be available
await test_matrix_authentication_service_graphql_endpoint(ingress_ready, generated_data, ssl_context)
# The marker should now show delegated_auth
assert await get_deployment_marker(kube_client, generated_data, "MATRIX_STACK_MSC3861") == "delegated_auth"
admin_token = await get_client_token(f"mas.{generated_data.server_name}", generated_data, ssl_context)
mas_created_user = await create_mas_user(
f"mas.{generated_data.server_name}",
f"synapse.{generated_data.server_name}",
"after-syn2mas",
generated_data.secrets_random,
False,
admin_token,
ssl_context,
pytestconfig,
)
# The MAS-issued tokens should also work
sync_result = await aiohttp_post_json(
f"https://synapse.{generated_data.server_name}/_matrix/client/unstable/org.matrix.simplified_msc3575/sync",
{},
{"Authorization": f"Bearer {mas_created_user}"},
ssl_context,
)
assert "pos" in sync_result, "The sync result failed, meaning the MAS-issued tokens are wrong"
revision, error = await deploy_with_values_patch(
generated_data, helm_client, {"matrixAuthenticationService": {"syn2mas": {"enabled": True}}}, timeout="15s"
)
assert error is not None, "There was no error"
assert revision.description
assert revision.status == pyhelm3.ReleaseRevisionStatus.FAILED
assert "pre-upgrade hooks failed" in revision.description
# Assert that MAS still works
await test_matrix_authentication_service_graphql_endpoint(ingress_ready, generated_data, ssl_context)