From 57b20291bd7274e2c4b099dbf2aff35634ab9312 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 5 Sep 2024 13:20:13 -0600 Subject: [PATCH] check for os.PathError Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/verification/attestation.go | 12 ++++++++++-- pkg/cmd/attestation/verification/attestation_test.go | 8 ++++++++ pkg/cmd/attestation/verification/sigstore.go | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/attestation/verification/attestation.go b/pkg/cmd/attestation/verification/attestation.go index 0f52adec5..d1659feef 100644 --- a/pkg/cmd/attestation/verification/attestation.go +++ b/pkg/cmd/attestation/verification/attestation.go @@ -53,13 +53,21 @@ func GetLocalAttestations(path string) ([]*api.Attestation, error) { case ".json": attestations, err := loadBundleFromJSONFile(path) if err != nil { - return nil, fmt.Errorf("bundle could not be loaded from JSON file at %s", path) + var pathErr *os.PathError + if errors.As(err, &pathErr) { + return nil, fmt.Errorf("bundle could not be loaded from JSON file at %s", path) + } + return nil, fmt.Errorf("bundle content could not be parsed") } return attestations, nil case ".jsonl": attestations, err := loadBundlesFromJSONLinesFile(path) if err != nil { - return nil, fmt.Errorf("bundles could not be loaded from JSON lines file at %s", path) + var pathErr *os.PathError + if errors.As(err, &pathErr) { + return nil, fmt.Errorf("bundles could not be loaded from JSON lines file at %s", path) + } + return nil, fmt.Errorf("bundle content could not be parsed") } return attestations, nil } diff --git a/pkg/cmd/attestation/verification/attestation_test.go b/pkg/cmd/attestation/verification/attestation_test.go index 66b337ad7..2db62d56d 100644 --- a/pkg/cmd/attestation/verification/attestation_test.go +++ b/pkg/cmd/attestation/verification/attestation_test.go @@ -87,6 +87,14 @@ func TestGetLocalAttestations(t *testing.T) { require.ErrorIs(t, err, ErrUnrecognisedBundleExtension) require.Nil(t, attestations) }) + + t.Run("with non-existent bundle file", func(t *testing.T) { + path := "../test/data/not-found-bundle.json" + attestations, err := GetLocalAttestations(path) + + require.ErrorContains(t, err, "bundle could not be loaded from JSON file") + require.Nil(t, attestations) + }) } func TestFilterAttestations(t *testing.T) { diff --git a/pkg/cmd/attestation/verification/sigstore.go b/pkg/cmd/attestation/verification/sigstore.go index f4aad8c22..822e7b621 100644 --- a/pkg/cmd/attestation/verification/sigstore.go +++ b/pkg/cmd/attestation/verification/sigstore.go @@ -186,7 +186,7 @@ func (v *LiveSigstoreVerifier) Verify(attestations []*api.Attestation, policy ve verifier, issuer, err := v.chooseVerifier(apr.Attestation.Bundle) if err != nil { return &SigstoreResults{ - Error: fmt.Errorf("failed to find recognized issuer from bundle content: %v", err), + Error: fmt.Errorf("failed to find recognized issuer from bundle content"), } }