7fb86f5524
- Caddyfile: access and error logs replace the /i/<code> segment and every query string with REDACTED (regexp filter on request>uri); Authorization stays redacted by default. Deferred response headers: Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, Referrer-Policy. - Pi compose: caddy as uid 10002 with group 1000, read-only root, cap_drop ALL plus NET_BIND_SERVICE (needed to exec the image's file-capability binary), tmpfs /config and /tmp; install.sh sets /certs 0750 1000:1000 and Caddy's /data to 10002. - deploy.sh and install.sh print the pulled image digests and append them to /opt/kvitt/deployed-digests, warning when a recorded tag changed digest; the workflow prints the pushed digests; kvitt_cicd needs only write:package. - Dev stack: up.sh stages only fcm-service-account.json into infra/dev/secrets/ (git-ignored), mounted read-only at /secrets. - README: restricted cert-sync key required (plain root key not acceptable), remote_ip and log checks, APK download note, owner decisions (backup encryption, APK limit, key form, token scope), overhaul lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TQd3pEP9UeN2uifbHknQ7B
102 lines
4.0 KiB
Bash
Executable File
102 lines
4.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Shared helpers for the Kvitt Pi scripts (install.sh, deploy.sh, bootstrap.sh, backup-now.sh, install-apk.sh).
|
|
# Sourced, not run. These scripts run ON the Kvitt Pi as the owner's user (a member of the docker group).
|
|
# shellcheck disable=SC2034 # variables are used by the scripts that source this file
|
|
|
|
KVITT_DIR=${KVITT_DIR:-/opt/kvitt}
|
|
PUBLIC_HOST=icehack.asuscomm.com
|
|
PUBLIC_PORT=8443
|
|
|
|
say() { printf '%s\n' "$*"; }
|
|
die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
|
|
|
|
# Value of KEY in an env file (default /opt/kvitt/.env); empty when unset.
|
|
env_value() {
|
|
local file=${2:-$KVITT_DIR/.env}
|
|
sed -n "s/^$1=//p" "$file" | tail -n 1
|
|
}
|
|
|
|
# Set KEY=VALUE in an env file in place (adds the line when missing). Keeps the file's mode.
|
|
env_set() {
|
|
local key=$1 value=$2 file=${3:-$KVITT_DIR/.env} tmp
|
|
tmp=$(mktemp "$file.XXXXXX")
|
|
if grep -q "^$key=" "$file"; then
|
|
awk -v k="$key" -v v="$value" 'index($0, k "=") == 1 { print k "=" v; next } { print }' "$file" > "$tmp" \
|
|
|| { rm -f "$tmp"; return 1; }
|
|
else
|
|
{ cat "$file" && printf '%s=%s\n' "$key" "$value"; } > "$tmp" || { rm -f "$tmp"; return 1; }
|
|
fi
|
|
if ! { chmod --reference="$file" "$tmp" && mv -f "$tmp" "$file"; }; then rm -f "$tmp"; return 1; fi
|
|
}
|
|
|
|
compose() {
|
|
docker compose --project-directory "$KVITT_DIR" -f "$KVITT_DIR/compose.yaml" --env-file "$KVITT_DIR/.env" "$@"
|
|
}
|
|
|
|
# Health status of a compose service's container: healthy, unhealthy, starting, or none.
|
|
service_health() {
|
|
local id
|
|
id=$(compose ps -q "$1" 2>/dev/null | head -n 1)
|
|
[[ -n "$id" ]] || { echo none; return; }
|
|
docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$id" 2>/dev/null || echo none
|
|
}
|
|
|
|
# Wait until a service is healthy (default 180 s). Returns 1 on timeout.
|
|
wait_healthy() {
|
|
local service=$1 timeout=${2:-180} waited=0 status
|
|
while :; do
|
|
status=$(service_health "$service")
|
|
[[ "$status" == healthy ]] && { say "$service: healthy"; return 0; }
|
|
if (( waited >= timeout )); then
|
|
say "$service: $status after ${timeout}s"
|
|
return 1
|
|
fi
|
|
sleep 5
|
|
waited=$((waited + 5))
|
|
done
|
|
}
|
|
|
|
# GET /health through Caddy on this Pi, with the real certificate check (no -k): proves TLS and the proxy path.
|
|
print_health() {
|
|
local out
|
|
if out=$(curl -fsS --max-time 15 --resolve "$PUBLIC_HOST:$PUBLIC_PORT:127.0.0.1" "https://$PUBLIC_HOST:$PUBLIC_PORT/health" 2>&1); then
|
|
say "GET https://$PUBLIC_HOST:$PUBLIC_PORT/health (via this Pi): $out"
|
|
return 0
|
|
fi
|
|
say "GET https://$PUBLIC_HOST:$PUBLIC_PORT/health (via this Pi) failed: $out"
|
|
return 1
|
|
}
|
|
|
|
# Print the registry digest of every image the stack runs and append one line per image to
|
|
# $KVITT_DIR/deployed-digests: "<UTC time> <KVITT_TAG> <image reference> <digest>" (D-117). The registry is plain
|
|
# HTTP on the LAN (D-001), so a swapped image would otherwise go unnoticed: a tag that was recorded before with a
|
|
# different digest prints a WARNING (expected only for a tag like `latest` that is meant to move).
|
|
record_digests() {
|
|
local tag=$1 log=$KVITT_DIR/deployed-digests stamp ref repo digest prev d
|
|
local -a refs repo_digests
|
|
stamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
|
mapfile -t refs < <(compose config --images)
|
|
touch "$log"
|
|
say "Image digests (also in $log):"
|
|
for ref in "${refs[@]}"; do
|
|
[[ -n "$ref" ]] || continue
|
|
digest=""
|
|
if [[ "$ref" == *@sha256:* ]]; then
|
|
digest=${ref#*@}
|
|
else
|
|
repo=${ref%:*}
|
|
read -r -a repo_digests <<< "$(docker image inspect --format '{{range .RepoDigests}}{{.}} {{end}}' "$ref" 2>/dev/null || true)"
|
|
for d in "${repo_digests[@]}"; do
|
|
if [[ "$d" == "$repo@"* ]]; then digest=${d#*@}; break; fi
|
|
done
|
|
fi
|
|
[[ -n "$digest" ]] || digest=unknown
|
|
prev=$(awk -v r="$ref" '$3 == r { d = $4 } END { print d }' "$log")
|
|
if [[ -n "$prev" && "$prev" != "$digest" ]]; then
|
|
say "WARNING: $ref was $prev at an earlier deploy and is $digest now: the image behind this tag changed"
|
|
fi
|
|
say " $ref $digest"
|
|
printf '%s %s %s %s\n' "$stamp" "$tag" "$ref" "$digest" >> "$log"
|
|
done
|
|
}
|