| 1 | #!/usr/bin/env python3 |
| 2 | import argparse |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import subprocess |
| 7 | import socket |
| 8 | import tempfile |
| 9 | import time |
| 10 | import urllib.error |
| 11 | import urllib.request |
| 12 | import uuid |
| 13 | |
| 14 | import router |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | parser = argparse.ArgumentParser() |
| 19 | parser.add_argument("--image", required=True) |
| 20 | parser.add_argument("--output", type=Path) |
| 21 | args = parser.parse_args() |
| 22 | container = "snowglobe-dashboard-package-test-" + uuid.uuid4().hex[:12] |
| 23 | proof = uuid.uuid4().hex + uuid.uuid4().hex |
| 24 | |
| 25 | def shell(*argv): |
| 26 | return subprocess.run(argv, capture_output=True, text=True, check=True, timeout=60).stdout |
| 27 | |
| 28 | with tempfile.TemporaryDirectory(prefix="snowglobe-dashboard-package-", dir="/run") as temporary: |
| 29 | data = Path(temporary) / "data" |
| 30 | data.mkdir() |
| 31 | os.chown(data, 65534, 65534) |
| 32 | token = Path(temporary) / "proxy.token" |
| 33 | token.write_text(proof) |
| 34 | os.chown(token, 0, 65534) |
| 35 | token.chmod(0o440) |
| 36 | season = data / "media/Indie Shows/Fixture/Season 1" |
| 37 | season.mkdir(parents=True) |
| 38 | (season / "S01E01 - Fixture.mp4").write_bytes(b"fixture") |
| 39 | (season / "S01E01 - Fixture.nfo").write_text("<episodedetails><title>Packaged worker</title></episodedetails>") |
| 40 | try: |
| 41 | shell("podman", "run", "--detach", "--name", container, "--read-only", "--cap-drop=ALL", |
| 42 | "--security-opt=no-new-privileges", "--memory=512m", "--cpus=2", "--pids-limit=128", |
| 43 | "--publish=127.0.0.1::7072", "--volume=" + str(data) + ":/data:rw", |
| 44 | "--volume=" + str(token) + ":/run/secrets/dashboard-proxy.token:ro", |
| 45 | "--tmpfs=/tmp:rw,noexec,nosuid,size=64m", "--env=SNOWGLOBE_DATA_DIR=/data", |
| 46 | "--env=SNOWGLOBE_DOMAIN=snowglobe.test", "--env=SNOWGLOBE_MEDIA_READ_ONLY=true", |
| 47 | "--env=SNOWGLOBE_YT_STATE=/data/yt", "--env=SNOWGLOBE_YT_CONFIG=/data/config", |
| 48 | "--env=SNOWGLOBE_YT_MEDIA=/data/media", args.image) |
| 49 | info = json.loads(shell("podman", "inspect", container))[0] |
| 50 | port = info["NetworkSettings"]["Ports"]["7072/tcp"][0]["HostPort"] |
| 51 | base = "http://127.0.0.1:" + port |
| 52 | |
| 53 | def get(path, supplied=proof): |
| 54 | headers = { |
| 55 | "User-Name": "fixture", "User-Groups": "infra-admin,media,media-manage", |
| 56 | } |
| 57 | if supplied is not None: |
| 58 | headers["Snowglobe-Proxy-Token"] = supplied |
| 59 | request = urllib.request.Request(base + path, headers=headers) |
| 60 | with urllib.request.urlopen(request, timeout=30) as response: |
| 61 | return response.read() |
| 62 | |
| 63 | deadline = time.monotonic() + 40 |
| 64 | while True: |
| 65 | try: |
| 66 | get("/") |
| 67 | break |
| 68 | except OSError: |
| 69 | if time.monotonic() >= deadline: |
| 70 | raise |
| 71 | time.sleep(.1) |
| 72 | for path in ["/", "/api/me", "/api/launcher", "/assets/probe"]: |
| 73 | for supplied in [None, "0" * 64, proof[:-1], proof + "0"]: |
| 74 | try: |
| 75 | get(path, supplied) |
| 76 | raise AssertionError("forged ingress reached " + path) |
| 77 | except urllib.error.HTTPError as error: |
| 78 | assert error.code == 403, (path, error.code) |
| 79 | assert json.loads(get("/api/me"))["name"] == "fixture" |
| 80 | python = next(value.split("=", 1)[1] for value in info["Config"]["Env"] if value.startswith("SNOWGLOBE_YT_PYTHON=")) |
| 81 | address = next(network["IPAddress"] for network in info["NetworkSettings"]["Networks"].values() if network["IPAddress"]) |
| 82 | neighbor = f"""import urllib.request, urllib.error |
| 83 | request = urllib.request.Request('http://{address}:7072/api/me', headers={{ |
| 84 | 'User-Name': 'snow', 'User-Groups': 'infra-admin', 'Snowglobe-Proxy-Token': '0' * 64, |
| 85 | }}) |
| 86 | try: |
| 87 | urllib.request.urlopen(request, timeout=5) |
| 88 | raise AssertionError('forged identity accepted') |
| 89 | except urllib.error.HTTPError as error: |
| 90 | assert error.code == 403, error.code |
| 91 | print('refused') |
| 92 | """ |
| 93 | assert shell("podman", "run", "--rm", "--read-only", "--cap-drop=ALL", "--security-opt=no-new-privileges", |
| 94 | "--entrypoint=" + python, args.image, "-c", neighbor).strip() == "refused" |
| 95 | unsecured = subprocess.run(["podman", "run", "--rm", "--read-only", "--network=none", "--cap-drop=ALL", |
| 96 | "--security-opt=no-new-privileges", "--unsetenv=SNOWGLOBE_PROXY_TOKEN_FILE", args.image], |
| 97 | capture_output=True, text=True, timeout=10) |
| 98 | assert unsecured.returncode != 0 and "Set SNOWGLOBE_PROXY_TOKEN_FILE" in unsecured.stderr, unsecured.stderr |
| 99 | |
| 100 | router.nomad = lambda path, _: {"/v1/services": []}[path] |
| 101 | os.environ.update(SNOWGLOBE_DOMAIN="snowglobe.test", SNOWGLOBE_DASHBOARD_PORT=port, SNOWGLOBE_PROXY_TOKEN_FILE=str(token)) |
| 102 | os.environ.pop("SNOWGLOBE_INTERNAL_PORT", None) |
| 103 | dashboard_route = router.render("fixture").split("snowglobe.snowglobe.test {\n", 1)[1] |
| 104 | with socket.socket() as reservation: |
| 105 | reservation.bind(("127.0.0.1", 0)) |
| 106 | proxy_port = reservation.getsockname()[1] |
| 107 | config = Path(temporary) / "Caddyfile" |
| 108 | config.write_text("{\n admin off\n auto_https off\n}\nhttp://127.0.0.1:" + str(proxy_port) + " {\n" + dashboard_route.replace(" tls internal\n", "")) |
| 109 | config.chmod(0o600) |
| 110 | log = Path(temporary) / "caddy.log" |
| 111 | caddy_binary = (Path("/proc") / shell("systemctl", "show", "-P", "MainPID", "caddy").strip() / "exe").resolve(strict=True) |
| 112 | with log.open("w") as output: |
| 113 | caddy = subprocess.Popen([str(caddy_binary), "run", "--config", str(config), "--adapter", "caddyfile"], |
| 114 | stdout=output, stderr=output, env={**os.environ, "XDG_DATA_HOME": temporary, "XDG_CONFIG_HOME": temporary}) |
| 115 | try: |
| 116 | deadline = time.monotonic() + 10 |
| 117 | while True: |
| 118 | try: |
| 119 | with socket.create_connection(("127.0.0.1", proxy_port), timeout=.2): |
| 120 | break |
| 121 | except OSError: |
| 122 | if time.monotonic() >= deadline or caddy.poll() is not None: |
| 123 | raise AssertionError(log.read_text().replace(proof, "<redacted>")) |
| 124 | time.sleep(.1) |
| 125 | forged = {"User-Name": "snow", "User-Groups": "infra-admin", "Snowglobe-Proxy-Token": "0" * 64} |
| 126 | for path in ("/api/me", "/api/deploys"): |
| 127 | request = urllib.request.Request("http://127.0.0.1:" + str(proxy_port) + path, headers=forged) |
| 128 | try: |
| 129 | urllib.request.urlopen(request, timeout=10) |
| 130 | raise AssertionError("forged identity survived Caddy") |
| 131 | except urllib.error.HTTPError as error: |
| 132 | assert error.code == 401, (path, error.code) |
| 133 | finally: |
| 134 | caddy.terminate() |
| 135 | try: |
| 136 | caddy.wait(timeout=10) |
| 137 | except subprocess.TimeoutExpired: |
| 138 | caddy.kill() |
| 139 | caddy.wait() |
| 140 | apps = json.loads(get("/api/launcher")) |
| 141 | assert any(app["id"] == "shale" for app in apps), apps |
| 142 | assert all(app["url"].endswith(".snowglobe.test") for app in apps), apps |
| 143 | icon = next(url for app in apps for url in (app["icon"] or {}).values() if url) |
| 144 | assert b"<svg" in get(icon) |
| 145 | library = json.loads(get("/api/youtube/library")) |
| 146 | assert len(library) == 1 and library[0]["title"] == "Packaged worker" and library[0]["episode"] == 1, library |
| 147 | assert "ffmpeg version" in shell("podman", "exec", container, "ffmpeg", "-version") |
| 148 | assert shell("podman", "exec", container, "yt-dlp", "--version").strip() |
| 149 | yaml = shell("podman", "exec", container, "python3", "-c", "import yaml; print(yaml.safe_load('ready: true')['ready'])") |
| 150 | assert yaml.strip() == "True", yaml |
| 151 | assert {mount["Destination"] for mount in info["Mounts"]} == {"/data", "/run/secrets/dashboard-proxy.token"}, info["Mounts"] |
| 152 | assert not next(mount["RW"] for mount in info["Mounts"] if mount["Destination"] == "/run/secrets/dashboard-proxy.token") |
| 153 | assert shell("podman", "exec", container, "id", "-u").strip() == "65534" |
| 154 | for denied in ["/opt/snowglobe/current", "/var/lib/snowglobe/nomad.token", "/run/podman/podman.sock", "/dev/zfs"]: |
| 155 | assert subprocess.run(["podman", "exec", container, "test", "-e", denied], capture_output=True).returncode == 1, denied |
| 156 | result = {"proxy_proof_required": "passed", "neighbor_identity_forgery_refused": "passed", |
| 157 | "wildcard_startup_without_proof_refused": "passed", |
| 158 | "caddy_identity_scrubbing_and_proof_injection": "passed", |
| 159 | "launcher_from_immutable_image": "passed", "service_icons": "passed", |
| 160 | "unprivileged_youtube_library": "passed", "python_yaml": "passed", |
| 161 | "ffmpeg_and_ytdlp": "passed", "host_release_and_control_paths_absent": "passed"} |
| 162 | if args.output: |
| 163 | args.output.write_text(json.dumps(result, indent=2) + "\n") |
| 164 | print(json.dumps(result)) |
| 165 | finally: |
| 166 | subprocess.run(["podman", "rm", "--force", container], capture_output=True) |
| 167 | |
| 168 | |
| 169 | if __name__ == "__main__": |
| 170 | main() |