feat: the bundled database's application gets its own restricted login (NetOrk/netork#2937)
CI / check (pull_request) Successful in 22s
CI / check (pull_request) Successful in 22s
remote/db_login.sh runs after .env names the version and before the broker and the engine: a container of the engine image being deployed (python -m netork.core.db_role_env) rewrites .env so that the owner's URL becomes MIGRATION_DATABASE_URL and DATABASE_URL a netork_app login with a password made on the host; netork-api's db_init creates the role. No bundled database, an image from before the module, an .env separated already or RESTRICTED_DB_LOGIN=off: .env stays as it is. Any other failure stops the deploy before the engine is touched.
This commit is contained in:
1 parent
e35d42a20b
commit
ec7d113cdb
5 files changed
+305
-7
No files matched your search
@@ -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)
|
||||
Reference in new issue
Block a user