@@ -450,6 +450,41 @@ def get_public_val(self):
450
450
return self .keyval ["public" ]
451
451
452
452
453
+ class Role :
454
+ """A container class containing the set of keyids and threshold associated
455
+ with a particular role.
456
+
457
+ Attributes:
458
+ keyids: A set of strings each of which represents a given key.
459
+ threshold: An integer representing the required number of keys for that
460
+ particular role.
461
+ unrecognized_fields: Dictionary of all unrecognized fields.
462
+
463
+ """
464
+
465
+ def __init__ (
466
+ self ,
467
+ keyids : set ,
468
+ threshold : int ,
469
+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
470
+ ) -> None :
471
+ self .keyids = keyids
472
+ self .threshold = threshold
473
+ if unrecognized_fields is None :
474
+ unrecognized_fields = {}
475
+ self .unrecognized_fields = unrecognized_fields
476
+
477
+ def to_dict (self ) -> Dict :
478
+ """Returns the dictionary representation of self."""
479
+ res_dict = {
480
+ "keyids" : self .keyids ,
481
+ "threshold" : self .threshold ,
482
+ ** self .unrecognized_fields ,
483
+ }
484
+
485
+ return res_dict
486
+
487
+
453
488
class Root (Signed ):
454
489
"""A container for the signed part of root metadata.
455
490
@@ -465,10 +500,7 @@ class Root(Signed):
465
500
roles: A dictionary that contains a list of signing keyids and
466
501
a signature threshold for each top level role::
467
502
{
468
- '<ROLE>': {
469
- 'keyids': ['<SIGNING KEY KEYID>', ...],
470
- 'threshold': <SIGNATURE THRESHOLD>,
471
- },
503
+ '<ROLE>': <Role istance>,
472
504
...
473
505
}
474
506
@@ -486,13 +518,12 @@ def __init__(
486
518
expires : datetime ,
487
519
consistent_snapshot : bool ,
488
520
keys : Mapping [str , Key ],
489
- roles : Mapping [str , Any ],
521
+ roles : Mapping [str , Role ],
490
522
unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
491
523
) -> None :
492
524
super ().__init__ (
493
525
_type , version , spec_version , expires , unrecognized_fields
494
526
)
495
- # TODO: Add a class for roles
496
527
self .consistent_snapshot = consistent_snapshot
497
528
self .keys = keys
498
529
self .roles = roles
@@ -513,6 +544,13 @@ def from_dict(cls, root_dict: Mapping[str, Any]) -> "Root":
513
544
unrecognized_key_fields = key
514
545
keys [keyid ] = Key (keytype , scheme , keyval , unrecognized_key_fields )
515
546
547
+ for role_str , role_dict in roles .items ():
548
+ keyids = role_dict .pop ("keyids" )
549
+ threshold = role_dict .pop ("threshold" )
550
+ # All fields left in the role_dict are unrecognized.
551
+ unrecognized_role_fields = role_dict
552
+ roles [role_str ] = Role (keyids , threshold , unrecognized_role_fields )
553
+
516
554
# All fields left in the root_dict are unrecognized.
517
555
unrecognized_fields = root_dict
518
556
return cls (
@@ -525,12 +563,15 @@ def to_dict(self) -> Dict[str, Any]:
525
563
keys = {}
526
564
for keyid , key in self .keys .items ():
527
565
keys [keyid ] = key .to_dict ()
566
+ roles = {}
567
+ for role_str , role in self .roles .items ():
568
+ roles [role_str ] = role .to_dict ()
528
569
529
570
root_dict .update (
530
571
{
531
572
"consistent_snapshot" : self .consistent_snapshot ,
532
573
"keys" : keys ,
533
- "roles" : self . roles ,
574
+ "roles" : roles ,
534
575
}
535
576
)
536
577
return root_dict
@@ -540,17 +581,17 @@ def add_key(
540
581
self , role : str , keyid : str , key_metadata : Mapping [str , Any ]
541
582
) -> None :
542
583
"""Adds new key for 'role' and updates the key store. """
543
- if keyid not in self .roles [role ][ " keyids" ] :
544
- self .roles [role ][ " keyids" ] .append (keyid )
584
+ if keyid not in self .roles [role ]. keyids :
585
+ self .roles [role ]. keyids .append (keyid )
545
586
self .keys [keyid ] = key_metadata
546
587
547
588
# Remove key for a role.
548
589
def remove_key (self , role : str , keyid : str ) -> None :
549
590
"""Removes key for 'role' and updates the key store. """
550
- if keyid in self .roles [role ][ " keyids" ] :
551
- self .roles [role ][ " keyids" ] .remove (keyid )
591
+ if keyid in self .roles [role ]. keyids :
592
+ self .roles [role ]. keyids .remove (keyid )
552
593
for keyinfo in self .roles .values ():
553
- if keyid in keyinfo [ " keyids" ] :
594
+ if keyid in keyinfo . keyids :
554
595
return
555
596
556
597
del self .keys [keyid ]
0 commit comments