Add insecure-registry and tls-skip-verify-registry flags (#537)

This commit is contained in:
Daisuke Taniwaki 2019-01-30 04:29:47 +09:00 committed by dlorenc
parent e3bf9fb062
commit f8f59ea4c6
8 changed files with 74 additions and 27 deletions

View File

@ -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._ _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 #### --cleanup
Set this flag to clean the filesystem at the end of the build. Set this flag to clean the filesystem at the end of the build.

View File

@ -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.Cache, "cache", "", false, "Use cache when building image")
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end") 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().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 // addHiddenFlags marks certain flags as hidden from the executor help text

21
pkg/cache/cache.go vendored
View File

@ -17,7 +17,9 @@ limitations under the License.
package cache package cache
import ( import (
"crypto/tls"
"fmt" "fmt"
"net/http"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -55,7 +57,24 @@ func (rc *RegistryCache) RetrieveLayer(ck string) (v1.Image, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("getting reference for %s", cache)) 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -42,3 +42,12 @@ func (b *multiArg) Set(value string) error {
func (b *multiArg) Type() string { func (b *multiArg) Type() string {
return "multi-arg type" return "multi-arg type"
} }
func (b *multiArg) Contains(v string) bool {
for _, s := range *b {
if s == v {
return true
}
}
return false
}

View File

@ -22,26 +22,28 @@ import (
// KanikoOptions are options that are set by command line arguments // KanikoOptions are options that are set by command line arguments
type KanikoOptions struct { type KanikoOptions struct {
DockerfilePath string DockerfilePath string
SrcContext string SrcContext string
SnapshotMode string SnapshotMode string
Bucket string Bucket string
TarPath string TarPath string
Target string Target string
CacheRepo string CacheRepo string
CacheDir string CacheDir string
Destinations multiArg Destinations multiArg
BuildArgs multiArg BuildArgs multiArg
Insecure bool Insecure bool
SkipTLSVerify bool SkipTLSVerify bool
InsecurePull bool InsecurePull bool
SkipTLSVerifyPull bool SkipTLSVerifyPull bool
SingleSnapshot bool SingleSnapshot bool
Reproducible bool Reproducible bool
NoPush bool NoPush bool
Cache bool Cache bool
Cleanup bool Cleanup bool
CacheTTL time.Duration CacheTTL time.Duration
InsecureRegistries multiArg
SkipTLSVerifyRegistries multiArg
} }
// WarmerOptions are options that are set by command line arguments to the cache warmer. // WarmerOptions are options that are set by command line arguments to the cache warmer.

View File

@ -145,6 +145,7 @@ func (s *stageBuilder) optimize(compositeKey CompositeCache, cfg v1.Config, cmds
if command.ShouldCacheOutput() { if command.ShouldCacheOutput() {
img, err := layerCache.RetrieveLayer(ck) img, err := layerCache.RetrieveLayer(ck)
if err != nil { if err != nil {
logrus.Debugf("Failed to retrieve layer: %s", err)
logrus.Infof("No cached layer found for cmd %s", command.String()) logrus.Infof("No cached layer found for cmd %s", command.String())
logrus.Debugf("Key missing was: %s", compositeKey.Key()) logrus.Debugf("Key missing was: %s", compositeKey.Key())
break break

View File

@ -73,8 +73,9 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
// continue pushing unless an error occurs // continue pushing unless an error occurs
for _, destRef := range destRefs { for _, destRef := range destRefs {
if opts.Insecure { registryName := destRef.Repository.Registry.Name()
newReg, err := name.NewInsecureRegistry(destRef.Repository.Registry.Name(), name.WeakValidation) if opts.Insecure || opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewInsecureRegistry(registryName, name.WeakValidation)
if err != nil { if err != nil {
return errors.Wrap(err, "getting new insecure registry") return errors.Wrap(err, "getting new insecure registry")
} }
@ -88,7 +89,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
// Create a transport to set our user-agent. // Create a transport to set our user-agent.
tr := http.DefaultTransport tr := http.DefaultTransport
if opts.SkipTLSVerify { if opts.SkipTLSVerify || opts.SkipTLSVerifyRegistries.Contains(registryName) {
tr.(*http.Transport).TLSClientConfig = &tls.Config{ tr.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }
@ -135,5 +136,7 @@ func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath strin
} }
cacheOpts := *opts cacheOpts := *opts
cacheOpts.Destinations = []string{cache} cacheOpts.Destinations = []string{cache}
cacheOpts.InsecureRegistries = opts.InsecureRegistries
cacheOpts.SkipTLSVerifyRegistries = opts.SkipTLSVerifyRegistries
return DoPush(empty, &cacheOpts) return DoPush(empty, &cacheOpts)
} }

View File

@ -100,8 +100,9 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
return nil, err return nil, err
} }
if opts.InsecurePull { registryName := ref.Context().RegistryStr()
newReg, err := name.NewInsecureRegistry(ref.Context().RegistryStr(), name.WeakValidation) if opts.InsecurePull || opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewInsecureRegistry(registryName, name.WeakValidation)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -116,7 +117,7 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
} }
tr := http.DefaultTransport.(*http.Transport) tr := http.DefaultTransport.(*http.Transport)
if opts.SkipTLSVerifyPull { if opts.SkipTLSVerifyPull || opts.SkipTLSVerifyRegistries.Contains(registryName) {
tr.TLSClientConfig = &tls.Config{ tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }