# VM test for matrix.nix + matrix-proxy.nix. Run: # nix build .#checks.x86_64-linux.matrix -L # # jupiter runs Synapse + MAS on postgres with /mnt/data's real traversal perms; # neptun runs the public caddy vhosts (local CA). Authentik isn't in the loop: the # test user comes from `mas manage register-user` and logs in with a compat token. { pkgs }: let # Throwaway, generated per build. keys = pkgs.runCommand "matrix-test-keys" { nativeBuildInputs = [ pkgs.openssl ]; } '' mkdir $out printf %s "ed25519 a_test $(openssl rand 32 | base64 | tr -d '=')" > $out/signing.key printf %s "$(openssl rand -hex 32)" > $out/macaroon printf %s "$(openssl rand -hex 32)" > $out/form printf %s "$(openssl rand -hex 32)" > $out/mas-shared printf %s "$(openssl rand -hex 32)" > $out/mas-encryption openssl genrsa -out $out/mas-rsa.pem 2048 printf dummy > $out/oidc ''; secret = name: user: group: mode: { source = "${keys}/${name}"; inherit user group mode; }; in pkgs.testers.runNixOSTest { name = "matrix"; nodes.jupiter = { config, ... }: { imports = [ ./matrix.nix ]; virtualisation.memorySize = 2048; environment.etc = { "matrix/signing.key" = secret "signing.key" "matrix-synapse" "matrix-synapse" "0400"; "matrix/macaroon" = secret "macaroon" "matrix-synapse" "matrix-synapse" "0400"; "matrix/form" = secret "form" "matrix-synapse" "matrix-synapse" "0400"; "matrix/mas-shared" = secret "mas-shared" "matrix-synapse" "matrix-synapse" "0440"; "matrix/mas-encryption" = secret "mas-encryption" "matrix-authentication-service" "matrix-authentication-service" "0400"; "matrix/mas-rsa.pem" = secret "mas-rsa.pem" "matrix-authentication-service" "matrix-authentication-service" "0400"; "matrix/oidc" = secret "oidc" "matrix-authentication-service" "matrix-authentication-service" "0400"; }; homelab.matrix = { signingKeyFile = "/etc/matrix/signing.key"; macaroonSecretFile = "/etc/matrix/macaroon"; formSecretFile = "/etc/matrix/form"; masSecretFile = "/etc/matrix/mas-shared"; masEncryptionFile = "/etc/matrix/mas-encryption"; masSigningKeyFile = "/etc/matrix/mas-rsa.pem"; oidcClientSecretFile = "/etc/matrix/oidc"; }; # jupiter's /mnt/data/AppData is drwx--x--- darman:users; the non-root owner # matters (tmpfiles' unsafe-path-transition check). users.users.darman = { isNormalUser = true; group = "users"; }; systemd.tmpfiles.rules = [ "d /mnt/data/AppData 0710 darman users -" ]; # Stand-in for the trusted tailscale0. networking.firewall.allowedTCPPorts = [ 8008 8080 ]; }; nodes.neptun = { imports = [ ../network/caddy.nix ./matrix-proxy.nix ]; homelab.matrixProxy.upstream = "jupiter"; services.caddy.globalConfig = '' local_certs skip_install_trust ''; }; testScript = '' import json import re from urllib.parse import quote start_all() jupiter.wait_for_unit("matrix-synapse.service") jupiter.wait_for_unit("matrix-authentication-service.service") jupiter.wait_for_open_port(8008) jupiter.wait_for_open_port(8080) neptun.wait_for_unit("caddy.service") neptun.wait_for_open_port(443) with subtest("synapse database has C collation"): out = jupiter.succeed( "sudo -u postgres psql -tAc \"SELECT datcollate || ',' || datctype FROM pg_database WHERE datname = 'matrix-synapse'\"" ).strip() assert out == "C,C", out def req(path, host="matrix.mgaction.town", args=""): return neptun.succeed( f"curl -sSf -k --resolve {host}:443:127.0.0.1 {args} 'https://{host}{path}'" ) def status(path, host="matrix.mgaction.town"): return neptun.succeed( f"curl -s -k -o /dev/null -w '%{{http_code}}' --resolve {host}:443:127.0.0.1 'https://{host}{path}'" ) with subtest("apex delegates to matrix.mgaction.town"): assert json.loads(req("/.well-known/matrix/server", "mgaction.town")) == { "m.server": "matrix.mgaction.town:443" } assert json.loads(req("/.well-known/matrix/client", "mgaction.town")) == { "m.homeserver": {"base_url": "https://matrix.mgaction.town"} } headers = req("/.well-known/matrix/client", "mgaction.town", "-D - -o /dev/null").lower() assert "access-control-allow-origin: *" in headers, headers assert status("/", "mgaction.town") == "404" with subtest("clients are pointed at MAS, SSO only"): meta = json.loads(req("/_matrix/client/v1/auth_metadata")) assert meta["issuer"] == "https://account.mgaction.town/", meta flows = [f["type"] for f in json.loads(req("/_matrix/client/v3/login"))["flows"]] assert "m.login.sso" in flows and "m.login.password" not in flows, flows req("/.well-known/openid-configuration", "account.mgaction.town") with subtest("admin API is not public"): assert status("/_synapse/admin/v1/server_version") == "404" jupiter.succeed("curl -sf http://localhost:8008/_synapse/admin/v1/server_version") with subtest("user registered in MAS can chat"): jupiter.succeed("sudo -u matrix-authentication-service mas manage register-user alice --yes --display-name Alice") out = jupiter.succeed("sudo -u matrix-authentication-service mas manage issue-compatibility-token alice 2>&1") m = re.search(r"mct_\w+", out) assert m, out token = m.group(0) auth = f"-H 'Authorization: Bearer {token}'" # Provisioning into synapse is an async MAS job. neptun.wait_until_succeeds( f"curl -sSf -k --resolve matrix.mgaction.town:443:127.0.0.1 {auth} https://matrix.mgaction.town/_matrix/client/v3/account/whoami | grep -F '@alice:mgaction.town'", timeout=60, ) room = json.loads(req("/_matrix/client/v3/createRoom", args=f"{auth} -X POST -d '{{}}'"))["room_id"] r = quote(room) req(f"/_matrix/client/v3/rooms/{r}/send/m.room.message/t1", args=f"""{auth} -X PUT -d '{{"msgtype":"m.text","body":"hello from the vm"}}'""") msgs = req(f"/_matrix/client/v3/rooms/{r}/messages?dir=b", args=auth) assert "hello from the vm" in msgs, msgs with subtest("media upload lands on the array"): neptun.succeed("head -c 5000000 /dev/urandom > /tmp/blob") up = json.loads(req("/_matrix/media/v3/upload?filename=blob", args=f"{auth} -X POST -H 'Content-Type: application/octet-stream' --data-binary @/tmp/blob")) assert up["content_uri"].startswith("mxc://mgaction.town/"), up jupiter.succeed("find /mnt/data/AppData/matrix-synapse -type f -size +4M | grep -q .") with subtest("survives a restart"): jupiter.succeed("systemctl restart matrix-authentication-service matrix-synapse") jupiter.wait_for_open_port(8008) jupiter.wait_for_open_port(8080) neptun.wait_until_succeeds( f"curl -sSf -k --resolve matrix.mgaction.town:443:127.0.0.1 {auth} https://matrix.mgaction.town/_matrix/client/v3/account/whoami", timeout=60, ) ''; }