~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
241 lines · 9.6 KB Raw History
#!/usr/bin/env bash
# Full-history end-to-end on a temporary local PLC + PDS.
#
# Seeds one canonical composer project, serves a fake Packagist index and
# artifact zips on 127.0.0.1, runs
# `--release-mode history --release-history-limit 2`, and asserts immutable
# version/manifest records, per-cycle limits, resume on the next run, and the
# history/artifact cycle counters. Localhost only; the NVD upstream and the
# package source are local stubs, so no live network service is used.
set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_DIR=$(cd "$SCRIPT_DIR/.." && pwd)

PDS_PORT=${PDS_PORT:-4830}
PDS_URL=http://127.0.0.1:$PDS_PORT
PLC_PORT=${PLC_PORT:-2589}
PLC_URL=http://127.0.0.1:$PLC_PORT
NVD_PORT=${NVD_PORT:-4840}
NVD_URL=http://127.0.0.1:$NVD_PORT
SRC_PORT=${SRC_PORT:-4850}
SRC_URL=http://127.0.0.1:$SRC_PORT
HANDLE=${HANDLE:-cve.test}
EMAIL=${EMAIL:-cve@example.com}
PASSWORD=${PASSWORD:-test-password}
BUILD_BINS=${BUILD_BINS:-1}
VULN_NOTIF_BIN=${VULN_NOTIF_BIN:-$REPO_DIR/target/release/vuln-notif}
IDENTITY_MIGRATE_BIN=${IDENTITY_MIGRATE_BIN:-$REPO_DIR/target/release/identity-migrate}

PROJECT_SLUG=acme-widget
PURL='pkg:composer/acme/widget'
VERSIONS=(1.0.0 1.1.0 1.2.0)

TMP_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/vuln-notif-history-e2e.XXXXXX")
PLC_STATE=$TMP_ROOT/plc-state
PDS_STATE=$TMP_ROOT/pds-state
PDS_DATA=$TMP_ROOT/pds-data
ACCOUNT_FILE=$TMP_ROOT/account.json
DB=$TMP_ROOT/cache.db
PURLDB=$TMP_ROOT/purl2cpe.db
NVD_DIR=$TMP_ROOT/nvd
SRC_DIR=$TMP_ROOT/source
NVD_PID=
SRC_PID=
PLC_STARTED=0
PDS_STARTED=0

note() { printf '\n== %s\n' "$*"; }
log() { printf '%s\n' "$*" >&2; }
fail() { printf 'RESULT: FAIL (%s)\n' "$*" >&2; exit 1; }
die() { fail "$*"; }

cleanup() {
  local rc=$?
  set +e
  if [ -n "$NVD_PID" ]; then kill "$NVD_PID" >/dev/null 2>&1 || true; fi
  if [ -n "$SRC_PID" ]; then kill "$SRC_PID" >/dev/null 2>&1 || true; fi
  if [ "$PDS_STARTED" = 1 ]; then
    STATE_DIR=$PDS_STATE "$SCRIPT_DIR/local_atproto_pds.sh" stop >/dev/null 2>&1
  fi
  if [ "$PLC_STARTED" = 1 ]; then
    STATE_DIR=$PLC_STATE "$SCRIPT_DIR/local_plc.sh" stop >/dev/null 2>&1
  fi
  if [ "$rc" -ne 0 ] && [ "${KEEP_TMP:-0}" = 1 ]; then
    printf '\ncleanup: kept %s for inspection\n' "$TMP_ROOT" >&2
  else
    rm -rf "$TMP_ROOT"
  fi
  exit "$rc"
}
trap cleanup EXIT

fetch_record() {
  local response
  FETCH_STATUS=000
  FETCH_BODY=
  response=$(curl -sS --max-time 15 -G \
    -H "Authorization: Bearer $ACCESS_JWT" \
    --data-urlencode "repo=$DID" \
    --data-urlencode "collection=$1" \
    --data-urlencode "rkey=$2" \
    -w '\n%{http_code}' \
    "$PDS_URL/xrpc/com.atproto.repo.getRecord") || return 0
  FETCH_STATUS=${response##*$'\n'}
  FETCH_BODY=${response%$'\n'*}
}

note "building release binaries"
if [ "$BUILD_BINS" = 1 ]; then
  (cd "$REPO_DIR" && cargo build --release --locked --bin vuln-notif --bin identity-migrate) ||
    die "cargo build failed"
fi
[ -x "$VULN_NOTIF_BIN" ] || die "missing $VULN_NOTIF_BIN"
[ -x "$IDENTITY_MIGRATE_BIN" ] || die "missing $IDENTITY_MIGRATE_BIN"
command -v jq >/dev/null || die "jq is required"
command -v sqlite3 >/dev/null || die "sqlite3 is required"
command -v python3 >/dev/null || die "python3 is required"
command -v shasum >/dev/null || die "shasum is required"

note "starting hono-plc on $PLC_URL"
plc_started_url=$(PORT=$PLC_PORT STATE_DIR=$PLC_STATE "$SCRIPT_DIR/local_plc.sh" start) ||
  die "local_plc.sh start failed"
PLC_STARTED=1
[ "$plc_started_url" = "$PLC_URL" ] || die "unexpected PLC URL: $plc_started_url"

note "starting atproto-pds on $PDS_URL"
pds_started_url=$(PDS_PORT=$PDS_PORT PLC_URL=$PLC_URL STATE_DIR=$PDS_STATE \
  PDS_DATA_DIRECTORY=$PDS_DATA "$SCRIPT_DIR/local_atproto_pds.sh" start) ||
  die "local_atproto_pds.sh start failed"
PDS_STARTED=1
[ "$pds_started_url" = "$PDS_URL" ] || die "unexpected PDS URL: $pds_started_url"
curl -fsS --max-time 5 "$PDS_URL/xrpc/_health" >/dev/null || die "PDS health check failed"

note "starting NVD stub on $NVD_URL"
mkdir -p "$NVD_DIR/rest/json/cves"
printf '{"resultsPerPage":0,"startIndex":0,"totalResults":0,"vulnerabilities":[]}\n' \
  >"$NVD_DIR/rest/json/cves/2.0"
python3 -m http.server "$NVD_PORT" --bind 127.0.0.1 --directory "$NVD_DIR" \
  >"$TMP_ROOT/nvd.log" 2>&1 &
NVD_PID=$!

note "building fake composer source on $SRC_URL"
mkdir -p "$SRC_DIR/p2/acme" "$SRC_DIR/artifacts"
ENTRIES=
for VERSION in "${VERSIONS[@]}"; do
  ZIP=$SRC_DIR/artifacts/acme-widget-$VERSION.zip
  printf 'history e2e artifact %s\n' "$VERSION" >"$ZIP"
  SHA=$(shasum -a 256 "$ZIP" | awk '{print $1}')
  ENTRY=$(jq -nc \
    --arg version "$VERSION" \
    --arg url "$SRC_URL/artifacts/acme-widget-$VERSION.zip" \
    --arg shasum "$SHA" \
    '{name:"acme/widget", version:$version, dist:{type:"zip", url:$url, shasum:$shasum}}')
  ENTRIES+="${ENTRY},"
done
ENTRIES=${ENTRIES%,}
jq -nc --argjson entries "[$ENTRIES]" \
  '{minified:"composer/2.0", packages:{"acme/widget":$entries}}' \
  >"$SRC_DIR/p2/acme/widget.json"
python3 -m http.server "$SRC_PORT" --bind 127.0.0.1 --directory "$SRC_DIR" \
  >"$TMP_ROOT/source.log" 2>&1 &
SRC_PID=$!
for _ in $(seq 1 50); do
  curl -fsS --max-time 1 "$SRC_URL/p2/acme/widget.json" >/dev/null 2>&1 && break
  sleep 0.1
done
curl -fsS --max-time 2 "$SRC_URL/p2/acme/widget.json" >/dev/null || die "source stub is not ready"

note "creating account $HANDLE"
ACCOUNT_FILE=$ACCOUNT_FILE PDS_URL=$PDS_URL PLC_URL=$PLC_URL \
  HANDLE=$HANDLE EMAIL=$EMAIL PASSWORD=$PASSWORD \
  "$SCRIPT_DIR/create_local_account.sh" >/dev/null || die "create_local_account.sh failed"
DID=$(jq -er '.did' "$ACCOUNT_FILE") || die "account file missing did"
ACCESS_JWT=$(jq -er '.session.accessJwt' "$ACCOUNT_FILE") || die "account file missing accessJwt"
ATP_APP_PASSWORD=$(jq -er '.app_password' "$ACCOUNT_FILE") || die "account file missing app_password"
export ATP_PDS_URL="$PDS_URL" ATP_HANDLE="$HANDLE" ATP_APP_PASSWORD
printf 'did: %s\n' "$DID"

note "initializing cache schema"
"$IDENTITY_MIGRATE_BIN" normalize-purl-slugs \
  --cache-db "$DB" --purldb "$PURLDB" --handle "did:local:history-e2e" \
  >"$TMP_ROOT/schema-init.json" || die "cache schema init failed"

note "seeding cache rows"
cat >"$TMP_ROOT/seed.sql" <<SQL
INSERT OR REPLACE INTO software
  (repo_did, slug, part, vendor, product, project_uri, project_cid, software_uri, software_cid, status, updated_at)
VALUES
  ('$DID', '$PROJECT_SLUG', 'a', 'acme', 'widget', '', '',
   'at://$DID/com.nahdig.software/$PROJECT_SLUG', 'bafysoftware', 'created', '2026-01-01T00:00:00.000Z');
INSERT OR REPLACE INTO software_purls
  (repo_did, purl, software_slug, purl_uri, purl_cid, status, updated_at)
VALUES ('$DID', '$PURL', '$PROJECT_SLUG',
        'at://$DID/com.nahdig.purl/acme-widget', 'bafypurl', 'created', '2026-01-01T00:00:00.000Z');
SQL
sqlite3 "$DB" <"$TMP_ROOT/seed.sql" || die "cache seed failed"

run_history() {
  "$VULN_NOTIF_BIN" --once \
    --release-mode history --release-limit 10 --release-recheck-seconds 0 \
    --release-history-limit 2 --release-concurrency 1 \
    --release-source-url "$SRC_URL" \
    --artifact-max-bytes 1048576 \
    --cache-db "$DB" --purldb "$PURLDB" \
    --pds-url "$PDS_URL" --handle "$HANDLE" --app-password "$ATP_APP_PASSWORD" \
    --nvd-url "$NVD_URL/rest/json/cves/2.0" --poll-interval 1 \
    >>"$TMP_ROOT/notif.stdout" 2>>"$TMP_ROOT/notif.log" ||
    die "vuln-notif history run failed; log: $(tail -5 "$TMP_ROOT/notif.log")"
}

note "run 1: history limit 2 of 3 versions"
run_history
[ "$(sqlite3 "$DB" "SELECT count(*) FROM release_versions;")" = 2 ] ||
  die "expected 2 version rows after the first cycle"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM release_history_jobs WHERE status='done';")" = 2 ] ||
  die "expected 2 done jobs after the first cycle"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM release_history_jobs WHERE status='pending';")" = 1 ] ||
  die "expected 1 pending job after the first cycle"
FIRST_STATS=$(grep '"event":"cycle_stats"' "$TMP_ROOT/notif.stdout" | tail -1)
jq -e --argjson versions "${#VERSIONS[@]}" '
  .release_mode == "history" and
  .release_history_projects_checked == 1 and
  .release_history_enqueued == $versions and
  .release_history_processed == 2 and
  .release_history_created == 2 and
  .release_history_failed == 0 and
  .artifact_downloads == 2 and
  .artifact_errors == 0
' <<<"$FIRST_STATS" >/dev/null || die "unexpected first-cycle stats: $FIRST_STATS"

note "asserting immutable version and manifest records"
for VERSION in 1.0.0 1.1.0; do
  fetch_record software.release.version "$PROJECT_SLUG:$VERSION"
  [ "$FETCH_STATUS" = 200 ] || die "version $VERSION missing (HTTP $FETCH_STATUS)"
  jq -e --arg slug "$PROJECT_SLUG" --arg version "$VERSION" '
    .value.version == $version and
    (.value.manifest.uri | contains("/software.release.manifest/")) and
    (.value.manifest.cid | length > 0)
  ' <<<"$FETCH_BODY" >/dev/null || die "version $VERSION shape: $FETCH_BODY"
done
MANIFESTS=$(sqlite3 "$DB" "SELECT count(*) FROM release_manifests;")
[ "$MANIFESTS" = 2 ] || die "expected 2 manifest rows, got $MANIFESTS"

note "run 2: history resumes the remaining version"
run_history
[ "$(sqlite3 "$DB" "SELECT count(*) FROM release_versions;")" = 3 ] ||
  die "expected 3 version rows after the resume"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM release_history_jobs WHERE status='done';")" = 3 ] ||
  die "expected 3 done jobs after the resume"
SECOND_STATS=$(grep '"event":"cycle_stats"' "$TMP_ROOT/notif.stdout" | tail -1)
jq -e '
  .release_mode == "history" and
  .release_history_projects_checked == 1 and
  .release_history_enqueued == 0 and
  .release_history_processed == 1 and
  .release_history_created == 1 and
  .release_history_failed == 0 and
  .artifact_downloads == 1
' <<<"$SECOND_STATS" >/dev/null || die "unexpected second-cycle stats: $SECOND_STATS"

printf '\nRESULT: PASS\n'