Manage .env from the admin panel
Add an Environment tab to /admin that lists every variable in .env.example and lets keys, OAuth settings, email settings and generated secrets be changed without SSH. Secrets are write-only; infrastructure variables are shown read-only. The backend never writes .env or touches Docker. It validates an edit and queues it in env-apply/run/request.json; env-apply/apply.py, run on the host by a systemd path unit (env-apply/install.sh), rewrites .env atomically, keeps .env.bak, and recreates only the services that read the change. backend/services/env_registry.json is the single list of variables, shared by the backend and the host script; a test keeps it in step with .env.example. It replaces the drifted checklist in handle_admin.go. The job capture token card moves to the new tab. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Executable
+266
@@ -0,0 +1,266 @@
|
||||
#!/usr/bin/env python3
|
||||
# =============================================================================
|
||||
# apply.py — the host half of the admin panel's Environment tab.
|
||||
#
|
||||
# WHAT IT DOES
|
||||
# Takes the change the backend queued in env-apply/run/request.json, merges
|
||||
# it into ./.env, recreates the containers that read the changed variables,
|
||||
# and reports the outcome in env-apply/run/status.json for the panel to show.
|
||||
#
|
||||
# WHY IT RUNS ON THE HOST
|
||||
# The backend is the internet-facing process. It is given neither write
|
||||
# access to .env nor the Docker socket, so the most a hijacked admin session
|
||||
# can do is ask for a change to a variable this script agrees is editable.
|
||||
# The list of those is backend/services/env_registry.json, read here from
|
||||
# the checkout — the same file the backend embeds.
|
||||
#
|
||||
# WHEN IT RUNS
|
||||
# On the Pi, started by the systemd path unit that install.sh sets up, the
|
||||
# moment request.json appears. By hand anywhere else:
|
||||
# python3 env-apply/apply.py
|
||||
# On a dev machine pass the compose files the stack was started with:
|
||||
# python3 env-apply/apply.py \
|
||||
# --compose-cmd "docker compose -f docker-compose.yml -f docker-compose.dev.yml"
|
||||
#
|
||||
# SAFETY
|
||||
# - The request is claimed by rename before anything else, so it is consumed
|
||||
# exactly once and its secrets leave the shared directory even on failure.
|
||||
# - .env is replaced atomically (temp file + rename) and the previous
|
||||
# version kept as .env.bak. If compose then rejects the file, the backup
|
||||
# is put back before anything is recreated.
|
||||
# - Only the services that read a changed variable are recreated (plus the
|
||||
# backend, whose own environment is what the panel reports). A plain
|
||||
# `docker compose up -d` would restart Postgres and nginx for any edit,
|
||||
# because they load the whole file too; the next deploy catches them up.
|
||||
# - An flock on run/lock keeps this from overlapping itself or the deploy
|
||||
# workflow, which takes the same lock.
|
||||
#
|
||||
# Standard library only: it has to run on a bare Raspberry Pi OS.
|
||||
# =============================================================================
|
||||
import argparse
|
||||
import datetime
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# Values made only of these are written bare; anything else is single-quoted,
|
||||
# which dotenv reads literally (no $ interpolation, no # comments).
|
||||
BARE_VALUE = re.compile(r"^[A-Za-z0-9_./:@+,=-]*$")
|
||||
|
||||
# How much compose output a failed run reports back to the panel.
|
||||
OUTPUT_TAIL_LINES = 40
|
||||
|
||||
|
||||
def now():
|
||||
return datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds")
|
||||
|
||||
|
||||
def format_line(name, value):
|
||||
if BARE_VALUE.match(value):
|
||||
return f"{name}={value}"
|
||||
return f"{name}='{value}'"
|
||||
|
||||
|
||||
def merge(text, changes):
|
||||
"""Return .env text with each changed variable's line replaced in place.
|
||||
|
||||
Comments, blank lines and ordering are kept. A variable with no line yet
|
||||
is appended.
|
||||
"""
|
||||
remaining = dict(changes)
|
||||
out = []
|
||||
for line in text.splitlines():
|
||||
match = re.match(r"^\s*(export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=", line)
|
||||
if match and match.group(2) in changes:
|
||||
name = match.group(2)
|
||||
out.append((match.group(1) or "") + format_line(name, changes[name]))
|
||||
remaining.pop(name, None)
|
||||
else:
|
||||
out.append(line)
|
||||
if remaining:
|
||||
if out and out[-1].strip():
|
||||
out.append("")
|
||||
out.append("# Added from the admin panel")
|
||||
for name in sorted(remaining):
|
||||
out.append(format_line(name, remaining[name]))
|
||||
return "\n".join(out) + "\n"
|
||||
|
||||
|
||||
def write_atomically(path, text):
|
||||
"""Replace path, keeping its mode and owner."""
|
||||
stat = os.stat(path)
|
||||
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), prefix=".env.tmp.")
|
||||
try:
|
||||
with os.fdopen(fd, "w") as handle:
|
||||
handle.write(text)
|
||||
handle.flush()
|
||||
os.fsync(handle.fileno())
|
||||
os.chmod(tmp, stat.st_mode & 0o7777)
|
||||
try:
|
||||
os.chown(tmp, stat.st_uid, stat.st_gid)
|
||||
except PermissionError:
|
||||
pass # not root and not the owner; the rename below still works
|
||||
os.replace(tmp, path)
|
||||
except BaseException:
|
||||
if os.path.exists(tmp):
|
||||
os.unlink(tmp)
|
||||
raise
|
||||
|
||||
|
||||
def load_registry(path):
|
||||
with open(path) as handle:
|
||||
return {entry["name"]: entry for entry in json.load(handle)}
|
||||
|
||||
|
||||
def check(changes, registry):
|
||||
"""Refuse anything the backend should never have asked for."""
|
||||
if not isinstance(changes, dict) or not changes:
|
||||
raise ValueError("the request changes nothing")
|
||||
for name, value in changes.items():
|
||||
entry = registry.get(name)
|
||||
if entry is None or not entry.get("editable"):
|
||||
raise ValueError(f"{name} is not editable")
|
||||
if not isinstance(value, str):
|
||||
raise ValueError(f"{name} is not a string")
|
||||
if "'" in value or any(ord(c) < 0x20 or ord(c) == 0x7F for c in value):
|
||||
raise ValueError(f"{name} holds a character .env cannot carry")
|
||||
|
||||
|
||||
def services_for(names, registry):
|
||||
services = {"backend"}
|
||||
for name in names:
|
||||
services.update(registry[name].get("services", []))
|
||||
return sorted(services)
|
||||
|
||||
|
||||
def run(cmd, cwd):
|
||||
result = subprocess.run(
|
||||
cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
|
||||
)
|
||||
return result.returncode, result.stdout
|
||||
|
||||
|
||||
def tail(text):
|
||||
return "\n".join(text.strip().splitlines()[-OUTPUT_TAIL_LINES:])
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--env-file", default=os.path.join(ROOT, ".env"))
|
||||
parser.add_argument("--run-dir", default=os.path.join(ROOT, "env-apply", "run"))
|
||||
parser.add_argument(
|
||||
"--registry",
|
||||
default=os.path.join(ROOT, "backend", "services", "env_registry.json"),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--compose-cmd",
|
||||
default="docker compose",
|
||||
help="compose invocation, e.g. to add -f files for a dev stack",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dry-run",
|
||||
action="store_true",
|
||||
help="report what would change and leave everything untouched",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
compose = shlex.split(args.compose_cmd)
|
||||
compose_dir = os.path.dirname(os.path.abspath(args.env_file))
|
||||
request_path = os.path.join(args.run_dir, "request.json")
|
||||
claimed_path = os.path.join(args.run_dir, "request.claimed")
|
||||
status_path = os.path.join(args.run_dir, "status.json")
|
||||
backup_path = args.env_file + ".bak"
|
||||
|
||||
def report(state, request_id, **extra):
|
||||
body = {"state": state, "id": request_id, "at": now(), **extra}
|
||||
tmp = status_path + ".tmp"
|
||||
with open(tmp, "w") as handle:
|
||||
json.dump(body, handle)
|
||||
os.chmod(tmp, 0o644)
|
||||
os.replace(tmp, status_path)
|
||||
|
||||
if args.dry_run:
|
||||
if not os.path.exists(request_path):
|
||||
print("No request pending.")
|
||||
return 0
|
||||
with open(request_path) as handle:
|
||||
request = json.load(handle)
|
||||
registry = load_registry(args.registry)
|
||||
check(request.get("set"), registry)
|
||||
names = sorted(request["set"])
|
||||
print("Would set:", ", ".join(names))
|
||||
print("Would recreate:", ", ".join(services_for(names, registry)))
|
||||
return 0
|
||||
|
||||
with open(os.path.join(args.run_dir, "lock"), "w") as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
|
||||
# Claim first: after this rename the request cannot be applied twice,
|
||||
# and a crash below leaves no secrets where the backend can read them.
|
||||
try:
|
||||
os.replace(request_path, claimed_path)
|
||||
except FileNotFoundError:
|
||||
print("No request pending.")
|
||||
return 0
|
||||
try:
|
||||
with open(claimed_path) as handle:
|
||||
request = json.load(handle)
|
||||
finally:
|
||||
os.unlink(claimed_path)
|
||||
|
||||
request_id = str(request.get("id", ""))
|
||||
try:
|
||||
registry = load_registry(args.registry)
|
||||
changes = request.get("set")
|
||||
check(changes, registry)
|
||||
names = sorted(changes)
|
||||
services = services_for(names, registry)
|
||||
report("applying", request_id, changed=names, services=services)
|
||||
|
||||
with open(args.env_file) as handle:
|
||||
before = handle.read()
|
||||
shutil.copy2(args.env_file, backup_path)
|
||||
os.chmod(backup_path, 0o600)
|
||||
write_atomically(args.env_file, merge(before, changes))
|
||||
|
||||
# Catch a file compose cannot parse before any container is
|
||||
# stopped for it.
|
||||
code, output = run(compose + ["config", "-q"], compose_dir)
|
||||
if code != 0:
|
||||
write_atomically(args.env_file, before)
|
||||
raise RuntimeError(
|
||||
"compose rejected the new .env; it was restored.\n" + tail(output)
|
||||
)
|
||||
|
||||
code, output = run(
|
||||
compose + ["up", "-d", "--no-deps", "--force-recreate"] + services,
|
||||
compose_dir,
|
||||
)
|
||||
if code != 0:
|
||||
# .env keeps the new values: they are valid, and the usual
|
||||
# cause here is a container that would not start, which the
|
||||
# old values would not fix.
|
||||
raise RuntimeError(
|
||||
".env was updated but recreating the containers failed.\n"
|
||||
+ tail(output)
|
||||
)
|
||||
except Exception as error: # noqa: BLE001 - everything is reported
|
||||
report("failed", request_id, output=str(error))
|
||||
print(f"Failed: {error}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
report("applied", request_id, changed=names, services=services)
|
||||
print(f"Applied {', '.join(names)}; recreated {', '.join(services)}.")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+80
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env bash
|
||||
# =============================================================================
|
||||
# install.sh — one-time setup, on the Pi, of the watcher behind the admin
|
||||
# panel's Environment tab.
|
||||
#
|
||||
# WHAT IT DOES
|
||||
# Installs two systemd units:
|
||||
# web-server-env-apply.path watches env-apply/run/request.json
|
||||
# web-server-env-apply.service runs apply.py when that file appears
|
||||
# The units are generated here rather than committed because they need this
|
||||
# checkout's absolute path and the user that owns it.
|
||||
#
|
||||
# HOW TO RUN
|
||||
# From the deploy checkout, as the user that runs docker compose there:
|
||||
# ./env-apply/install.sh
|
||||
# It calls sudo for the three steps that need root. Re-running is safe and
|
||||
# is how to pick up a change to the units below.
|
||||
#
|
||||
# WHY A PATH UNIT
|
||||
# PathExists fires as soon as the backend writes the request, and also at
|
||||
# boot if a request was queued while the Pi was down. apply.py renames the
|
||||
# request away before doing anything else, so the unit cannot retrigger on
|
||||
# the same file.
|
||||
#
|
||||
# The service runs as the checkout's owner, not root: it needs exactly what
|
||||
# the deploy workflow has (write .env, run docker compose) and no more.
|
||||
# DOCKER_API_VERSION and COMPOSE_BAKE mirror .gitea/workflows/deploy.yaml.
|
||||
# =============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
RUN_USER="$(id -un)"
|
||||
RUN_DIR="${ROOT}/env-apply/run"
|
||||
|
||||
if [ "$RUN_USER" = "root" ]; then
|
||||
echo "Run this as the user that owns the checkout, not root." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! command -v python3 >/dev/null; then
|
||||
echo "python3 is required." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# The backend bind-mounts this directory. If Docker creates it first it is
|
||||
# root-owned and apply.py cannot write its status there.
|
||||
mkdir -p "$RUN_DIR"
|
||||
if [ "$(stat -c %U "$RUN_DIR")" != "$RUN_USER" ]; then
|
||||
sudo chown "$RUN_USER" "$RUN_DIR"
|
||||
fi
|
||||
|
||||
sudo tee /etc/systemd/system/web-server-env-apply.path >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Watch for .env changes queued from the admin panel
|
||||
|
||||
[Path]
|
||||
PathExists=${RUN_DIR}/request.json
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
sudo tee /etc/systemd/system/web-server-env-apply.service >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Apply a .env change queued from the admin panel
|
||||
After=docker.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=${RUN_USER}
|
||||
WorkingDirectory=${ROOT}
|
||||
Environment=DOCKER_API_VERSION=1.41
|
||||
Environment=COMPOSE_BAKE=false
|
||||
ExecStart=/usr/bin/env python3 ${ROOT}/env-apply/apply.py
|
||||
EOF
|
||||
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now web-server-env-apply.path
|
||||
|
||||
echo "Installed. Watching ${RUN_DIR}/request.json as ${RUN_USER}."
|
||||
echo "Logs: journalctl -u web-server-env-apply.service"
|
||||
Reference in New Issue
Block a user