Skip to content

Commit c844449

Browse files
committed
Delegations have a field named name, not role
For reference, see the [spec]. [spec]: https://theupdateframework.github.io/specification/latest/#delegations
1 parent 1fc4def commit c844449

File tree

4 files changed

+80
-53
lines changed

4 files changed

+80
-53
lines changed

tuf/src/client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ where
10141014
}
10151015
}
10161016

1017-
let role_meta = match snapshot.meta().get(delegation.role()) {
1017+
let role_meta = match snapshot.meta().get(delegation.name()) {
10181018
Some(m) => m,
10191019
None if delegation.terminating() => {
10201020
return (true, Err(Error::TargetNotFound(target.clone())));
@@ -1053,12 +1053,12 @@ where
10531053

10541054
let raw_signed_meta = match self
10551055
.remote
1056-
.fetch_metadata(delegation.role(), version, role_length, role_hashes)
1056+
.fetch_metadata(delegation.name(), version, role_length, role_hashes)
10571057
.await
10581058
{
10591059
Ok(m) => m,
10601060
Err(e) => {
1061-
warn!("Failed to fetch metadata {:?}: {:?}", delegation.role(), e);
1061+
warn!("Failed to fetch metadata {:?}: {:?}", delegation.name(), e);
10621062
if delegation.terminating() {
10631063
return (true, Err(e));
10641064
} else {
@@ -1070,7 +1070,7 @@ where
10701070
match self.tuf.update_delegated_targets(
10711071
start_time,
10721072
&targets_role,
1073-
delegation.role(),
1073+
delegation.name(),
10741074
&raw_signed_meta,
10751075
) {
10761076
Ok(_) => {
@@ -1082,14 +1082,14 @@ where
10821082

10831083
match self
10841084
.local
1085-
.store_metadata(delegation.role(), MetadataVersion::None, &raw_signed_meta)
1085+
.store_metadata(delegation.name(), MetadataVersion::None, &raw_signed_meta)
10861086
.await
10871087
{
10881088
Ok(_) => (),
10891089
Err(e) => {
10901090
warn!(
10911091
"Error storing metadata {:?} locally: {:?}",
1092-
delegation.role(),
1092+
delegation.name(),
10931093
e
10941094
)
10951095
}
@@ -1098,7 +1098,7 @@ where
10981098
let meta = self
10991099
.tuf
11001100
.trusted_delegations()
1101-
.get(delegation.role())
1101+
.get(delegation.name())
11021102
.unwrap()
11031103
.clone();
11041104
let f: Pin<Box<dyn Future<Output = _>>> =
@@ -1108,7 +1108,7 @@ where
11081108
current_depth + 1,
11091109
target,
11101110
snapshot,
1111-
Some((&meta, delegation.role().clone())),
1111+
Some((&meta, delegation.name().clone())),
11121112
));
11131113
let (term, res) = f.await;
11141114

tuf/src/database.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ impl<D: DataInterchange> Database<D> {
850850
};
851851

852852
for trusted_delegation in trusted_delegations.roles() {
853-
if trusted_delegation.role() != role {
853+
if trusted_delegation.name() != role {
854854
continue;
855855
}
856856

@@ -898,21 +898,21 @@ impl<D: DataInterchange> Database<D> {
898898
return Ok(d.clone());
899899
}
900900

901-
fn lookup<D: DataInterchange>(
901+
fn lookup<'a, D: DataInterchange>(
902902
start_time: &DateTime<Utc>,
903-
tuf: &Database<D>,
903+
tuf: &'a Database<D>,
904904
default_terminate: bool,
905905
current_depth: u32,
906906
target_path: &TargetPath,
907-
delegations: &Delegations,
907+
delegations: &'a Delegations,
908908
parents: &[HashSet<TargetPath>],
909-
visited: &mut HashSet<MetadataPath>,
909+
visited: &mut HashSet<&'a MetadataPath>,
910910
) -> (bool, Option<TargetDescription>) {
911911
for delegation in delegations.roles() {
912-
if visited.contains(delegation.role()) {
912+
if visited.contains(delegation.name()) {
913913
return (delegation.terminating(), None);
914914
}
915-
let _ = visited.insert(delegation.role().clone());
915+
let _ = visited.insert(delegation.name());
916916

917917
let mut new_parents = parents.to_owned();
918918
new_parents.push(delegation.paths().clone());
@@ -921,7 +921,7 @@ impl<D: DataInterchange> Database<D> {
921921
return (delegation.terminating(), None);
922922
}
923923

924-
let trusted_delegation = match tuf.trusted_delegations.get(delegation.role()) {
924+
let trusted_delegation = match tuf.trusted_delegations.get(delegation.name()) {
925925
Some(trusted_delegation) => trusted_delegation,
926926
None => return (delegation.terminating(), None),
927927
};

tuf/src/interchange/cjson/shims.rs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -368,82 +368,109 @@ pub struct PublicKeyValue {
368368

369369
#[derive(Serialize, Deserialize)]
370370
pub struct Delegation {
371-
role: metadata::MetadataPath,
371+
name: metadata::MetadataPath,
372372
terminating: bool,
373373
threshold: u32,
374374
#[serde(rename = "keyids")]
375375
key_ids: Vec<crypto::KeyId>,
376376
paths: Vec<metadata::TargetPath>,
377377
}
378378

379-
impl Delegation {
380-
pub fn from(meta: &metadata::Delegation) -> Self {
381-
let mut paths = meta
379+
impl From<&metadata::Delegation> for Delegation {
380+
fn from(delegation: &metadata::Delegation) -> Self {
381+
let mut paths = delegation
382382
.paths()
383383
.iter()
384384
.cloned()
385385
.collect::<Vec<metadata::TargetPath>>();
386386
paths.sort();
387-
let mut key_ids = meta
387+
388+
let mut key_ids = delegation
388389
.key_ids()
389390
.iter()
390391
.cloned()
391392
.collect::<Vec<crypto::KeyId>>();
392393
key_ids.sort();
393394

394395
Delegation {
395-
role: meta.role().clone(),
396-
terminating: meta.terminating(),
397-
threshold: meta.threshold(),
396+
name: delegation.name().clone(),
397+
terminating: delegation.terminating(),
398+
threshold: delegation.threshold(),
398399
key_ids,
399400
paths,
400401
}
401402
}
403+
}
402404

403-
pub fn try_into(self) -> Result<metadata::Delegation> {
404-
let paths = self
405-
.paths
406-
.iter()
407-
.cloned()
408-
.collect::<HashSet<metadata::TargetPath>>();
409-
if paths.len() != self.paths.len() {
410-
return Err(Error::Encoding("Non-unique delegation paths.".into()));
411-
}
405+
impl TryFrom<Delegation> for metadata::Delegation {
406+
type Error = Error;
412407

413-
let key_ids = self
414-
.key_ids
415-
.iter()
416-
.cloned()
417-
.collect::<HashSet<crypto::KeyId>>();
418-
if key_ids.len() != self.key_ids.len() {
408+
fn try_from(delegation: Delegation) -> Result<Self> {
409+
let delegation_key_ids_len = delegation.key_ids.len();
410+
let key_ids = delegation.key_ids.into_iter().collect::<HashSet<_>>();
411+
412+
if key_ids.len() != delegation_key_ids_len {
419413
return Err(Error::Encoding("Non-unique delegation key IDs.".into()));
420414
}
421415

422-
metadata::Delegation::new(self.role, self.terminating, self.threshold, key_ids, paths)
416+
let delegation_paths_len = delegation.paths.len();
417+
let paths = delegation.paths.into_iter().collect::<HashSet<_>>();
418+
419+
if paths.len() != delegation_paths_len {
420+
return Err(Error::Encoding("Non-unique delegation paths.".into()));
421+
}
422+
423+
metadata::Delegation::new(
424+
delegation.name,
425+
delegation.terminating,
426+
delegation.threshold,
427+
key_ids,
428+
paths,
429+
)
423430
}
424431
}
425432

426433
#[derive(Serialize, Deserialize)]
427434
pub struct Delegations {
428435
#[serde(deserialize_with = "deserialize_reject_duplicates::deserialize")]
429436
keys: BTreeMap<crypto::KeyId, crypto::PublicKey>,
430-
roles: Vec<metadata::Delegation>,
437+
roles: Vec<Delegation>,
431438
}
432439

433-
impl Delegations {
434-
pub fn from(delegations: &metadata::Delegations) -> Delegations {
440+
impl From<&metadata::Delegations> for Delegations {
441+
fn from(delegations: &metadata::Delegations) -> Delegations {
442+
let mut roles = delegations
443+
.roles()
444+
.iter()
445+
.map(Delegation::from)
446+
.collect::<Vec<Delegation>>();
447+
448+
// We want our roles in a consistent order.
449+
roles.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
450+
435451
Delegations {
436452
keys: delegations
437453
.keys()
438454
.iter()
439455
.map(|(id, key)| (id.clone(), key.clone()))
440456
.collect(),
441-
roles: delegations.roles().clone(),
457+
roles,
442458
}
443459
}
460+
}
461+
462+
impl TryFrom<Delegations> for metadata::Delegations {
463+
type Error = Error;
444464

445-
pub fn try_into(self) -> Result<metadata::Delegations> {
446-
metadata::Delegations::new(self.keys.into_iter().collect(), self.roles)
465+
fn try_from(delegations: Delegations) -> Result<metadata::Delegations> {
466+
metadata::Delegations::new(
467+
delegations.keys.into_iter().collect(),
468+
delegations
469+
.roles
470+
.into_iter()
471+
.map(|delegation| delegation.try_into())
472+
.collect::<Result<Vec<_>>>()?,
473+
)
447474
}
448475
}
449476

tuf/src/metadata.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ impl Delegations {
20432043
if roles.len()
20442044
!= roles
20452045
.iter()
2046-
.map(|r| &r.role)
2046+
.map(|r| &r.name)
20472047
.collect::<HashSet<&MetadataPath>>()
20482048
.len()
20492049
{
@@ -2087,7 +2087,7 @@ impl<'de> Deserialize<'de> for Delegations {
20872087
/// A delegated targets role.
20882088
#[derive(Debug, Clone, PartialEq, Eq)]
20892089
pub struct Delegation {
2090-
role: MetadataPath,
2090+
name: MetadataPath,
20912091
terminating: bool,
20922092
threshold: u32,
20932093
key_ids: HashSet<KeyId>,
@@ -2097,7 +2097,7 @@ pub struct Delegation {
20972097
impl Delegation {
20982098
/// Create a new delegation.
20992099
pub fn new(
2100-
role: MetadataPath,
2100+
name: MetadataPath,
21012101
terminating: bool,
21022102
threshold: u32,
21032103
key_ids: HashSet<KeyId>,
@@ -2122,7 +2122,7 @@ impl Delegation {
21222122
}
21232123

21242124
Ok(Delegation {
2125-
role,
2125+
name,
21262126
terminating,
21272127
threshold,
21282128
key_ids,
@@ -2131,8 +2131,8 @@ impl Delegation {
21312131
}
21322132

21332133
/// An immutable reference to the delegations's metadata path (role).
2134-
pub fn role(&self) -> &MetadataPath {
2135-
&self.role
2134+
pub fn name(&self) -> &MetadataPath {
2135+
&self.name
21362136
}
21372137

21382138
/// Whether or not this delegation is terminating.
@@ -3002,7 +3002,7 @@ mod test {
30023002
},
30033003
"roles": [
30043004
{
3005-
"role": "foo/bar",
3005+
"name": "foo/bar",
30063006
"terminating": false,
30073007
"threshold": 1,
30083008
"keyids": ["a9f3ebc9b138762563a9c27b6edd439959e559709babd123e8d449ba2c18c61a"],

0 commit comments

Comments
 (0)