#!/usr/bin/env bash # Metadata/status end-to-end on a temporary local PLC + PDS. # # Seeds one canonical composer project plus a cache CVE link, serves a fake # Packagist index and artifact on 127.0.0.1, runs `--release-mode extras # --release-ecosystems composer`, and asserts the projectMeta record, the # append-only deprecated status, git metadata on the manifest artifact, the # local project/subject links, and that a second run makes no new writes. # Localhost only; the NVD upstream and the package registry 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) FIXTURES=$REPO_DIR/tests/fixtures/metadata_e2e PDS_PORT=${PDS_PORT:-4890} PDS_URL=http://127.0.0.1:$PDS_PORT PLC_PORT=${PLC_PORT:-2593} PLC_URL=http://127.0.0.1:$PLC_PORT NVD_PORT=${NVD_PORT:-4891} NVD_URL=http://127.0.0.1:$NVD_PORT SRC_PORT=${SRC_PORT:-4892} 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} TMP_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/vuln-notif-metadata-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 COLLECTIONS=( software.release.project software.release.manifest software.release.version software.release.channel com.nahdig.release.projectMeta software.release.status ) 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 # Sets FETCH_STATUS / FETCH_BODY for one record read. 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'*} } # Sets LIST_STATUS / LIST_BODY for one collection listing. list_records() { local response LIST_STATUS=000 LIST_BODY= response=$(curl -sS --max-time 15 -G \ -H "Authorization: Bearer $ACCESS_JWT" \ --data-urlencode "repo=$DID" \ --data-urlencode "collection=$1" \ --data-urlencode "limit=100" \ -w '\n%{http_code}' \ "$PDS_URL/xrpc/com.atproto.repo.listRecords") || return 0 LIST_STATUS=${response##*$'\n'} LIST_BODY=${response%$'\n'*} } # Writes a stable "collection uri cid" line per record for a before/after diff. snapshot_records() { local out=$1 collection : >"$out" for collection in "${COLLECTIONS[@]}"; do list_records "$collection" [ "$LIST_STATUS" = 200 ] || die "listRecords $collection HTTP $LIST_STATUS: $LIST_BODY" jq -r --arg collection "$collection" \ '.records[] | "\($collection) \(.uri) \(.cid)"' <<<"$LIST_BODY" | sort >>"$out" done } assert_one_record() { local collection=$1 list_records "$collection" [ "$LIST_STATUS" = 200 ] || die "listRecords $collection HTTP $LIST_STATUS: $LIST_BODY" [ "$(jq '.records | length' <<<"$LIST_BODY")" = 1 ] || die "expected one $collection record: $LIST_BODY" } 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 [ -f "$FIXTURES/expected.json" ] || die "missing $FIXTURES/expected.json" [ -f "$FIXTURES/registry.json" ] || die "missing $FIXTURES/registry.json" [ -f "$FIXTURES/seed.sql" ] || die "missing $FIXTURES/seed.sql" [ -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" command -v curl >/dev/null || die "curl is required" PROJECT_SLUG=$(jq -er '.slug' "$FIXTURES/expected.json") || die "fixture has no slug" PURL=$(jq -er '.purl' "$FIXTURES/expected.json") || die "fixture has no purl" VERSION=$(jq -er '.version' "$FIXTURES/expected.json") || die "fixture has no version" CVE_ID=$(jq -er '.cve' "$FIXTURES/expected.json") || die "fixture has no cve" EXPECTED_DESCRIPTION=$(jq -er '.description' "$FIXTURES/expected.json") || die "fixture has no description" EXPECTED_HOMEPAGE=$(jq -er '.homepage' "$FIXTURES/expected.json") || die "fixture has no homepage" EXPECTED_LICENSE=$(jq -er '.license' "$FIXTURES/expected.json") || die "fixture has no license" EXPECTED_REPOSITORY=$(jq -er '.repository' "$FIXTURES/expected.json") || die "fixture has no repository" EXPECTED_SCHEME=$(jq -er '.versioningScheme' "$FIXTURES/expected.json") || die "fixture has no versioningScheme" EXPECTED_STATUS=$(jq -er '.status' "$FIXTURES/expected.json") || die "fixture has no status" EXPECTED_REASON=$(jq -er '.reason' "$FIXTURES/expected.json") || die "fixture has no reason" 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=$! for _ in $(seq 1 50); do curl -fsS --max-time 1 "$NVD_URL/rest/json/cves/2.0" >/dev/null 2>&1 && break sleep 0.1 done curl -fsS --max-time 2 "$NVD_URL/rest/json/cves/2.0" >/dev/null || die "NVD stub is not ready" note "building fake composer registry on $SRC_URL" mkdir -p "$SRC_DIR/p2/acme" "$SRC_DIR/artifacts" ARTIFACT=$SRC_DIR/artifacts/acme-widget-$VERSION.zip # The PDS enforces `software.release.manifest` key: tid, and the manifest rkey # is derived from the artifact content. This payload keeps that deterministic # rkey a valid TID; do not change it without recomputing the rkey. printf 'metadata e2e artifact %s fixture\n' "$VERSION" >"$ARTIFACT" ARTIFACT_SHA=$(shasum -a 256 "$ARTIFACT" | awk '{print $1}') ARTIFACT_URL=$SRC_URL/artifacts/acme-widget-$VERSION.zip jq --arg url "$ARTIFACT_URL" --arg sha "$ARTIFACT_SHA" ' .packages["acme/widget"][0].dist.url = $url | .packages["acme/widget"][0].dist.shasum = $sha ' "$FIXTURES/registry.json" >"$SRC_DIR/p2/acme/widget.json" || die "rendering the registry fixture failed" 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 "registry 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:metadata-e2e" \ >"$TMP_ROOT/schema-init.json" || die "cache schema init failed" note "seeding cache rows" sed -e "s|__DID__|$DID|g" \ -e "s|__PROJECT_SLUG__|$PROJECT_SLUG|g" \ -e "s|__PURL__|$PURL|g" \ -e "s|__CVE_ID__|$CVE_ID|g" \ "$FIXTURES/seed.sql" >"$TMP_ROOT/seed.sql" || die "rendering the seed SQL failed" sqlite3 "$DB" <"$TMP_ROOT/seed.sql" || die "cache seed failed" run_extras() { "$VULN_NOTIF_BIN" --once \ --release-mode extras --release-limit 10 --release-recheck-seconds 0 \ --release-ecosystems composer --release-sources composer \ --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 extras run failed; log: $(tail -5 "$TMP_ROOT/notif.log")" } note "run: extras publishes project metadata, a release, and a deprecated status" run_extras STATS=$(grep '"event":"cycle_stats"' "$TMP_ROOT/notif.stdout" | tail -1) || die "first cycle_stats event is missing" # The pinned PDS enforces `software.release.status` key: tid. Status rkeys are # content-addressed and can carry a first character outside TID's 234567ab, in # which case the PDS rejects the append and the cycle reports one failure. if ! jq -e ' .release_mode == "extras" and .release_extras_projects_checked == 1 and .release_extras_projects_created == 1 and .release_extras_versions_created == 1 and .release_extras_manifests_created == 1 and .release_extras_statuses_created == 1 and .release_extras_failures == 0 and .artifact_downloads == 1 and .artifact_errors == 0 ' <<<"$STATS" >/dev/null; then if jq -e ' .release_extras_versions_created == 1 and .release_extras_statuses_created == 0 and .release_extras_failures > 0 ' <<<"$STATS" >/dev/null; then log "hint: the status append was rejected; the PDS requires a TID rkey whose first character is in 234567ab" fi die "unexpected extras cycle stats: $STATS" fi [ "$(sqlite3 "$DB" "SELECT count(*) FROM release_projects;")" = 1 ] || die "expected one release_projects row" [ "$(sqlite3 "$DB" "SELECT latest_version FROM release_projects WHERE slug='$PROJECT_SLUG';")" = "$VERSION" ] || die "unexpected latest version" [ "$(sqlite3 "$DB" "SELECT source FROM release_project_state WHERE slug='$PROJECT_SLUG';")" = "composer" ] || die "expected composer project state" [ "$(sqlite3 "$DB" "SELECT canonical_purl FROM release_project_state WHERE slug='$PROJECT_SLUG';")" = "$PURL" ] || die "unexpected canonical purl" PROJECT_URI=$(sqlite3 "$DB" "SELECT project_uri FROM release_projects WHERE slug='$PROJECT_SLUG';") MANIFEST_URI=$(sqlite3 "$DB" "SELECT manifest_uri FROM release_projects WHERE slug='$PROJECT_SLUG';") MANIFEST_CID=$(sqlite3 "$DB" "SELECT manifest_cid FROM release_projects WHERE slug='$PROJECT_SLUG';") VERSION_URI=$(sqlite3 "$DB" "SELECT version_uri FROM release_projects WHERE slug='$PROJECT_SLUG';") CHANNEL_URI=$(sqlite3 "$DB" "SELECT channel_uri FROM release_projects WHERE slug='$PROJECT_SLUG';") [ -n "$PROJECT_URI" ] && [ -n "$MANIFEST_URI" ] && [ -n "$MANIFEST_CID" ] && [ -n "$VERSION_URI" ] && [ -n "$CHANNEL_URI" ] || die "release projection is missing URIs" case "$PROJECT_URI" in "at://$DID/"*) ;; *) die "project ref is not same-DID: $PROJECT_URI" ;; esac case "$VERSION_URI" in "at://$DID/"*) ;; *) die "version ref is not same-DID: $VERSION_URI" ;; esac note "asserting one project/manifest/version/channel/projectMeta/status record" for collection in "${COLLECTIONS[@]}"; do assert_one_record "$collection" done note "asserting projectMeta metadata and same-DID project ref" fetch_record com.nahdig.release.projectMeta "$PROJECT_SLUG" [ "$FETCH_STATUS" = 200 ] || die "projectMeta record missing (HTTP $FETCH_STATUS)" jq -e --arg project "$PROJECT_URI" --arg slug "$PROJECT_SLUG" \ --arg description "$EXPECTED_DESCRIPTION" --arg homepage "$EXPECTED_HOMEPAGE" \ --arg license "$EXPECTED_LICENSE" --arg repository "$EXPECTED_REPOSITORY" \ --arg scheme "$EXPECTED_SCHEME" ' .value["$type"] == "com.nahdig.release.projectMeta" and .value.project == $project and .value.name == $slug and .value.description == $description and .value.homepage == $homepage and .value.license == $license and .value.repository == $repository and .value.versioningScheme == $scheme ' <<<"$FETCH_BODY" >/dev/null || die "projectMeta record shape: $FETCH_BODY" note "asserting manifest version metadata (git repository; target when present)" fetch_record software.release.manifest "${MANIFEST_URI##*/}" [ "$FETCH_STATUS" = 200 ] || die "manifest record missing (HTTP $FETCH_STATUS)" jq -e --arg project "$PROJECT_URI" --arg url "$ARTIFACT_URL" \ --arg repository "$EXPECTED_REPOSITORY" ' .value.project == $project and (.value.artifacts | length) == 1 and .value.artifacts[0].url == $url and (.value.artifacts[0].cid | length > 0) and (.value.artifacts[0].metadata | type == "array") and ([.value.artifacts[0].metadata[] | select(.repository == $repository)] | length == 1) and ((.value.artifacts[0].target == null) or (.value.artifacts[0].target | type == "object")) ' <<<"$FETCH_BODY" >/dev/null || die "manifest record shape: $FETCH_BODY" note "asserting the version record points at the immutable manifest" fetch_record software.release.version "$PROJECT_SLUG:$VERSION" [ "$FETCH_STATUS" = 200 ] || die "version record missing (HTTP $FETCH_STATUS)" jq -e --arg project "$PROJECT_URI" --arg version "$VERSION" \ --arg manifest "$MANIFEST_URI" --arg manifest_cid "$MANIFEST_CID" ' .value.project == $project and .value.version == $version and .value.manifest.uri == $manifest and .value.manifest.cid == $manifest_cid ' <<<"$FETCH_BODY" >/dev/null || die "version record shape: $FETCH_BODY" fetch_record software.release.channel "$PROJECT_SLUG:latest" [ "$FETCH_STATUS" = 200 ] || die "channel record missing (HTTP $FETCH_STATUS)" jq -e --arg project "$PROJECT_URI" --arg version "$VERSION_URI" ' .value.project == $project and .value.name == "latest" and .value.version.uri == $version ' <<<"$FETCH_BODY" >/dev/null || die "channel record shape: $FETCH_BODY" note "asserting the append-only deprecated status for the version" list_records software.release.status [ "$LIST_STATUS" = 200 ] || die "listRecords status HTTP $LIST_STATUS: $LIST_BODY" jq -e --arg subject "$VERSION_URI" --arg status "$EXPECTED_STATUS" --arg reason "$EXPECTED_REASON" ' (.records | length) == 1 and .records[0].value["$type"] == "software.release.status" and .records[0].value.subject == $subject and .records[0].value.status == $status and .records[0].value.reason == $reason and ((.records[0].value | has("kind") | not) or .records[0].value.kind == "security" or .records[0].value.kind == "broken" or .records[0].value.kind == "superseded") ' <<<"$LIST_BODY" >/dev/null || die "status record shape: $LIST_BODY" note "asserting local version metadata" [ "$(sqlite3 "$DB" "SELECT count(*) FROM release_version_meta WHERE project_slug='$PROJECT_SLUG' AND version='$VERSION' AND source='composer' AND raw_version='$VERSION';")" = 1 ] || die "expected one composer release_version_meta row" note "asserting local link index" [ "$(sqlite3 "$DB" "SELECT count(*) FROM links WHERE src_collection='com.nahdig.release.projectMeta' AND path='project' AND subject_uri='$PROJECT_URI';")" = 1 ] || die "expected the projectMeta -> project link" [ "$(sqlite3 "$DB" "SELECT count(*) FROM links WHERE src_collection='software.release.status' AND path='subject' AND subject_uri='$VERSION_URI';")" = 1 ] || die "expected the status -> version link" [ "$(sqlite3 "$DB" "SELECT count(*) FROM links WHERE subject_uri='';")" = 0 ] || die "empty link targets were indexed" note "snapshotting PDS records before the second run" snapshot_records "$TMP_ROOT/before.txt" note "run 2: an unchanged cycle makes no new writes" run_extras snapshot_records "$TMP_ROOT/after.txt" if ! cmp -s "$TMP_ROOT/before.txt" "$TMP_ROOT/after.txt"; then diff -u "$TMP_ROOT/before.txt" "$TMP_ROOT/after.txt" >&2 || true die "the second run changed the PDS record set" fi SECOND_STATS=$(grep '"event":"cycle_stats"' "$TMP_ROOT/notif.stdout" | tail -1) || die "second cycle_stats event is missing" jq -e ' .release_mode == "extras" and .release_extras_projects_checked == 1 and .release_extras_unchanged == 1 and .release_extras_versions_created == 0 and .release_extras_manifests_created == 0 and .release_extras_statuses_created == 0 and .release_extras_failures == 0 ' <<<"$SECOND_STATS" >/dev/null || die "unexpected second-cycle stats: $SECOND_STATS" printf '\nRESULT: PASS\n'