Skip to content

Commit

Permalink
skip tls verification if default transport is used with insecure option
Browse files Browse the repository at this point in the history
Signed-off-by: Jose R. Gonzalez <jose@flutes.dev>
  • Loading branch information
komish committed Feb 7, 2023
1 parent 62f183e commit 223f982
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 650 deletions.
619 changes: 0 additions & 619 deletions go.sum

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion pkg/crane/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package crane

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

"github.com/google/go-containerregistry/pkg/authn"
Expand All @@ -30,6 +31,10 @@ type Options struct {
Remote []remote.Option
Platform *v1.Platform
Keychain authn.Keychain

transport *http.Transport
insecure bool
transportSet bool
}

// GetOptions exposes the underlying []remote.Option, []name.Option, and
Expand All @@ -47,26 +52,44 @@ func makeOptions(opts ...Option) Options {
},
Keychain: authn.DefaultKeychain,
}

for _, o := range opts {
o(&opt)
}

// Allow for untrusted certificates if the user passed Insecure but no custom
// transport.
if opt.insecure && !opt.transportSet {
opt.transport = remote.DefaultTransport.(*http.Transport).Clone()
opt.transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true, //nolint: gosec
}

WithTransport(opt.transport)(&opt)
}

return opt
}

// Option is a functional option for crane.
type Option func(*Options)

// WithTransport is a functional option for overriding the default transport
// for remote operations.
// for remote operations. Setting a transport will override the Insecure option's
// configuration allowing for image registries to use untrusted certificates.
func WithTransport(t http.RoundTripper) Option {
return func(o *Options) {
o.Remote = append(o.Remote, remote.WithTransport(t))
o.transportSet = true
}
}

// Insecure is an Option that allows image references to be fetched without TLS.
// This will also allow for untrusted (e.g. self-signed) certificates in cases where
// the default transport is used (i.e. when WithTransport is not used).
func Insecure(o *Options) {
o.Name = append(o.Name, name.Insecure)
o.insecure = true
}

// WithPlatform is an Option to specify the platform.
Expand Down
39 changes: 39 additions & 0 deletions pkg/crane/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package crane

import (
"errors"
"testing"

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

func TestInsecureOptionTracking(t *testing.T) {
want := true
opts := GetOptions(Insecure)

if got := opts.insecure; got != want {
t.Errorf("got %t\nwant: %t", got, want)
}
}

func TestTransportOptionTracking(t *testing.T) {
want := true
opts := GetOptions(WithTransport(remote.DefaultTransport))

if got := opts.transportSet; got != want {
t.Errorf("got %t\nwant: %t", got, want)
}
}

func TestInsecureTransport(t *testing.T) {
want := true
opts := GetOptions(Insecure)
if opts.transport.TLSClientConfig == nil {
t.Fatal(errors.New("TLSClientConfig was nil and should be set"))

}

if got := opts.transport.TLSClientConfig.InsecureSkipVerify; got != want {
t.Errorf("got: %t\nwant: %t", got, want)
}
}
34 changes: 17 additions & 17 deletions pkg/v1/fake/image.go

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

26 changes: 13 additions & 13 deletions pkg/v1/fake/index.go

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

10 changes: 10 additions & 0 deletions pkg/v1/zz_deepcopy_generated.go

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

0 comments on commit 223f982

Please sign in to comment.