//! Identity migration record-body rewrites: golden legacy RHEL variants are //! repointed at one canonical project, raw fields survive, legacy release //! records are deleted last, and cross-DID refs are rejected. 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, PROJECT_COLLECTION, PURL_COLLECTION, SOFTWARE_COLLECTION, STATUS_COLLECTION, VERSION_COLLECTION, }; use vuln_notif::db::DbHandle; use vuln_notif::db::model::{Link, SoftwareMark}; use vuln_notif::migrate::apply::{ApplyOptions, apply_plan, apply_plan_with_snapshot}; use vuln_notif::migrate::plan::build_plan; use vuln_notif::migrate::remap::{ LegacyReason, LegacyReleaseKind, RemapMap, StrongRef, canonical_project_record, legacy_release_records, repoint_record, }; use vuln_notif::migrate::verify::verify_plan; use vuln_notif::migrate::{ ALL_COLLECTIONS, MigrationAction, RemoteInventory, project_uri, split_uri, }; use vuln_notif::pds::Publisher; const CANONICAL_SLUG: &str = "red-hat-enterprise-linux"; const LEGACY_SLUGS: [&str; 3] = [ "red-hat-red-hat-enterprise-linux-8", "redhat-enterprise-linux", "redhat-rhel-els", ]; const CVE_ID: &str = "CVE-2024-0001"; struct Fixture { _dir: TempDir, cache: PathBuf, db: DbHandle, pds: Arc, publisher: Publisher, legacy: Value, expected: Value, } fn fixture(name: &str) -> Value { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("tests/fixtures/identity-migration") .join(name); let raw = std::fs::read_to_string(&path) .unwrap_or_else(|error| panic!("read {}: {error}", path.display())); serde_json::from_str(&raw).unwrap_or_else(|error| panic!("parse {}: {error}", path.display())) } fn remote_record<'a>(legacy: &'a Value, collection: &str, rkey: &str) -> &'a Value { legacy["remote"] .as_array() .expect("remote records") .iter() .find(|record| { record["collection"].as_str() == Some(collection) && record["rkey"].as_str() == Some(rkey) }) .unwrap_or_else(|| panic!("fixture record {collection}/{rkey}")) .get("value") .expect("record value") } async fn setup() -> Fixture { let legacy = fixture("legacy.json"); let expected = fixture("expected.json"); let dir = tempfile::tempdir().expect("tempdir"); let cache = dir.path().join("cache.db"); let db = DbHandle::open(&cache).await.expect("cache db"); for row in legacy["software_rows"].as_array().expect("software rows") { db.mark_software(SoftwareMark { repo_did: REPO.to_owned(), slug: row["slug"].as_str().expect("slug").to_owned(), part: row["part"].as_str().expect("part").to_owned(), vendor: row["vendor"].as_str().expect("vendor").to_owned(), product: row["product"].as_str().expect("product").to_owned(), project_uri: String::new(), project_cid: String::new(), software_uri: String::new(), software_cid: String::new(), }) .await .expect("seed software"); } for row in legacy["purls"].as_array().expect("purl rows") { let slug = row["software_slug"].as_str().expect("software slug"); db.mark_purl( REPO, row["purl"].as_str().expect("purl"), slug, &record_uri(REPO, SOFTWARE_COLLECTION, slug), "bafyseed", ) .await .expect("seed purl"); } db.upsert_cves(REPO, &[legacy["cve"].clone()]) .await .expect("seed cve"); db.commit().await.expect("commit seed"); let pds = FakePds::new(); for record in legacy["remote"].as_array().expect("remote") { pds.seed_record( record["collection"].as_str().expect("collection"), record["rkey"].as_str().expect("rkey"), record["value"].clone(), ); } let publisher = make_publisher(pds.clone(), &db); publisher.login().await.expect("login"); Fixture { _dir: dir, cache, db, pds, publisher, legacy, expected, } } async fn plan_for(fixture: &Fixture) -> vuln_notif::migrate::MigrationPlan { build_plan(&fixture.publisher, REPO, &fixture.cache, None) .await .expect("plan") } fn legacy_project_uri(slug: &str) -> String { project_uri(REPO, slug) } fn record_by_rkey<'a>( plan: &'a vuln_notif::migrate::MigrationPlan, action: MigrationAction, rkey: &str, ) -> &'a vuln_notif::migrate::MigrationEntry { plan.entries .iter() .find(|entry| { entry.action == action && entry .legacy_uri .as_deref() .and_then(split_uri) .is_some_and(|(_, _, record_key)| record_key == rkey) }) .unwrap_or_else(|| panic!("missing {action:?} entry for {rkey}")) } fn assert_expected_records(pds: &FakePds, expected: &Value, group: &str, collection: &str) { for (rkey, want) in expected[group].as_object().expect("expected group") { let got = pds .record(collection, rkey) .unwrap_or_else(|| panic!("missing {collection}/{rkey}")); assert_eq!(&got, want, "{collection}/{rkey}"); } } #[tokio::test] async fn plan_maps_rhel_variants_and_types_status_reasons() { let fixture = setup().await; let plan = plan_for(&fixture).await; for slug in LEGACY_SLUGS { let entry = record_by_rkey(&plan, MigrationAction::RepointSoftware, slug); assert_eq!( entry.canonical_slug.as_deref(), Some(CANONICAL_SLUG), "{slug}" ); } assert_eq!( plan.count(MigrationAction::CreateProject), 0, "canonical project already exists" ); assert_eq!(plan.count(MigrationAction::RepointStatus), 3); let status = record_by_rkey(&plan, MigrationAction::DeleteReleaseRecord, "3statusels"); assert!( status.reason.contains("orphaned_status_subject"), "{}", status.reason ); // The typed enumerator reports the same decision without plan formatting. let remote = RemoteInventory::load(&fixture.publisher, ALL_COLLECTIONS) .await .expect("remote inventory"); let mut map = RemapMap::new(REPO); for slug in LEGACY_SLUGS { map.add_project( &legacy_project_uri(slug), StrongRef::new(project_uri(REPO, CANONICAL_SLUG), PUT_CID), ); } map.mark_deleted(&record_uri( REPO, VERSION_COLLECTION, "redhat-rhel-els:6-els", )); let legacy = legacy_release_records(&remote, &map).expect("legacy records"); let orphan = legacy .iter() .find(|record| record.rkey == "3statusels") .expect("orphan status"); assert_eq!(orphan.kind, LegacyReleaseKind::Status); assert_eq!( orphan.reason, LegacyReason::OrphanedStatusSubject { subject: record_uri(REPO, VERSION_COLLECTION, "redhat-rhel-els:6-els"), } ); assert!( legacy .iter() .any(|record| record.rkey == "red-hat-red-hat-enterprise-linux-8") ); } #[tokio::test] async fn pure_repoint_preserves_raw_fields_and_dedupes() { let fixture = setup().await; let mut map = RemapMap::new(REPO); for slug in LEGACY_SLUGS { map.add_project( &legacy_project_uri(slug), StrongRef::new(project_uri(REPO, CANONICAL_SLUG), PUT_CID), ); } let software_uri = record_uri( REPO, SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", ); map.add_software(&software_uri, StrongRef::new(&software_uri, PUT_CID)); let legacy_version = record_uri( REPO, VERSION_COLLECTION, "red-hat-red-hat-enterprise-linux-8:8.0", ); let canonical_version = record_uri(REPO, VERSION_COLLECTION, "red-hat-enterprise-linux:8.0"); map.add_release(&legacy_version, StrongRef::new(&canonical_version, PUT_CID)); let software = remote_record( &fixture.legacy, SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", ); let rewritten = repoint_record(SOFTWARE_COLLECTION, software, &map) .expect("repoint software") .expect("software changed"); assert_eq!( rewritten["project"]["uri"], project_uri(REPO, CANONICAL_SLUG) ); for field in ["part", "vendor", "product", "cpes", "purls", "note"] { assert_eq!(rewritten[field], software[field], "{field}"); } let purl = remote_record(&fixture.legacy, PURL_COLLECTION, "purlhash8"); let rewritten = repoint_record(PURL_COLLECTION, purl, &map) .expect("repoint purl") .expect("purl changed"); let refs = rewritten["software"].as_array().expect("software refs"); assert_eq!(refs.len(), 1, "identical refs are deduped"); assert_eq!(refs[0]["cid"], PUT_CID); let cve = remote_record(&fixture.legacy, ATPROTO_COLLECTION, CVE_ID); let rewritten = repoint_record(ATPROTO_COLLECTION, cve, &map) .expect("repoint cve") .expect("cve changed"); assert_eq!(rewritten["createdAt"], cve["createdAt"]); assert_eq!(rewritten["nvd"], cve["nvd"]); let items = rewritten["software"].as_array().expect("cve software"); assert_eq!(items.len(), 1, "one canonical project ref per CVE"); assert_eq!( items[0]["project"]["uri"], project_uri(REPO, CANONICAL_SLUG) ); assert_eq!( items[0]["versions"][0]["uri"], canonical_version, "known release successors are repointed" ); assert_eq!( items[0]["channels"], cve["software"][0]["channels"], "unknown same-DID refs are preserved" ); assert_eq!(items[0]["vendor"], "Red Hat"); let status = remote_record(&fixture.legacy, STATUS_COLLECTION, "3status8"); let rewritten = repoint_record(STATUS_COLLECTION, status, &map) .expect("repoint status") .expect("status changed"); assert_eq!(rewritten["subject"], canonical_version); assert_eq!(rewritten["status"], "active"); assert_eq!( canonical_project_record(CANONICAL_SLUG), json!({"$type": PROJECT_COLLECTION, "name": CANONICAL_SLUG}) ); } #[tokio::test] async fn cross_did_refs_are_rejected() { let fixture = setup().await; let mut map = RemapMap::new(REPO); for slug in LEGACY_SLUGS { map.add_project( &legacy_project_uri(slug), StrongRef::new(project_uri(REPO, CANONICAL_SLUG), PUT_CID), ); } let software_uri = record_uri( REPO, SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", ); map.add_software(&software_uri, StrongRef::new(&software_uri, PUT_CID)); let mut software = remote_record( &fixture.legacy, SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", ) .clone(); software["project"]["uri"] = Value::String("at://did:plc:other/software.release.project/evil".to_owned()); let error = repoint_record(SOFTWARE_COLLECTION, &software, &map).expect_err("cross-DID"); assert!(error.to_string().contains("cross-DID"), "{error}"); let mut purl = remote_record(&fixture.legacy, PURL_COLLECTION, "purlhash8").clone(); purl["software"][0]["uri"] = Value::String("at://did:plc:other/com.nahdig.software/evil".to_owned()); let error = repoint_record(PURL_COLLECTION, &purl, &map).expect_err("cross-DID"); assert!(error.to_string().contains("cross-DID"), "{error}"); let mut cve = remote_record(&fixture.legacy, ATPROTO_COLLECTION, CVE_ID).clone(); cve["software"][0]["project"]["uri"] = Value::String("at://did:plc:other/software.release.project/evil".to_owned()); let error = repoint_record(ATPROTO_COLLECTION, &cve, &map).expect_err("cross-DID"); assert!(error.to_string().contains("cross-DID"), "{error}"); } #[tokio::test] async fn apply_rewrites_bodies_deletes_legacy_and_is_idempotent() { let fixture = setup().await; let plan = plan_for(&fixture).await; let snapshot_dir = fixture._dir.path().join("snapshot"); let report = apply_plan_with_snapshot( &fixture.publisher, &fixture.db, &plan, &ApplyOptions::default(), &snapshot_dir, ) .await .expect("apply"); let software_snapshot = std::fs::read_to_string(snapshot_dir.join(format!("{SOFTWARE_COLLECTION}.jsonl"))) .expect("software snapshot"); assert!( software_snapshot.contains(&legacy_project_uri("red-hat-red-hat-enterprise-linux-8")), "snapshot captures the pre-rewrite body" ); assert_eq!(report.created_projects, 0); assert_eq!(report.software_repoints, 3); assert_eq!(report.purl_repoints, 3); assert_eq!(report.cve_repoints, 1); assert_eq!(report.status_repoints, 3); assert_eq!(report.deleted, 12); assert_expected_records( &fixture.pds, &fixture.expected, "software", SOFTWARE_COLLECTION, ); assert_expected_records(&fixture.pds, &fixture.expected, "purl", PURL_COLLECTION); assert_expected_records(&fixture.pds, &fixture.expected, "cve", ATPROTO_COLLECTION); assert_expected_records(&fixture.pds, &fixture.expected, "status", STATUS_COLLECTION); assert!( fixture .pds .record(PROJECT_COLLECTION, CANONICAL_SLUG) .is_some() ); for uri in fixture.expected["deleted"].as_array().expect("deleted") { let uri = uri.as_str().expect("uri"); let (_, collection, rkey) = split_uri(uri).expect("delete uri"); assert!( fixture.pds.record(collection, rkey).is_none(), "{uri} still exists" ); } // Every repoint (software, purl, CVE, status) lands before the delete // batches, so no strongRef is left dangling during the rewrite. let calls = fixture.pds.calls(); let first_delete = calls .iter() .position(|call| call.method == "applyWrites") .expect("applyWrites"); assert!( calls[first_delete..] .iter() .all(|call| call.method == "applyWrites"), "repoints must finish before legacy release records are deleted" ); let before = fixture.pds.count("putRecord") + fixture.pds.count("applyWrites") + fixture.pds.count("deleteRecord"); let second = apply_plan( &fixture.publisher, &fixture.db, &plan, &ApplyOptions::default(), ) .await .expect("second apply"); assert_eq!(second.applied, 0); assert_eq!(second.deleted, 0); let after = fixture.pds.count("putRecord") + fixture.pds.count("applyWrites") + fixture.pds.count("deleteRecord"); assert_eq!(after, before, "second apply performs no writes"); } #[tokio::test] async fn verify_accepts_completed_migration_and_catches_corruption() { let fixture = setup().await; let plan = plan_for(&fixture).await; apply_plan( &fixture.publisher, &fixture.db, &plan, &ApplyOptions::default(), ) .await .expect("apply"); let report = verify_plan(&fixture.publisher, &fixture.db, &plan) .await .expect("verify"); assert!(report.ok(), "{:?}", report.errors); // Stale project CID on a rewritten software record. let mut software = remote_record( &fixture.legacy, SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", ) .clone(); software["project"] = json!({ "uri": project_uri(REPO, CANONICAL_SLUG), "cid": "bafystale", }); fixture.pds.seed_record( SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", software, ); // Legacy project backlink on the CVE. let mut cve = remote_record(&fixture.legacy, ATPROTO_COLLECTION, CVE_ID).clone(); cve["software"] = json!([{ "project": { "uri": legacy_project_uri("red-hat-red-hat-enterprise-linux-8"), "cid": PUT_CID, }, "part": "o", "vendor": "Red Hat", "product": "Red Hat Enterprise Linux 8" }]); fixture.pds.seed_record(ATPROTO_COLLECTION, CVE_ID, cve); // Status still names the deleted legacy version. fixture.pds.seed_record( STATUS_COLLECTION, "3status8", json!({ "$type": STATUS_COLLECTION, "subject": record_uri( REPO, VERSION_COLLECTION, "red-hat-red-hat-enterprise-linux-8:8.0" ), "status": "active", }), ); // Local link from a record outside the plan still points at a legacy project. fixture .db .replace_links( REPO, &record_uri(REPO, ATPROTO_COLLECTION, "CVE-9999"), ATPROTO_COLLECTION, "CVE-9999", Some(PUT_CID), &[Link { path: "software[0].project".to_owned(), subject_uri: legacy_project_uri("red-hat-red-hat-enterprise-linux-8"), subject_cid: Some(PUT_CID.to_owned()), }], ) .await .expect("seed backlink"); fixture.db.commit().await.expect("commit backlink"); let report = verify_plan(&fixture.publisher, &fixture.db, &plan) .await .expect("verify corruption"); assert!(!report.ok()); assert!(report.stale_refs >= 1); let errors = report.errors.join("\n"); assert!(errors.contains("stale cid"), "{errors}"); assert!(errors.contains("legacy project"), "{errors}"); assert!(errors.contains("still has a backlink"), "{errors}"); assert!(errors.contains("subject"), "{errors}"); } #[tokio::test] async fn apply_rejects_cross_did_software_ref() { let fixture = setup().await; let mut software = remote_record( &fixture.legacy, SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", ) .clone(); software["project"]["uri"] = Value::String("at://did:plc:other/software.release.project/evil".to_owned()); fixture.pds.seed_record( SOFTWARE_COLLECTION, "red-hat-red-hat-enterprise-linux-8", software, ); let plan = plan_for(&fixture).await; let error = apply_plan( &fixture.publisher, &fixture.db, &plan, &ApplyOptions::default(), ) .await .expect_err("cross-DID software ref"); assert!(error.to_string().contains("cross-DID"), "{error}"); }