feat: improvement
This commit is contained in:
18 files changed
+2538
-108
No files matched your search
@@ -124,6 +124,26 @@ pub struct SealMeta {
|
||||
/// from the file's own contents; [`SealFile::verify`] rebuilds a seal from the
|
||||
/// live course and compares this against it.
|
||||
pub digest: String,
|
||||
|
||||
/// The digests this file had before it was rewritten, oldest first.
|
||||
///
|
||||
/// A seal is meant to be tamper-evident, which puts a migration that
|
||||
/// renames an id inside it in an awkward position: the id is part of
|
||||
/// [`SealFile::digest_input`], so rewriting the file invalidates the digest
|
||||
/// that vouched for it, and recomputing it quietly would produce a record
|
||||
/// that looks untouched and is not.
|
||||
///
|
||||
/// So a migration does both — recomputes the digest and leaves the old one
|
||||
/// here. What the seal then says is the honest thing: these are the
|
||||
/// contents, this is what they hash to, and here is what the file hashed to
|
||||
/// before each rewrite. Anyone holding an earlier copy, a backup or a git
|
||||
/// revision, can check it against the right entry.
|
||||
///
|
||||
/// Deliberately outside [`SealFile::digest_input`]: a field that recorded
|
||||
/// past digests and was itself covered by the current one could not be
|
||||
/// appended to without invalidating what it describes.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub superseded_digests: Vec<String>,
|
||||
}
|
||||
|
||||
/// What wrote a seal.
|
||||
@@ -458,6 +478,7 @@ pub fn build(catalog: &Catalog, record: &AssessmentFile, opts: &Options) -> Resu
|
||||
content: opts.content,
|
||||
dropped,
|
||||
digest: String::new(),
|
||||
superseded_digests: Vec::new(),
|
||||
},
|
||||
items,
|
||||
forms: sealed_forms,
|
||||
@@ -787,6 +808,39 @@ impl SealFile {
|
||||
buf
|
||||
}
|
||||
|
||||
/// Renames the items this seal froze, keeping the digest honest.
|
||||
///
|
||||
/// The only sanctioned way to rewrite a seal. It recomputes the digest,
|
||||
/// because the ids are part of what the digest covers, and records the
|
||||
/// previous one in [`SealMeta::superseded_digests`], because a recomputed
|
||||
/// digest with no trace of the recompute is a record that claims never to
|
||||
/// have been touched.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `rename` - a map from old item id to new.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// How many placements were renamed. Zero leaves the file alone, digest
|
||||
/// included.
|
||||
pub fn rename_items(&mut self, rename: &BTreeMap<String, String>) -> usize {
|
||||
let mut renamed = 0;
|
||||
for item in &mut self.items {
|
||||
if let Some(new) = rename.get(&item.item) {
|
||||
item.item = new.clone();
|
||||
renamed += 1;
|
||||
}
|
||||
}
|
||||
if renamed == 0 {
|
||||
return 0;
|
||||
}
|
||||
let previous = std::mem::take(&mut self.seal.digest);
|
||||
self.seal.superseded_digests.push(previous);
|
||||
self.seal.digest = self.recompute_digest();
|
||||
renamed
|
||||
}
|
||||
|
||||
/// Recomputes this file's digest from its own contents.
|
||||
///
|
||||
/// # Returns
|
||||
@@ -1195,6 +1249,7 @@ mod tests {
|
||||
content: true,
|
||||
dropped: Vec::new(),
|
||||
digest: String::new(),
|
||||
superseded_digests: Vec::new(),
|
||||
},
|
||||
items: vec![SealedItem {
|
||||
number: 1,
|
||||
@@ -1343,6 +1398,31 @@ mod tests {
|
||||
assert_eq!(short("sha256:0123456789abcdef"), "sha256:01234567");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renaming_an_item_recomputes_the_digest_and_says_it_did() {
|
||||
let mut seal = sample();
|
||||
seal.seal.digest = seal.recompute_digest();
|
||||
let original = seal.seal.digest.clone();
|
||||
assert!(seal.check_self().is_empty());
|
||||
|
||||
let mut rename = BTreeMap::new();
|
||||
rename.insert(seal.items[0].item.clone(), "q-renamed".to_string());
|
||||
assert_eq!(seal.rename_items(&rename), 1);
|
||||
|
||||
// The digest covers the ids, so it has to move — and the file has to
|
||||
// admit that it moved rather than looking untouched.
|
||||
assert_eq!(seal.items[0].item, "q-renamed");
|
||||
assert_ne!(seal.seal.digest, original);
|
||||
assert_eq!(seal.seal.superseded_digests, vec![original]);
|
||||
assert!(seal.check_self().is_empty(), "{:?}", seal.check_self());
|
||||
|
||||
// A rename that matches nothing leaves the file entirely alone.
|
||||
let steady = seal.seal.digest.clone();
|
||||
assert_eq!(seal.rename_items(&BTreeMap::new()), 0);
|
||||
assert_eq!(seal.seal.digest, steady);
|
||||
assert_eq!(seal.seal.superseded_digests.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_yaml() {
|
||||
let file = sample();
|
||||
|
||||
Reference in new issue
Block a user