Files
zapret-autotune/deploy/managed-target/check_static.py
T
2026-10-15 18:25:03 +03:00

100 lines
6.5 KiB
Python

"""Static deployment guard; run with python3 deploy/managed-target/check_static.py."""
from pathlib import Path
directory = Path(__file__).resolve().parent
root = directory.parents[1]
# ponytail: guard the current literal COPY and deny-all/allowlist ignore shape;
# use a Dockerfile/ignore parser if these become general patterns or multi-module.
ignore = [line for line in (root / ".dockerignore").read_text().splitlines()
if line and not line.startswith("#")]
assert ignore[0] == "*" and all(line.startswith("!") for line in ignore[1:])
package_roots = {path.relative_to(root).parts[0] for path in root.rglob("*.go")}
for name in ("Dockerfile", "Dockerfile.target"):
builder = (root / name).read_text().split("\nFROM ", 1)[0]
before_build = builder[:builder.index("go build")]
for package in package_roots:
assert f"COPY {package}/ ./{package}/\n" in before_build, (name, package)
assert f"!{package}/" in ignore and f"!{package}/**" in ignore, package
compose = (directory / "compose.yaml").read_text()
entrypoint = (directory.parents[1] / "managed-target-entrypoint.sh").read_text()
readme = (directory / "README.md").read_text()
# ponytail: checks the committed Compose shape, not arbitrary YAML; use a YAML
# parser if this definition grows beyond its single target service.
assert " privileged: false\n" in compose
assert " image: ${ZAPRET_AUTOTUNE_IMAGE:-zapret-autotune-managed-target:local}\n" in compose
assert " cap_drop: [ALL]\n" in compose
assert " cap_add: [NET_ADMIN, NET_RAW, SETUID, SETGID, KILL, SYS_PTRACE]\n" in compose
assert "pid:" not in compose # Keep inspection confined to the private PID namespace.
assert 'ZAPRET_AUTOTUNE_SOCKS_PROXY: socks5h://127.0.0.1:1080' in compose
assert compose.count(" - type: bind\n") == 1
assert " source: ./target-state\n" in compose
assert " target: /var/lib/zapret-autotune/target\n" in compose
assert " create_host_path: false\n" in compose
assert "SS_PASSWORD: ${SS_PASSWORD:?Set SS_PASSWORD in the deployment environment}" in compose
for setting in (
'SS_DNS_SERVER: ${SS_DNS_SERVER-}',
'ZAPRET_AUTOTUNE_DNS_MODE: ${ZAPRET_AUTOTUNE_DNS_MODE-doh}',
'ZAPRET_AUTOTUNE_DOH_URL: ${ZAPRET_AUTOTUNE_DOH_URL-https://dns.google/dns-query}',
'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS: ${ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS-}',
'ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC: ${ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC-5}',
'ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}',
'ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE: ${ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE-cold}',
'ZAPRET_AUTOTUNE_VIDEO_ID: ${ZAPRET_AUTOTUNE_VIDEO_ID-dQw4w9WgXcQ}',
"ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS: '${ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS:-}'",
):
assert setting in compose, setting
assert entrypoint.index('target-config-check || exit 64') < entrypoint.index('cat > "$config_dir/server.json"')
assert 'set -- "$@" -d "$dns_address"' in entrypoint
assert entrypoint.index('start_dns || return $?') < entrypoint.index('set -- ss-server')
assert 'dns-forwarder readiness-check "$dns_pid"' in entrypoint
assert 'dns-forwarder readiness-cleanup "$dns_pid"' in entrypoint
assert 'exec 8<"$config_dir"' in entrypoint
assert 'exec 9<"$config_dir/dns-ready"' in entrypoint
assert 'rm -f "$config_dir/dns-ready"' not in entrypoint
assert 'while dns_alive && kill -0 "$server_pid"' in entrypoint
assert entrypoint.index('bounded_wait "$pid" 5') < entrypoint.index('bounded_wait "$dns_pid" 5')
assert '5353' not in compose
assert 'net.ipv4.ip_unprivileged_port_start: "0"' in compose
assert 'eval ' not in entrypoint
assert "secrets:" not in compose
assert "ss_password=${SS_PASSWORD-}" in entrypoint
assert "unset SS_PASSWORD" in entrypoint
assert "export ss_password" not in entrypoint
assert "export SS_PASSWORD" not in entrypoint
assert "Docker administrators/root can still read container" in readme
assert "removed\nfrom the entrypoint environment" in readme
assert "No host networking" in readme
assert ' restart: "no"\n' in compose
assert " stop_grace_period: ${ZAPRET_AUTOTUNE_STOP_GRACE_PERIOD-240s}\n" in compose
assert 'ZAPRET_AUTOTUNE_RESOURCE_APPLY: ${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0}' in compose
assert 'bounded_wait "$monitor_pid" 45' in entrypoint
assert '/bin/sh /opt/managed-target-supervision.sh --watchdog' in entrypoint
assert 'setsid /bin/sh /opt/managed-target-supervision.sh --monitor' in entrypoint
assert entrypoint.index('if ! cleanup_session; then') < entrypoint.index('if cleanup_command 45 /usr/local/bin/zapret-autotune managed reconcile; then')
assert 'COPY managed-target-supervision.sh /opt/managed-target-supervision.sh' in (directory.parents[1] / 'Dockerfile.target').read_text()
assert '!managed-target-supervision.sh' in (directory.parents[1] / '.dockerignore').read_text()
assert '"$count" -ge 3' in entrypoint
assert 'BLOCKED: generic target restart budget; checks paused' in entrypoint
assert '"$((now - started))" -lt 21600' in entrypoint
assert "backoff=15" in entrypoint
assert "systemctl" not in entrypoint and "docker " not in entrypoint
assert "cleanup_command 30 /opt/zapret2/init.d/sysv/zapret2 stop" in entrypoint
assert 'generic_cleanup_deadline=$(($(date +%s) + 80))' in entrypoint
assert 'timeout -k 1 "$cleanup_command_cap" "$@"' in entrypoint
for command, seconds in (("/usr/local/bin/zapret-autotune managed init", 45),
("/usr/local/bin/zapret-autotune managed reconcile", 45),
("/opt/zapret2/init.d/sysv/zapret2 start", 30)):
assert f"timeout -k 5 {seconds} {command} || return 78" in entrypoint
assert entrypoint.index("managed reconcile || return 78") < entrypoint.index("zapret2 start || return 78")
experimental = 'managed apply --googlevideo-experiment native-circular --googlevideo-scope experimental_googlevideo_suffix --googlevideo-request startup || exit 78'
assert entrypoint.index('zapret2 start || return 78') < entrypoint.index(experimental)
assert entrypoint.index(experimental) < entrypoint.index('launch_controller "$@" || return 78')
generic_apply = 'launch_controller managed resource-apply --resource-config "$ZAPRET_AUTOTUNE_RESOURCE_CONFIG"'
assert entrypoint.index('managed reconcile || return 78') < entrypoint.index(generic_apply) < entrypoint.index('zapret2 start || return 78')
assert entrypoint.index(generic_apply) < entrypoint.index('set -- managed monitor')
assert entrypoint.index('managed experimental-request-check || return 78') < entrypoint.index('managed init || return 78')
assert 'rm "${ZAPRET_CONFIG%/*}/.autotune-experimental-suffix-request"' not in entrypoint