Skip to content

Commit

Permalink
Merge pull request #548 from fluxcd/fetch-improve-err
Browse files Browse the repository at this point in the history
http/fetch: improve error messages
  • Loading branch information
hiddeco authored May 10, 2023
2 parents 2d0a389 + 7916c22 commit d25c2af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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

0 comments on commit d25c2af

Please sign in to comment.