Skip to content

Commit 85125c4

Browse files
Better error formatting.
1 parent a0f437b commit 85125c4

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

tuf/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ class UnsupportedAlgorithmError(Error):
8080

8181
class BadHashError(Error):
8282
"""Indicate an error while checking the value a hash object."""
83-
pass
83+
84+
def __init__(self, expected_hash, observed_hash):
85+
self.expected_hash = expected_hash
86+
self.observed_hash = observed_hash
87+
88+
def __str__(self):
89+
return 'Observed hash ('+str(self.observed_hash)+\
90+
') != expected hash ('+str(self.expected_hash)+')'
8491

8592

8693

@@ -120,7 +127,12 @@ class ForbiddenTargetError(RepositoryError):
120127

121128
class ExpiredMetadataError(Error):
122129
"""Indicate that a TUF Metadata file has expired."""
123-
pass
130+
131+
def __init__(self, expiry_time):
132+
self.expiry_time = expiry_time # UTC
133+
134+
def __str__(self):
135+
return 'Metadata expired on '+str(self.expiry_time)+'.'
124136

125137

126138

@@ -154,8 +166,12 @@ class CryptoError(Error):
154166

155167
class BadSignatureError(CryptoError):
156168
"""Indicate that some metadata file had a bad signature."""
157-
pass
158169

170+
def __init__(self, metadata_role_name):
171+
self.metadata_role_name = metadata_role_name
172+
173+
def __str__(self):
174+
return str(self.metadata_role_name)+' metadata bad signature!'
159175

160176

161177

tuf/client/updater.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,7 @@ def __check_hashes(self, file_object, trusted_hashes):
627627
digest_object.update(file_object.read())
628628
computed_hash = digest_object.hexdigest()
629629
if trusted_hash != computed_hash:
630-
raise tuf.BadHashError('Hashes do not match! Expected '+
631-
trusted_hash+' got '+computed_hash)
630+
raise tuf.BadHashError(trusted_hash, computed_hash)
632631
else:
633632
logger.info('The file\'s '+algorithm+' hash is correct: '+trusted_hash)
634633

@@ -835,7 +834,7 @@ def __verify_uncompressed_metadata_file(self, metadata_file_object,
835834
# Verify the signature on the downloaded metadata object.
836835
valid = tuf.sig.verify(metadata_signable, metadata_role)
837836
if not valid:
838-
raise tuf.BadSignatureError()
837+
raise tuf.BadSignatureError(metadata_role)
839838

840839

841840

@@ -1742,10 +1741,11 @@ def _ensure_not_expired(self, metadata_role):
17421741
# an exception. 'expires' is in YYYY-MM-DD HH:MM:SS format, so
17431742
# convert it to seconds since the epoch, which is the time format
17441743
# returned by time.time() (i.e., current time), before comparing.
1745-
if tuf.formats.parse_time(expires) < time.time():
1746-
message = 'Metadata '+repr(rolepath)+' expired on '+repr(expires)+'.'
1747-
logger.error(message)
1748-
raise tuf.ExpiredMetadataError(message)
1744+
current_time = time.time()
1745+
expiry_time = tuf.formats.parse_time(expires)
1746+
if expiry_time < current_time:
1747+
logger.error('Metadata '+repr(rolepath)+' expired on '+repr(expires)+'.')
1748+
raise tuf.ExpiredMetadataError(expires)
17491749

17501750

17511751

0 commit comments

Comments
 (0)