feat(deploy): hardened safe hub redeploy + reusable auth smoke 🛡️🚀
Some checks are pending
CI / vet-build-test (push) Waiting to run
Some checks are pending
CI / vet-build-test (push) Waiting to run
- scripts/hub-auth-smoke.sh: throwaway-instance smoke proving invite revocation works + +s attenuation holds (admin without +s denied conferring it). No python/jq dep (curl+sed). Reusable by redeploy + CI. Verified locally. - deploy/redeploy-hub.sh: rewritten from build→scp→restart into a SAFE pipeline — migration dry-run on a COPY of live data (live untouched) → auth smoke the new binary on the NAS → back up binary+DB (stopped, WAL quiescent) → swap + sync unit → post-verify (hub answers + /v1/invite/revoke=401 proves new code) → AUTO-ROLLBACK on failure. The hub DB is canonical history; never YOLO a binary onto it. - deploy/devbox-hub.service: add --gc-every 24h (self-maintain; blobs don't grow unbounded). HTTPS via NPM note.
This commit is contained in:
parent
35820386cb
commit
735b5b2c4b
3 changed files with 124 additions and 13 deletions
|
|
@ -6,7 +6,10 @@ After=network-online.target
|
||||||
Wants=network-online.target
|
Wants=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/mnt/tank/apps/devbox/devbox-hub serve --data /mnt/tank/apps/devbox/data --listen 0.0.0.0:8088
|
# --gc-every: self-maintain (prune old snapshots + unreferenced chunks) daily, so
|
||||||
|
# blobs don't accumulate forever. Bind plain :8088 for the LAN; HTTPS is fronted by
|
||||||
|
# NPM at https://hub.shoemoney.ai (the API has bearer auth, so TLS-terminate there).
|
||||||
|
ExecStart=/mnt/tank/apps/devbox/devbox-hub serve --data /mnt/tank/apps/devbox/data --listen 0.0.0.0:8088 --gc-every 24h
|
||||||
WorkingDirectory=/mnt/tank/apps/devbox
|
WorkingDirectory=/mnt/tank/apps/devbox
|
||||||
User=shoemoney
|
User=shoemoney
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,72 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Cross-compile the devbox hub for the NAS (linux/amd64, no CGO — pure-Go sqlite),
|
# redeploy-hub.sh — SAFE redeploy of the devbox hub to the live NAS.
|
||||||
# ship it, and restart the systemd service. Run from anywhere in the repo.
|
#
|
||||||
|
# The hub DB is the canonical index to every share's history, so this never YOLOs a
|
||||||
|
# binary onto it. The pipeline, in order:
|
||||||
|
# 1. build linux/amd64 (hub + client)
|
||||||
|
# 2. migration DRY-RUN on a COPY of live data (scripts/verify-hub-migration.sh) — live untouched
|
||||||
|
# 3. auth SMOKE the new binary in a throwaway instance on the NAS (invite-revoke + +s)
|
||||||
|
# 4. BACK UP the live binary + DB (stopped, so WAL is quiescent)
|
||||||
|
# 5. swap the binary + sync the systemd unit, restart
|
||||||
|
# 6. VERIFY the hub answers + the new code is live (/v1/invite/revoke → 401, not 404)
|
||||||
|
# 7. AUTO-ROLLBACK to the previous binary on any post-deploy failure
|
||||||
|
#
|
||||||
|
# Counts before/after are printed for eyeball (not a hard gate — concurrent client
|
||||||
|
# activity can legitimately move them; the migration safety is proven in step 2).
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
cd "$(dirname "$0")/.."
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
HUB=nas # ssh alias for 192.168.1.10
|
HUB="${HUB:-nas}" # ssh alias for the NAS (192.168.1.10)
|
||||||
DEST=/mnt/tank/apps/devbox
|
DEST="${DEST:-/mnt/tank/apps/devbox}"
|
||||||
URL=http://192.168.1.10:8088
|
LANIP="${LANIP:-192.168.1.10}"
|
||||||
|
URL="http://$LANIP:8088"
|
||||||
|
say() { printf '\n\033[1m%s\033[0m\n' "$*"; }
|
||||||
|
counts() { curl -sf "$URL/metrics" 2>/dev/null | grep -E '^devbox_(devices|shares|snapshots|chunks) ' | sort; }
|
||||||
|
|
||||||
echo "🔨 building linux/amd64..."
|
say "🔨 build linux/amd64 (hub + client, trimmed static)"
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /tmp/devbox-hub-linux ./cmd/devbox-hub
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags '-s -w' -o /tmp/dh-hub ./cmd/devbox-hub
|
||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags '-s -w' -o /tmp/dh-client ./cmd/devbox
|
||||||
|
|
||||||
echo "🚚 shipping to ${HUB}:${DEST}..."
|
say "🧪 migration dry-run on a COPY of live data (live hub untouched)"
|
||||||
scp -q /tmp/devbox-hub-linux "${HUB}:${DEST}/devbox-hub.new"
|
scripts/verify-hub-migration.sh
|
||||||
ssh "$HUB" "mv ${DEST}/devbox-hub.new ${DEST}/devbox-hub && chmod +x ${DEST}/devbox-hub && sudo systemctl restart devbox-hub && systemctl is-active devbox-hub"
|
|
||||||
|
|
||||||
echo "✅ redeployed. hub metrics:"
|
say "🧪 auth smoke the NEW binary on the NAS (throwaway instance, prod untouched)"
|
||||||
curl --retry 10 --retry-connrefused --retry-delay 1 --max-time 15 -sf "${URL}/metrics" | grep '^devbox_' || echo "⚠️ hub not answering yet"
|
scp -q /tmp/dh-hub /tmp/dh-client scripts/hub-auth-smoke.sh "$HUB:/tmp/"
|
||||||
|
ssh "$HUB" 'bash /tmp/hub-auth-smoke.sh /tmp/dh-hub /tmp/dh-client 8388'
|
||||||
|
|
||||||
|
say "🔢 pre-deploy counts"
|
||||||
|
PRE="$(counts)"; echo "${PRE:-<hub not answering>}"
|
||||||
|
|
||||||
|
say "💾 back up live binary + DB, swap, sync unit, restart"
|
||||||
|
scp -q deploy/devbox-hub.service "$HUB:/tmp/devbox-hub.service"
|
||||||
|
scp -q /tmp/dh-hub "$HUB:$DEST/devbox-hub.new"
|
||||||
|
ssh "$HUB" 'bash -s' "$DEST" <<'DEPLOY'
|
||||||
|
set -e
|
||||||
|
DEST="$1"; cd "$DEST"
|
||||||
|
STAMP=$(date +%Y%m%d-%H%M%S); mkdir -p backups
|
||||||
|
cp -a devbox-hub "backups/devbox-hub.$STAMP"; ln -sf "devbox-hub.$STAMP" backups/devbox-hub.rollback
|
||||||
|
sudo systemctl stop devbox-hub
|
||||||
|
for f in data/devbox-hub.db data/devbox-hub.db-wal data/devbox-hub.db-shm; do
|
||||||
|
[ -f "$f" ] && cp -a "$f" "backups/$(basename "$f").$STAMP"
|
||||||
|
done
|
||||||
|
mv devbox-hub.new devbox-hub && chmod +x devbox-hub
|
||||||
|
sudo cp /tmp/devbox-hub.service /etc/systemd/system/devbox-hub.service && sudo systemctl daemon-reload
|
||||||
|
sudo systemctl start devbox-hub
|
||||||
|
sleep 2; systemctl is-active devbox-hub >/dev/null && echo " service active (backup: backups/devbox-hub.$STAMP)"
|
||||||
|
DEPLOY
|
||||||
|
|
||||||
|
say "🔎 post-deploy verify (auto-rollback on failure)"
|
||||||
|
rollback() {
|
||||||
|
echo "↩️ ROLLING BACK to the previous binary"
|
||||||
|
ssh "$HUB" "cd '$DEST' && sudo systemctl stop devbox-hub && cp -a backups/devbox-hub.rollback devbox-hub && sudo systemctl start devbox-hub && sleep 2 && systemctl is-active devbox-hub" || true
|
||||||
|
echo "🛑 redeploy FAILED — rolled back to the previous binary"; exit 1
|
||||||
|
}
|
||||||
|
curl --retry 20 --retry-connrefused --retry-delay 1 --max-time 25 -sf "$URL/metrics" >/dev/null || rollback
|
||||||
|
code=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$URL/v1/invite/revoke")
|
||||||
|
[ "$code" = 401 ] || { echo " ✗ /v1/invite/revoke = $code (want 401 — new code not live)"; rollback; }
|
||||||
|
echo " ✓ /v1/invite/revoke present (401 unauth) — P2 code is LIVE"
|
||||||
|
|
||||||
|
say "🔢 post-deploy counts (compare to pre — device/share/chunk should match)"
|
||||||
|
echo "$(counts)"
|
||||||
|
|
||||||
|
say "✅ hub redeployed safely — P2 (+s attenuation · invite revocation) + P3 (gc-every) are live."
|
||||||
|
|
|
||||||
55
scripts/hub-auth-smoke.sh
Executable file
55
scripts/hub-auth-smoke.sh
Executable file
|
|
@ -0,0 +1,55 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# hub-auth-smoke.sh — smoke-test a hub binary's M8a auth on a THROWAWAY instance
|
||||||
|
# (never touches a real hub): proves invite revocation works and +s attenuation
|
||||||
|
# holds. Reusable by redeploy-hub.sh and by hand / CI.
|
||||||
|
#
|
||||||
|
# scripts/hub-auth-smoke.sh <devbox-hub-bin> <devbox-bin> [port]
|
||||||
|
#
|
||||||
|
# Needs: curl, sed (no python/jq). The two binaries must match the host's OS/arch.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
HUB_BIN="${1:?usage: hub-auth-smoke.sh <devbox-hub-bin> <devbox-bin> [port]}"
|
||||||
|
DEVBOX_BIN="${2:?need the devbox client binary too}"
|
||||||
|
PORT="${3:-8388}"
|
||||||
|
BASE="http://127.0.0.1:$PORT"
|
||||||
|
T="$(mktemp -d)"
|
||||||
|
cleanup() { pkill -9 -f "$HUB_BIN serve --data $T/data" 2>/dev/null || true; rm -rf "$T"; }
|
||||||
|
trap cleanup EXIT
|
||||||
|
mkdir -p "$T/data" "$T/work"; echo smoke > "$T/work/f.txt"
|
||||||
|
fail() { echo "🛑 SMOKE FAIL: $*"; exit 1; }
|
||||||
|
tok() { sed -n 's/.*"token":"\([^"]*\)".*/\1/p'; } # pluck a JSON token field
|
||||||
|
|
||||||
|
"$HUB_BIN" serve --data "$T/data" --listen "127.0.0.1:$PORT" >"$T/hub.log" 2>&1 &
|
||||||
|
for _ in $(seq 1 25); do curl -sf "$BASE/metrics" >/dev/null 2>&1 && break; sleep 0.3; done
|
||||||
|
curl -sf "$BASE/metrics" >/dev/null 2>&1 || fail "hub never came up (see $T/hub.log)"
|
||||||
|
|
||||||
|
join() { # $1=cfgdir $2=token -> stdout: device bearer
|
||||||
|
XDG_CONFIG_HOME="$1" "$DEVBOX_BIN" join "$BASE" "$2" >/dev/null
|
||||||
|
sed -n 's/^bearer *= *"\([^"]*\)".*/\1/p' "$1/devbox/daemon.toml"
|
||||||
|
}
|
||||||
|
api() { curl -s -H "Authorization: Bearer $1" -H 'Content-Type: application/json' "${@:2}"; }
|
||||||
|
|
||||||
|
# Owner enrolls and publishes legacy share 'team' (implicit owner).
|
||||||
|
OWNER=$(join "$T/owner" "$("$HUB_BIN" token --data "$T/data")")
|
||||||
|
[ -n "$OWNER" ] || fail "owner join produced no bearer"
|
||||||
|
XDG_CONFIG_HOME="$T/owner" "$DEVBOX_BIN" publish "$T/work" team >/dev/null
|
||||||
|
|
||||||
|
# 1) Invite revocation: mint → revoke(200) → redeem must be refused.
|
||||||
|
INV=$(api "$OWNER" -d '{"share":"team","principal":"bob","role":"editor"}' "$BASE/v1/invite" | tok)
|
||||||
|
[ -n "$INV" ] || fail "invite mint returned no token"
|
||||||
|
code=$(api "$OWNER" -o /dev/null -w '%{http_code}' -d "{\"token\":\"$INV\"}" "$BASE/v1/invite/revoke")
|
||||||
|
[ "$code" = 200 ] || fail "revoke expected 200, got $code (old binary? /v1/invite/revoke missing)"
|
||||||
|
if XDG_CONFIG_HOME="$T/bob" "$DEVBOX_BIN" join "$BASE" "$INV" >/dev/null 2>&1; then fail "a REVOKED invite still redeemed"; fi
|
||||||
|
echo " ✅ invite revocation — minted, revoked (200), redeem refused"
|
||||||
|
|
||||||
|
# 2) +s attenuation: an admin WITHOUT +s must not confer +s; but may grant normally.
|
||||||
|
ADMINV=$(api "$OWNER" -d '{"share":"team","principal":"adm","role":"admin","reshare":false}' "$BASE/v1/invite" | tok)
|
||||||
|
ADM=$(join "$T/adm" "$ADMINV")
|
||||||
|
[ -n "$ADM" ] || fail "admin join produced no bearer"
|
||||||
|
code=$(api "$ADM" -o /dev/null -w '%{http_code}' -d '{"share":"team","principal":"carol","role":"editor","reshare":true}' "$BASE/v1/invite")
|
||||||
|
[ "$code" = 403 ] || fail "admin-without-+s conferring +s expected 403, got $code (ESCALATION reopened)"
|
||||||
|
code=$(api "$ADM" -o /dev/null -w '%{http_code}' -d '{"share":"team","principal":"dave","role":"editor","reshare":false}' "$BASE/v1/invite")
|
||||||
|
[ "$code" = 200 ] || fail "admin granting a non-resharing editor expected 200, got $code"
|
||||||
|
echo " ✅ +s attenuation — admin denied conferring +s (403); normal grant ok (200)"
|
||||||
|
|
||||||
|
echo "🎉 hub auth smoke passed"
|
||||||
Loading…
Reference in a new issue