1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 10:28:36 +00:00

fix imageRef matching (#5956)

Signed-off-by: ShutingZhao <shuting@nirmata.com>

Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
shuting 2023-01-10 17:44:31 +08:00 committed by GitHub
parent 91f2622384
commit 68fb237d25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 108 additions and 0 deletions

View file

@ -339,6 +339,10 @@ func (iv *imageVerifier) verifyImage(
}
if len(imageVerify.Attestors) > 0 {
if !matchImageReferences(imageVerify.ImageReferences, imageInfo.String()) {
return nil, ""
}
ruleResp, cosignResp := iv.verifyAttestors(ctx, imageVerify.Attestors, imageVerify, imageInfo, "")
if ruleResp.Status != response.RuleStatusPass {
return ruleResp, ""
@ -722,3 +726,12 @@ func evaluateConditions(
pass := variables.EvaluateAnyAllConditions(log, ctx, c)
return pass, nil
}
func matchImageReferences(imageReferences []string, image string) bool {
for _, imageRef := range imageReferences {
if wildcard.Match(imageRef, image) {
return true
}
}
return false
}

View file

@ -0,0 +1,9 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: validate-signatures
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,53 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: validate-signatures
annotations:
pod-policies.kyverno.io/autogen-controllers: none
spec:
validationFailureAction: enforce
webhookTimeoutSeconds: 30
background: false
rules:
- name: check-1
match:
any:
- resources:
kinds:
- Pod
verifyImages:
- attestors:
- count: 1
entries:
- keys:
publicKeys: |-
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE8nXRh950IZbRj8Ra/N9sbqOPZrfM
5/KAQN0/KjHcorm/J5yctVd7iEcnessRQjU917hmKO6JWVGHpDguIyakZA==
-----END PUBLIC KEY-----
imageReferences:
- ghcr.io/kyverno/test-verify-image:*
mutateDigest: true
required: true
verifyDigest: true
- name: check-2
match:
any:
- resources:
kinds:
- Pod
verifyImages:
- attestors:
- count: 1
entries:
- keys:
publicKeys: |-
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOUD2uzRHLnx1oH6XAnF+8haL73BF
zh9pMI1x1/c4Nj/w+rsrgMCDyV/S8hmsXEbizhYD3QndVtV1piBDfDIb8w==
-----END PUBLIC KEY-----
imageReferences:
- my.local.repo/*
mutateDigest: false
required: true
verifyDigest: false

View file

@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: signed
namespace: default
spec:
containers:
- name: signed

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: signed
namespace: default
spec:
containers:
- image: ghcr.io/kyverno/test-verify-image:signed
imagePullPolicy: IfNotPresent
name: signed

View file

@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- command: kubectl delete -f 01-policy.yaml,02-pod.yaml --force --wait=true --ignore-not-found=true

View file

@ -0,0 +1,11 @@
## Description
A `VerifyImages` rule specifying multiple attestors should allow pod creation with valid images.
## Expected Behavior
The pod `signed` should be created successfully.
## Reference Issue(s)
Slack discussion - https://kubernetes.slack.com/archives/CLGR9BJU9/p1673303296239259.