2022-10-12 08:52:42 +02:00
package webhook
import (
2022-10-27 15:35:32 +05:30
"encoding/json"
2022-10-12 08:52:42 +02:00
"testing"
2022-10-27 15:35:32 +05:30
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/autogen"
2022-10-12 08:52:42 +02:00
"gotest.tools/assert"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2023-03-13 10:27:49 +01:00
"k8s.io/apimachinery/pkg/runtime/schema"
2022-10-12 08:52:42 +02:00
)
func Test_webhook_isEmpty ( t * testing . T ) {
2024-01-27 21:00:22 +08:00
empty := newWebhook ( DefaultWebhookTimeout , admissionregistrationv1 . Ignore , [ ] admissionregistrationv1 . MatchCondition { } )
2022-10-12 08:52:42 +02:00
assert . Equal ( t , empty . isEmpty ( ) , true )
2024-01-27 21:00:22 +08:00
notEmpty := newWebhook ( DefaultWebhookTimeout , admissionregistrationv1 . Ignore , [ ] admissionregistrationv1 . MatchCondition { } )
2024-01-26 16:07:42 +01:00
notEmpty . set ( GroupVersionResourceScope {
GroupVersionResource : schema . GroupVersionResource { Group : "" , Version : "v1" , Resource : "pods" } ,
Scope : admissionregistrationv1 . NamespacedScope ,
2023-03-13 15:44:39 +01:00
} )
2022-10-12 08:52:42 +02:00
assert . Equal ( t , notEmpty . isEmpty ( ) , false )
}
2022-10-27 15:35:32 +05:30
var policy = `
{
"apiVersion" : "kyverno.io/v1" ,
"kind" : "ClusterPolicy" ,
"metadata" : {
"name" : "disallow-unsigned-images"
} ,
"spec" : {
"validationFailureAction" : "enforce" ,
"background" : false ,
"rules" : [
{
"name" : "replace-image-registry" ,
"match" : {
"any" : [
{
"resources" : {
"kinds" : [
"Pod"
]
}
}
]
} ,
"mutate" : {
"foreach" : [
{
"list" : "request.object.spec.containers" ,
"patchStrategicMerge" : {
"spec" : {
"containers" : [
{
"name" : "{{ element.name }}" ,
"image" : "{{ regex_replace_all_literal('.*(.*)/', '{{element.image}}', 'pratikrshah/' )}}"
}
]
}
}
}
]
}
} ,
{
"name" : "disallow-unsigned-images-rule" ,
"match" : {
"any" : [
{
"resources" : {
"kinds" : [
"Pod"
]
}
}
]
} ,
"verifyImages" : [
{
"imageReferences" : [
"*"
] ,
"verifyDigest" : false ,
"required" : null ,
"mutateDigest" : false ,
"attestors" : [
{
"count" : 1 ,
"entries" : [
{
"keys" : {
"publicKeys" : "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEHsra9WSDxt9qv84KF4McNVCGjMFq\ne96mWCQxGimL9Ltj6F3iXmlo8sUalKfJ7SBXpy8hwyBfXBBAmCalsp5xEw==\n-----END PUBLIC KEY-----"
}
}
]
}
]
}
]
} ,
{
"name" : "check-image" ,
"match" : {
"any" : [
{
"resources" : {
"kinds" : [
"Pod"
]
}
}
]
} ,
"context" : [
{
"name" : "keys" ,
"configMap" : {
"name" : "keys" ,
"namespace" : "default"
}
}
] ,
"verifyImages" : [
{
"imageReferences" : [
"ghcr.io/myorg/myimage*"
] ,
"required" : true ,
"attestors" : [
{
"count" : 1 ,
"entries" : [
{
"keys" : {
"publicKeys" : "{{ keys.data.production }}"
}
}
]
}
]
}
]
}
]
}
}
`
func Test_RuleCount ( t * testing . T ) {
var cpol kyverno . ClusterPolicy
err := json . Unmarshal ( [ ] byte ( policy ) , & cpol )
assert . NilError ( t , err )
status := cpol . GetStatus ( )
rules := autogen . ComputeRules ( & cpol )
setRuleCount ( rules , status )
assert . Equal ( t , status . RuleCount . Validate , 0 )
assert . Equal ( t , status . RuleCount . Generate , 0 )
assert . Equal ( t , status . RuleCount . Mutate , 1 )
assert . Equal ( t , status . RuleCount . VerifyImages , 2 )
}