Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*.env.local
*.env.backup
.env.bak-*
.env.bak
*.env.bak
*.env.production
stack.env
**/stack.env
Expand Down
4 changes: 2 additions & 2 deletions docs/dependency-track.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ stop mid-deploy, either finish before 02:00 or remove the containers

```bash
cd ~/83rr-poweredge && git pull
cp -p .env ".env.$(date +%F)-pre-deptrack.bak"
cp -p .env .env.bak && chmod 600 .env.bak # one backup, overwritten each time
grep -q '^DEPTRACK_DB_PASSWORD=' .env || \
echo "DEPTRACK_DB_PASSWORD=$(openssl rand -base64 32 | tr -d '/+=' | cut -c1-32)" >> .env
grep -c '^NTFY_PUBLISH_TOKEN=' .env # expect 1 (already used by OpenSearch/Kuma)
Expand Down Expand Up @@ -580,5 +580,5 @@ them, so the 02:00 backup skips the dump instead of failing on a stopped DB;
the volumes stay). If the change has to come out entirely, follow
**Rollback / removal** above: the cron line, a revert PR + `git pull`, and an
nginx reload. Until the revert lands, nginx keeps the vhost; it just returns
502 for deptrack.ops because of the variable `proxy_pass`. Restore `.env` from the `.env.<date>-pre-deptrack.bak` copy if needed.
502 for deptrack.ops because of the variable `proxy_pass`. Restore `.env` from the `.env.bak` copy if needed.
No other service is touched by this change, so nothing else needs restarting.
6 changes: 3 additions & 3 deletions infrastructure/homelab-mcp/scripts/env_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
printf 'HOMELAB_MCP_JWT_SIGNING_KEY=%s\\n' "$(openssl rand -hex 32)" \\
| python3 infrastructure/homelab-mcp/scripts/env_set.py --if-missing

- Backs .env up first (.env.bak-YYYYmmdd-HHMMSS, mode 600), once per run.
- Backs .env up first to a single .env.bak (mode 600), overwritten on every run,
so copies of the secrets don't pile up.
- Replaces an existing KEY= line (and drops duplicates of it) or appends one.
- --if-missing leaves a key alone when it already has a non-empty value.
- Values with spaces or '#' are single-quoted so docker compose reads them whole.
Expand All @@ -20,7 +21,6 @@
import shutil
import sys
import tempfile
from datetime import datetime
from pathlib import Path

KEY_RE = re.compile(r"^[A-Z][A-Z0-9_]*$")
Expand Down Expand Up @@ -90,7 +90,7 @@ def main(argv: list[str] | None = None) -> int:
out.append(f"{key}={value}")

if path.exists():
backup = path.with_name(f"{path.name}.bak-{datetime.now():%Y%m%d-%H%M%S}")
backup = path.with_name(f"{path.name}.bak")
shutil.copy2(path, backup)
os.chmod(backup, 0o600)
mode = path.stat().st_mode & 0o777
Expand Down
16 changes: 14 additions & 2 deletions infrastructure/homelab-mcp/tests/test_homelab_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,10 +757,22 @@ def test_env_set_replaces_appends_and_never_prints_values(tmp_path):
assert "s3cret" not in r.stdout + r.stderr and "AAAAC3" not in r.stdout
assert env_file.read_text() == "A=1\nHOMELAB_MCP_X=s3cret-value\n# c\nHOMELAB_MCP_KEY='ssh-ed25519 AAAAC3'\n"
assert stat.S_IMODE(env_file.stat().st_mode) == 0o600
(backup,) = tmp_path.glob(".env.bak-*")
(backup,) = tmp_path.glob(".env.bak*")
assert backup.name == ".env.bak"
assert "old" in backup.read_text() and stat.S_IMODE(backup.stat().st_mode) == 0o600


def test_env_set_keeps_one_backup_overwritten_each_run(tmp_path):
(tmp_path / ".env").write_text("A=1\n")
(tmp_path / ".env.bak").write_text("STALE=1\n")
(tmp_path / ".env.bak").chmod(0o644)
assert run_env_set(tmp_path, "A=2\n").returncode == 0
assert run_env_set(tmp_path, "A=3\n").returncode == 0
(backup,) = tmp_path.glob(".env.bak*")
assert backup.read_text() == "A=2\n" and stat.S_IMODE(backup.stat().st_mode) == 0o600
assert (tmp_path / ".env").read_text() == "A=3\n"


def test_env_set_if_missing_keeps_existing(tmp_path):
(tmp_path / ".env").write_text("K=keep\nE=\n")
r = run_env_set(tmp_path, "K=new\nE=filled\n", "--if-missing")
Expand All @@ -773,4 +785,4 @@ def test_env_set_refuses_bad_input(tmp_path, stdin):
(tmp_path / ".env").write_text("A=1\n")
r = run_env_set(tmp_path, stdin)
assert r.returncode == 2 and (tmp_path / ".env").read_text() == "A=1\n"
assert not list(tmp_path.glob(".env.bak-*"))
assert not list(tmp_path.glob(".env.bak*"))