Skip to content

Commit

Permalink
gh attestation verify handles empty JSONL files (cli#9541)
Browse files Browse the repository at this point in the history
* handle empty jsonl files

Signed-off-by: Meredith Lancaster <[email protected]>

* check processed attestations slice length

Signed-off-by: Meredith Lancaster <[email protected]>

* update err name and message

Signed-off-by: Meredith Lancaster <[email protected]>

---------

Signed-off-by: Meredith Lancaster <[email protected]>
  • Loading branch information
malancas authored Sep 4, 2024
1 parent 9a0a7d4 commit 34d7ef7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/cmd/attestation/verification/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var ErrUnrecognisedBundleExtension = errors.New("bundle file extension not supported, must be json or jsonl")
var ErrEmptyBundleFile = errors.New("provided bundle file is empty")

type FetchAttestationsConfig struct {
APIClient api.Client
Expand Down Expand Up @@ -94,6 +95,10 @@ func loadBundlesFromJSONLinesFile(path string) ([]*api.Attestation, error) {
attestations = append(attestations, &a)
}

if len(attestations) == 0 {
return nil, ErrEmptyBundleFile
}

return attestations, nil
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/attestation/verification/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ func TestLoadBundlesFromJSONLinesFile(t *testing.T) {
})
}

func TestLoadBundlesFromJSONLinesFile_RejectEmptyJSONLFile(t *testing.T) {
// Create a temporary file
emptyJSONL, err := os.CreateTemp("", "empty.jsonl")
require.NoError(t, err)
err = emptyJSONL.Close()
require.NoError(t, err)

attestations, err := loadBundlesFromJSONLinesFile(emptyJSONL.Name())

require.ErrorIs(t, err, ErrEmptyBundleFile)
require.Nil(t, attestations)
}

func TestLoadBundleFromJSONFile(t *testing.T) {
path := "../test/data/sigstore-js-2.1.0-bundle.json"
attestations, err := loadBundleFromJSONFile(path)
Expand Down

0 comments on commit 34d7ef7

Please sign in to comment.