feat: the bundled database's application gets its own restricted login (NetOrk/netork#2937) #17
No files matched your search
@@ -105,28 +105,38 @@ started earlier pulls whatever image the registry held before, which is stale.
|
|||||||
here before anything changed: the host keeps its previous compose files and
|
here before anything changed: the host keeps its previous compose files and
|
||||||
`.env`, so a hand-typed `docker compose up -d` cannot move the database either.
|
`.env`, so a hand-typed `docker compose up -d` cannot move the database either.
|
||||||
See *Moving the bundled database*. This step runs before `.env` is written.
|
See *Moving the bundled database*. This step runs before `.env` is written.
|
||||||
6. Reconciles the broker, `redis` (Valkey): recreated only if its definition changed,
|
6. On the bundled database, gives netOrk's application a database login of its own that
|
||||||
|
cannot change or delete the audit log (NetOrk/netork#2937), unless `.env` has one
|
||||||
|
already (`MIGRATION_DATABASE_URL` set) or says `RESTRICTED_DB_LOGIN=off`
|
||||||
|
(`remote/db_login.sh`). A container of the engine image being deployed rewrites
|
||||||
|
`.env`: the owner's URL moves to `MIGRATION_DATABASE_URL`, and `DATABASE_URL`
|
||||||
|
becomes `netork_app` with a password generated on the host. Nothing of the file is
|
||||||
|
printed. `netork-api` creates the role when it starts. An external database, or an
|
||||||
|
image from before this change, leaves `.env` alone. Any other failure stops the deploy
|
||||||
|
with the old engine running. The way back is in netOrk's `docs/TECHNICAL_DESIGN.md`,
|
||||||
|
"The application login that cannot change the audit log".
|
||||||
|
7. Reconciles the broker, `redis` (Valkey): recreated only if its definition changed,
|
||||||
and waited for until healthy, before the engine comes up against it. If it does
|
and waited for until healthy, before the engine comes up against it. If it does
|
||||||
not get healthy within 120 s, the deploy stops with the old engine running.
|
not get healthy within 120 s, the deploy stops with the old engine running.
|
||||||
7. Hands the `ssl_certs` volume to `netork`, the user the API and nginx run as
|
8. Hands the `ssl_certs` volume to `netork`, the user the API and nginx run as
|
||||||
(NetOrk/netork#1389), in one container of the engine image run as root with only the
|
(NetOrk/netork#1389), in one container of the engine image run as root with only the
|
||||||
capability to chown. A volume written by the old root nginx would keep the UI from
|
capability to chown. A volume written by the old root nginx would keep the UI from
|
||||||
starting. `chown -c` names only what it changed; on a host handed over earlier it
|
starting. `chown -c` names only what it changed; on a host handed over earlier it
|
||||||
does nothing. If it fails, the deploy stops with the old containers running.
|
does nothing. If it fails, the deploy stops with the old containers running.
|
||||||
8. Force-recreates the API, the workers, `netork-beat`, `flower` and, on UI servers,
|
9. Force-recreates the API, the workers, `netork-beat`, `flower` and, on UI servers,
|
||||||
`netork-ui`. If the recreate fails, it is tried once more after 5 s
|
`netork-ui`. If the recreate fails, it is tried once more after 5 s
|
||||||
(`DEPLOY_RECREATE_RETRY_DELAY`). Compose's parallel recreate can lose a container it
|
(`DEPLOY_RECREATE_RETRY_DELAY`). Compose's parallel recreate can lose a container it
|
||||||
just renamed and leave the rest stopped. If the second attempt fails too, the
|
just renamed and leave the rest stopped. If the second attempt fails too, the
|
||||||
container states are printed and the deploy fails.
|
container states are printed and the deploy fails.
|
||||||
9. Reconciles `apt-cacher-ng` and `signal-api`: each is recreated only if its
|
10. Reconciles `apt-cacher-ng` and `signal-api`: each is recreated only if its
|
||||||
definition changed.
|
definition changed.
|
||||||
It then removes services netOrk no longer ships, container and volume, where a host
|
It then removes services netOrk no longer ships, container and volume, where a host
|
||||||
still has them: today the bundled `registry` (NetOrk/netork#763). It reports only
|
still has them: today the bundled `registry` (NetOrk/netork#763). It reports only
|
||||||
what it actually removed; on a host that was cleaned earlier it says nothing.
|
what it actually removed; on a host that was cleaned earlier it says nothing.
|
||||||
10. Verifies that the containers run exactly the image that was pulled. If they don't, the
|
11. Verifies that the containers run exactly the image that was pulled. If they don't, the
|
||||||
deploy fails.
|
deploy fails.
|
||||||
11. Runs `alembic upgrade head` inside `netork-api`.
|
12. Runs `alembic upgrade head` inside `netork-api`.
|
||||||
12. Prunes unused images.
|
13. Prunes unused images.
|
||||||
|
|
||||||
A failing step stops that server's deploy with a non-zero exit, and the other servers
|
A failing step stops that server's deploy with a non-zero exit, and the other servers
|
||||||
carry on. The script exits non-zero if any server failed and names those servers.
|
carry on. The script exits non-zero if any server failed and names those servers.
|
||||||
|
|||||||
@@ -385,6 +385,22 @@ for kv in "REGISTRY_HOST=${REGISTRY_HOST}" "NETORK_VERSION=${SERVER_VERSION}"; d
|
|||||||
done
|
done
|
||||||
REMOTE_ENV
|
REMOTE_ENV
|
||||||
|
|
||||||
|
# On the bundled database, the application's own login that cannot change the
|
||||||
|
# audit log (NetOrk/netork#2937). The engine image of this version rewrites
|
||||||
|
# .env (remote/db_login.sh); it runs after the line above, so the file it
|
||||||
|
# writes names this version, and before the engine is recreated, so the new
|
||||||
|
# containers start with the new login. netork-api's db_init makes the role.
|
||||||
|
# A failure stops the deploy here, with the old engine running.
|
||||||
|
echo "[${SERVER}] Checking the application's database login..."
|
||||||
|
local login_status=0
|
||||||
|
# shellcheck disable=SC2029 # the image expands here on purpose
|
||||||
|
ssh "$SERVER" "ENGINE_IMAGE='${ENGINE_IMAGE}' bash -s" \
|
||||||
|
< "${HERE}/remote/db_login.sh" 2>&1 | sed "s/^/[${SERVER}] /" || login_status=$?
|
||||||
|
if [[ $login_status -ne 0 ]]; then
|
||||||
|
echo "[${SERVER}] FAILED: the application's database login (its output above says why)" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# The broker, before the engine that cannot work without it. Plain `up -d`
|
# The broker, before the engine that cannot work without it. Plain `up -d`
|
||||||
# recreates it only when its compose definition changed, so an unchanged
|
# recreates it only when its compose definition changed, so an unchanged
|
||||||
# broker keeps running and keeps its queue. It used to be in no list at all,
|
# broker keeps running and keeps its queue. It used to be in no list at all,
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# db_login.sh: on the bundled database, netOrk's application gets a login of its
|
||||||
|
# own that cannot change the audit log (NetOrk/netork#2937). deploy.sh runs it on
|
||||||
|
# the host, in ~/netork, after the database step and before the engine is
|
||||||
|
# recreated, so the new containers start with the new .env.
|
||||||
|
#
|
||||||
|
# The engine image being deployed decides, not this script: `python -m
|
||||||
|
# netork.core.db_role_env` reads .env and answers with the whole new file (exit 0)
|
||||||
|
# or nothing (exit 3: separated already, switched off with RESTRICTED_DB_LOGIN=off,
|
||||||
|
# or not the bundled database's owner login). The password it makes goes from the
|
||||||
|
# container into .env and nowhere else; nothing of the file is printed.
|
||||||
|
#
|
||||||
|
# - No bundled database (external PostgreSQL): nothing to do; there the role is an
|
||||||
|
# infrastructure change.
|
||||||
|
# - An image from before the module: .env stays as it is, the deploy goes on.
|
||||||
|
# - Any other failure: exit 1, .env as it was, the deploy stops before the engine.
|
||||||
|
#
|
||||||
|
# Env: ENGINE_IMAGE, the engine image of the version being deployed.
|
||||||
|
set -euo pipefail
|
||||||
|
cd ~/netork
|
||||||
|
|
||||||
|
if [ -z "$(docker ps -aq --filter label=com.docker.compose.project=netork \
|
||||||
|
--filter label=com.docker.compose.service=postgres | head -1)" ]; then
|
||||||
|
echo "no bundled database: the application's database login stays as it is"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
umask 077
|
||||||
|
new=.env.db-login.new
|
||||||
|
err=.env.db-login.err
|
||||||
|
trap 'rm -f "$new" "$err"' EXIT
|
||||||
|
status=0
|
||||||
|
docker run --rm -i --network none --entrypoint python "$ENGINE_IMAGE" \
|
||||||
|
-m netork.core.db_role_env < .env > "$new" 2> "$err" || status=$?
|
||||||
|
|
||||||
|
case $status in
|
||||||
|
0)
|
||||||
|
# in place: .env keeps its owner and mode
|
||||||
|
cat "$new" > .env
|
||||||
|
echo "the application now has its own restricted database login (netork_app)"
|
||||||
|
;;
|
||||||
|
3)
|
||||||
|
echo "the application's database login stays as it is"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
if grep -q "No module named netork.core.db_role_env" "$err"; then
|
||||||
|
echo "this version cannot restrict the application's database login: it stays as it is"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
echo "could not set up the application's database login (exit $status):" >&2
|
||||||
|
tail -c 500 "$err" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
"""deploy.sh runs remote/db_login.sh on every host (NetOrk/netork#2937).
|
||||||
|
|
||||||
|
On the bundled database the application gets a login of its own that cannot
|
||||||
|
change the audit log. The step runs after the database step and after .env names
|
||||||
|
the new version, and before the broker and the engine, so the recreated
|
||||||
|
containers start with the new login. A failure stops the deploy there, with the
|
||||||
|
old engine still running.
|
||||||
|
|
||||||
|
The remote side is a fake `ssh` that records every command and what it was fed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh"
|
||||||
|
DB_LOGIN = Path(__file__).resolve().parent.parent / "remote" / "db_login.sh"
|
||||||
|
|
||||||
|
_FAKE_SSH = """#!/bin/sh
|
||||||
|
for arg in "$@"; do cmd="$arg"; done
|
||||||
|
case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac
|
||||||
|
printf '%s\\n----\\n' "$cmd" >> "{state}/commands"
|
||||||
|
case "$cmd" in
|
||||||
|
*"ENGINE_IMAGE="*"bash -s"*) cat > "{state}/db-login-stdin"; exit {login_exit} ;;
|
||||||
|
esac
|
||||||
|
cat > /dev/null
|
||||||
|
exit 0
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _deploy(tmp_path: Path, *, login_exit: int = 0):
|
||||||
|
bin_dir = tmp_path / "bin"
|
||||||
|
bin_dir.mkdir()
|
||||||
|
fake = bin_dir / "ssh"
|
||||||
|
fake.write_text(
|
||||||
|
_FAKE_SSH.replace("{state}", str(tmp_path)).replace("{login_exit}", str(login_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,
|
||||||
|
)
|
||||||
|
commands = (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_login_step_runs_with_the_deployed_engine_image(tmp_path: Path) -> None:
|
||||||
|
result, commands = _deploy(tmp_path)
|
||||||
|
|
||||||
|
assert result.returncode == 0, result.stdout + result.stderr
|
||||||
|
(step,) = [c for c in commands if "ENGINE_IMAGE=" in c]
|
||||||
|
assert step.strip() == "ENGINE_IMAGE='registry.example/netork/engine:latest-dev' bash -s"
|
||||||
|
assert (tmp_path / "db-login-stdin").read_text() == DB_LOGIN.read_text()
|
||||||
|
|
||||||
|
|
||||||
|
def test_it_runs_after_env_names_the_version_and_before_broker_and_engine(tmp_path: Path) -> None:
|
||||||
|
_, commands = _deploy(tmp_path)
|
||||||
|
|
||||||
|
login = _index(commands, "ENGINE_IMAGE=")
|
||||||
|
env_write = next(i for i, c in enumerate(commands) if c.strip() == "bash -s")
|
||||||
|
assert _index(commands, "bash -s -- reconcile") < login
|
||||||
|
assert env_write < login
|
||||||
|
assert login < _index(commands, "--wait-timeout 120 redis")
|
||||||
|
assert login < _index(commands, "--force-recreate")
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_failure_stops_before_the_engine(tmp_path: Path) -> None:
|
||||||
|
result, commands = _deploy(tmp_path, login_exit=1)
|
||||||
|
|
||||||
|
assert result.returncode != 0
|
||||||
|
assert not any("--force-recreate" in c for c in commands)
|
||||||
|
assert not any("redis" in c and "up -d" in c for c in commands)
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
"""The bundled database's restricted application login (NetOrk/netork#2937): remote/db_login.sh.
|
||||||
|
|
||||||
|
deploy.sh runs it on the host after the database step and before the engine is
|
||||||
|
recreated. The engine image being deployed decides (``python -m
|
||||||
|
netork.core.db_role_env``, ``.env`` on stdin): exit 0 and the new ``.env`` on
|
||||||
|
stdout, or exit 3 and nothing to do. The script only writes what it got back,
|
||||||
|
in place, so the file keeps its owner and mode.
|
||||||
|
|
||||||
|
- no bundled database (external PostgreSQL): nothing runs, ``.env`` stays;
|
||||||
|
- an image without the module (a version before it): ``.env`` stays, the deploy goes on;
|
||||||
|
- any other failure stops the deploy before the engine is touched, ``.env`` as it was.
|
||||||
|
|
||||||
|
The remote side is a fake ``docker`` that records each call.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import stat
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SCRIPT = Path(__file__).resolve().parent.parent / "remote" / "db_login.sh"
|
||||||
|
ENGINE = "registry.example/netork/engine:0.60.0"
|
||||||
|
BEFORE = "POSTGRES_PASSWORD=pg\nDATABASE_URL=postgresql+asyncpg://netork:pw@postgres:5432/netork\n"
|
||||||
|
AFTER = BEFORE + "MIGRATION_DATABASE_URL=owner\n"
|
||||||
|
|
||||||
|
_FAKE_DOCKER = r"""#!/bin/bash
|
||||||
|
printf '%s\n' "$*" >> "$FAKE_STATE/calls"
|
||||||
|
case "$1" in
|
||||||
|
ps) [ -n "$FAKE_PG_CID" ] && echo "$FAKE_PG_CID"; exit 0 ;;
|
||||||
|
run)
|
||||||
|
cat > "$FAKE_STATE/stdin"
|
||||||
|
case "$FAKE_MODE" in
|
||||||
|
changed) printf '%s' "$FAKE_AFTER"; exit 0 ;;
|
||||||
|
nothing) exit 3 ;;
|
||||||
|
old) echo "/opt/venv/bin/python: No module named netork.core.db_role_env" >&2; exit 1 ;;
|
||||||
|
broken) echo "Traceback: boom" >&2; exit 1 ;;
|
||||||
|
esac ;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _run(tmp_path: Path, *, mode: str, bundled: bool = True) -> subprocess.CompletedProcess[str]:
|
||||||
|
home = tmp_path / "home"
|
||||||
|
(home / "netork").mkdir(parents=True)
|
||||||
|
env_file = home / "netork" / ".env"
|
||||||
|
env_file.write_text(BEFORE)
|
||||||
|
env_file.chmod(0o640)
|
||||||
|
bin_dir = tmp_path / "bin"
|
||||||
|
bin_dir.mkdir()
|
||||||
|
docker = bin_dir / "docker"
|
||||||
|
docker.write_text(_FAKE_DOCKER)
|
||||||
|
docker.chmod(0o755)
|
||||||
|
return subprocess.run(
|
||||||
|
["bash", "-s"],
|
||||||
|
stdin=SCRIPT.open(),
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
env={
|
||||||
|
"PATH": f"{bin_dir}:{os.environ['PATH']}",
|
||||||
|
"HOME": str(home),
|
||||||
|
"ENGINE_IMAGE": ENGINE,
|
||||||
|
"FAKE_STATE": str(tmp_path),
|
||||||
|
"FAKE_MODE": mode,
|
||||||
|
"FAKE_AFTER": AFTER,
|
||||||
|
"FAKE_PG_CID": "pgcid" if bundled else "",
|
||||||
|
},
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _env(tmp_path: Path) -> Path:
|
||||||
|
return tmp_path / "home" / "netork" / ".env"
|
||||||
|
|
||||||
|
|
||||||
|
def _leftovers(tmp_path: Path) -> list[str]:
|
||||||
|
return sorted(p.name for p in (tmp_path / "home" / "netork").iterdir() if p.name != ".env")
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_new_env_from_the_engine_image_is_written_in_place(tmp_path):
|
||||||
|
result = _run(tmp_path, mode="changed")
|
||||||
|
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
assert _env(tmp_path).read_text() == AFTER
|
||||||
|
assert stat.S_IMODE(_env(tmp_path).stat().st_mode) == 0o640
|
||||||
|
assert (tmp_path / "stdin").read_text() == BEFORE
|
||||||
|
run = next(c for c in (tmp_path / "calls").read_text().splitlines() if c.startswith("run "))
|
||||||
|
assert run == (
|
||||||
|
f"run --rm -i --network none --entrypoint python {ENGINE} -m netork.core.db_role_env"
|
||||||
|
)
|
||||||
|
assert "restricted" in result.stdout
|
||||||
|
assert "owner" not in result.stdout + result.stderr # nothing of the file is echoed
|
||||||
|
assert _leftovers(tmp_path) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_nothing_to_do_leaves_the_file(tmp_path):
|
||||||
|
result = _run(tmp_path, mode="nothing")
|
||||||
|
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
assert _env(tmp_path).read_text() == BEFORE
|
||||||
|
assert _leftovers(tmp_path) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_without_a_bundled_database_the_image_is_not_asked(tmp_path):
|
||||||
|
result = _run(tmp_path, mode="changed", bundled=False)
|
||||||
|
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
assert _env(tmp_path).read_text() == BEFORE
|
||||||
|
assert "run " not in (tmp_path / "calls").read_text()
|
||||||
|
|
||||||
|
|
||||||
|
def test_an_image_without_the_module_leaves_the_file_and_goes_on(tmp_path):
|
||||||
|
result = _run(tmp_path, mode="old")
|
||||||
|
|
||||||
|
assert result.returncode == 0, result.stderr
|
||||||
|
assert _env(tmp_path).read_text() == BEFORE
|
||||||
|
assert "cannot" in result.stdout
|
||||||
|
assert _leftovers(tmp_path) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_any_other_failure_stops_with_the_file_as_it_was(tmp_path):
|
||||||
|
result = _run(tmp_path, mode="broken")
|
||||||
|
|
||||||
|
assert result.returncode == 1
|
||||||
|
assert _env(tmp_path).read_text() == BEFORE
|
||||||
|
assert "boom" in result.stderr
|
||||||
|
assert _leftovers(tmp_path) == []
|
||||||
Reference in new issue
Block a user