Skip to content

Commit 38b6d44

Browse files
Jussi Kukkonenjoshuagl
Jussi Kukkonen
andcommitted
Metadata API: Rewrite comments
Try to keep dostrings and comments to the point, avoid mentioning details if they are not necessary or are likely to become outdated and try to minimize number of comment lines. Co-authored-by: Joshua Lock <[email protected]> Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent 39ed706 commit 38b6d44

File tree

1 file changed

+53
-112
lines changed

1 file changed

+53
-112
lines changed

tuf/api/metadata.py

+53-112
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646
SignedSerializer,
4747
)
4848

49-
# Disable the "C0302: Too many lines in module" warning which warns for modules
50-
# with more 1000 lines, because all of the code here is logically connected
51-
# and currently, we are above 1000 lines by a small margin.
52-
# pylint: disable=C0302
49+
# pylint: disable=too-many-lines
5350

5451
# We aim to support SPECIFICATION_VERSION and require the input metadata
5552
# files to have the same major version (the first number) as ours.
@@ -65,7 +62,6 @@ class Metadata:
6562
Attributes:
6663
signed: A subclass of Signed, which has the actual metadata payload,
6764
i.e. one of Targets, Snapshot, Timestamp or Root.
68-
6965
signatures: An ordered dictionary of keyids to Signature objects, each
7066
signing the canonical serialized representation of 'signed'.
7167
"""
@@ -92,8 +88,8 @@ def from_dict(cls, metadata: Dict[str, Any]) -> "Metadata":
9288
9389
Returns:
9490
A TUF Metadata object.
95-
9691
"""
92+
9793
# Dispatch to contained metadata class on metadata _type field.
9894
_type = metadata["signed"]["_type"]
9995

@@ -148,8 +144,8 @@ def from_file(
148144
149145
Returns:
150146
A TUF Metadata object.
151-
152147
"""
148+
153149
if storage_backend is None:
154150
storage_backend = FilesystemBackend()
155151

@@ -202,20 +198,18 @@ def to_file(
202198
203199
Arguments:
204200
filename: The path to write the file to.
205-
serializer: A MetadataSerializer subclass instance that implements
206-
the desired wireline format serialization. Per default a
207-
JSONSerializer is used.
208-
storage_backend: An object that implements
209-
securesystemslib.storage.StorageBackendInterface. Per default
210-
a (local) FilesystemBackend is used.
201+
serializer: A MetadataSerializer instance that implements the
202+
desired serialization format. Default is JSONSerializer.
203+
storage_backend: A StorageBackendInterface implementation. Default
204+
is FilesystemBackend (i.e. a local file).
211205
212206
Raises:
213207
tuf.api.serialization.SerializationError:
214208
The metadata object cannot be serialized.
215209
securesystemslib.exceptions.StorageError:
216210
The file cannot be written.
217-
218211
"""
212+
219213
if serializer is None:
220214
# Use local scope import to avoid circular import errors
221215
# pylint: disable=import-outside-toplevel
@@ -237,14 +231,12 @@ def sign(
237231
"""Creates signature over 'signed' and assigns it to 'signatures'.
238232
239233
Arguments:
240-
signer: An object implementing the securesystemslib.signer.Signer
241-
interface.
234+
signer: A securesystemslib.signer.Signer implementation.
242235
append: A boolean indicating if the signature should be appended to
243236
the list of signatures or replace any existing signatures. The
244237
default behavior is to replace signatures.
245-
signed_serializer: A SignedSerializer subclass instance that
246-
implements the desired canonicalization format. Per default a
247-
CanonicalJSONSerializer is used.
238+
signed_serializer: A SignedSerializer that implements the desired
239+
serialization format. Default is CanonicalJSONSerializer.
248240
249241
Raises:
250242
tuf.api.serialization.SerializationError:
@@ -255,8 +247,8 @@ def sign(
255247
256248
Returns:
257249
A securesystemslib-style signature object.
258-
259250
"""
251+
260252
if signed_serializer is None:
261253
# Use local scope import to avoid circular import errors
262254
# pylint: disable=import-outside-toplevel
@@ -407,8 +399,9 @@ def bump_version(self) -> None:
407399

408400
class Key:
409401
"""A container class representing the public portion of a Key.
402+
410403
Please note that "Key" instances are not semanticly validated during
411-
initialization. We consider this as responsibility of securesystemslib.
404+
initialization: this only happens at signature verification time.
412405
413406
Attributes:
414407
keyid: An identifier string that must uniquely identify a key within
@@ -420,7 +413,6 @@ class Key:
420413
"rsassa-pss-sha256", "ed25519", and "ecdsa-sha2-nistp256".
421414
keyval: A dictionary containing the public portion of the key.
422415
unrecognized_fields: Dictionary of all unrecognized fields.
423-
424416
"""
425417

426418
def __init__(
@@ -512,15 +504,15 @@ def verify_signature(
512504

513505

514506
class Role:
515-
"""A container class containing the set of keyids and threshold associated
516-
with a particular role.
507+
"""Container that defines which keys are required to sign roles metadata.
508+
509+
Role defines how many keys are required to successfully sign the roles
510+
metadata, and which keys are accepted.
517511
518512
Attributes:
519-
keyids: A set of strings each of which represents a given key.
520-
threshold: An integer representing the required number of keys for that
521-
particular role.
513+
keyids: A set of strings representing signing keys for this role.
514+
threshold: Number of keys required to sign this role's metadata.
522515
unrecognized_fields: Dictionary of all unrecognized fields.
523-
524516
"""
525517

526518
def __init__(
@@ -564,29 +556,14 @@ class Root(Signed):
564556
Attributes:
565557
consistent_snapshot: An optional boolean indicating whether the
566558
repository supports consistent snapshots.
567-
keys: A dictionary that contains a public key store used to verify
568-
top level roles metadata signatures::
569-
570-
{
571-
'<KEYID>': <Key instance>,
572-
...
573-
},
574-
575-
roles: A dictionary that contains a list of signing keyids and
576-
a signature threshold for each top level role::
577-
578-
{
579-
'<ROLE>': <Role istance>,
580-
...
581-
}
582-
559+
keys: Dictionary of keyids to Keys. Defines the keys used in 'roles'.
560+
roles: Dictionary of role names to Roles. Defines which keys are
561+
required to sign the metadata for a specific role.
583562
"""
584563

585564
_signed_type = "root"
586565

587-
# TODO: determine an appropriate value for max-args and fix places where
588-
# we violate that. This __init__ function takes 7 arguments, whereas the
589-
# default max-args value for pylint is 5
566+
# TODO: determine an appropriate value for max-args
590567
# pylint: disable=too-many-arguments
591568
def __init__(
592569
self,
@@ -709,18 +686,8 @@ class MetaFile(BaseFile):
709686
Attributes:
710687
version: An integer indicating the version of the metadata file.
711688
length: An optional integer indicating the length of the metadata file.
712-
hashes: An optional dictionary mapping hash algorithms to the
713-
hashes resulting from applying them over the metadata file
714-
contents.::
715-
716-
'hashes': {
717-
'<HASH ALGO 1>': '<METADATA FILE HASH 1>',
718-
'<HASH ALGO 2>': '<METADATA FILE HASH 2>',
719-
...
720-
}
721-
689+
hashes: An optional dictionary of hash algorithm names to hash values.
722690
unrecognized_fields: Dictionary of all unrecognized fields.
723-
724691
"""
725692

726693
def __init__(
@@ -767,10 +734,11 @@ def to_dict(self) -> Dict[str, Any]:
767734
return res_dict
768735

769736
def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
770-
"""Verifies that the length and hashes of "data" match expected
771-
values.
737+
"""Verifies that the length and hashes of "data" match expected values.
738+
772739
Args:
773740
data: File object or its content in bytes.
741+
774742
Raises:
775743
LengthOrHashMismatchError: Calculated length or hashes do not
776744
match expected values.
@@ -786,13 +754,11 @@ def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
786754
class Timestamp(Signed):
787755
"""A container for the signed part of timestamp metadata.
788756
789-
Attributes:
790-
meta: A dictionary that contains information about snapshot metadata::
791-
792-
{
793-
'snapshot.json': <MetaFile INSTANCE>
794-
}
757+
Timestamp contains information about the snapshot Metadata file.
795758
759+
Attributes:
760+
meta: A dictionary of filenames to MetaFiles. The only valid key value
761+
is the snapshot filename, as defined by the specification.
796762
"""
797763

798764
_signed_type = "timestamp"
@@ -834,15 +800,10 @@ def update(self, snapshot_meta: MetaFile) -> None:
834800
class Snapshot(Signed):
835801
"""A container for the signed part of snapshot metadata.
836802
837-
Attributes:
838-
meta: A dictionary that contains information about targets metadata::
839-
840-
{
841-
'targets.json': <MetaFile INSTANCE>,
842-
'<DELEGATED TARGETS ROLE 1>.json': <MetaFile INSTANCE>,
843-
'<DELEGATED TARGETS ROLE 2>.json': <MetaFile INSTANCE>,
844-
}
803+
Snapshot contains information about all target Metadata files.
845804
805+
Attributes:
806+
meta: A dictionary of target metadata filenames to MetaFile objects.
846807
"""
847808

848809
_signed_type = "snapshot"
@@ -891,18 +852,14 @@ class DelegatedRole(Role):
891852
892853
Attributes:
893854
name: A string giving the name of the delegated role.
894-
keyids: A set of strings each of which represents a given key.
895-
threshold: An integer representing the required number of keys for that
896-
particular role.
897855
terminating: A boolean indicating whether subsequent delegations
898-
should be considered.
856+
should be considered during a target lookup.
899857
paths: An optional list of strings, where each string describes
900-
a path that the role is trusted to provide.
858+
a path pattern that the role is trusted to provide.
901859
path_hash_prefixes: An optional list of HEX_DIGESTs used to succinctly
902860
describe a set of target paths. Only one of the attributes "paths"
903-
and "path_hash_prefixes" is allowed to be set.
861+
and "path_hash_prefixes" is allowed within a DelegatedRole.
904862
unrecognized_fields: Dictionary of all unrecognized fields.
905-
906863
"""
907864

908865
def __init__(
@@ -965,12 +922,11 @@ class Delegations:
965922
"""A container object storing information about all delegations.
966923
967924
Attributes:
968-
keys: A dictionary of keyids and key objects containing information
969-
about the corresponding key.
970-
roles: A list of DelegatedRole instances containing information about
971-
all delegated roles.
925+
keys: Dictionary of keyids to Keys. Defines the keys used in 'roles'.
926+
roles: List of DelegatedRoles that define which keys are required to
927+
sign the metadata for a specific role. The roles order also
928+
defines the order that role delegations are considered in.
972929
unrecognized_fields: Dictionary of all unrecognized fields.
973-
974930
"""
975931

976932
def __init__(
@@ -1014,17 +970,8 @@ class TargetFile(BaseFile):
1014970
1015971
Attributes:
1016972
length: An integer indicating the length of the target file.
1017-
hashes: A dictionary mapping hash algorithms to the
1018-
hashes resulting from applying them over the metadata file
1019-
contents::
1020-
1021-
'hashes': {
1022-
'<HASH ALGO 1>': '<TARGET FILE HASH 1>',
1023-
'<HASH ALGO 2>': '<TARGET FILE HASH 2>',
1024-
...
1025-
}
973+
hashes: A dictionary of hash algorithm names to hash values.
1026974
unrecognized_fields: Dictionary of all unrecognized fields.
1027-
1028975
"""
1029976

1030977
def __init__(
@@ -1067,10 +1014,11 @@ def to_dict(self) -> Dict[str, Any]:
10671014
}
10681015

10691016
def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
1070-
"""Verifies that the length and hashes of "data" match expected
1071-
values.
1017+
"""Verifies that length and hashes of "data" match expected values.
1018+
10721019
Args:
10731020
data: File object or its content in bytes.
1021+
10741022
Raises:
10751023
LengthOrHashMismatchError: Calculated length or hashes do not
10761024
match expected values.
@@ -1082,25 +1030,18 @@ def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
10821030
class Targets(Signed):
10831031
"""A container for the signed part of targets metadata.
10841032
1085-
Attributes:
1086-
targets: A dictionary that contains information about target files::
1087-
1088-
{
1089-
'<TARGET FILE NAME>': <TargetFile INSTANCE>,
1090-
...
1091-
}
1092-
1093-
delegations: An optional object containing a list of delegated target
1094-
roles and public key store used to verify their metadata
1095-
signatures.
1033+
Targets contains verifying information about target files and also
1034+
delegates responsibility to other Targets roles.
10961035
1036+
Attributes:
1037+
targets: A dictionary of target filenames to TargetFiles
1038+
delegations: An optional Delegations that defines how this Targets
1039+
further delegates responsibility to other Targets Metadata files.
10971040
"""
10981041

10991042
_signed_type = "targets"
11001043

1101-
# TODO: determine an appropriate value for max-args and fix places where
1102-
# we violate that. This __init__ function takes 7 arguments, whereas the
1103-
# default max-args value for pylint is 5
1044+
# TODO: determine an appropriate value for max-args
11041045
# pylint: disable=too-many-arguments
11051046
def __init__(
11061047
self,

0 commit comments

Comments
 (0)