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

johnandersen777.bsky.social/vuln-notif

0
0
main
554 lines · 16.2 KB Raw History
//! Identity migration admin tool: plan grouping, dependency-ordered apply,
//! cursor resume, idempotence, verification, snapshots, and purl slug
//! normalization.

mod common;

use std::path::PathBuf;
use std::sync::Arc;

use serde_json::{Value, json};
use tempfile::TempDir;

use common::*;
use vuln_notif::constants::{
    ATPROTO_COLLECTION, CHANNEL_COLLECTION, MANIFEST_COLLECTION, PROJECT_COLLECTION,
    PURL_COLLECTION, SOFTWARE_COLLECTION, STATUS_COLLECTION, VERSION_COLLECTION,
};
use vuln_notif::db::DbHandle;
use vuln_notif::db::model::SoftwareMark;
use vuln_notif::migrate::apply::{ApplyOptions, apply_plan};
use vuln_notif::migrate::plan::build_plan;
use vuln_notif::migrate::snapshot::write_snapshot;
use vuln_notif::migrate::verify::verify_plan;
use vuln_notif::migrate::{MigrationAction, normalize_purl_slugs};
use vuln_notif::pds::Publisher;

const LEGACY_SLUG: &str = "red-hat-red-hat-enterprise-linux-8";
const CANONICAL_SLUG: &str = "red-hat-enterprise-linux";
const CVE_ID: &str = "CVE-2024-0001";
const PURL: &str = "pkg:rpm/redhat/enterprise-linux@8";

struct Fixture {
    _dir: TempDir,
    cache: PathBuf,
    db: DbHandle,
    pds: Arc<FakePds>,
    publisher: Publisher,
}

impl Fixture {
    fn legacy_uri(&self, collection: &str) -> String {
        record_uri(REPO, collection, LEGACY_SLUG)
    }

    fn project_uri(&self, slug: &str) -> String {
        record_uri(REPO, PROJECT_COLLECTION, slug)
    }
}

async fn seed_local(db: &DbHandle) {
    let cve = json!({
        "id": CVE_ID,
        "configurations": [{"nodes": [{"cpeMatch": [
            {"criteria": "cpe:2.3:o:redhat:enterprise_linux:9.0:*:*:*:*:*:*:*"}
        ]}]}],
        "affected": [
            {
                "vendor": "Red Hat",
                "product": "Red Hat Enterprise Linux 8",
                "version": "8.0",
                "cpes": ["cpe:2.3:o:redhat:enterprise_linux:8.0:*:*:*:*:*:*:*"],
            },
            {
                "vendor": "Red Hat",
                "product": "Red Hat Enterprise Linux 9",
                "version": "9.4",
                "cpes": ["cpe:2.3:o:redhat:enterprise_linux:9.0:*:*:*:*:*:*:*"],
            },
        ],
    });
    db.upsert_cves(REPO, &[cve]).await.expect("seed cves");
    for (slug, part, vendor, product) in [
        (LEGACY_SLUG, "o", "Red Hat", "Red Hat Enterprise Linux 8"),
        ("redhat-enterprise-linux", "o", "redhat", "enterprise_linux"),
    ] {
        db.mark_software(SoftwareMark {
            repo_did: REPO.to_owned(),
            slug: slug.to_owned(),
            part: part.to_owned(),
            vendor: vendor.to_owned(),
            product: product.to_owned(),
            project_uri: String::new(),
            project_cid: String::new(),
            software_uri: String::new(),
            software_cid: String::new(),
        })
        .await
        .expect("seed software");
    }
    // Legacy rows stored the full AT-URI instead of the slug.
    db.mark_purl(
        REPO,
        PURL,
        &record_uri(REPO, SOFTWARE_COLLECTION, LEGACY_SLUG),
        "at://seed/purl",
        "bafyseed",
    )
    .await
    .expect("seed purl");
    // The db actor keeps writes in its transaction until an explicit commit.
    db.commit().await.expect("commit seed");
}

fn seed_remote(pds: &FakePds) {
    let project = record_uri(REPO, PROJECT_COLLECTION, LEGACY_SLUG);
    let version_uri = record_uri(REPO, VERSION_COLLECTION, &format!("{LEGACY_SLUG}:8.0"));
    let manifest_uri = record_uri(REPO, MANIFEST_COLLECTION, "3manifest");
    let software_uri = record_uri(REPO, SOFTWARE_COLLECTION, LEGACY_SLUG);
    let purl_rkey = "purlhash";

    pds.seed_record(
        PROJECT_COLLECTION,
        LEGACY_SLUG,
        json!({"$type": PROJECT_COLLECTION, "name": LEGACY_SLUG}),
    );
    pds.seed_record(
        CHANNEL_COLLECTION,
        &format!("{LEGACY_SLUG}:8"),
        json!({
            "$type": CHANNEL_COLLECTION,
            "project": project,
            "name": "8",
            "version": strong_ref(&version_uri, PUT_CID),
        }),
    );
    pds.seed_record(
        VERSION_COLLECTION,
        &format!("{LEGACY_SLUG}:8.0"),
        json!({
            "$type": VERSION_COLLECTION,
            "project": project,
            "version": "8.0",
            "manifest": strong_ref(&manifest_uri, PUT_CID),
        }),
    );
    pds.seed_record(
        MANIFEST_COLLECTION,
        "3manifest",
        json!({"$type": MANIFEST_COLLECTION, "project": project, "artifacts": []}),
    );
    pds.seed_record(
        STATUS_COLLECTION,
        "3status",
        json!({"$type": STATUS_COLLECTION, "subject": version_uri, "status": "active"}),
    );
    pds.seed_record(
        SOFTWARE_COLLECTION,
        LEGACY_SLUG,
        json!({
            "$type": SOFTWARE_COLLECTION,
            "project": strong_ref(&project, PUT_CID),
            "part": "o",
            "vendor": "Red Hat",
            "product": "Red Hat Enterprise Linux 8",
        }),
    );
    pds.seed_record(
        PURL_COLLECTION,
        purl_rkey,
        json!({
            "$type": PURL_COLLECTION,
            "purl": PURL,
            "software": [strong_ref(&software_uri, "bafyold")],
        }),
    );
    pds.seed_record(
        ATPROTO_COLLECTION,
        CVE_ID,
        json!({
            "$type": ATPROTO_COLLECTION,
            "createdAt": "2026-01-01T00:00:00.000Z",
            "cve": CVE_ID,
            "nvd": strong_ref(&record_uri(REPO, COLLECTION, CVE_ID), PUT_CID),
            "software": [{
                "project": strong_ref(&project, PUT_CID),
                "part": "o",
                "vendor": "Red Hat",
                "product": "Red Hat Enterprise Linux 8",
                "versions": [strong_ref(&version_uri, PUT_CID)],
            }],
        }),
    );
}

async fn setup() -> Fixture {
    let dir = tempfile::tempdir().expect("tempdir");
    let cache = dir.path().join("cache.db");
    let db = DbHandle::open(&cache).await.expect("cache db");
    seed_local(&db).await;
    let pds = FakePds::new();
    seed_remote(&pds);
    let publisher = make_publisher(pds.clone(), &db);
    publisher.login().await.expect("login");
    Fixture {
        _dir: dir,
        cache,
        db,
        pds,
        publisher,
    }
}

async fn plan_for(fixture: &Fixture) -> vuln_notif::migrate::MigrationPlan {
    build_plan(&fixture.publisher, REPO, &fixture.cache, None)
        .await
        .expect("plan")
}

fn write_calls(pds: &FakePds) -> Vec<String> {
    pds.calls()
        .into_iter()
        .filter(|call| {
            matches!(
                call.method,
                "putRecord" | "createRecord" | "deleteRecord" | "applyWrites" | "uploadBlob"
            )
        })
        .map(|call| call.method.to_owned())
        .collect()
}

#[tokio::test]
async fn plan_groups_rhel_variants_and_writes_nothing() {
    let fixture = setup().await;
    let baseline = fixture.pds.calls().len();
    let plan = plan_for(&fixture).await;

    let create = plan
        .entries
        .iter()
        .find(|entry| entry.action == MigrationAction::CreateProject)
        .expect("create canonical project");
    assert_eq!(create.canonical_slug.as_deref(), Some(CANONICAL_SLUG));

    let software = plan
        .entries
        .iter()
        .find(|entry| {
            entry.action == MigrationAction::RepointSoftware
                && entry.legacy_slug.as_deref() == Some(LEGACY_SLUG)
        })
        .expect("repoint legacy software");
    assert_eq!(software.canonical_slug.as_deref(), Some(CANONICAL_SLUG));

    for collection in [
        PROJECT_COLLECTION,
        CHANNEL_COLLECTION,
        VERSION_COLLECTION,
        MANIFEST_COLLECTION,
        STATUS_COLLECTION,
    ] {
        assert!(
            plan.entries.iter().any(|entry| {
                entry.action == MigrationAction::DeleteReleaseRecord
                    && entry
                        .legacy_uri
                        .as_deref()
                        .is_some_and(|uri| uri.contains(&format!("/{collection}/")))
            }),
            "missing delete action for {collection}"
        );
    }
    assert!(
        plan.entries
            .iter()
            .any(|entry| entry.action == MigrationAction::RepointCve)
    );
    assert!(
        plan.entries
            .iter()
            .any(|entry| entry.action == MigrationAction::RepointPurl)
    );
    assert!(
        plan.entries
            .iter()
            .any(|entry| entry.action == MigrationAction::NormalizePurlSlug)
    );

    // Dry-run plan performs reads only.
    assert!(write_calls(&fixture.pds).is_empty());
    assert!(
        fixture.pds.calls()[baseline..]
            .iter()
            .all(|call| call.method == "listRecords")
    );
    // The local cache is untouched: no cursor rows, purl stays raw.
    assert!(
        fixture
            .db
            .list_identity_migration(REPO, None, 0)
            .await
            .expect("cursor rows")
            .is_empty()
    );
    let purl = fixture
        .db
        .get_purl(REPO, PURL)
        .await
        .expect("purl")
        .expect("purl row");
    assert!(purl.software_slug.starts_with("at://"));
}

#[tokio::test]
async fn apply_is_ordered_batched_resumable_and_idempotent() {
    let fixture = setup().await;
    let plan = plan_for(&fixture).await;

    // Create the canonical project, then fail the first software repoint.
    fixture.pds.push_put(response(
        200,
        json!({
            "uri": fixture.project_uri(CANONICAL_SLUG),
            "cid": PUT_CID,
        }),
    ));
    fixture.pds.push_put(response(
        400,
        json!({"error": "InvalidRequest", "message": "injected failure"}),
    ));

    let options = ApplyOptions {
        batch: 2,
        limit: 0,
        resume: false,
    };
    let error = apply_plan(&fixture.publisher, &fixture.db, &plan, &options)
        .await
        .expect_err("injected failure");
    assert!(error.to_string().contains("injected failure"), "{error}");

    let rows = fixture
        .db
        .list_identity_migration(REPO, None, 0)
        .await
        .expect("cursor rows");
    assert!(
        rows.iter()
            .any(|row| row.action.as_deref() == Some("create_project") && row.status == "done")
    );
    assert!(
        rows.iter()
            .any(|row| row.action.as_deref() == Some("repoint_software") && row.status == "error")
    );

    // Resume skips completed entries, retries the failed one, and batches the
    // five legacy deletes two at a time.
    let resumed = apply_plan(
        &fixture.publisher,
        &fixture.db,
        &plan,
        &ApplyOptions {
            batch: 2,
            limit: 0,
            resume: true,
        },
    )
    .await
    .expect("resume apply");
    assert_eq!(resumed.deleted, 5);
    assert_eq!(
        fixture
            .db
            .list_identity_migration(REPO, Some("done"), 0)
            .await
            .expect("done rows")
            .len(),
        plan.entries.len()
    );

    let calls = fixture.pds.calls();
    let writes: Vec<&ApiCall> = calls
        .iter()
        .filter(|call| {
            matches!(
                call.method,
                "putRecord" | "createRecord" | "deleteRecord" | "applyWrites"
            )
        })
        .collect();
    let first = |collection: &str| {
        writes
            .iter()
            .position(|call| call.method == "putRecord" && call.collection == collection)
            .expect("put for collection")
    };
    assert!(first(PROJECT_COLLECTION) < first(SOFTWARE_COLLECTION));
    assert!(first(SOFTWARE_COLLECTION) < first(PURL_COLLECTION));
    assert!(first(PURL_COLLECTION) < first(ATPROTO_COLLECTION));
    let apply_calls: Vec<&ApiCall> = writes
        .iter()
        .copied()
        .filter(|call| call.method == "applyWrites")
        .collect();
    assert_eq!(apply_calls.len(), 3);
    assert!(
        apply_calls
            .iter()
            .all(|call| call.writes.as_ref().is_some_and(|writes| writes.len() <= 2))
    );
    let deleted: usize = apply_calls
        .iter()
        .map(|call| call.writes.as_ref().map_or(0, Vec::len))
        .sum();
    assert_eq!(deleted, 5);
    let last_put = writes
        .iter()
        .rposition(|call| call.method == "putRecord")
        .expect("put");
    let first_apply = writes
        .iter()
        .position(|call| call.method == "applyWrites")
        .expect("apply");
    assert!(last_put < first_apply);

    // A second apply is a no-op.
    let before = write_calls(&fixture.pds).len();
    let second = apply_plan(
        &fixture.publisher,
        &fixture.db,
        &plan,
        &ApplyOptions {
            batch: 2,
            limit: 0,
            resume: false,
        },
    )
    .await
    .expect("second apply");
    assert_eq!(second.applied, 0);
    assert_eq!(second.deleted, 0);
    assert_eq!(write_calls(&fixture.pds).len(), before);
}

#[tokio::test]
async fn verify_catches_stale_cid_and_legacy_cve_ref() {
    let fixture = setup().await;
    let plan = plan_for(&fixture).await;
    apply_plan(
        &fixture.publisher,
        &fixture.db,
        &plan,
        &ApplyOptions::default(),
    )
    .await
    .expect("apply");

    // Software record keeps a stale project CID.
    fixture.pds.seed_record(
        SOFTWARE_COLLECTION,
        LEGACY_SLUG,
        json!({
            "$type": SOFTWARE_COLLECTION,
            "project": {
                "uri": fixture.project_uri(CANONICAL_SLUG),
                "cid": "bafystale",
            },
            "part": "o",
            "vendor": "Red Hat",
            "product": "Red Hat Enterprise Linux 8",
        }),
    );
    // CVE record still points at the deleted legacy project.
    fixture.pds.seed_record(
        ATPROTO_COLLECTION,
        CVE_ID,
        json!({
            "$type": ATPROTO_COLLECTION,
            "createdAt": "2026-01-01T00:00:00.000Z",
            "cve": CVE_ID,
            "nvd": strong_ref(&record_uri(REPO, COLLECTION, CVE_ID), PUT_CID),
            "software": [{
                "project": strong_ref(&fixture.legacy_uri(PROJECT_COLLECTION), PUT_CID),
                "part": "o",
                "vendor": "Red Hat",
                "product": "Red Hat Enterprise Linux 8",
            }],
        }),
    );

    let report = verify_plan(&fixture.publisher, &fixture.db, &plan)
        .await
        .expect("verify");
    assert!(!report.ok());
    assert!(
        report
            .errors
            .iter()
            .any(|error| error.contains("stale cid")),
        "{:?}",
        report.errors
    );
    assert!(
        report
            .errors
            .iter()
            .any(|error| error.contains("legacy project")),
        "{:?}",
        report.errors
    );
    assert!(report.stale_refs >= 1);
}

#[tokio::test]
async fn normalize_purl_slugs_converts_at_uris() {
    let fixture = setup().await;
    let raw = record_uri(REPO, SOFTWARE_COLLECTION, LEGACY_SLUG);
    let purl = fixture
        .db
        .get_purl(REPO, PURL)
        .await
        .expect("purl")
        .expect("purl row");
    assert_eq!(purl.software_slug, raw);

    let updated = normalize_purl_slugs(&fixture.db, REPO)
        .await
        .expect("normalize");
    assert_eq!(updated, 1);
    let purl = fixture
        .db
        .get_purl(REPO, PURL)
        .await
        .expect("purl")
        .expect("purl row");
    assert_eq!(purl.software_slug, LEGACY_SLUG);
}

#[tokio::test]
async fn snapshot_writes_uri_cid_and_value_jsonl() {
    let fixture = setup().await;
    let plan = plan_for(&fixture).await;
    let dir = fixture._dir.path().join("snapshot");
    let count = write_snapshot(&fixture.publisher, &plan, &dir)
        .await
        .expect("snapshot");
    assert!(count >= 8);

    let path = dir.join(format!("{PROJECT_COLLECTION}.jsonl"));
    let raw = std::fs::read_to_string(&path).expect("project snapshot");
    let project_uri = fixture.legacy_uri(PROJECT_COLLECTION);
    let line = raw
        .lines()
        .map(|line| serde_json::from_str::<Value>(line).expect("json line"))
        .find(|line| line.get("uri").and_then(Value::as_str) == Some(project_uri.as_str()))
        .expect("legacy project line");
    assert_eq!(line.get("cid").and_then(Value::as_str), Some(PUT_CID));
    assert_eq!(
        line.get("value")
            .and_then(|value| value.get("name"))
            .and_then(Value::as_str),
        Some(LEGACY_SLUG)
    );
}