Skip to content

Commit 519cf75

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 c7937b6 commit 519cf75

File tree

4 files changed

+82
-53
lines changed

4 files changed

+82
-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: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use chrono::offset::Utc;
22
use chrono::prelude::*;
33
use serde_derive::{Deserialize, Serialize};
44
use std::collections::{BTreeMap, HashSet};
5+
use std::convert::{TryFrom, TryInto};
56

67
use crate::crypto;
78
use crate::error::Error;
@@ -368,82 +369,109 @@ pub struct PublicKeyValue {
368369

369370
#[derive(Serialize, Deserialize)]
370371
pub struct Delegation {
371-
role: metadata::MetadataPath,
372+
name: metadata::MetadataPath,
372373
terminating: bool,
373374
threshold: u32,
374375
#[serde(rename = "keyids")]
375376
key_ids: Vec<crypto::KeyId>,
376377
paths: Vec<metadata::TargetPath>,
377378
}
378379

379-
impl Delegation {
380-
pub fn from(meta: &metadata::Delegation) -> Self {
381-
let mut paths = meta
380+
impl From<&metadata::Delegation> for Delegation {
381+
fn from(delegation: &metadata::Delegation) -> Self {
382+
let mut paths = delegation
382383
.paths()
383384
.iter()
384385
.cloned()
385386
.collect::<Vec<metadata::TargetPath>>();
386387
paths.sort();
387-
let mut key_ids = meta
388+
389+
let mut key_ids = delegation
388390
.key_ids()
389391
.iter()
390392
.cloned()
391393
.collect::<Vec<crypto::KeyId>>();
392394
key_ids.sort();
393395

394396
Delegation {
395-
role: meta.role().clone(),
396-
terminating: meta.terminating(),
397-
threshold: meta.threshold(),
397+
name: delegation.name().clone(),
398+
terminating: delegation.terminating(),
399+
threshold: delegation.threshold(),
398400
key_ids,
399401
paths,
400402
}
401403
}
404+
}
402405

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-
}
406+
impl TryFrom<Delegation> for metadata::Delegation {
407+
type Error = Error;
412408

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() {
409+
fn try_from(delegation: Delegation) -> Result<Self> {
410+
let delegation_key_ids_len = delegation.key_ids.len();
411+
let key_ids = delegation.key_ids.into_iter().collect::<HashSet<_>>();
412+
413+
if key_ids.len() != delegation_key_ids_len {
419414
return Err(Error::Encoding("Non-unique delegation key IDs.".into()));
420415
}
421416

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

426434
#[derive(Serialize, Deserialize)]
427435
pub struct Delegations {
428436
#[serde(deserialize_with = "deserialize_reject_duplicates::deserialize")]
429437
keys: BTreeMap<crypto::KeyId, crypto::PublicKey>,
430-
roles: Vec<metadata::Delegation>,
438+
roles: Vec<Delegation>,
431439
}
432440

433-
impl Delegations {
434-
pub fn from(delegations: &metadata::Delegations) -> Delegations {
441+
impl From<&metadata::Delegations> for Delegations {
442+
fn from(delegations: &metadata::Delegations) -> Delegations {
443+
let mut roles = delegations
444+
.roles()
445+
.iter()
446+
.map(Delegation::from)
447+
.collect::<Vec<Delegation>>();
448+
449+
// We want our roles in a consistent order.
450+
roles.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
451+
435452
Delegations {
436453
keys: delegations
437454
.keys()
438455
.iter()
439456
.map(|(id, key)| (id.clone(), key.clone()))
440457
.collect(),
441-
roles: delegations.roles().clone(),
458+
roles,
442459
}
443460
}
461+
}
462+
463+
impl TryFrom<Delegations> for metadata::Delegations {
464+
type Error = Error;
444465

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

tuf/src/metadata.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use serde::ser::{Error as SerializeError, Serialize, Serializer};
88
use serde_derive::{Deserialize, Serialize};
99
use std::borrow::{Borrow, Cow};
1010
use std::collections::{HashMap, HashSet};
11+
use std::convert::TryInto;
1112
use std::fmt::{self, Debug, Display};
1213
use std::marker::PhantomData;
1314
use std::str;
@@ -2043,7 +2044,7 @@ impl Delegations {
20432044
if roles.len()
20442045
!= roles
20452046
.iter()
2046-
.map(|r| &r.role)
2047+
.map(|r| &r.name)
20472048
.collect::<HashSet<&MetadataPath>>()
20482049
.len()
20492050
{
@@ -2087,7 +2088,7 @@ impl<'de> Deserialize<'de> for Delegations {
20872088
/// A delegated targets role.
20882089
#[derive(Debug, Clone, PartialEq, Eq)]
20892090
pub struct Delegation {
2090-
role: MetadataPath,
2091+
name: MetadataPath,
20912092
terminating: bool,
20922093
threshold: u32,
20932094
key_ids: HashSet<KeyId>,
@@ -2097,7 +2098,7 @@ pub struct Delegation {
20972098
impl Delegation {
20982099
/// Create a new delegation.
20992100
pub fn new(
2100-
role: MetadataPath,
2101+
name: MetadataPath,
21012102
terminating: bool,
21022103
threshold: u32,
21032104
key_ids: HashSet<KeyId>,
@@ -2122,7 +2123,7 @@ impl Delegation {
21222123
}
21232124

21242125
Ok(Delegation {
2125-
role,
2126+
name,
21262127
terminating,
21272128
threshold,
21282129
key_ids,
@@ -2131,8 +2132,8 @@ impl Delegation {
21312132
}
21322133

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

21382139
/// Whether or not this delegation is terminating.
@@ -3002,7 +3003,7 @@ mod test {
30023003
},
30033004
"roles": [
30043005
{
3005-
"role": "foo/bar",
3006+
"name": "foo/bar",
30063007
"terminating": false,
30073008
"threshold": 1,
30083009
"keyids": ["a9f3ebc9b138762563a9c27b6edd439959e559709babd123e8d449ba2c18c61a"],

0 commit comments

Comments
 (0)