Files
deploy/tests/test_deploy_ssl_certs_owner.py
Christian Manivong 30da942a7a
CI / check (pull_request) Successful in 18s
fix: hand ssl_certs to netork before the swap (NetOrk/netork#1389)
netork-ui ran nginx as root, and its entrypoint wrote the self-signed
certificate into ssl_certs as root, the key at 0600. From NetOrk/netork#1389
on, nginx runs as netork, the API's user, and cannot read that key, so the
UI would not start on any existing host.

Before the containers are recreated, one container of the engine image of
the version being deployed, as root with only CHOWN, runs
`chown -Rc netork:netork /app/ssl`. -c names only what it changed, so a
host handed over earlier sees nothing. A failure stops the deploy with the
old containers running. Older engine images carry the same netork user, so
deploying an older version stays possible.
2026-10-22 15:09:50 +02:00

96 lines
3.3 KiB
Python

"""The ssl_certs volume is handed to netork before the containers are recreated
(NetOrk/netork#1388, #1389).
netork-ui ran nginx as root, and its entrypoint wrote the self-signed
certificate into ssl_certs as root, the key at 0600. From netork#1389 on, nginx
runs as netork (uid 1000, the API's user), and cannot read a key root wrote:
the UI would not start. One container of the engine image, as root with only
the capability to chown, hands the volume over before the swap. `chown -c`
reports only what it changed, so on a host already handed over it does
nothing and says nothing. Should it fail, the deploy stops with the old
containers still running.
The remote side is a fake `ssh` that records every command it is given.
"""
from __future__ import annotations
import os
import subprocess
from pathlib import Path
DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh"
HAND_OVER = (
"run --rm --no-deps --user 0 --cap-add CHOWN --entrypoint chown netork-api "
"-Rc netork:netork /app/ssl"
)
_FAKE_SSH = """#!/bin/sh
for arg in "$@"; do cmd="$arg"; done
case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac
cat > /dev/null
printf '%s\\n----\\n' "$cmd" >> "{state}/commands"
case "$cmd" in *"--entrypoint chown netork-api"*) exit {chown_exit} ;; esac
exit 0
"""
def _deploy(
tmp_path: Path, chown_exit: int = 0
) -> tuple[subprocess.CompletedProcess[str], list[str]]:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
fake = bin_dir / "ssh"
fake.write_text(
_FAKE_SSH.replace("{state}", str(tmp_path)).replace("{chown_exit}", str(chown_exit))
)
fake.chmod(0o755)
result = subprocess.run(
["bash", str(DEPLOY), "testhost"],
env={
"PATH": f"{bin_dir}:{os.environ['PATH']}",
"HOME": str(tmp_path),
"DEPLOY_ENV_FILE": "/dev/null",
"REGISTRY_HOST": "registry.example",
"NETORK_VERSION": "latest-dev",
},
stdin=subprocess.DEVNULL,
capture_output=True,
text=True,
timeout=60,
check=False,
)
# Whitespace as the remote shell splits it: continuation lines leave runs of blanks.
commands = [" ".join(c.split()) for c in (tmp_path / "commands").read_text().split("\n----\n")]
return result, commands
def _index(commands: list[str], needle: str) -> int:
return next(i for i, c in enumerate(commands) if needle in c)
def test_the_volume_is_handed_over_after_the_pull_and_before_the_swap(tmp_path: Path) -> None:
result, commands = _deploy(tmp_path)
assert result.returncode == 0, result.stderr
pull = _index(commands, "pull netork-api")
hand_over = _index(commands, HAND_OVER)
swap = _index(commands, "up -d --no-deps --force-recreate")
assert pull < hand_over < swap
def test_it_runs_the_image_of_the_version_being_deployed(tmp_path: Path) -> None:
_, commands = _deploy(tmp_path)
command = commands[_index(commands, HAND_OVER)]
assert "NETORK_VERSION='latest-dev'" in command
assert "-f docker-compose.yml -f docker-compose.registry.yml" in command
def test_a_failed_hand_over_stops_before_the_swap(tmp_path: Path) -> None:
result, commands = _deploy(tmp_path, chown_exit=1)
assert result.returncode != 0
assert not any("--force-recreate" in c for c in commands)
assert "handing ssl_certs to netork" in result.stderr