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

Add support for using k8schain under a flag. #972

Merged
merged 1 commit into from
Oct 29, 2021
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
12 changes: 0 additions & 12 deletions cmd/cosign/cli/options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
package options

import (
"context"
"reflect"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/v1/remote"
)

// OneOf ensures that only one of the supplied interfaces is set to a non-zero value.
Expand All @@ -38,11 +34,3 @@ func NOf(args ...interface{}) int {
}
return n
}

func defaultRegistryClientOpts(ctx context.Context) []remote.Option {
return []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithContext(ctx),
remote.WithUserAgent("cosign/" + VersionInfo().GitVersion),
}
}
27 changes: 24 additions & 3 deletions cmd/cosign/cli/options/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"crypto/tls"
"net/http"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/k8schain"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
Expand All @@ -27,8 +29,9 @@ import (

// RegistryOptions is the wrapper for the registry options.
type RegistryOptions struct {
AllowInsecure bool
RefOpts ReferenceOptions
AllowInsecure bool
KubernetesKeychain bool
RefOpts ReferenceOptions
}

var _ Interface = (*RegistryOptions)(nil)
Expand All @@ -37,6 +40,10 @@ var _ Interface = (*RegistryOptions)(nil)
func (o *RegistryOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")

cmd.Flags().BoolVar(&o.KubernetesKeychain, "k8s-keychain", false,
"whether to use the kubernetes keychain instead of the default keychain (supports workload identity).")

o.RefOpts.AddFlags(cmd)
}

Expand All @@ -56,7 +63,21 @@ func (o *RegistryOptions) ClientOpts(ctx context.Context) ([]ociremote.Option, e
}

func (o *RegistryOptions) GetRegistryClientOpts(ctx context.Context) []remote.Option {
opts := defaultRegistryClientOpts(ctx)
opts := []remote.Option{
remote.WithContext(ctx),
remote.WithUserAgent("cosign/" + VersionInfo().GitVersion),
}

if o.KubernetesKeychain {
kc, err := k8schain.NewNoClient(ctx)
if err != nil {
panic(err.Error())
}
opts = append(opts, remote.WithAuthFromKeychain(kc))
} else {
opts = append(opts, remote.WithAuthFromKeychain(authn.DefaultKeychain))
}

if o != nil && o.AllowInsecure {
opts = append(opts, remote.WithTransport(&http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}})) // #nosec G402
}
Expand Down
7 changes: 4 additions & 3 deletions doc/cosign.md

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

5 changes: 3 additions & 2 deletions doc/cosign_attach.md

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

6 changes: 4 additions & 2 deletions doc/cosign_attach_sbom.md

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

6 changes: 4 additions & 2 deletions doc/cosign_attach_signature.md

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

6 changes: 4 additions & 2 deletions doc/cosign_attest.md

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

6 changes: 4 additions & 2 deletions doc/cosign_clean.md

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

5 changes: 3 additions & 2 deletions doc/cosign_completion.md

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

6 changes: 4 additions & 2 deletions doc/cosign_copy.md

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

5 changes: 3 additions & 2 deletions doc/cosign_dockerfile.md

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

6 changes: 4 additions & 2 deletions doc/cosign_dockerfile_verify.md

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

5 changes: 3 additions & 2 deletions doc/cosign_download.md

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

6 changes: 4 additions & 2 deletions doc/cosign_download_sbom.md

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

6 changes: 4 additions & 2 deletions doc/cosign_download_signature.md

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

5 changes: 3 additions & 2 deletions doc/cosign_generate-key-pair.md

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

6 changes: 4 additions & 2 deletions doc/cosign_generate.md

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

5 changes: 3 additions & 2 deletions doc/cosign_initialize.md

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

5 changes: 3 additions & 2 deletions doc/cosign_manifest.md

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

Loading