|
| 1 | +#!/bin/bash |
| 2 | +# Verify deployed runtime files against ~/.pi/agent/baudbot-manifest.json. |
| 3 | +# |
| 4 | +# Modes: |
| 5 | +# off - skip verification (exit 0) |
| 6 | +# warn - log warnings on mismatch (exit 0) |
| 7 | +# strict - fail on mismatch (exit 1) |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +MODE="${BAUDBOT_STARTUP_INTEGRITY_MODE:-warn}" |
| 12 | +MANIFEST_FILE="${BAUDBOT_MANIFEST_FILE:-$HOME/.pi/agent/baudbot-manifest.json}" |
| 13 | +STATUS_FILE="${BAUDBOT_INTEGRITY_STATUS_FILE:-$HOME/.pi/agent/manifest-integrity-status.json}" |
| 14 | +AGENT_HOME="${BAUDBOT_HOME:-$HOME}" |
| 15 | +RELEASE_ROOT="${BAUDBOT_CURRENT_LINK:-/opt/baudbot/current}" |
| 16 | + |
| 17 | +# Expected mutable content that should not block startup if present in a manifest. |
| 18 | +EXCLUDE_REGEX='^\.pi/agent/(sessions|memory|logs)/|\.log$' |
| 19 | + |
| 20 | +mkdir -p "$(dirname "$STATUS_FILE")" |
| 21 | + |
| 22 | +write_status() { |
| 23 | + local status="$1" |
| 24 | + local checked_files="$2" |
| 25 | + local skipped_files="$3" |
| 26 | + local missing_files="$4" |
| 27 | + local hash_mismatches="$5" |
| 28 | + |
| 29 | + cat >"$STATUS_FILE" <<EOF |
| 30 | +{ |
| 31 | + "checked_at": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")", |
| 32 | + "mode": "$MODE", |
| 33 | + "status": "$status", |
| 34 | + "manifest": "$MANIFEST_FILE", |
| 35 | + "checked_files": $checked_files, |
| 36 | + "skipped_files": $skipped_files, |
| 37 | + "missing_files": $missing_files, |
| 38 | + "hash_mismatches": $hash_mismatches |
| 39 | +} |
| 40 | +EOF |
| 41 | + chmod 600 "$STATUS_FILE" 2>/dev/null || true |
| 42 | +} |
| 43 | + |
| 44 | +case "$MODE" in |
| 45 | + off|warn|strict) ;; |
| 46 | + *) |
| 47 | + echo "⚠️ Unknown BAUDBOT_STARTUP_INTEGRITY_MODE='$MODE' (expected: off|warn|strict). Falling back to warn." >&2 |
| 48 | + MODE="warn" |
| 49 | + ;; |
| 50 | +esac |
| 51 | + |
| 52 | +if [ "$MODE" = "off" ]; then |
| 53 | + echo "Startup integrity check disabled (BAUDBOT_STARTUP_INTEGRITY_MODE=off)." |
| 54 | + write_status "skipped" 0 0 0 0 |
| 55 | + exit 0 |
| 56 | +fi |
| 57 | + |
| 58 | +if [ ! -f "$MANIFEST_FILE" ]; then |
| 59 | + echo "⚠️ Deploy manifest not found: $MANIFEST_FILE" >&2 |
| 60 | + write_status "warn" 0 0 0 0 |
| 61 | + if [ "$MODE" = "strict" ]; then |
| 62 | + echo "❌ Startup integrity verification failed (missing manifest, strict mode)." >&2 |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + exit 0 |
| 66 | +fi |
| 67 | + |
| 68 | +if ! command -v jq >/dev/null 2>&1; then |
| 69 | + echo "⚠️ jq not found; cannot parse deploy manifest for startup integrity check." >&2 |
| 70 | + write_status "warn" 0 0 0 0 |
| 71 | + if [ "$MODE" = "strict" ]; then |
| 72 | + echo "❌ Startup integrity verification failed (jq missing, strict mode)." >&2 |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + exit 0 |
| 76 | +fi |
| 77 | + |
| 78 | +if ! jq -e '.files and (.files | type == "object")' "$MANIFEST_FILE" >/dev/null 2>&1; then |
| 79 | + echo "⚠️ Invalid manifest format (missing .files object): $MANIFEST_FILE" >&2 |
| 80 | + write_status "warn" 0 0 0 0 |
| 81 | + if [ "$MODE" = "strict" ]; then |
| 82 | + echo "❌ Startup integrity verification failed (invalid manifest, strict mode)." >&2 |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + exit 0 |
| 86 | +fi |
| 87 | + |
| 88 | +checked_files=0 |
| 89 | +skipped_files=0 |
| 90 | +missing_files=0 |
| 91 | +hash_mismatches=0 |
| 92 | + |
| 93 | +while IFS=$'\t' read -r rel_path expected_hash; do |
| 94 | + [ -n "$rel_path" ] || continue |
| 95 | + |
| 96 | + if [[ "$rel_path" =~ $EXCLUDE_REGEX ]]; then |
| 97 | + skipped_files=$((skipped_files + 1)) |
| 98 | + continue |
| 99 | + fi |
| 100 | + |
| 101 | + if [[ "$rel_path" == release/* ]]; then |
| 102 | + full_path="$RELEASE_ROOT/${rel_path#release/}" |
| 103 | + else |
| 104 | + full_path="$AGENT_HOME/$rel_path" |
| 105 | + fi |
| 106 | + |
| 107 | + checked_files=$((checked_files + 1)) |
| 108 | + |
| 109 | + if [ ! -f "$full_path" ]; then |
| 110 | + echo "⚠️ Missing file from manifest: $rel_path ($full_path)" >&2 |
| 111 | + missing_files=$((missing_files + 1)) |
| 112 | + continue |
| 113 | + fi |
| 114 | + |
| 115 | + actual_hash=$(sha256sum "$full_path" | awk '{print $1}') |
| 116 | + if [ "$actual_hash" != "$expected_hash" ]; then |
| 117 | + echo "⚠️ Hash mismatch: $rel_path" >&2 |
| 118 | + hash_mismatches=$((hash_mismatches + 1)) |
| 119 | + fi |
| 120 | +done < <(jq -r '.files | to_entries[] | [.key, .value] | @tsv' "$MANIFEST_FILE") |
| 121 | + |
| 122 | +if [ "$missing_files" -eq 0 ] && [ "$hash_mismatches" -eq 0 ]; then |
| 123 | + echo "✅ Startup integrity check passed ($checked_files files, $skipped_files skipped)." |
| 124 | + write_status "pass" "$checked_files" "$skipped_files" 0 0 |
| 125 | + exit 0 |
| 126 | +fi |
| 127 | + |
| 128 | +total_issues=$((missing_files + hash_mismatches)) |
| 129 | +echo "⚠️ Startup integrity check found $total_issues issue(s) ($missing_files missing, $hash_mismatches hash mismatch)." >&2 |
| 130 | + |
| 131 | +if [ "$MODE" = "strict" ]; then |
| 132 | + write_status "fail" "$checked_files" "$skipped_files" "$missing_files" "$hash_mismatches" |
| 133 | + echo "❌ Strict mode enabled; refusing to start." >&2 |
| 134 | + exit 1 |
| 135 | +fi |
| 136 | + |
| 137 | +write_status "warn" "$checked_files" "$skipped_files" "$missing_files" "$hash_mismatches" |
| 138 | +exit 0 |
0 commit comments