mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-13 19:28:55 +00:00
allow alternate image repositories (#2393)
* allow alternate image repositories Signed-off-by: Jim Bugwadia <jim@nirmata.com> * generate CRD YAMLs Signed-off-by: Jim Bugwadia <jim@nirmata.com>
This commit is contained in:
parent
8910d8287a
commit
23af42dc92
9 changed files with 68 additions and 3 deletions
|
@ -1520,6 +1520,12 @@ spec:
|
|||
description: Key is the PEM encoded public key that the
|
||||
image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository
|
||||
to use for image signatures that match this rule. If
|
||||
specified Repository will override the default OCI image
|
||||
repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
@ -4699,6 +4705,12 @@ spec:
|
|||
description: Key is the PEM encoded public key that the
|
||||
image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository
|
||||
to use for image signatures that match this rule. If
|
||||
specified Repository will override the default OCI image
|
||||
repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
|
|
@ -61,6 +61,7 @@ var (
|
|||
disableMetricsExport bool
|
||||
policyControllerResyncPeriod time.Duration
|
||||
imagePullSecrets string
|
||||
imageSignatureRepository string
|
||||
setupLog = log.Log.WithName("setup")
|
||||
)
|
||||
|
||||
|
@ -79,7 +80,8 @@ func main() {
|
|||
flag.BoolVar(&disableMetricsExport, "disable-metrics", false, "Set this flag to 'true', to enable exposing the metrics.")
|
||||
flag.StringVar(&metricsPort, "metrics-port", "8000", "Expose prometheus metrics at the given port, default to 8000.")
|
||||
flag.DurationVar(&policyControllerResyncPeriod, "background-scan", time.Hour, "Perform background scan every given interval, e.g., 30s, 15m, 1h.")
|
||||
flag.StringVar(&imagePullSecrets, "imagePullSecrets", "", "Secret resource names for image registry access credentials")
|
||||
flag.StringVar(&imagePullSecrets, "imagePullSecrets", "", "Secret resource names for image registry access credentials.")
|
||||
flag.StringVar(&imageSignatureRepository, "imageSignatureRepository", "", "Alternate repository for image signatures. Can be overridden per rule via `verifyImages.Repository`.")
|
||||
|
||||
if err := flag.Set("v", "2"); err != nil {
|
||||
setupLog.Error(err, "failed to set log level")
|
||||
|
@ -164,6 +166,10 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
if imageSignatureRepository != "" {
|
||||
cosign.ImageSignatureRepository = imageSignatureRepository
|
||||
}
|
||||
|
||||
// KYVERNO CRD INFORMER
|
||||
// watches CRD resources:
|
||||
// - ClusterPolicy, Policy
|
||||
|
|
|
@ -1514,6 +1514,12 @@ spec:
|
|||
description: Key is the PEM encoded public key that the
|
||||
image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository
|
||||
to use for image signatures that match this rule. If
|
||||
specified Repository will override the default OCI image
|
||||
repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
|
|
@ -1515,6 +1515,12 @@ spec:
|
|||
description: Key is the PEM encoded public key that the
|
||||
image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository
|
||||
to use for image signatures that match this rule. If
|
||||
specified Repository will override the default OCI image
|
||||
repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
|
|
@ -952,6 +952,9 @@ spec:
|
|||
key:
|
||||
description: Key is the PEM encoded public key that the image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository to use for image signatures that match this rule. If specified Repository will override the default OCI image repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
@ -3132,6 +3135,9 @@ spec:
|
|||
key:
|
||||
description: Key is the PEM encoded public key that the image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository to use for image signatures that match this rule. If specified Repository will override the default OCI image repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
|
|
@ -939,6 +939,9 @@ spec:
|
|||
key:
|
||||
description: Key is the PEM encoded public key that the image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository to use for image signatures that match this rule. If specified Repository will override the default OCI image repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
@ -3091,6 +3094,9 @@ spec:
|
|||
key:
|
||||
description: Key is the PEM encoded public key that the image is signed with.
|
||||
type: string
|
||||
repository:
|
||||
description: Repository is an optional alternate OCI repository to use for image signatures that match this rule. If specified Repository will override the default OCI image repository configured for the installation.
|
||||
type: string
|
||||
type: object
|
||||
type: array
|
||||
type: object
|
||||
|
|
|
@ -435,6 +435,10 @@ type ImageVerification struct {
|
|||
|
||||
// Key is the PEM encoded public key that the image is signed with.
|
||||
Key string `json:"key,omitempty" yaml:"key,omitempty"`
|
||||
|
||||
// Repository is an optional alternate OCI repository to use for image signatures that match this rule.
|
||||
// If specified Repository will override the default OCI image repository configured for the installation.
|
||||
Repository string `json:"repository,omitempty" yaml:"repository,omitempty"`
|
||||
}
|
||||
|
||||
// Generation defines how new resources should be created and managed.
|
||||
|
|
|
@ -19,6 +19,11 @@ import (
|
|||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
var (
|
||||
// Alternate signature repository
|
||||
ImageSignatureRepository string
|
||||
)
|
||||
|
||||
// Initialize loads the image pull secrets and initializes the default auth method for container registry API calls
|
||||
func Initialize(client kubernetes.Interface, namespace, serviceAccount string, imagePullSecrets []string) error {
|
||||
var kc authn.Keychain
|
||||
|
@ -37,7 +42,7 @@ func Initialize(client kubernetes.Interface, namespace, serviceAccount string, i
|
|||
return nil
|
||||
}
|
||||
|
||||
func Verify(imageRef string, key []byte, log logr.Logger) (digest string, err error) {
|
||||
func Verify(imageRef string, key []byte, repository string, log logr.Logger) (digest string, err error) {
|
||||
pubKey, err := decodePEM(key)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to decode PEM %v", string(key))
|
||||
|
@ -56,6 +61,16 @@ func Verify(imageRef string, key []byte, log logr.Logger) (digest string, err er
|
|||
return "", errors.Wrap(err, "failed to parse image")
|
||||
}
|
||||
|
||||
cosignOpts.SignatureRepo = ref.Context()
|
||||
if repository != "" {
|
||||
signatureRepo, err := name.NewRepository(repository)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to parse signature repository %s", repository)
|
||||
}
|
||||
|
||||
cosignOpts.SignatureRepo = signatureRepo
|
||||
}
|
||||
|
||||
verified, err := cosign.Verify(context.Background(), ref, cosignOpts)
|
||||
if err != nil {
|
||||
msg := err.Error()
|
||||
|
|
|
@ -64,6 +64,10 @@ func VerifyAndPatchImages(policyContext *PolicyContext) (resp *response.EngineRe
|
|||
func verifyAndPatchImages(logger logr.Logger, policyContext *PolicyContext, rule *v1.Rule, imageVerify *v1.ImageVerification, images map[string]*context.ImageInfo, resp *response.EngineResponse) {
|
||||
imagePattern := imageVerify.Image
|
||||
key := imageVerify.Key
|
||||
repository := cosign.ImageSignatureRepository
|
||||
if imageVerify.Repository != "" {
|
||||
repository = imageVerify.Repository
|
||||
}
|
||||
|
||||
for _, imageInfo := range images {
|
||||
image := imageInfo.String()
|
||||
|
@ -88,7 +92,7 @@ func verifyAndPatchImages(logger logr.Logger, policyContext *PolicyContext, rule
|
|||
}
|
||||
|
||||
start := time.Now()
|
||||
digest, err := cosign.Verify(image, []byte(key), logger)
|
||||
digest, err := cosign.Verify(image, []byte(key), repository, logger)
|
||||
if err != nil {
|
||||
logger.Info("failed to verify image", "image", image, "key", key, "error", err, "duration", time.Since(start).Seconds())
|
||||
ruleResp.Success = false
|
||||
|
|
Loading…
Add table
Reference in a new issue