1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! 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<FakePds>,
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}");
}