Skip to content

Commit 4b02071

Browse files
authored
Merge pull request cli#11796 from cli/eugeene/release_filter_initiator_type
add initiator_type for attestations
2 parents 6b19a85 + 8d701dc commit 4b02071

8 files changed

Lines changed: 51 additions & 12 deletions

File tree

pkg/cmd/attestation/api/attestation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var ErrNoAttestationsFound = errors.New("no attestations found")
1818
type Attestation struct {
1919
Bundle *bundle.Bundle `json:"bundle"`
2020
BundleURL string `json:"bundle_url"`
21+
Initiator string `json:"initiator"`
2122
}
2223

2324
type AttestationsResponse struct {

pkg/cmd/attestation/api/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type FetchParams struct {
3434
Owner string
3535
PredicateType string
3636
Repo string
37+
Initiator string
3738
}
3839

3940
func (p *FetchParams) Validate() error {
@@ -147,6 +148,17 @@ func (c *LiveClient) getAttestations(params FetchParams) ([]*Attestation, error)
147148
}
148149

149150
url = newURL
151+
152+
// filter by the initiator type
153+
if params.Initiator != "" {
154+
filtered := make([]*Attestation, 0, len(resp.Attestations))
155+
for _, att := range resp.Attestations {
156+
if att.Initiator == params.Initiator {
157+
filtered = append(filtered, att)
158+
}
159+
}
160+
resp.Attestations = filtered
161+
}
150162
attestations = append(attestations, resp.Attestations...)
151163

152164
return nil

pkg/cmd/attestation/api/client_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/cli/cli/v2/pkg/cmd/attestation/io"
77
"github.com/cli/cli/v2/pkg/cmd/attestation/test/data"
8-
98
"github.com/stretchr/testify/require"
109
)
1110

@@ -17,7 +16,8 @@ const (
1716

1817
func NewClientWithMockGHClient(hasNextPage bool) Client {
1918
fetcher := mockDataGenerator{
20-
NumAttestations: 5,
19+
NumUserAttestations: 5,
20+
NumGitHubAttestations: 4,
2121
}
2222
l := io.NewTestHandler()
2323

@@ -47,12 +47,21 @@ var testFetchParamsWithOwner = FetchParams{
4747
Limit: DefaultLimit,
4848
Owner: testOwner,
4949
PredicateType: "https://slsa.dev/provenance/v1",
50+
Initiator: "user",
5051
}
5152
var testFetchParamsWithRepo = FetchParams{
5253
Digest: testDigest,
5354
Limit: DefaultLimit,
5455
Repo: testRepo,
5556
PredicateType: "https://slsa.dev/provenance/v1",
57+
Initiator: "user",
58+
}
59+
60+
var testFetchParamsWithRepoWithGitHubInitiator = FetchParams{
61+
Digest: testDigest,
62+
Limit: DefaultLimit,
63+
Repo: testRepo,
64+
Initiator: "github",
5665
}
5766

5867
type getByTestCase struct {
@@ -93,6 +102,11 @@ var getByTestCases = []getByTestCase{
93102
expectedAttestations: 7,
94103
hasNextPage: true,
95104
},
105+
{
106+
name: "get by digest with repo and GitHub initiator",
107+
params: testFetchParamsWithRepoWithGitHubInitiator,
108+
expectedAttestations: 4,
109+
},
96110
}
97111

98112
func TestGetByDigest(t *testing.T) {
@@ -115,7 +129,7 @@ func TestGetByDigest(t *testing.T) {
115129

116130
func TestGetByDigest_NoAttestationsFound(t *testing.T) {
117131
fetcher := mockDataGenerator{
118-
NumAttestations: 5,
132+
NumUserAttestations: 5,
119133
}
120134

121135
httpClient := &mockHttpClient{}
@@ -135,7 +149,7 @@ func TestGetByDigest_NoAttestationsFound(t *testing.T) {
135149

136150
func TestGetByDigest_Error(t *testing.T) {
137151
fetcher := mockDataGenerator{
138-
NumAttestations: 5,
152+
NumUserAttestations: 5,
139153
}
140154

141155
c := LiveClient{
@@ -339,7 +353,7 @@ func TestGetAttestationsRetries(t *testing.T) {
339353
getAttestationRetryInterval = 0
340354

341355
fetcher := mockDataGenerator{
342-
NumAttestations: 5,
356+
NumUserAttestations: 5,
343357
}
344358

345359
c := &LiveClient{
@@ -369,7 +383,7 @@ func TestGetAttestationsMaxRetries(t *testing.T) {
369383
getAttestationRetryInterval = 0
370384

371385
fetcher := mockDataGenerator{
372-
NumAttestations: 5,
386+
NumUserAttestations: 5,
373387
}
374388

375389
c := &LiveClient{

pkg/cmd/attestation/api/mock_client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ func makeTestReleaseAttestation() Attestation {
1010
return Attestation{
1111
Bundle: data.GitHubReleaseBundle(nil),
1212
BundleURL: "https://example.com",
13+
Initiator: "github",
1314
}
1415
}
1516

1617
func makeTestAttestation() Attestation {
17-
return Attestation{Bundle: data.SigstoreBundle(nil), BundleURL: "https://example.com"}
18+
return Attestation{
19+
Bundle: data.SigstoreBundle(nil),
20+
BundleURL: "https://example.com",
21+
Initiator: "user",
22+
}
1823
}
1924

2025
type MockClient struct {

pkg/cmd/attestation/api/mock_githubApiClient_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func (m mockAPIClient) REST(hostname, method, p string, body io.Reader, data int
2727

2828
type mockDataGenerator struct {
2929
mock.Mock
30-
NumAttestations int
30+
NumUserAttestations int
31+
NumGitHubAttestations int
3132
}
3233

3334
func (m *mockDataGenerator) OnRESTSuccess(hostname, method, p string, body io.Reader, data interface{}) (string, error) {
@@ -76,12 +77,15 @@ func (m *mockDataGenerator) OnREST500ErrorHandler() func(hostname, method, p str
7677
}
7778

7879
func (m *mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p string, body io.Reader, data interface{}, hasNext bool) (string, error) {
79-
atts := make([]*Attestation, m.NumAttestations)
80-
for j := 0; j < m.NumAttestations; j++ {
80+
atts := make([]*Attestation, m.NumUserAttestations+m.NumGitHubAttestations)
81+
for j := 0; j < m.NumUserAttestations; j++ {
8182
att := makeTestAttestation()
8283
atts[j] = &att
8384
}
84-
85+
for j := m.NumUserAttestations; j < m.NumUserAttestations+m.NumGitHubAttestations; j++ {
86+
att := makeTestReleaseAttestation()
87+
atts[j] = &att
88+
}
8589
resp := AttestationsResponse{
8690
Attestations: atts,
8791
}

pkg/cmd/attestation/verify/attestation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func getAttestations(o *Options, a artifact.DigestedArtifact) ([]*api.Attestatio
2525
Owner: o.Owner,
2626
PredicateType: o.PredicateType,
2727
Repo: o.Repo,
28+
Initiator: "user",
2829
}
2930

3031
attestations, err := o.APIClient.GetByDigest(params)

pkg/cmd/release/verify-asset/verify_asset.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ func verifyAssetRun(config *VerifyAssetConfig) error {
161161
// The limit is set to 100 to ensure we fetch all attestations for a given SHA.
162162
// While multiple attestations can exist for a single SHA,
163163
// only one attestation is associated with each release tag.
164-
Limit: 100,
164+
Initiator: "github",
165+
Limit: 100,
165166
})
166167
if err != nil {
167168
return fmt.Errorf("no attestations found for tag %s (%s)", tagName, releaseRefDigest.DigestWithAlg())

pkg/cmd/release/verify/verify.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func verifyRun(config *VerifyConfig) error {
143143
PredicateType: shared.ReleasePredicateType,
144144
Owner: baseRepo.RepoOwner(),
145145
Repo: baseRepo.RepoOwner() + "/" + baseRepo.RepoName(),
146+
Initiator: "github",
146147
// TODO: Allow this value to be set via a flag.
147148
// The limit is set to 100 to ensure we fetch all attestations for a given SHA.
148149
// While multiple attestations can exist for a single SHA,

0 commit comments

Comments
 (0)