Skip to content

Commit

Permalink
Switch CheckOpts to take ociremote.Options. (#738)
Browse files Browse the repository at this point in the history
* Switch `CheckOpts` to take `ociremote.Option`s.

This is as a precursor to starting to fold some other stuff together, e.g. the SigSuffix option.

Signed-off-by: Matt Moore <[email protected]>

* Rename RemoteOpts to ClientOpts

Signed-off-by: Matt Moore <[email protected]>
  • Loading branch information
mattmoor authored Sep 21, 2021
1 parent e7395a7 commit f815e25
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
5 changes: 5 additions & 0 deletions cmd/cosign/cli/options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/v1/remote"
ociremote "github.com/sigstore/cosign/internal/oci/remote"
)

// OneOf ensures that only one of the supplied interfaces is set to a non-zero value.
Expand All @@ -46,6 +47,10 @@ type RegistryOpts struct {
AllowInsecure bool
}

func (co *RegistryOpts) ClientOpts(ctx context.Context) []ociremote.Option {
return []ociremote.Option{ociremote.WithRemoteOptions(co.GetRegistryClientOpts(ctx)...)}
}

func (co *RegistryOpts) GetRegistryClientOpts(ctx context.Context) []remote.Option {
opts := defaultRegistryClientOpts(ctx)
if co != nil && co.AllowInsecure {
Expand Down
6 changes: 2 additions & 4 deletions cmd/cosign/cli/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ func (c *VerifyCommand) Exec(ctx context.Context, args []string) (err error) {
return &options.KeyParseError{}
}

remoteOpts := c.RegistryOpts.GetRegistryClientOpts(ctx)

co := &cosign.CheckOpts{
Annotations: *c.Annotations,
RegistryClientOpts: remoteOpts,
RegistryClientOpts: c.RegistryOpts.ClientOpts(ctx),
}
if c.CheckClaims {
co.ClaimVerifier = cosign.SimpleClaimVerifier
Expand Down Expand Up @@ -168,7 +166,7 @@ func (c *VerifyCommand) Exec(ctx context.Context, args []string) (err error) {
co.SigVerifier = pubKey

for _, img := range args {
ref, err := sign.GetAttachedImageRef(img, c.Attachment, remoteOpts...)
ref, err := sign.GetAttachedImageRef(img, c.Attachment, c.RegistryOpts.GetRegistryClientOpts(ctx)...)
if err != nil {
return errors.Wrapf(err, "resolving attachment type %s for image %s", c.Attachment, img)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/verify/verify_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *VerifyAttestationCommand) Exec(ctx context.Context, args []string) (err
}

co := &cosign.CheckOpts{
RegistryClientOpts: c.GetRegistryClientOpts(ctx),
RegistryClientOpts: c.ClientOpts(ctx),
SigTagSuffixOverride: cosign.AttestationTagSuffix,
}
if c.CheckClaims {
Expand Down
2 changes: 1 addition & 1 deletion copasetic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func main() {
PKOpts: []signature.PublicKeyOption{ctxOpt},
ClaimVerifier: cosign.SimpleClaimVerifier,
RootCerts: fulcio.GetRoots(),
RegistryClientOpts: regOpts.GetRegistryClientOpts(bctx.Context),
RegistryClientOpts: regOpts.ClientOpts(bctx.Context),
RekorURL: *rekorURL,
}
sps, _, err := cosign.Verify(bctx.Context, ref, co)
Expand Down
8 changes: 3 additions & 5 deletions pkg/cosign/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/pkg/errors"

"github.com/sigstore/cosign/internal/oci"
Expand All @@ -47,7 +46,7 @@ type CheckOpts struct {
// SigTagSuffixOverride overrides the suffix of the derived signature image tag. Default: ".sig"
SigTagSuffixOverride string
// RegistryClientOpts are the options for interacting with the container registry.
RegistryClientOpts []remote.Option
RegistryClientOpts []ociremote.Option

// Annotations optionally specifies image signature annotations to verify.
Annotations map[string]interface{}
Expand Down Expand Up @@ -85,9 +84,8 @@ func Verify(ctx context.Context, signedImgRef name.Reference, co *CheckOpts) (ch
}
}

opts := []ociremote.Option{
ociremote.WithRemoteOptions(co.RegistryClientOpts...),
}
opts := co.RegistryClientOpts

// These are all the signatures attached to our image that we know how to parse.
if co.SigTagSuffixOverride != "" {
opts = append(opts, ociremote.WithSignatureSuffix(co.SigTagSuffixOverride))
Expand Down
16 changes: 10 additions & 6 deletions pkg/sget/sget.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/sigstore/cosign/cmd/cosign/cli/fulcio"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/cmd/cosign/cli/verify"
ociremote "github.com/sigstore/cosign/internal/oci/remote"
"github.com/sigstore/cosign/pkg/cosign"
sigs "github.com/sigstore/cosign/pkg/signature"
)
Expand Down Expand Up @@ -57,12 +58,14 @@ func (sg *SecureGet) Do(ctx context.Context) error {
}
}

opts := []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithContext(ctx),
}

co := &cosign.CheckOpts{
ClaimVerifier: cosign.SimpleClaimVerifier,
RegistryClientOpts: []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithContext(ctx),
},
ClaimVerifier: cosign.SimpleClaimVerifier,
RegistryClientOpts: []ociremote.Option{ociremote.WithRemoteOptions(opts...)},
}
if sg.KeyRef != "" {
pub, err := sigs.LoadPublicKey(ctx, sg.KeyRef)
Expand All @@ -83,7 +86,8 @@ func (sg *SecureGet) Do(ctx context.Context) error {
verify.PrintVerification(sg.ImageRef, sp, "text")
}

img, err := remote.Image(ref, co.RegistryClientOpts...)
// TODO(mattmoor): Depending on what this is, use the higher-level stuff.
img, err := remote.Image(ref, opts...)
if err != nil {
return err
}
Expand Down

0 comments on commit f815e25

Please sign in to comment.