Skip to content

Commit

Permalink
exceptions: Make more errors RepositoryErrors
Browse files Browse the repository at this point in the history
Try to make errors derive from RepositoryError if they can be the
result of malicious or malfunctioning remote repository: The idea
here is that client can handle just RepositoryError instead of
individual errors (as it cannot do anything about any of them).

Also improve variable naming.

This is backwards compatible.

Signed-off-by: Jussi Kukkonen <[email protected]>
  • Loading branch information
Jussi Kukkonen committed May 21, 2021
1 parent f935ea3 commit 91935fa
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions tuf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class UnsupportedAlgorithmError(Error):
"""Indicate an error while trying to identify a user-specified algorithm."""


class BadHashError(Error):
class RepositoryError(Error):
"""Indicate an error with a repository's state, such as a missing file."""


class BadHashError(RepositoryError):
"""Indicate an error while checking the value of a hash object."""

def __init__(self, expected_hash, observed_hash):
Expand All @@ -89,12 +93,6 @@ def __repr__(self):
# repr(self.observed_hash) + ')')




class BadVersionNumberError(Error):
"""Indicate an error for metadata that contains an invalid version number."""


class BadPasswordError(Error):
"""Indicate an error after encountering an invalid password."""

Expand All @@ -103,8 +101,8 @@ class UnknownKeyError(Error):
"""Indicate an error while verifying key-like objects (e.g., keyids)."""


class RepositoryError(Error):
"""Indicate an error with a repository's state, such as a missing file."""
class BadVersionNumberError(RepositoryError):
"""Indicate an error for metadata that contains an invalid version number."""


class MissingLocalRepositoryError(RepositoryError):
Expand All @@ -119,36 +117,29 @@ class ForbiddenTargetError(RepositoryError):
"""Indicate that a role signed for a target that it was not delegated to."""


class ExpiredMetadataError(Error):
class ExpiredMetadataError(RepositoryError):
"""Indicate that a TUF Metadata file has expired."""


class ReplayedMetadataError(RepositoryError):
"""Indicate that some metadata has been replayed to the client."""

def __init__(self, metadata_role, previous_version, current_version):
def __init__(self, metadata_role, downloaded_version, current_version):
super(ReplayedMetadataError, self).__init__()

self.metadata_role = metadata_role
self.previous_version = previous_version
self.downloaded_version = downloaded_version
self.current_version = current_version


def __str__(self):
return (
'Downloaded ' + repr(self.metadata_role) + ' is older (' +
repr(self.previous_version) + ') than the version currently '
repr(self.downloaded_version) + ') than the version currently '
'installed (' + repr(self.current_version) + ').')

def __repr__(self):
return self.__class__.__name__ + ' : ' + str(self)

# # Directly instance-reproducing:
# return (
# self.__class__.__name__ + '(' + repr(self.metadata_role) + ', ' +
# repr(self.previous_version) + ', ' + repr(self.current_version) + ')')



class CryptoError(Error):
"""Indicate any cryptography-related errors."""
Expand Down Expand Up @@ -250,7 +241,7 @@ class InvalidNameError(Error):
"""Indicate an error while trying to validate any type of named object."""


class UnsignedMetadataError(Error):
class UnsignedMetadataError(RepositoryError):
"""Indicate metadata object with insufficient threshold of signatures."""

def __init__(self, message, signable):
Expand Down

0 comments on commit 91935fa

Please sign in to comment.