Skip to content

Commit

Permalink
refactor: unify SnapshotMeta and TimestampMeta
Browse files Browse the repository at this point in the history
The `METAFILES` object contained by `snapshot.json` and by
`timestamp.json` is the same. This commit removes the duplicated
structures that were previously used to hold the same data.

`SnapshotMeta` and `TimestampMeta` are now replaced by `Metafiles`.

Signed-off-by: Flavio Castelli <fcastelli@suse.com>
  • Loading branch information
flavio committed Jul 10, 2024
1 parent 4474051 commit 33b059b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 43 deletions.
16 changes: 8 additions & 8 deletions tough/src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::key_source::KeySource;
use crate::schema::decoded::{Decoded, Hex};
use crate::schema::key::Key;
use crate::schema::{
Hashes, KeyHolder, PathSet, Role, RoleType, Root, Signed, Snapshot, SnapshotMeta, Target,
Targets, Timestamp, TimestampMeta,
Hashes, KeyHolder, Metafiles, PathSet, Role, RoleType, Root, Signed, Snapshot, Target, Targets,
Timestamp,
};
use crate::transport::{IntoVec, Transport};
use crate::{encode_filename, Limits};
Expand Down Expand Up @@ -700,13 +700,13 @@ impl RepositoryEditor {
Ok(snapshot)
}

/// Build a `SnapshotMeta` struct from a given `SignedRole<R>`. This metadata
/// Build a `Metafiles` struct from a given `SignedRole<R>`. This metadata
/// includes the sha256 and length of the signed role.
fn snapshot_meta<R>(role: &SignedRole<R>) -> SnapshotMeta
fn snapshot_meta<R>(role: &SignedRole<R>) -> Metafiles
where
R: Role,
{
SnapshotMeta {
Metafiles {
hashes: Some(Hashes {
sha256: role.sha256.to_vec().into(),
_extra: HashMap::new(),
Expand Down Expand Up @@ -738,13 +738,13 @@ impl RepositoryEditor {
Ok(timestamp)
}

/// Build a `TimestampMeta` struct from a given `SignedRole<R>`. This metadata
/// Build a `Metafiles` struct from a given `SignedRole<R>`. This metadata
/// includes the sha256 and length of the signed role.
fn timestamp_meta<R>(role: &SignedRole<R>) -> TimestampMeta
fn timestamp_meta<R>(role: &SignedRole<R>) -> Metafiles
where
R: Role,
{
TimestampMeta {
Metafiles {
hashes: Some(Hashes {
sha256: role.sha256.to_vec().into(),
_extra: HashMap::new(),
Expand Down
40 changes: 5 additions & 35 deletions tough/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ pub struct Snapshot {
/// Determines when metadata should be considered expired and no longer trusted by clients.
pub expires: DateTime<Utc>,

/// A list of what the TUF spec calls 'METAFILES' (`SnapshotMeta` objects). The TUF spec
/// A list of what the TUF spec calls 'METAFILES' (`Metafiles` objects). The TUF spec
/// describes the hash key in 4.4: METAPATH is the file path of the metadata on the repository
/// relative to the metadata base URL. For snapshot.json, these are top-level targets metadata
/// and delegated targets metadata.
pub meta: HashMap<String, SnapshotMeta>,
pub meta: HashMap<String, Metafiles>,

/// Extra arguments found during deserialization.
///
Expand All @@ -272,7 +272,7 @@ pub struct Snapshot {
pub _extra: HashMap<String, Value>,
}

/// Represents a metadata file in a `snapshot.json` file.
/// Represents a metadata file in a `snapshot.json` and in a `timestamp.json` file.
/// TUF 4.4: METAFILES is an object whose format is the following:
/// ```text
/// { METAPATH : {
Expand All @@ -292,7 +292,7 @@ pub struct Snapshot {
/// },
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
pub struct SnapshotMeta {
pub struct Metafiles {
/// LENGTH is the integer length in bytes of the metadata file at METAPATH. It is OPTIONAL and
/// can be omitted to reduce the snapshot metadata file size. In that case the client MUST use a
/// custom download limit for the listed metadata.
Expand Down Expand Up @@ -1112,7 +1112,7 @@ pub struct Timestamp {

/// METAFILES is the same as described for the snapshot.json file. In the case of the
/// timestamp.json file, this MUST only include a description of the snapshot.json file.
pub meta: HashMap<String, TimestampMeta>,
pub meta: HashMap<String, Metafiles>,

/// Extra arguments found during deserialization.
///
Expand All @@ -1124,36 +1124,6 @@ pub struct Timestamp {
pub _extra: HashMap<String, Value>,
}

/// METAFILES is the same as described for the snapshot.json file. In the case of the timestamp.json
/// file, this MUST only include a description of the snapshot.json file.
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
pub struct TimestampMeta {
/// The integer length in bytes of the snapshot.json file. It is OPTIONAL and
/// can be omitted to reduce the snapshot metadata file size. In that case the client MUST use a
/// custom download limit for the listed metadata.
#[serde(skip_serializing_if = "Option::is_none")]
pub length: Option<u64>,

/// The hashes of the snapshot.json file. HASHES
/// is OPTIONAL and can be omitted to reduce the snapshot metadata file size. In that case the
/// repository MUST guarantee that VERSION alone unambiguously identifies the metadata at
/// METAPATH.
#[serde(skip_serializing_if = "Option::is_none")]
pub hashes: Option<Hashes>,

/// An integer that is greater than 0. Clients MUST NOT replace a metadata file with a version
/// number less than the one currently trusted.
pub version: NonZeroU64,

/// Extra arguments found during deserialization.
///
/// We must store these to correctly verify signatures for this object.
///
/// If you're instantiating this struct, you should make this `HashMap::empty()`.
#[serde(flatten)]
pub _extra: HashMap<String, Value>,
}

impl Timestamp {
/// Creates a new `Timestamp` object.
pub fn new(spec_version: String, version: NonZeroU64, expires: DateTime<Utc>) -> Self {
Expand Down

0 comments on commit 33b059b

Please sign in to comment.