refactor timestamp metadata
diff --git a/src/client.rs b/src/client.rs
index 033bc31..32fa253 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -169,16 +169,7 @@
T: Repository<D>,
{
let snapshot_description = match tuf.timestamp() {
- Some(ts) => {
- match ts.meta().get(&MetadataPath::from_role(&Role::Snapshot)) {
- Some(d) => Ok(d),
- None => Err(Error::VerificationFailure(
- "Timestamp metadata did not contain a description of the \
- current snapshot metadata."
- .into(),
- )),
- }
- }
+ Some(ts) => Ok(ts.snapshot()),
None => Err(Error::MissingMetadata(Role::Timestamp)),
}?
.clone();
diff --git a/src/metadata.rs b/src/metadata.rs
index 3ef2718..c92126f 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -637,7 +637,7 @@
pub struct TimestampMetadata {
version: u32,
expires: DateTime<Utc>,
- meta: HashMap<MetadataPath, MetadataDescription>,
+ snapshot: MetadataDescription,
}
impl TimestampMetadata {
@@ -645,7 +645,7 @@
pub fn new(
version: u32,
expires: DateTime<Utc>,
- meta: HashMap<MetadataPath, MetadataDescription>,
+ snapshot: MetadataDescription,
) -> Result<Self> {
if version < 1 {
return Err(Error::IllegalArgument(format!(
@@ -657,7 +657,7 @@
Ok(TimestampMetadata {
version: version,
expires: expires,
- meta: meta,
+ snapshot: snapshot,
})
}
@@ -671,9 +671,9 @@
&self.expires
}
- /// An immutable reference to the metadata paths and descriptions.
- pub fn meta(&self) -> &HashMap<MetadataPath, MetadataDescription> {
- &self.meta
+ /// An immutable reference to the snapshot description.
+ pub fn snapshot(&self) -> &MetadataDescription {
+ &self.snapshot
}
}
@@ -1502,27 +1502,22 @@
let timestamp = TimestampMetadata::new(
1,
Utc.ymd(2017, 1, 1).and_hms(0, 0, 0),
- hashmap!{
- MetadataPath::new("foo".into()).unwrap() =>
- MetadataDescription::new(
- 1,
- 100,
- hashmap! { HashAlgorithm::Sha256 => HashValue::new(vec![]) }
- ).unwrap(),
- },
+ MetadataDescription::new(
+ 1,
+ 100,
+ hashmap! { HashAlgorithm::Sha256 => HashValue::new(vec![]) }
+ ).unwrap(),
).unwrap();
let jsn = json!({
"type": "timestamp",
"version": 1,
"expires": "2017-01-01T00:00:00Z",
- "meta": {
- "foo": {
- "version": 1,
- "size": 100,
- "hashes": {
- "sha256": "",
- },
+ "snapshot": {
+ "version": 1,
+ "size": 100,
+ "hashes": {
+ "sha256": "",
},
},
});
diff --git a/src/shims.rs b/src/shims.rs
index 72da19f..6a9541d 100644
--- a/src/shims.rs
+++ b/src/shims.rs
@@ -140,7 +140,7 @@
typ: metadata::Role,
version: u32,
expires: DateTime<Utc>,
- meta: HashMap<metadata::MetadataPath, metadata::MetadataDescription>,
+ snapshot: metadata::MetadataDescription,
}
impl TimestampMetadata {
@@ -149,7 +149,7 @@
typ: metadata::Role::Timestamp,
version: metadata.version(),
expires: metadata.expires().clone(),
- meta: metadata.meta().clone(),
+ snapshot: metadata.snapshot().clone(),
})
}
@@ -161,7 +161,7 @@
)));
}
- metadata::TimestampMetadata::new(self.version, self.expires, self.meta)
+ metadata::TimestampMetadata::new(self.version, self.expires, self.snapshot)
}
}
diff --git a/src/tuf.rs b/src/tuf.rs
index 36793e9..3020aa0 100644
--- a/src/tuf.rs
+++ b/src/tuf.rs
@@ -161,24 +161,15 @@
let snapshot = {
let root = self.safe_root_ref()?;
let timestamp = self.safe_timestamp_ref()?;
- let snapshot_description = timestamp
- .meta()
- .get(&MetadataPath::from_role(&Role::Snapshot))
- .ok_or_else(|| {
- Error::VerificationFailure(
- "Timestamp metadata had no description of the snapshot metadata".into(),
- )
- })?;
-
let current_version = self.snapshot.as_ref().map(|t| t.version()).unwrap_or(0);
- if snapshot_description.version() < current_version {
+ if timestamp.snapshot().version() < current_version {
return Err(Error::VerificationFailure(format!(
"Attempted to roll back snapshot metadata at version {} to {}.",
current_version,
- snapshot_description.version()
+ timestamp.snapshot().version()
)));
- } else if snapshot_description.version() == current_version {
+ } else if timestamp.snapshot().version() == current_version {
return Ok(false);
}
@@ -190,11 +181,11 @@
let snapshot: SnapshotMetadata = D::deserialize(&signed_snapshot.signed())?;
- if snapshot.version() != snapshot_description.version() {
+ if snapshot.version() != timestamp.snapshot().version() {
return Err(Error::VerificationFailure(format!(
"The timestamp metadata reported that the snapshot metadata should be at \
version {} but version {} was found instead.",
- snapshot_description.version(),
+ timestamp.snapshot().version(),
snapshot.version()
)));
}
diff --git a/tests/integration.rs b/tests/integration.rs
index b7ae23d..bbd7cf1 100644
--- a/tests/integration.rs
+++ b/tests/integration.rs
@@ -1,9 +1,11 @@
extern crate chrono;
+#[macro_use]
+extern crate maplit;
extern crate tuf;
use chrono::prelude::*;
use chrono::offset::Utc;
-use std::collections::{HashSet, HashMap};
+use std::collections::HashMap;
use tuf::Tuf;
use tuf::crypto::{PrivateKey, SignatureScheme, HashAlgorithm};
use tuf::interchange::JsonDataInterchange;
@@ -34,21 +36,10 @@
timestamp_key.public().clone(),
];
- let mut key_ids = HashSet::new();
- key_ids.insert(root_key.key_id().clone());
- let root_def = RoleDefinition::new(1, key_ids).unwrap();
-
- let mut key_ids = HashSet::new();
- key_ids.insert(snapshot_key.key_id().clone());
- let snapshot_def = RoleDefinition::new(1, key_ids).unwrap();
-
- let mut key_ids = HashSet::new();
- key_ids.insert(targets_key.key_id().clone());
- let targets_def = RoleDefinition::new(1, key_ids).unwrap();
-
- let mut key_ids = HashSet::new();
- key_ids.insert(timestamp_key.key_id().clone());
- let timestamp_def = RoleDefinition::new(1, key_ids).unwrap();
+ let root_def = RoleDefinition::new(1, hashset!(root_key.key_id().clone())).unwrap();
+ let snapshot_def = RoleDefinition::new(1, hashset!(snapshot_key.key_id().clone())).unwrap();
+ let targets_def = RoleDefinition::new(1, hashset!(targets_key.key_id().clone())).unwrap();
+ let timestamp_def = RoleDefinition::new(1, hashset!(timestamp_key.key_id().clone())).unwrap();
let root = RootMetadata::new(
1,
@@ -71,11 +62,8 @@
Tuf::<JsonDataInterchange>::from_root_pinned(signed, &[root_key.key_id().clone()]).unwrap();
//// build the timestamp ////
- let mut meta_map = HashMap::new();
- let path = MetadataPath::new("snapshot".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
- let timestamp = TimestampMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), meta_map)
+ let snap = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
+ let timestamp = TimestampMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), snap)
.unwrap();
let signed = SignedMetadata::<JsonDataInterchange, TimestampMetadata>::new(
@@ -87,13 +75,12 @@
tuf.update_timestamp(signed).unwrap();
//// build the snapshot ////
- let mut meta_map = HashMap::new();
- let path = MetadataPath::new("targets".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
- let path = MetadataPath::new("delegation".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
+ let meta_map = hashmap! {
+ MetadataPath::new("targets".into()).unwrap() =>
+ MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap(),
+ MetadataPath::new("delegation".into()).unwrap() =>
+ MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap(),
+ };
let snapshot = SnapshotMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), meta_map)
.unwrap();
@@ -141,12 +128,10 @@
//// build the delegation ////
let target_file: &[u8] = b"bar";
- let target_path = TargetPath::new("foo".into()).unwrap();
- let target_description = TargetDescription::from_reader(target_file, &[HashAlgorithm::Sha256])
- .unwrap();
-
- let mut target_map = HashMap::new();
- let _ = target_map.insert(target_path, target_description);
+ let target_map = hashmap! {
+ TargetPath::new("foo".into()).unwrap() =>
+ TargetDescription::from_reader(target_file, &[HashAlgorithm::Sha256]).unwrap(),
+ };
let delegation =
TargetsMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), target_map, None).unwrap();
@@ -182,21 +167,10 @@
timestamp_key.public().clone(),
];
- let mut key_ids = HashSet::new();
- key_ids.insert(root_key.key_id().clone());
- let root_def = RoleDefinition::new(1, key_ids).unwrap();
-
- let mut key_ids = HashSet::new();
- key_ids.insert(snapshot_key.key_id().clone());
- let snapshot_def = RoleDefinition::new(1, key_ids).unwrap();
-
- let mut key_ids = HashSet::new();
- key_ids.insert(targets_key.key_id().clone());
- let targets_def = RoleDefinition::new(1, key_ids).unwrap();
-
- let mut key_ids = HashSet::new();
- key_ids.insert(timestamp_key.key_id().clone());
- let timestamp_def = RoleDefinition::new(1, key_ids).unwrap();
+ let root_def = RoleDefinition::new(1, hashset!(root_key.key_id().clone())).unwrap();
+ let snapshot_def = RoleDefinition::new(1, hashset!(snapshot_key.key_id().clone())).unwrap();
+ let targets_def = RoleDefinition::new(1, hashset!(targets_key.key_id().clone())).unwrap();
+ let timestamp_def = RoleDefinition::new(1, hashset!(timestamp_key.key_id().clone())).unwrap();
let root = RootMetadata::new(
1,
@@ -219,11 +193,8 @@
Tuf::<JsonDataInterchange>::from_root_pinned(signed, &[root_key.key_id().clone()]).unwrap();
//// build the timestamp ////
- let mut meta_map = HashMap::new();
- let path = MetadataPath::new("snapshot".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
- let timestamp = TimestampMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), meta_map)
+ let snap = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
+ let timestamp = TimestampMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), snap)
.unwrap();
let signed = SignedMetadata::<JsonDataInterchange, TimestampMetadata>::new(
@@ -235,16 +206,14 @@
tuf.update_timestamp(signed).unwrap();
//// build the snapshot ////
- let mut meta_map = HashMap::new();
- let path = MetadataPath::new("targets".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
- let path = MetadataPath::new("delegation-a".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
- let path = MetadataPath::new("delegation-b".into()).unwrap();
- let desc = MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap();
- let _ = meta_map.insert(path, desc);
+ let meta_map = hashmap! {
+ MetadataPath::new("targets".into()).unwrap() =>
+ MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap(),
+ MetadataPath::new("delegation-a".into()).unwrap() =>
+ MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap(),
+ MetadataPath::new("delegation-b".into()).unwrap() =>
+ MetadataDescription::from_reader(&*vec![0u8], 1, &[HashAlgorithm::Sha256]).unwrap(),
+ };
let snapshot = SnapshotMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), meta_map)
.unwrap();
@@ -327,12 +296,10 @@
//// build delegation B ////
let target_file: &[u8] = b"bar";
- let target_path = TargetPath::new("foo".into()).unwrap();
- let target_description = TargetDescription::from_reader(target_file, &[HashAlgorithm::Sha256])
- .unwrap();
-
- let mut target_map = HashMap::new();
- let _ = target_map.insert(target_path, target_description);
+ let target_map = hashmap! {
+ TargetPath::new("foo".into()).unwrap() =>
+ TargetDescription::from_reader(target_file, &[HashAlgorithm::Sha256]).unwrap(),
+ };
let delegation =
TargetsMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), target_map, None).unwrap();
diff --git a/tests/simple_example.rs b/tests/simple_example.rs
index b9cac7b..0dd3172 100644
--- a/tests/simple_example.rs
+++ b/tests/simple_example.rs
@@ -167,12 +167,8 @@
JsonDataInterchange::canonicalize(&JsonDataInterchange::serialize(&signed)?)?;
//// build the timestamp ////
- let meta_map =
- hashmap! {
- MetadataPath::new("snapshot".into())? =>
- MetadataDescription::from_reader(&*snapshot_bytes, 1, &[HashAlgorithm::Sha256])?,
- };
- let timestamp = TimestampMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), meta_map)?;
+ let snap = MetadataDescription::from_reader(&*snapshot_bytes, 1, &[HashAlgorithm::Sha256])?;
+ let timestamp = TimestampMetadata::new(1, Utc.ymd(2038, 1, 1).and_hms(0, 0, 0), snap)?;
let signed = SignedMetadata::<JsonDataInterchange, TimestampMetadata>::new(
×tamp,