mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-17 19:01:57 +02:00
* Add mTLS (client cert) support Add support for Mutual TLS (mTLS) client certificate authentication. The expected format of the new --registry-client-cert flag is the same as the existing --registry-certificate flag, which will allow different client certificates for different registries: --registry-client-cert my.registry.url=/path/to/cert.crt,/path/to/key.key * tidy: Rename mTLS (Client Cert) flag to be in line with others This flag didn't describe that it was for the client certs uses with the registry. Although this should be reasonably obvious, I like the consistency with the other registry flag. * test: Added unit tests for mTLS (Client Cert) loading * test: Add 2 more tests for comma split formatting since the comma splitting is a new portion of code let's make sure that that format works well too in other cases * tidy: Fix formatting of flag help text * tidy: Made invalid cert format error consistent I was running the tests and saw the message: Failed to load client certificate/key '/path/to/client/certificate.cert' for my.registry.name, format is my.registry.name=/path/to/cert,/path/to/key I then realized that it'd be a lot nicer if this showed the user what they input, and how they should change it (rather than decomposing it: Failed to load client certificate/key 'my.registry.name=/path/to/client/certificate.cert', expected format: my.registry.name=/path/to/cert,/path/to/key * test: Fixed incorrect test argument This didn't fail the test before because it's only attempting to show that certs only get loaded and used for their associated registry but it's important to keep this correct. This case is covered by the test below, "RegistriesClientCertificates incorrect cert format" * doc: Add new flag to README.md * mod: Fail to push if there was a problem loading client certs Rather than warning that there was an issue, we should fail if the requested client certificates were not found or failed to load. This feels a lot better than waiting for the build to finish then failing later. * mod: Return an error if the certificate authority fails to load, just like client certs The MakeTransport function was changed in the previous commit to allow returning errors if there was a problem loading certificates, rather than just print warnings. This feels a lot better as you get the error immediately that there's a problem to fix, rather than getting a warning, then later an error that the server's certificate could not be verified. * tidy: fix golint issues
145 lines
4.4 KiB
Go
145 lines
4.4 KiB
Go
/*
|
|
Copyright 2018 Google LLC
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package remote
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
|
"github.com/GoogleContainerTools/kaniko/pkg/creds"
|
|
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
manifestCache = make(map[string]v1.Image)
|
|
)
|
|
|
|
// RetrieveRemoteImage retrieves the manifest for the specified image from the specified registry
|
|
func RetrieveRemoteImage(image string, opts config.RegistryOptions, customPlatform string) (v1.Image, error) {
|
|
logrus.Infof("Retrieving image manifest %s", image)
|
|
|
|
cachedRemoteImage := manifestCache[image]
|
|
if cachedRemoteImage != nil {
|
|
logrus.Infof("Returning cached image manifest")
|
|
return cachedRemoteImage, nil
|
|
}
|
|
|
|
ref, err := name.ParseReference(image, name.WeakValidation)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if ref.Context().RegistryStr() == name.DefaultRegistry {
|
|
ref, err := normalizeReference(ref, image)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, registryMirror := range opts.RegistryMirrors {
|
|
var newReg name.Registry
|
|
if opts.InsecurePull || opts.InsecureRegistries.Contains(registryMirror) {
|
|
newReg, err = name.NewRegistry(registryMirror, name.WeakValidation, name.Insecure)
|
|
} else {
|
|
newReg, err = name.NewRegistry(registryMirror, name.StrictValidation)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ref := setNewRegistry(ref, newReg)
|
|
|
|
logrus.Infof("Retrieving image %s from registry mirror %s", ref, registryMirror)
|
|
remoteImage, err := remote.Image(ref, remoteOptions(registryMirror, opts, customPlatform)...)
|
|
if err != nil {
|
|
logrus.Warnf("Failed to retrieve image %s from registry mirror %s: %s. Will try with the next mirror, or fallback to the default registry.", ref, registryMirror, err)
|
|
continue
|
|
}
|
|
|
|
manifestCache[image] = remoteImage
|
|
|
|
return remoteImage, nil
|
|
}
|
|
}
|
|
|
|
registryName := ref.Context().RegistryStr()
|
|
if opts.InsecurePull || opts.InsecureRegistries.Contains(registryName) {
|
|
newReg, err := name.NewRegistry(registryName, name.WeakValidation, name.Insecure)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ref = setNewRegistry(ref, newReg)
|
|
}
|
|
|
|
logrus.Infof("Retrieving image %s from registry %s", ref, registryName)
|
|
|
|
remoteImage, err := remote.Image(ref, remoteOptions(registryName, opts, customPlatform)...)
|
|
|
|
if remoteImage != nil {
|
|
manifestCache[image] = remoteImage
|
|
}
|
|
|
|
return remoteImage, err
|
|
}
|
|
|
|
// normalizeReference adds the library/ prefix to images without it.
|
|
//
|
|
// It is mostly useful when using a registry mirror that is not able to perform
|
|
// this fix automatically.
|
|
func normalizeReference(ref name.Reference, image string) (name.Reference, error) {
|
|
if !strings.ContainsRune(image, '/') {
|
|
return name.ParseReference("library/"+image, name.WeakValidation)
|
|
}
|
|
|
|
return ref, nil
|
|
}
|
|
|
|
func setNewRegistry(ref name.Reference, newReg name.Registry) name.Reference {
|
|
switch r := ref.(type) {
|
|
case name.Tag:
|
|
r.Repository.Registry = newReg
|
|
return r
|
|
case name.Digest:
|
|
r.Repository.Registry = newReg
|
|
return r
|
|
default:
|
|
return ref
|
|
}
|
|
}
|
|
|
|
func remoteOptions(registryName string, opts config.RegistryOptions, customPlatform string) []remote.Option {
|
|
tr, err := util.MakeTransport(opts, registryName)
|
|
|
|
// The MakeTransport function will only return errors if there was a problem
|
|
// with registry certificates (Verification or mTLS)
|
|
if err != nil {
|
|
logrus.Fatalf("Unable to setup transport for registry %q: %v", customPlatform, err)
|
|
}
|
|
|
|
// The platform value has previously been validated.
|
|
platform, err := v1.ParsePlatform(customPlatform)
|
|
if err != nil {
|
|
logrus.Fatalf("Invalid platform %q: %v", customPlatform, err)
|
|
}
|
|
|
|
return []remote.Option{remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain()), remote.WithPlatform(*platform)}
|
|
}
|