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
//! `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<String>,
}
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<VerifyReport> {
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<String> = BTreeSet::new();
let mut legacy_project_slugs: BTreeSet<String> = BTreeSet::new();
let mut legacy_project_uris: BTreeSet<String> = BTreeSet::new();
let mut deleted_uris: BTreeSet<String> = BTreeSet::new();
let mut managed_uris: BTreeSet<String> = 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<String> = 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<String> = 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)
}