2020-07-02 19:49:10 +00:00
|
|
|
package policycache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-05-06 19:02:06 +00:00
|
|
|
"fmt"
|
2020-07-02 19:49:10 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-10-29 16:13:20 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-03-28 14:01:27 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2021-05-06 19:02:06 +00:00
|
|
|
lv1 "github.com/kyverno/kyverno/pkg/client/listers/kyverno/v1"
|
2020-07-02 19:49:10 +00:00
|
|
|
"gotest.tools/assert"
|
2021-05-06 19:02:06 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2020-07-02 19:49:10 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
|
|
)
|
|
|
|
|
2021-05-06 19:02:06 +00:00
|
|
|
type dummyLister struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl dummyLister) List(selector labels.Selector) (ret []*kyverno.ClusterPolicy, err error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl dummyLister) Get(name string) (*kyverno.ClusterPolicy, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl dummyLister) ListResources(selector labels.Selector) (ret []*kyverno.ClusterPolicy, err error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// type dymmyNsNamespace struct {}
|
|
|
|
|
|
|
|
type dummyNsLister struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl dummyNsLister) Policies(name string) lv1.PolicyNamespaceLister {
|
|
|
|
return dummyNsLister{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl dummyNsLister) List(selector labels.Selector) (ret []*kyverno.Policy, err error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl dummyNsLister) Get(name string) (*kyverno.Policy, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2020-07-02 19:49:10 +00:00
|
|
|
func Test_All(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-07-02 19:49:10 +00:00
|
|
|
policy := newPolicy(t)
|
2021-05-06 19:02:06 +00:00
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-06 19:02:06 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
2020-07-02 19:49:10 +00:00
|
|
|
|
2021-05-06 19:02:06 +00:00
|
|
|
// get
|
2021-05-08 01:07:41 +00:00
|
|
|
mutate := pCache.get(Mutate, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
2020-07-02 19:49:10 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
|
|
|
}
|
2021-05-08 01:07:41 +00:00
|
|
|
generate := pCache.get(Generate, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-05-06 19:02:06 +00:00
|
|
|
kind := "pod"
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
assert.Assert(t, len(validateEnforce) == 0)
|
2020-07-02 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Add_Duplicate_Policy(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-07-02 19:49:10 +00:00
|
|
|
policy := newPolicy(t)
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-06 19:02:06 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
2020-07-02 19:49:10 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
mutate := pCache.get(Mutate, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
2020-07-02 19:49:10 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
|
|
|
}
|
2021-05-08 01:07:41 +00:00
|
|
|
generate := pCache.get(Generate, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 19:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 18:48:34 +00:00
|
|
|
func Test_Add_Validate_Audit(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-07-09 18:48:34 +00:00
|
|
|
policy := newPolicy(t)
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2020-07-09 18:48:34 +00:00
|
|
|
|
|
|
|
policy.Spec.ValidationFailureAction = "audit"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-06 19:02:06 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
2020-07-09 18:48:34 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
2021-05-11 19:45:34 +00:00
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
2021-05-06 19:02:06 +00:00
|
|
|
}
|
2020-07-09 18:48:34 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateAudit := pCache.get(ValidateAudit, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateAudit))
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 18:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 03:29:39 +00:00
|
|
|
func Test_Add_Remove(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-07-14 03:29:39 +00:00
|
|
|
policy := newPolicy(t)
|
2021-05-06 19:02:06 +00:00
|
|
|
kind := "Pod"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2021-07-28 19:59:53 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate enforce policy, found %v", len(validateEnforce))
|
2020-07-14 03:29:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 19:59:53 +00:00
|
|
|
mutate := pCache.get(Mutate, kind, "")
|
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
|
|
|
|
|
|
|
generate := pCache.get(Generate, kind, "")
|
2021-07-29 05:14:31 +00:00
|
|
|
if len(generate) != 1 {
|
2021-07-28 19:59:53 +00:00
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-07-28 19:59:53 +00:00
|
|
|
deletedValidateEnforce := pCache.get(ValidateEnforce, kind, "")
|
|
|
|
if len(deletedValidateEnforce) != 0 {
|
|
|
|
t.Errorf("expected 0 validate enforce policy, found %v", len(deletedValidateEnforce))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Add_Remove_Any(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newAnyPolicy(t)
|
|
|
|
kind := "Pod"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2021-07-28 19:59:53 +00:00
|
|
|
|
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, "")
|
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate enforce policy, found %v", len(validateEnforce))
|
|
|
|
}
|
|
|
|
|
|
|
|
mutate := pCache.get(Mutate, kind, "")
|
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
|
|
|
|
|
|
|
generate := pCache.get(Generate, kind, "")
|
2021-07-29 05:14:31 +00:00
|
|
|
if len(generate) != 1 {
|
2021-07-28 19:59:53 +00:00
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-05-08 01:07:41 +00:00
|
|
|
deletedValidateEnforce := pCache.get(ValidateEnforce, kind, "")
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(deletedValidateEnforce) != 0 {
|
|
|
|
t.Errorf("expected 0 validate enforce policy, found %v", len(deletedValidateEnforce))
|
2020-07-14 03:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 19:49:10 +00:00
|
|
|
func Test_Remove_From_Empty_Cache(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, nil, nil)
|
2020-07-02 19:49:10 +00:00
|
|
|
policy := newPolicy(t)
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2020-07-02 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
2020-07-14 03:29:39 +00:00
|
|
|
"metadata": {
|
|
|
|
"name": "test-policy"
|
|
|
|
},
|
2020-07-02 19:49:10 +00:00
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "deny-privileged-disallowpriviligedescalation",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
2021-05-06 19:02:06 +00:00
|
|
|
"Pod",
|
|
|
|
"Namespace"
|
2020-07-02 19:49:10 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"deny": {
|
2021-03-02 04:31:06 +00:00
|
|
|
"conditions": {
|
2021-05-06 19:02:06 +00:00
|
|
|
"all": [
|
|
|
|
{
|
|
|
|
"key": "a",
|
|
|
|
"operator": "Equals",
|
|
|
|
"value": "a"
|
|
|
|
}
|
2021-03-02 04:31:06 +00:00
|
|
|
]
|
|
|
|
}
|
2020-07-02 19:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "deny-privileged-disallowpriviligedescalation",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"pattern": {
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"image": "!*:latest"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "annotate-host-path",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
2021-05-06 19:02:06 +00:00
|
|
|
"Pod",
|
|
|
|
"Namespace"
|
2020-07-02 19:49:10 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mutate": {
|
2022-01-05 01:36:33 +00:00
|
|
|
"patchStrategicMerge": {
|
2020-07-02 19:49:10 +00:00
|
|
|
"metadata": {
|
|
|
|
"annotations": {
|
|
|
|
"+(cluster-autoscaler.kubernetes.io/safe-to-evict)": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
2021-05-06 19:02:06 +00:00
|
|
|
"Namespace",
|
|
|
|
"Pod"
|
2020-07-02 19:49:10 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"generate": {
|
|
|
|
"kind": "NetworkPolicy",
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"namespace": "{{request.object.metadata.name}}",
|
|
|
|
"data": {
|
|
|
|
"spec": {
|
|
|
|
"podSelector": {
|
|
|
|
},
|
|
|
|
"policyTypes": [
|
|
|
|
"Ingress"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-07-28 19:59:53 +00:00
|
|
|
func newAnyPolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "test-policy"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "deny-privileged-disallowpriviligedescalation",
|
|
|
|
"match": {
|
|
|
|
"any": [
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"names": [
|
|
|
|
"dev"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"namespaces": [
|
|
|
|
"prod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"deny": {
|
|
|
|
"conditions": {
|
|
|
|
"all": [
|
|
|
|
{
|
|
|
|
"key": "a",
|
|
|
|
"operator": "Equals",
|
|
|
|
"value": "a"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "deny-privileged-disallowpriviligedescalation",
|
|
|
|
"match": {
|
|
|
|
"all": [
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"names": [
|
|
|
|
"dev"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"namespaces": [
|
|
|
|
"prod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"pattern": {
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"image": "!*:latest"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "annotate-host-path",
|
|
|
|
"match": {
|
|
|
|
"any": [
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"names": [
|
|
|
|
"dev"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"namespaces": [
|
|
|
|
"prod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"mutate": {
|
2022-01-05 01:36:33 +00:00
|
|
|
"patchStrategicMerge": {
|
2021-07-28 19:59:53 +00:00
|
|
|
"metadata": {
|
|
|
|
"annotations": {
|
|
|
|
"+(cluster-autoscaler.kubernetes.io/safe-to-evict)": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"match": {
|
|
|
|
"any": [
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"names": [
|
|
|
|
"dev"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
],
|
|
|
|
"namespaces": [
|
|
|
|
"prod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"generate": {
|
|
|
|
"kind": "NetworkPolicy",
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"namespace": "{{request.object.metadata.name}}",
|
|
|
|
"data": {
|
|
|
|
"spec": {
|
|
|
|
"podSelector": {},
|
|
|
|
"policyTypes": [
|
|
|
|
"Ingress"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
2022-03-30 14:28:09 +00:00
|
|
|
func newNsPolicy(t *testing.T) kyverno.PolicyInterface {
|
2020-08-19 16:07:23 +00:00
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "test-policy",
|
|
|
|
"namespace": "test"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "deny-privileged-disallowpriviligedescalation",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"deny": {
|
2021-03-02 04:31:06 +00:00
|
|
|
"conditions": {
|
|
|
|
"all": [
|
|
|
|
{
|
|
|
|
"key": "a",
|
|
|
|
"operator": "Equals",
|
|
|
|
"value": "a"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "deny-privileged-disallowpriviligedescalation",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"pattern": {
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"image": "!*:latest"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "annotate-host-path",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mutate": {
|
2022-01-05 01:36:33 +00:00
|
|
|
"patchStrategicMerge": {
|
2020-08-19 16:07:23 +00:00
|
|
|
"metadata": {
|
|
|
|
"annotations": {
|
|
|
|
"+(cluster-autoscaler.kubernetes.io/safe-to-evict)": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
2021-05-06 19:02:06 +00:00
|
|
|
"Pod"
|
2020-08-19 16:07:23 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"generate": {
|
|
|
|
"kind": "NetworkPolicy",
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"namespace": "{{request.object.metadata.name}}",
|
|
|
|
"data": {
|
|
|
|
"spec": {
|
|
|
|
"podSelector": {
|
|
|
|
},
|
|
|
|
"policyTypes": [
|
|
|
|
"Ingress"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.Policy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-03-30 14:28:09 +00:00
|
|
|
return policy
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 19:45:34 +00:00
|
|
|
func newGVKPolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "add-networkpolicy1",
|
|
|
|
"annotations": {
|
|
|
|
"policies.kyverno.io/category": "Workload Management"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"rbac.authorization.k8s.io/v1beta1/ClusterRole"
|
|
|
|
],
|
|
|
|
"name": "*"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"exclude": {
|
|
|
|
"resources": {
|
|
|
|
"namespaces": [
|
|
|
|
"kube-system",
|
|
|
|
"default",
|
|
|
|
"kube-public",
|
|
|
|
"kyverno"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"generate": {
|
|
|
|
"kind": "NetworkPolicy",
|
|
|
|
"name": "default-deny-ingress",
|
|
|
|
"namespace": "default",
|
|
|
|
"synchronize": true,
|
|
|
|
"data": {
|
|
|
|
"spec": {
|
|
|
|
"podSelector": {},
|
|
|
|
"policyTypes": [
|
|
|
|
"Ingress"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
2022-03-30 14:28:09 +00:00
|
|
|
func newUserTestPolicy(t *testing.T) kyverno.PolicyInterface {
|
2021-05-11 19:45:34 +00:00
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"apiVersion": "kyverno.io/v1",
|
|
|
|
"kind": "Policy",
|
|
|
|
"metadata": {
|
|
|
|
"name": "require-dep-purpose-label",
|
|
|
|
"namespace": "default"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "require-dep-purpose-label",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Deployment"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"validate": {
|
|
|
|
"message": "You must have label purpose with value production set on all new Deployment.",
|
|
|
|
"pattern": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"purpose": "production"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.Policy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-03-30 14:28:09 +00:00
|
|
|
return policy
|
2021-05-11 19:45:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 19:20:21 +00:00
|
|
|
func newgenratePolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "add-networkpolicy",
|
|
|
|
"annotations": {
|
|
|
|
"policies.kyverno.io/title": "Add Network Policy",
|
|
|
|
"policies.kyverno.io/category": "Multi-Tenancy",
|
|
|
|
"policies.kyverno.io/subject": "NetworkPolicy"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "audit",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "default-deny",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Namespace"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"generate": {
|
|
|
|
"kind": "NetworkPolicy",
|
|
|
|
"name": "default-deny",
|
|
|
|
"namespace": "{{request.object.metadata.name}}",
|
|
|
|
"synchronize": true,
|
|
|
|
"data": {
|
|
|
|
"spec": {
|
|
|
|
"podSelector": {},
|
|
|
|
"policyTypes": [
|
|
|
|
"Ingress",
|
|
|
|
"Egress"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
func newMutatePolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "logger-sidecar"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"background": false,
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"StatefulSet"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mutate": {
|
|
|
|
"patchesJson6902": "- op: add\n path: /spec/template/spec/containers/-1\n value: {\"name\": \"logger\", \"image\": \"nginx\"}\n- op: add\n path: /spec/template/spec/volumes/-1\n value: {\"name\": \"logs\",\"emptyDir\": {\"medium\": \"Memory\"}}\n- op: add\n path: /spec/template/spec/containers/0/volumeMounts/-1\n value: {\"mountPath\": \"/opt/app/logs\",\"name\": \"logs\"}"
|
|
|
|
},
|
|
|
|
"name": "logger-sidecar",
|
|
|
|
"preconditions": [
|
|
|
|
{
|
|
|
|
"key": "{{ request.object.spec.template.metadata.annotations.\"logger.k8s/inject\"}}",
|
|
|
|
"operator": "Equals",
|
|
|
|
"value": "true"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "logger",
|
|
|
|
"operator": "NotIn",
|
|
|
|
"value": "{{ request.object.spec.template.spec.containers[].name }}"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"validationFailureAction": "audit"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
2022-03-30 14:28:09 +00:00
|
|
|
func newNsMutatePolicy(t *testing.T) kyverno.PolicyInterface {
|
2021-06-30 19:20:21 +00:00
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "logger-sidecar",
|
|
|
|
"namespace": "logger"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"background": false,
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"StatefulSet"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mutate": {
|
|
|
|
"patchesJson6902": "- op: add\n path: /spec/template/spec/containers/-1\n value: {\"name\": \"logger\", \"image\": \"nginx\"}\n- op: add\n path: /spec/template/spec/volumes/-1\n value: {\"name\": \"logs\",\"emptyDir\": {\"medium\": \"Memory\"}}\n- op: add\n path: /spec/template/spec/containers/0/volumeMounts/-1\n value: {\"mountPath\": \"/opt/app/logs\",\"name\": \"logs\"}"
|
|
|
|
},
|
|
|
|
"name": "logger-sidecar",
|
|
|
|
"preconditions": [
|
|
|
|
{
|
|
|
|
"key": "{{ request.object.spec.template.metadata.annotations.\"logger.k8s/inject\"}}",
|
|
|
|
"operator": "Equals",
|
|
|
|
"value": "true"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "logger",
|
|
|
|
"operator": "NotIn",
|
|
|
|
"value": "{{ request.object.spec.template.spec.containers[].name }}"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"validationFailureAction": "audit"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.Policy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-03-30 14:28:09 +00:00
|
|
|
return policy
|
2021-06-30 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 12:36:44 +00:00
|
|
|
func newValidateAuditPolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "check-label-app-audit"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"background": false,
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"name": "check-label-app",
|
|
|
|
"validate": {
|
|
|
|
"message": "The label 'app' is required.",
|
|
|
|
"pattern": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "?*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"validationFailureAction": "audit",
|
|
|
|
"validationFailureActionOverrides": [
|
|
|
|
{
|
|
|
|
"action": "enforce",
|
|
|
|
"namespaces": [
|
|
|
|
"default"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"action": "audit",
|
|
|
|
"namespaces": [
|
|
|
|
"test"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
|
|
|
func newValidateEnforcePolicy(t *testing.T) *kyverno.ClusterPolicy {
|
|
|
|
rawPolicy := []byte(`{
|
|
|
|
"metadata": {
|
|
|
|
"name": "check-label-app-enforce"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"background": false,
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"name": "check-label-app",
|
|
|
|
"validate": {
|
|
|
|
"message": "The label 'app' is required.",
|
|
|
|
"pattern": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "?*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"validationFailureActionOverrides": [
|
|
|
|
{
|
|
|
|
"action": "enforce",
|
|
|
|
"namespaces": [
|
|
|
|
"default"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"action": "audit",
|
|
|
|
"namespaces": [
|
|
|
|
"test"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var policy *kyverno.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
2020-08-19 16:07:23 +00:00
|
|
|
func Test_Ns_All(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-08-19 16:07:23 +00:00
|
|
|
policy := newNsPolicy(t)
|
2021-05-06 19:02:06 +00:00
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2020-08-19 16:07:23 +00:00
|
|
|
nspace := policy.GetNamespace()
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-06 19:02:06 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-05-06 19:02:06 +00:00
|
|
|
// get
|
2021-05-08 01:07:41 +00:00
|
|
|
mutate := pCache.get(Mutate, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
|
|
|
}
|
2021-05-08 01:07:41 +00:00
|
|
|
generate := pCache.get(Generate, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
// remove
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-05-06 19:02:06 +00:00
|
|
|
kind := "pod"
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
assert.Assert(t, len(validateEnforce) == 0)
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ns_Add_Duplicate_Policy(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-08-19 16:07:23 +00:00
|
|
|
policy := newNsPolicy(t)
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2020-08-19 16:07:23 +00:00
|
|
|
nspace := policy.GetNamespace()
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-06 19:02:06 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
mutate := pCache.get(Mutate, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
|
|
|
}
|
2021-05-08 01:07:41 +00:00
|
|
|
generate := pCache.get(Generate, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ns_Add_Validate_Audit(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-08-19 16:07:23 +00:00
|
|
|
policy := newNsPolicy(t)
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2021-05-06 19:02:06 +00:00
|
|
|
nspace := policy.GetNamespace()
|
2022-03-30 14:28:09 +00:00
|
|
|
policy.GetSpec().ValidationFailureAction = "audit"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-06 19:02:06 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
|
2021-05-08 01:07:41 +00:00
|
|
|
validateAudit := pCache.get(ValidateAudit, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateAudit))
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ns_Add_Remove(t *testing.T) {
|
2021-05-06 19:02:06 +00:00
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
2020-08-19 16:07:23 +00:00
|
|
|
policy := newNsPolicy(t)
|
|
|
|
nspace := policy.GetNamespace()
|
2021-05-06 19:02:06 +00:00
|
|
|
kind := "Pod"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2021-05-08 01:07:41 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate enforce policy, found %v", len(validateEnforce))
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-05-08 01:07:41 +00:00
|
|
|
deletedValidateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
2021-05-06 19:02:06 +00:00
|
|
|
if len(deletedValidateEnforce) != 0 {
|
|
|
|
t.Errorf("expected 0 validate enforce policy, found %v", len(deletedValidateEnforce))
|
2020-08-19 16:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-11 19:45:34 +00:00
|
|
|
|
|
|
|
func Test_GVk_Cache(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newGVKPolicy(t)
|
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-11 19:45:34 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
|
|
|
|
|
|
|
generate := pCache.get(Generate, kind, "")
|
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GVK_Add_Remove(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newGVKPolicy(t)
|
|
|
|
kind := "ClusterRole"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2021-05-11 19:45:34 +00:00
|
|
|
generate := pCache.get(Generate, kind, "")
|
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-05-11 19:45:34 +00:00
|
|
|
deletedGenerate := pCache.get(Generate, kind, "")
|
|
|
|
if len(deletedGenerate) != 0 {
|
|
|
|
t.Errorf("expected 0 generate policy, found %v", len(deletedGenerate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Add_Validate_Enforce(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newUserTestPolicy(t)
|
|
|
|
nspace := policy.GetNamespace()
|
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-05-11 19:45:34 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate policy, found %v", len(validateEnforce))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ns_Add_Remove_User(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newUserTestPolicy(t)
|
|
|
|
nspace := policy.GetNamespace()
|
|
|
|
kind := "Deployment"
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2021-05-11 19:45:34 +00:00
|
|
|
validateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
|
|
|
if len(validateEnforce) != 1 {
|
|
|
|
t.Errorf("expected 1 validate enforce policy, found %v", len(validateEnforce))
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy)
|
2021-05-11 19:45:34 +00:00
|
|
|
deletedValidateEnforce := pCache.get(ValidateEnforce, kind, nspace)
|
|
|
|
if len(deletedValidateEnforce) != 0 {
|
|
|
|
t.Errorf("expected 0 validate enforce policy, found %v", len(deletedValidateEnforce))
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 19:20:21 +00:00
|
|
|
|
|
|
|
func Test_Mutate_Policy(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newMutatePolicy(t)
|
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-06-30 19:20:21 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
|
|
|
|
|
|
|
// get
|
|
|
|
mutate := pCache.get(Mutate, kind, "")
|
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Generate_Policy(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newgenratePolicy(t)
|
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
2022-03-28 14:01:27 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-06-30 19:20:21 +00:00
|
|
|
for _, kind := range rule.MatchResources.Kinds {
|
|
|
|
|
|
|
|
// get
|
|
|
|
generate := pCache.get(Generate, kind, "")
|
|
|
|
if len(generate) != 1 {
|
|
|
|
t.Errorf("expected 1 generate policy, found %v", len(generate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_NsMutate_Policy(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy := newMutatePolicy(t)
|
|
|
|
nspolicy := newNsMutatePolicy(t)
|
|
|
|
//add
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(nspolicy)
|
|
|
|
pCache.add(policy)
|
|
|
|
pCache.add(nspolicy)
|
2021-06-30 19:20:21 +00:00
|
|
|
|
|
|
|
nspace := policy.GetNamespace()
|
|
|
|
// get
|
|
|
|
mutate := pCache.get(Mutate, "StatefulSet", "")
|
|
|
|
if len(mutate) != 1 {
|
|
|
|
t.Errorf("expected 1 mutate policy, found %v", len(mutate))
|
|
|
|
}
|
|
|
|
|
|
|
|
// get
|
|
|
|
nsMutate := pCache.get(Mutate, "StatefulSet", nspace)
|
|
|
|
if len(nsMutate) != 1 {
|
|
|
|
t.Errorf("expected 1 namespace mutate policy, found %v", len(nsMutate))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-01-21 12:36:44 +00:00
|
|
|
|
|
|
|
func Test_Validate_Enforce_Policy(t *testing.T) {
|
|
|
|
pCache := newPolicyCache(log.Log, dummyLister{}, dummyNsLister{})
|
|
|
|
policy1 := newValidateAuditPolicy(t)
|
|
|
|
policy2 := newValidateEnforcePolicy(t)
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.add(policy1)
|
|
|
|
pCache.add(policy2)
|
2022-01-21 12:36:44 +00:00
|
|
|
|
|
|
|
validateEnforce := pCache.get(ValidateEnforce, "Pod", "")
|
|
|
|
if len(validateEnforce) != 2 {
|
|
|
|
t.Errorf("adding: expected 2 validate enforce policy, found %v", len(validateEnforce))
|
|
|
|
}
|
|
|
|
|
|
|
|
validateAudit := pCache.get(ValidateAudit, "Pod", "")
|
|
|
|
if len(validateAudit) != 0 {
|
|
|
|
t.Errorf("adding: expected 0 validate audit policy, found %v", len(validateAudit))
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:20:00 +00:00
|
|
|
pCache.remove(policy1)
|
|
|
|
pCache.remove(policy2)
|
2022-01-21 12:36:44 +00:00
|
|
|
|
|
|
|
validateEnforce = pCache.get(ValidateEnforce, "Pod", "")
|
|
|
|
if len(validateEnforce) != 0 {
|
|
|
|
t.Errorf("removing: expected 0 validate enforce policy, found %v", len(validateEnforce))
|
|
|
|
}
|
|
|
|
|
|
|
|
validateAudit = pCache.get(ValidateAudit, "Pod", "")
|
|
|
|
if len(validateAudit) != 0 {
|
|
|
|
t.Errorf("removing: expected 0 validate audit policy, found %v", len(validateAudit))
|
|
|
|
}
|
|
|
|
}
|