//! `verify`: re-read the repository and assert the migration is complete. //! //! Verification is intentionally independent of apply's bookkeeping: it reads //! records back through `listRecords`, checks canonical project existence and //! strongRef CIDs, and inspects local backlinks on every legacy project that //! the plan deletes. use std::collections::BTreeSet; use anyhow::{Result, bail}; use serde::Serialize; use serde_json::Value; use super::{ ALL_COLLECTIONS, MigrationAction, MigrationPlan, RemoteInventory, collect_strong_refs, slug_of_project_uri, split_uri, }; use crate::constants::{ ATPROTO_COLLECTION, CHANNEL_COLLECTION, MANIFEST_COLLECTION, PROJECT_COLLECTION, PURL_COLLECTION, SOFTWARE_COLLECTION, STATUS_COLLECTION, VERSION_COLLECTION, }; use crate::db::DbHandle; use crate::pds::Publisher; #[derive(Debug, Clone, Default, Serialize)] pub struct VerifyReport { pub canonical_projects: usize, pub legacy_projects: usize, pub software_repoints: usize, pub purl_repoints: usize, pub cve_repoints: usize, pub status_repoints: usize, pub deletions: usize, pub stale_refs: usize, pub errors: Vec, } impl VerifyReport { pub fn ok(&self) -> bool { self.errors.is_empty() } } /// Verify a plan after (or before) apply. Errors are reported, not returned: /// the caller decides the exit code from [`VerifyReport::ok`]. pub async fn verify_plan( publisher: &Publisher, db: &DbHandle, plan: &MigrationPlan, ) -> Result { let repo = plan.repo_did.as_str(); if publisher.repo() != repo { bail!( "plan repo {repo} does not match session repo {}", publisher.repo() ); } let remote = RemoteInventory::load(publisher, ALL_COLLECTIONS).await?; let mut report = VerifyReport::default(); let mut canonical_slugs: BTreeSet = BTreeSet::new(); let mut legacy_project_slugs: BTreeSet = BTreeSet::new(); let mut legacy_project_uris: BTreeSet = BTreeSet::new(); let mut deleted_uris: BTreeSet = BTreeSet::new(); let mut managed_uris: BTreeSet = BTreeSet::new(); for entry in &plan.entries { if let Some(uri) = &entry.legacy_uri { managed_uris.insert(uri.clone()); } match entry.action { MigrationAction::CreateProject => { if let Some(slug) = &entry.canonical_slug { canonical_slugs.insert(slug.clone()); } } MigrationAction::DeleteReleaseRecord => { report.deletions += 1; if let Some(slug) = &entry.canonical_slug { canonical_slugs.insert(slug.clone()); } if let Some(slug) = &entry.legacy_slug { legacy_project_slugs.insert(slug.clone()); } if let Some(uri) = &entry.legacy_uri { deleted_uris.insert(uri.clone()); if split_uri(uri) .is_some_and(|(_, collection, _)| collection == PROJECT_COLLECTION) { legacy_project_uris.insert(uri.clone()); } } } MigrationAction::RepointSoftware => { report.software_repoints += 1; if let Some(slug) = &entry.canonical_slug { canonical_slugs.insert(slug.clone()); } } MigrationAction::RepointPurl => report.purl_repoints += 1, MigrationAction::RepointCve => { report.cve_repoints += 1; if let Some(slug) = &entry.canonical_slug { canonical_slugs.insert(slug.clone()); } } MigrationAction::RepointStatus => { report.status_repoints += 1; if let Some(slug) = &entry.canonical_slug { canonical_slugs.insert(slug.clone()); } } MigrationAction::NormalizePurlSlug => {} } } // A legacy project may only be referenced by records the migration itself // is repointing or deleting. for uri in &legacy_project_uris { let backlinks = db.backlinks(repo, uri, None, None, 1_000).await?; for link in backlinks { if !managed_uris.contains(&link.src_uri) { report.errors.push(format!( "legacy project {uri} still has a backlink from {}", link.src_uri )); } } } for slug in &canonical_slugs { match remote.find_rkey(PROJECT_COLLECTION, slug) { Some(record) => { report.canonical_projects += 1; if let Some(name) = record.value.get("name").and_then(|value| value.as_str()) && name != slug { report .errors .push(format!("canonical project {slug} has name {name}")); } } None => report .errors .push(format!("canonical project {slug} is missing")), } } let mut known_canonical: BTreeSet = remote .records(PROJECT_COLLECTION) .iter() .map(|record| record.rkey.clone()) .filter(|slug| !legacy_project_slugs.contains(slug)) .collect(); known_canonical.extend(canonical_slugs.iter().cloned()); // A repointed status must now name its canonical subject. for entry in plan .entries .iter() .filter(|entry| entry.action == MigrationAction::RepointStatus) { let (Some(uri), Some(canonical)) = (&entry.legacy_uri, &entry.canonical_uri) else { continue; }; let Some(record) = remote.find_uri(uri) else { report .errors .push(format!("repointed status record {uri} is missing")); continue; }; match record.value.get("subject").and_then(Value::as_str) { Some(subject) if subject == canonical => {} Some(subject) => report.errors.push(format!( "status {} subject {subject} != canonical {canonical}", record.rkey )), None => report .errors .push(format!("status {} has no subject", record.rkey)), } } // Deleted legacy records must be gone after apply. for uri in &deleted_uris { if remote.find_uri(uri).is_some() { report .errors .push(format!("legacy record {uri} still exists")); } } // Software must not keep a legacy project backlink. for record in remote.records(SOFTWARE_COLLECTION) { let Some(slug) = record .value .get("project") .and_then(|project| project.get("uri")) .and_then(Value::as_str) .and_then(slug_of_project_uri) else { continue; }; if legacy_project_slugs.contains(slug) || !known_canonical.contains(slug) { report.errors.push(format!( "software {} still points at legacy project {slug}", record.rkey )); } } // Release records and status subjects must be same-DID and canonical. for collection in [CHANNEL_COLLECTION, VERSION_COLLECTION, MANIFEST_COLLECTION] { for record in remote.records(collection) { let Some(project) = record.value.get("project").and_then(Value::as_str) else { continue; }; if let Some((did, _, _)) = split_uri(project) && did != repo { report .errors .push(format!("cross-DID reference {project} in {}", record.uri)); } if let Some(slug) = slug_of_project_uri(project) && (legacy_project_slugs.contains(slug) || !known_canonical.contains(slug)) { report.errors.push(format!( "{collection} {} still points at legacy project {slug}", record.rkey )); } } } for record in remote.records(STATUS_COLLECTION) { let Some(subject) = record.value.get("subject").and_then(Value::as_str) else { report .errors .push(format!("status {} has no subject", record.rkey)); continue; }; if let Some((did, _, _)) = split_uri(subject) && did != repo { report .errors .push(format!("cross-DID reference {subject} in {}", record.uri)); } if deleted_uris.contains(subject) { report.errors.push(format!( "status {} subject {subject} was deleted", record.rkey )); } if let Some(slug) = slug_of_project_uri(subject) { if legacy_project_slugs.contains(slug) || !known_canonical.contains(slug) { report.errors.push(format!( "status {} subject points at legacy project {slug}", record.rkey )); } } else if let Some((_, collection, rkey)) = split_uri(subject) && matches!(collection, CHANNEL_COLLECTION | VERSION_COLLECTION) && let Some((project, _)) = rkey.split_once(':') && (legacy_project_slugs.contains(project) || !known_canonical.contains(project)) { report.errors.push(format!( "status {} subject points at legacy project {project}", record.rkey )); } } for entry in plan .entries .iter() .filter(|entry| entry.action == MigrationAction::RepointCve) { let Some(uri) = &entry.legacy_uri else { continue; }; let Some(record) = remote.find_uri(uri) else { report .errors .push(format!("repointed CVE record {uri} is missing")); continue; }; let Some(software) = record .value .get("software") .and_then(|value| value.as_array()) else { report .errors .push(format!("CVE {} has no software refs", record.rkey)); continue; }; let mut seen: BTreeSet = BTreeSet::new(); for item in software { let Some(project_uri) = item .get("project") .and_then(|project| project.get("uri")) .and_then(|value| value.as_str()) else { report .errors .push(format!("CVE {} has an invalid project ref", record.rkey)); continue; }; let Some(slug) = slug_of_project_uri(project_uri) else { report.errors.push(format!( "CVE {} has a non-project ref {project_uri}", record.rkey )); continue; }; if legacy_project_slugs.contains(slug) || !known_canonical.contains(slug) { report.errors.push(format!( "CVE {} still points at legacy project {slug}", record.rkey )); } if !seen.insert(project_uri.to_owned()) { report.errors.push(format!( "CVE {} has duplicate refs for {project_uri}", record.rkey )); } } if seen.len() != 1 { report.errors.push(format!( "CVE {} has {} canonical project refs (expected 1)", record.rkey, seen.len() )); } } let mut refs = Vec::new(); for collection in [SOFTWARE_COLLECTION, PURL_COLLECTION, ATPROTO_COLLECTION] { for record in remote.records(collection) { collect_strong_refs(&record.value, &mut refs); } } for (uri, cid) in refs { let Some((did, collection, _)) = split_uri(&uri) else { continue; }; if did != repo && ALL_COLLECTIONS.contains(&collection) { report .errors .push(format!("cross-DID reference {uri} (expected {repo})")); continue; } if let Some(record) = remote.find_uri(&uri) && record.cid != cid { report.stale_refs += 1; report .errors .push(format!("stale cid for {uri}: {} != {cid}", record.cid)); } } Ok(report) }