Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep inspect attestations when filtering download #3031

Merged
merged 1 commit into from
Jun 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cosign/cli/options/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (o *SBOMDownloadOptions) AddFlags(cmd *cobra.Command) {
// AddFlags implements Interface
func (o *AttestationDownloadOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.PredicateType, "predicate-type", "",
"download attestation with matching predicateType annotation")
"download attestation with matching predicateType")
cmd.Flags().StringVar(&o.Platform, "platform", "",
"download attestation for a specific platform image")
}
2 changes: 1 addition & 1 deletion doc/cosign_download_attestation.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 25 additions & 16 deletions pkg/cosign/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cosign
import (
"context"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand All @@ -26,6 +27,7 @@ import (
"sync"

"github.com/google/go-containerregistry/pkg/name"
"github.com/in-toto/in-toto-golang/in_toto"
"github.com/sigstore/cosign/v2/pkg/cosign/bundle"
"github.com/sigstore/cosign/v2/pkg/oci"
ociremote "github.com/sigstore/cosign/v2/pkg/oci/remote"
Expand Down Expand Up @@ -150,28 +152,35 @@ func FetchAttestations(se oci.SignedEntity, predicateType string) ([]Attestation
g.SetLimit(runtime.NumCPU())

for _, att := range l {
if predicateType != "" {
anns, err := att.Annotations()
if err != nil {
return nil, err
}
pt, ok := anns["predicateType"]
// Skip attestation if predicateType annotation is not present or predicateType annotation does not match supplied predicate type
if !ok || pt != predicateType {
continue
}
}
att := att
var a AttestationPayload
g.Go(func() error {
attestPayload, _ := att.Payload()
err := json.Unmarshal(attestPayload, &a)
rawPayload, err := att.Payload()
if err != nil {
return err
return fmt.Errorf("fetching payload: %w", err)
}
var payload AttestationPayload
if err := json.Unmarshal(rawPayload, &payload); err != nil {
return fmt.Errorf("unmarshaling payload: %w", err)
}

if predicateType != "" {
var decodedPayload []byte
decodedPayload, err = base64.StdEncoding.DecodeString(payload.PayLoad)
if err != nil {
return fmt.Errorf("decoding payload: %w", err)
}
var statement in_toto.Statement
if err := json.Unmarshal(decodedPayload, &statement); err != nil {
return fmt.Errorf("unmarshaling statement: %w", err)
}
if statement.PredicateType != predicateType {
return nil
}
}

attMu.Lock()
defer attMu.Unlock()
attestations = append(attestations, a)
attestations = append(attestations, payload)
return nil
})
}
Expand Down