Skip to content

Commit

Permalink
Make k8s keychain vs default keychain configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
  • Loading branch information
marcofranssen committed Feb 14, 2022
1 parent 9a1ac90 commit 0d93f72
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/slsa-provenance/cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func OCI() *cobra.Command {
return err
}

opts := oci.WithDefaultClientOptions(cmd.Context(), true)
opts := o.GetRegistryClientOpts(cmd.Context())
subjecter := oci.NewContainerSubjecter(repo, digest, tags, opts...)

env := &github.Environment{
Expand Down
21 changes: 18 additions & 3 deletions cmd/slsa-provenance/cli/options/oci.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package options

import (
"context"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"

"github.com/philips-labs/slsa-provenance-action/lib/oci"
)

// OCIOptions Commandline flags used for the generate oci command.
type OCIOptions struct {
GenerateOptions
Repository string
Digest string
Tags []string
Repository string
Digest string
Tags []string
AllowInsecure bool
KubernetesKeychain bool
}

// GetRepository The oci repository to search for the given tags.
Expand Down Expand Up @@ -39,4 +46,12 @@ func (o *OCIOptions) AddFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&o.Repository, "repository", "", "The repository of the oci artifact.")
cmd.PersistentFlags().StringVar(&o.Digest, "digest", "", "The digest for the oci artifact.")
cmd.PersistentFlags().StringSliceVar(&o.Tags, "tags", []string{"latest"}, "The given tags for this oci release.")
cmd.Flags().BoolVar(&o.AllowInsecure, "allow-insecure", 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).")
}

// GetRegistryClientOpts sets some sane default options for crane to authenticate
// private registries
func (o *OCIOptions) GetRegistryClientOpts(ctx context.Context) []crane.Option {
return oci.WithDefaultClientOptions(ctx, o.KubernetesKeychain, o.AllowInsecure)
}
8 changes: 7 additions & 1 deletion lib/oci/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package oci

import (
"context"
"crypto/tls"
"net/http"

"github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
Expand All @@ -13,7 +15,7 @@ import (

// WithDefaultClientOptions sets some sane default options for crane to authenticate
// private registries
func WithDefaultClientOptions(ctx context.Context, k8sKeychain bool) []crane.Option {
func WithDefaultClientOptions(ctx context.Context, k8sKeychain, allowInsecure bool) []crane.Option {
opts := []crane.Option{
crane.WithContext(ctx),
}
Expand All @@ -30,5 +32,9 @@ func WithDefaultClientOptions(ctx context.Context, k8sKeychain bool) []crane.Opt
opts = append(opts, crane.WithAuthFromKeychain(authn.DefaultKeychain))
}

if allowInsecure {
opts = append(opts, crane.WithTransport(&http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}})) // #nosec G402
}

return opts
}
2 changes: 1 addition & 1 deletion lib/oci/subjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestSubjects(t *testing.T) {

repo := "ghcr.io/philips-labs/slsa-provenance"

opts := WithDefaultClientOptions(context.Background(), false)
opts := WithDefaultClientOptions(context.Background(), false, false)

errorCases := []struct {
name string
Expand Down

0 comments on commit 0d93f72

Please sign in to comment.