Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review and document ngclient exceptions #1787

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tuf/ngclient/_internal/trusted_metadata_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def update_root(self, data: bytes) -> Metadata[Root]:
data: unverified new root metadata as bytes

Raises:
RuntimeError: This function is called after updating timestamp.
RepositoryError: Metadata failed to load or verify. The actual
error type and content will contain more details.

Expand Down Expand Up @@ -198,6 +199,7 @@ def update_timestamp(self, data: bytes) -> Metadata[Timestamp]:
data: unverified new timestamp metadata as bytes

Raises:
RuntimeError: This function is called after updating snapshot.
RepositoryError: Metadata failed to load or verify as final
timestamp. The actual error type and content will contain
more details.
Expand Down Expand Up @@ -281,6 +283,8 @@ def update_snapshot(
match data. Default is False.

Raises:
RuntimeError: This function is called before updating timestamp
or after updating targets.
RepositoryError: data failed to load or verify as final snapshot.
The actual error type and content will contain more details.

Expand Down Expand Up @@ -385,6 +389,7 @@ def update_delegated_targets(
delegator_name: The name of the role delegating to the new metadata

Raises:
RuntimeError: This function is called before updating snapshot.
RepositoryError: Metadata failed to load or verify. The actual
error type and content will contain more details.

Expand Down
11 changes: 11 additions & 0 deletions tuf/ngclient/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def download_bytes(self, url: str, max_length: int) -> bytes:
"""Download bytes from given url

Returns the downloaded bytes, otherwise like download_file()

Args:
url: a URL string that represents the location of the file.
max_length: upper bound of data size in bytes.

Raises:
exceptions.DownloadLengthMismatchError: downloaded bytes exceed
'max_length'.

Returns:
The content of the file in bytes.
"""
with self.download_file(url, max_length) as dl_file:
return dl_file.read()
17 changes: 7 additions & 10 deletions tuf/ngclient/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def refresh(self) -> None:
Raises:
OSError: New metadata could not be written to disk
RepositoryError: Metadata failed to verify in some way
TODO: download-related errors
DownloadError: Download of a metadata file failed in some way
"""

self._load_root()
Expand Down Expand Up @@ -157,7 +157,7 @@ def get_targetinfo(self, target_path: str) -> Optional[TargetFile]:
Raises:
OSError: New metadata could not be written to disk
RepositoryError: Metadata failed to verify in some way
TODO: download-related errors
DownloadError: Download of a metadata file failed in some way

Returns:
A TargetFile instance or None.
Expand Down Expand Up @@ -216,8 +216,10 @@ def download_target(

Raises:
ValueError: Invalid arguments
TODO: download-related errors
TODO: file write errors
DownloadError: Download of the target file failed in some way
RepositoryError: Downloaded target failed to be verified in some way
exceptions.StorageError: Downloaded target could not be written
to disk

Returns:
Local path to downloaded file
Expand Down Expand Up @@ -248,12 +250,7 @@ def download_target(
with self._fetcher.download_file(
full_url, targetinfo.length
) as target_file:
try:
targetinfo.verify_length_and_hashes(target_file)
except exceptions.LengthOrHashMismatchError as e:
raise exceptions.RepositoryError(
f"{target_filepath} length or hashes do not match"
) from e
targetinfo.verify_length_and_hashes(target_file)

sslib_util.persist_temp_file(target_file, filepath)

Expand Down