interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / spark / edge-http-apply.sh

103 lines · 5675 bytes Raw Blame History
1#!/usr/bin/env bash
2# Spark front door: answer public http (TCP 80) with a 301 to https.
3# forge-iplreach (L1008 QW1, card t_0ef9cf5d). Source of record: infra/spark/README-edge.md, "Plain http".
4#
5# sudo bash infra/spark/edge-http-apply.sh <infra dir> (from ~/work/forge/web, as the Spark user via sudo)
6#
7# Order, stopping at the first failure:
8# 1. preconditions (nothing changed yet): files present, not applied before, the :443 front-door rule
9# present exactly once, port 4480 free, 192.168.1.108 on this host, `nginx -t` and `nft -c` pass;
10# 2. build the new firewall file from the live one (two insertions, nothing else) and `nft -c` it;
11# 3. back up /etc/atlas/ingress.nft to /etc/nginx/forge-backups/http80-<UTC>/ (+ NEW_FILES, README);
12# 4. install sites-enabled/http-redirect, `nginx -t`, `systemctl reload nginx` (a reload, never a restart),
13# wait for 192.168.1.108:4480 and check the redirect on it directly;
14# 5. install the new firewall file and load it with `nft -f` (the same command atlas-ingress.service runs);
15# 6. live checks from the house LAN (spark/edge-http-checks.sh, run as $SUDO_USER, who has the hub key).
16# Any failure after step 3 runs spark/edge-http-rollback.sh on the backup.
17set -euo pipefail
18INFRA=$(cd "${1:?usage: edge-http-apply.sh <infra dir>}" && pwd)
19NFT=${EDGE_NFT:-/etc/atlas/ingress.nft}
20SITE_SRC=$INFRA/spark/nginx/sites-http-redirect
21SITE_DST=${EDGE_SITE_DST:-/etc/nginx/sites-enabled/http-redirect}
22BKROOT=${EDGE_BKROOT:-/etc/nginx/forge-backups}
23ADDR=192.168.1.108
24PORT=4480
25RULE443='iifname "enP7s7" ip daddr 192.168.1.108 tcp dport 443 accept'
26STAMP=$(date -u +%Y%m%dT%H%M%SZ)
27BK=$BKROOT/http80-$STAMP
28die() { echo "edge-http-apply: FAIL: $*" >&2; exit 1; }
29say() { echo "edge-http-apply: $*"; }
30
31[ "$(id -u)" = 0 ] || die "run as root (sudo)"
32# 1. preconditions
33for f in "$SITE_SRC" "$INFRA/spark/edge-http-nft.awk" "$INFRA/spark/edge-http-rollback.sh" "$INFRA/spark/edge-http-checks.sh"; do
34 [ -f "$f" ] || die "missing $f"
35done
36[ ! -e "$SITE_DST" ] || die "$SITE_DST exists (already applied? roll back first)"
37grep -q 'http80_redirect' "$NFT" && die "$NFT already has chain http80_redirect"
38[ "$(grep -cF "$RULE443" "$NFT")" = 1 ] || die "the :443 front-door rule is not in $NFT exactly once"
39[ "$(tail -n 1 "$NFT")" = "}" ] || die "$NFT does not end with the table's closing brace"
40[ "$(grep -c '^table ' "$NFT")" = 1 ] || die "$NFT has more than one table"
41if ss -ltnH "sport = :$PORT" | grep -q .; then die "port $PORT is in use"; fi
42ip -4 addr show | grep -q "inet $ADDR/" || die "$ADDR is not an address of this host"
43nginx -t -q 2>/dev/null || die "nginx -t fails before any change"
44nft -c -f "$NFT" || die "live $NFT does not pass nft -c before any change"
45say "preconditions ok"
46
47# 2. new firewall file: the 4480 accept right after the :443 rule, and the redirect chain before the
48# table's closing brace. Every other line is byte-identical (checked below).
49NEW=$(mktemp)
50trap 'rm -f "$NEW"' EXIT
51awk -f "$INFRA/spark/edge-http-nft.awk" "$NFT" > "$NEW"
52added=$(diff "$NFT" "$NEW" | grep -c '^>' || true)
53removed=$(diff "$NFT" "$NEW" | grep -c '^<' || true)
54[ "$added" = 5 ] && [ "$removed" = 0 ] || die "new firewall file differs by +$added/-$removed lines (want +5/-0)"
55nft -c -f "$NEW" || die "new firewall file fails nft -c"
56say "new firewall file ok (+5 lines, nft -c passes)"
57
58# 3. backup
59mkdir -p "$BK"
60cp -p "$NFT" "$BK/ingress.nft"
61printf '%s\n' "$SITE_DST" > "$BK/NEW_FILES"
62cat > "$BK/README" <<EOF
63edge-http-apply.sh backup, $STAMP. Roll back with:
64 sudo bash $INFRA/spark/edge-http-rollback.sh $BK
65ingress.nft = $NFT before the change (sha256 $(sha256sum "$NFT" | cut -c1-64)).
66NEW_FILES lists files that did not exist before and are removed on rollback.
67EOF
68say "backup $BK"
69rollback() {
70 echo "edge-http-apply: rolling back to $BK" >&2
71 bash "$INFRA/spark/edge-http-rollback.sh" "$BK" >&2 || echo "edge-http-apply: ROLLBACK FAILED, see above" >&2
72}
73
74# 4. nginx
75install -m 644 "$SITE_SRC" "$SITE_DST" || { rollback; die "install of the redirect site failed"; }
76if ! nginx -t -q 2>/dev/null; then rollback; die "nginx -t fails with the new site"; fi
77# set -e would exit here without rollback if the reload command itself fails.
78if ! ${EDGE_RELOAD:-systemctl reload nginx}; then rollback; die "nginx reload failed"; fi
79up=0
80for _ in $(seq 1 20); do
81 if ss -ltnH "sport = :$PORT" | grep -q "$ADDR:$PORT"; then up=1; break; fi
82 sleep 0.5
83done
84[ "$up" = 1 ] || { rollback; die "nginx is not listening on $ADDR:$PORT after reload"; }
85loc=$(curl -sS -o /dev/null -m 5 -w '%{http_code} %{redirect_url}' -H 'Host: interplane.io' "http://$ADDR:$PORT/quick-start/?a=1" || true)
86[ "$loc" = "301 https://interplane.io/quick-start/?a=1" ] || { rollback; die "direct redirect check got '$loc'"; }
87say "nginx reloaded; $ADDR:$PORT answers 301 https://interplane.io/quick-start/?a=1"
88
89# 5. firewall
90install -m 600 "$NEW" "$NFT" || { rollback; die "install of the firewall file failed"; }
91if ! nft -f "$NFT"; then rollback; die "nft -f of the new file failed"; fi
92nft list chain inet atlas_ingress http80_redirect > /dev/null || { rollback; die "chain http80_redirect not live"; }
93say "firewall loaded (chain http80_redirect live)"
94
95# 6. live checks from the LAN (EDGE_CHECKS: the rehearsal runs its own command, as root, instead)
96if [ -n "${EDGE_CHECKS:-}" ]; then
97 if ! bash -c "$EDGE_CHECKS"; then rollback; die "live checks failed"; fi
98else
99 U=${SUDO_USER:-}
100 [ -n "$U" ] || { rollback; die "no SUDO_USER to run the LAN checks as"; }
101 if ! sudo -u "$U" -H bash "$INFRA/spark/edge-http-checks.sh"; then rollback; die "live checks failed"; fi
102fi
103say "APPLIED. Backup $BK"