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

http/fetch: improve error messages #548

Merged
merged 1 commit into from
May 10, 2023
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
16 changes: 10 additions & 6 deletions http/fetch/archive_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func (r *ArchiveFetcher) Fetch(archiveURL, digest, dir string) error {

resp, err := r.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download archive, error: %w", err)
return fmt.Errorf("failed to download archive: %w", err)
}
defer resp.Body.Close()

if code := resp.StatusCode; code != http.StatusOK {
if code == http.StatusNotFound {
return FileNotFoundError
}
return fmt.Errorf("failed to download archive from %s, status: %s", archiveURL, resp.Status)
return fmt.Errorf("failed to download archive from %s (status: %s)", archiveURL, resp.Status)
}

f, err := os.CreateTemp("", "fetch.*.tmp")
Expand Down Expand Up @@ -130,7 +130,7 @@ func (r *ArchiveFetcher) Fetch(archiveURL, digest, dir string) error {
// Ensure that the digest of the downloaded file matches the
// known digest.
if err := r.verifyDigest(digest, f); err != nil {
return err
return fmt.Errorf("failed to verify archive: %w", err)
}

// Jump back at the beginning of the file stream again.
Expand All @@ -148,15 +148,19 @@ func (r *ArchiveFetcher) Fetch(archiveURL, digest, dir string) error {
}

// verifyDigest verifies the digest of the reader, and returns an error if it
// doesn't match.
// doesn't match, fails to parse, or is empty.
func (r *ArchiveFetcher) verifyDigest(dig string, reader io.Reader) error {
if dig == "" {
return fmt.Errorf("empty digest")
}

if !strings.Contains(dig, ":") {
dig = "sha256:" + dig
}

d, err := digest.Parse(dig)
if err != nil {
return fmt.Errorf("failed to parse digest: %w", err)
return fmt.Errorf("failed to parse digest '%s': %w", dig, err)
}

// Verify reader's data.
Expand All @@ -165,7 +169,7 @@ func (r *ArchiveFetcher) verifyDigest(dig string, reader io.Reader) error {
return err
}
if !verifier.Verified() {
return fmt.Errorf("failed to verify archive: computed digest doesn't match provided '%s' (check whether file size exceeds max download size)", dig)
return fmt.Errorf("computed digest doesn't match provided '%s' (check whether file size exceeds max download size)", dig)
}
return nil
}
8 changes: 8 additions & 0 deletions http/fetch/archive_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func TestArchiveFetcher_Fetch(t *testing.T) {
maxUntarSize: 1,
wantErr: true,
},
{
name: "fails with empty digest error",
url: artifactURL,
digest: "",
maxDownloadSize: -1,
maxUntarSize: -1,
wantErr: true,
},
{
name: "fails with digest parsing error",
url: artifactURL,
Expand Down