diff --git a/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl b/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl index effb317d..84e66579 100644 --- a/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl +++ b/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl @@ -114,10 +114,6 @@ frontend http-blackhole {{- end -}} -# a backend which responds to everything with a 204 -backend return_204 - http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization" - # a fake backend which fonxes every request with a 500. Useful for # handling overloads etc. backend return_500 diff --git a/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl b/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl index 791d228a..06839e90 100644 --- a/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl +++ b/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl @@ -73,7 +73,12 @@ frontend synapse-http-in http-request set-var(req.backend) path,map_reg(/synapse/path_map_file_get,main) if has_get_map METH_GET http-request set-var(req.backend) path,map_reg(/synapse/path_map_file,main) unless { var(req.backend) -m found } - use_backend return_204 if { method OPTIONS } +{{- if $root.Values.matrixAuthenticationService.enabled }} + acl rendezvous path_beg /_matrix/client/unstable/org.matrix.msc4108/rendezvous + acl rendezvous path_beg /_synapse/client/rendezvous + use_backend return_204_rendezvous if { method OPTIONS } rendezvous +{{- end }} + use_backend return_204_synapse if { method OPTIONS } {{- range .ingress.additionalPaths -}} {{- if eq .availability "internally_and_externally" }} @@ -194,4 +199,14 @@ backend synapse-be_{{ $additionalPathId }} {{- end }} {{- end }} +{{- end }} + +# a backend which responds to everything with a 204 mirroring https://github.com/element-hq/synapse/blob/v1.124.0/synapse/http/server.py#L901-L932 +backend return_204_synapse + http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, HEAD, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization, Date" hdr "Access-Control-Expose-Headers" "Synapse-Trace-Id, Server" + +{{- if $root.Values.matrixAuthenticationService.enabled }} + +backend return_204_rendezvous + http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, HEAD, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, Content-Type, Accept, Content-Type, If-Match, If-None-Match" hdr "Access-Control-Expose-Headers" "Synapse-Trace-Id, Server, ETag" {{- end -}} diff --git a/charts/matrix-stack/templates/synapse/_synapse_config.tpl b/charts/matrix-stack/templates/synapse/_synapse_config.tpl index 40a9f537..9c95f29a 100644 --- a/charts/matrix-stack/templates/synapse/_synapse_config.tpl +++ b/charts/matrix-stack/templates/synapse/_synapse_config.tpl @@ -114,6 +114,8 @@ experimental_features: admin_token: ${MAS_SHARED_SECRET} introspection_endpoint: "http://{{ $root.Release.Name }}-matrix-authentication-service.{{ $root.Release.Namespace }}.svc.cluster.local:8080/oauth2/introspect" + # QR Code Login. Requires MAS + msc4108_enabled: true password_config: localdb_enabled: false enabled: false diff --git a/newsfragments/232.added.md b/newsfragments/232.added.md new file mode 100644 index 00000000..81179117 --- /dev/null +++ b/newsfragments/232.added.md @@ -0,0 +1 @@ +Support QR code login when MAS is enabled. diff --git a/tests/integration/lib/utils.py b/tests/integration/lib/utils.py index daa9247c..aa137cb2 100644 --- a/tests/integration/lib/utils.py +++ b/tests/integration/lib/utils.py @@ -6,6 +6,8 @@ import asyncio import base64 import json import os +from collections.abc import AsyncGenerator +from contextlib import asynccontextmanager from dataclasses import dataclass from pathlib import Path from ssl import SSLContext @@ -57,6 +59,15 @@ def b64encode(value: str): return base64.b64encode(value.encode("utf-8")).decode("utf-8") +@asynccontextmanager +async def aiohttp_client(ssl_context: SSLContext) -> AsyncGenerator[RetryClient]: + async with ( + aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, + RetryClient(session, retry_options=retry_options, raise_for_status=True) as client, + ): + yield client + + async def aiottp_get_json(url: str, ssl_context: SSLContext) -> Any: """Do an async HTTP GET against a url, retry exponentially on 429s. It expects a JSON response. @@ -70,9 +81,8 @@ async def aiottp_get_json(url: str, ssl_context: SSLContext) -> Any: host = urlparse(url).hostname async with ( - aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, - RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, - retry.get( + aiohttp_client(ssl_context) as client, + client.get( url.replace(host, "127.0.0.1"), headers={"Host": host}, server_hostname=host, @@ -99,9 +109,8 @@ async def aiohttp_post_json(url: str, data: dict, headers: dict, ssl_context: SS host = urlparse(url).hostname async with ( - aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, - RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, - retry.post( + aiohttp_client(ssl_context) as client, + client.post( url.replace(host, "127.0.0.1"), headers=headers | {"Host": host}, server_hostname=host, json=data ) as response, ): diff --git a/tests/integration/test_synapse.py b/tests/integration/test_synapse.py index bdda3aba..26e4cb72 100644 --- a/tests/integration/test_synapse.py +++ b/tests/integration/test_synapse.py @@ -9,7 +9,7 @@ import pytest from .fixtures import ESSData from .lib.synapse import assert_downloaded_content, download_media, upload_media -from .lib.utils import KubeCtl, aiohttp_post_json, aiottp_get_json, value_file_has +from .lib.utils import KubeCtl, aiohttp_client, aiohttp_post_json, aiottp_get_json, value_file_has @pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="Synapse not deployed") @@ -26,6 +26,9 @@ async def test_synapse_can_access_client_api( ) assert "unstable_features" in json_content + supports_qr_code_login = value_file_has("matrixAuthenticationService.enabled", True) + assert supports_qr_code_login == json_content["unstable_features"]["org.matrix.msc4108"] + @pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="Synapse not deployed") @pytest.mark.parametrize("users", [("sliding-sync-user",)], indirect=True) @@ -88,3 +91,26 @@ async def test_synapse_media_upload_fetch_authenticated( content_upload_json["content_uri"].replace(f"mxc://{generated_data.server_name}/", ""), content_download_sha256, ) + + +@pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="MAS not deployed") +@pytest.mark.asyncio_cooperative +async def test_rendezvous_cors_headers_are_only_set_with_mas(ingress_ready, generated_data: ESSData, ssl_context): + await ingress_ready("synapse") + async with ( + aiohttp_client(ssl_context) as client, + client.options( + f"https://synapse.{generated_data.server_name}/_matrix/client/unstable/org.matrix.msc4108/rendezvous", + ) as response, + ): + assert "Access-Control-Allow-Origin" in response.headers + assert response.headers["Access-Control-Allow-Origin"] == "*" + + assert "Access-Control-Allow-Headers" in response.headers + supports_qr_code_login = value_file_has("matrixAuthenticationService.enabled", True) + assert ("If-Match" in response.headers["Access-Control-Allow-Headers"]) == supports_qr_code_login + + assert "Access-Control-Expose-Headers" in response.headers + assert "Synapse-Trace-Id" in response.headers["Access-Control-Expose-Headers"] + assert "Server" in response.headers["Access-Control-Expose-Headers"] + assert ("ETag" in response.headers["Access-Control-Expose-Headers"]) == supports_qr_code_login diff --git a/tests/manifests/test_configs_and_mounts_consistency.py b/tests/manifests/test_configs_and_mounts_consistency.py index 7cc67e55..e695beb4 100644 --- a/tests/manifests/test_configs_and_mounts_consistency.py +++ b/tests/manifests/test_configs_and_mounts_consistency.py @@ -57,7 +57,7 @@ def get_volume_from_mount(template, volume_mount): def match_in_content(container_name, mounted_keys, mount_path, matches_in): found_mount = False for match_in in matches_in: - for match in re.findall(rf"{mount_path}/([^\s\n\")`;,]+(?!.*noqa))", match_in): + for match in re.findall(rf"(?:^|\s|\"){mount_path}/([^\s\n\")`;,]+(?!.*noqa))", match_in): assert f"{mount_path}/{match}" in mounted_keys, ( f"{mount_path}/{match} used in {container_name} but it is not found " f"from any mounted secret or configmap"