Skip to content

Commit

Permalink
Add insecure-registry and tls-skip-verify-registry flags (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki authored and dlorenc committed Jan 29, 2019
1 parent e3bf9fb commit f8f59ea
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 27 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ If `--destination=gcr.io/kaniko-project/test`, then cached layers will be stored

_This flag must be used in conjunction with the `--cache=true` flag._

#### --insecure-registry

Set this flag to use plain HTTP requests when accessing a registry. It is supposed to be useed for testing purposes only and should not be used in production!
You can set it multiple times for multiple registries.

#### --skip-tls-verify-registry

Set this flag to skip TLS cerificate validation when accessing a registry. It is supposed to be useed for testing purposes only and should not be used in production!
You can set it multiple times for multiple registries.

#### --cleanup

Set this flag to clean the filesystem at the end of the build.
Expand Down
2 changes: 2 additions & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func addKanikoOptionsFlags(cmd *cobra.Command) {
RootCmd.PersistentFlags().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image")
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end")
RootCmd.PersistentFlags().DurationVarP(&opts.CacheTTL, "cache-ttl", "", time.Hour*336, "Cache timeout in hours. Defaults to two weeks.")
RootCmd.PersistentFlags().VarP(&opts.InsecureRegistries, "insecure-registry", "", "Insecure registry using plain HTTP to push and pull. Set it repeatedly for multiple registries.")
RootCmd.PersistentFlags().VarP(&opts.SkipTLSVerifyRegistries, "skip-tls-verify-registry", "", "Insecure registry ignoring TLS verify to push and pull. Set it repeatedly for multiple registries.")
}

// addHiddenFlags marks certain flags as hidden from the executor help text
Expand Down
21 changes: 20 additions & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package cache

import (
"crypto/tls"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -55,7 +57,24 @@ func (rc *RegistryCache) RetrieveLayer(ck string) (v1.Image, error) {
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("getting reference for %s", cache))
}
img, err := remote.Image(cacheRef, remote.WithAuthFromKeychain(creds.GetKeychain()))

registryName := cacheRef.Repository.Registry.Name()
if rc.Opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewInsecureRegistry(registryName, name.WeakValidation)
if err != nil {
return nil, err
}
cacheRef.Repository.Registry = newReg
}

tr := http.DefaultTransport.(*http.Transport)
if rc.Opts.SkipTLSVerifyRegistries.Contains(registryName) {
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}

img, err := remote.Image(cacheRef, remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain()))
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ func (b *multiArg) Set(value string) error {
func (b *multiArg) Type() string {
return "multi-arg type"
}

func (b *multiArg) Contains(v string) bool {
for _, s := range *b {
if s == v {
return true
}
}
return false
}
42 changes: 22 additions & 20 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ import (

// KanikoOptions are options that are set by command line arguments
type KanikoOptions struct {
DockerfilePath string
SrcContext string
SnapshotMode string
Bucket string
TarPath string
Target string
CacheRepo string
CacheDir string
Destinations multiArg
BuildArgs multiArg
Insecure bool
SkipTLSVerify bool
InsecurePull bool
SkipTLSVerifyPull bool
SingleSnapshot bool
Reproducible bool
NoPush bool
Cache bool
Cleanup bool
CacheTTL time.Duration
DockerfilePath string
SrcContext string
SnapshotMode string
Bucket string
TarPath string
Target string
CacheRepo string
CacheDir string
Destinations multiArg
BuildArgs multiArg
Insecure bool
SkipTLSVerify bool
InsecurePull bool
SkipTLSVerifyPull bool
SingleSnapshot bool
Reproducible bool
NoPush bool
Cache bool
Cleanup bool
CacheTTL time.Duration
InsecureRegistries multiArg
SkipTLSVerifyRegistries multiArg
}

// WarmerOptions are options that are set by command line arguments to the cache warmer.
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (s *stageBuilder) optimize(compositeKey CompositeCache, cfg v1.Config, cmds
if command.ShouldCacheOutput() {
img, err := layerCache.RetrieveLayer(ck)
if err != nil {
logrus.Debugf("Failed to retrieve layer: %s", err)
logrus.Infof("No cached layer found for cmd %s", command.String())
logrus.Debugf("Key missing was: %s", compositeKey.Key())
break
Expand Down
9 changes: 6 additions & 3 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {

// continue pushing unless an error occurs
for _, destRef := range destRefs {
if opts.Insecure {
newReg, err := name.NewInsecureRegistry(destRef.Repository.Registry.Name(), name.WeakValidation)
registryName := destRef.Repository.Registry.Name()
if opts.Insecure || opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewInsecureRegistry(registryName, name.WeakValidation)
if err != nil {
return errors.Wrap(err, "getting new insecure registry")
}
Expand All @@ -88,7 +89,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {

// Create a transport to set our user-agent.
tr := http.DefaultTransport
if opts.SkipTLSVerify {
if opts.SkipTLSVerify || opts.SkipTLSVerifyRegistries.Contains(registryName) {
tr.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
Expand Down Expand Up @@ -135,5 +136,7 @@ func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath strin
}
cacheOpts := *opts
cacheOpts.Destinations = []string{cache}
cacheOpts.InsecureRegistries = opts.InsecureRegistries
cacheOpts.SkipTLSVerifyRegistries = opts.SkipTLSVerifyRegistries
return DoPush(empty, &cacheOpts)
}
7 changes: 4 additions & 3 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
return nil, err
}

if opts.InsecurePull {
newReg, err := name.NewInsecureRegistry(ref.Context().RegistryStr(), name.WeakValidation)
registryName := ref.Context().RegistryStr()
if opts.InsecurePull || opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewInsecureRegistry(registryName, name.WeakValidation)
if err != nil {
return nil, err
}
Expand All @@ -116,7 +117,7 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
}

tr := http.DefaultTransport.(*http.Transport)
if opts.SkipTLSVerifyPull {
if opts.SkipTLSVerifyPull || opts.SkipTLSVerifyRegistries.Contains(registryName) {
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
Expand Down

0 comments on commit f8f59ea

Please sign in to comment.