Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
22 changes: 12 additions & 10 deletions pkg/cmd/attestation/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,25 @@ func (c *LiveClient) fetchBundleFromAttestations(attestations []*Attestation) ([
return fmt.Errorf("attestation has no bundle or bundle URL")
}

// for now, we fallback to the bundle field if the bundle URL is empty
if a.BundleURL == "" {
c.logger.VerbosePrintf("Bundle URL is empty. Falling back to bundle field\n\n")
// If the bundle field is nil, try to fetch the bundle with the provided URL
if a.Bundle == nil {
c.logger.VerbosePrintf("Bundle field is empty. Trying to fetch with bundle URL\n\n")
b, err := c.GetBundle(a.BundleURL)
if err != nil {
return fmt.Errorf("failed to fetch bundle with URL: %w", err)
}
fetched[i] = &Attestation{
Bundle: a.Bundle,
Bundle: b,
}
return nil
}

// otherwise fetch the bundle with the provided URL
b, err := c.GetBundle(a.BundleURL)
if err != nil {
return fmt.Errorf("failed to fetch bundle with URL: %w", err)
}
// otherwise fall back to the bundle field
c.logger.VerbosePrintf("Fetching bundle from Bundle field\n\n")
fetched[i] = &Attestation{
Bundle: b,
Bundle: a.Bundle,
}

return nil
})
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/cmd/attestation/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,23 @@ func TestGetByDigest_Error(t *testing.T) {
require.Nil(t, attestations)
}

func TestFetchBundleFromAttestations(t *testing.T) {
func TestFetchBundleFromAttestations_BundleURL(t *testing.T) {
httpClient := &mockHttpClient{}
client := LiveClient{
httpClient: httpClient,
logger: io.NewTestHandler(),
}

att1 := makeTestAttestation()
att1.Bundle = nil
att2 := makeTestAttestation()
att2.Bundle = nil
// zero out the bundle field so it tries fetching by URL
attestations := []*Attestation{&att1, &att2}
fetched, err := client.fetchBundleFromAttestations(attestations)
require.NoError(t, err)
require.Len(t, fetched, 2)
require.Equal(t, "application/vnd.dev.sigstore.bundle.v0.3+json", fetched[0].Bundle.GetMediaType())
require.NotNil(t, "application/vnd.dev.sigstore.bundle.v0.3+json", fetched[0].Bundle.GetMediaType())
httpClient.AssertNumberOfCalls(t, "OnGetSuccess", 2)
}

Expand All @@ -211,7 +214,7 @@ func TestFetchBundleFromAttestations_InvalidAttestation(t *testing.T) {
require.Nil(t, fetched, 2)
}

func TestFetchBundleFromAttestations_Fail(t *testing.T) {
func TestFetchBundleFromAttestations_Fail_BundleURL(t *testing.T) {
httpClient := &failAfterOneCallHttpClient{}

c := &LiveClient{
Expand All @@ -220,7 +223,10 @@ func TestFetchBundleFromAttestations_Fail(t *testing.T) {
}

att1 := makeTestAttestation()
att1.Bundle = nil
att2 := makeTestAttestation()
att2.Bundle = nil
// zero out the bundle field so it tries fetching by URL
attestations := []*Attestation{&att1, &att2}
fetched, err := c.fetchBundleFromAttestations(attestations)
require.Error(t, err)
Expand All @@ -237,6 +243,7 @@ func TestFetchBundleFromAttestations_FetchByURLFail(t *testing.T) {
}

a := makeTestAttestation()
a.Bundle = nil
attestations := []*Attestation{&a}
bundle, err := c.fetchBundleFromAttestations(attestations)
require.Error(t, err)
Expand Down
Loading