2019-06-07 14:46:18 +03:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2020-01-10 17:15:44 -08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-06-07 14:46:18 +03:00
|
|
|
"testing"
|
|
|
|
|
2019-11-13 13:41:08 -08:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
2020-01-10 17:15:44 -08:00
|
|
|
context "github.com/nirmata/kyverno/pkg/engine/context"
|
2020-01-07 17:06:17 -08:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/utils"
|
2019-06-07 14:46:18 +03:00
|
|
|
"gotest.tools/assert"
|
2020-01-10 17:15:44 -08:00
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
2019-08-21 12:03:53 -07:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-06-07 14:46:18 +03:00
|
|
|
)
|
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
// Match multiple kinds
|
|
|
|
func TestResourceDescriptionMatch_MultipleKind(t *testing.T) {
|
|
|
|
rawResource := []byte(`{
|
|
|
|
"apiVersion": "apps/v1",
|
|
|
|
"kind": "Deployment",
|
|
|
|
"metadata": {
|
|
|
|
"name": "nginx-deployment",
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"replicas": 3,
|
|
|
|
"selector": {
|
|
|
|
"matchLabels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"template": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx:1.7.9",
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"containerPort": 80
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := utils.ConvertToUnstructured(rawResource)
|
2019-08-21 12:03:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to convert raw resource to unstructured: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
resourceDescription := kyverno.ResourceDescription{
|
|
|
|
Kinds: []string{"Deployment", "Pods"},
|
2019-06-07 14:46:18 +03:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: nil,
|
|
|
|
MatchExpressions: nil,
|
|
|
|
},
|
|
|
|
}
|
2019-09-27 16:35:09 -07:00
|
|
|
rule := kyverno.Rule{MatchResources: kyverno.MatchResources{ResourceDescription: resourceDescription}}
|
2019-08-21 12:03:53 -07:00
|
|
|
|
2020-02-07 14:45:43 +05:30
|
|
|
if err := MatchesResourceDescription(*resource, rule, kyverno.RequestInfo{}); err != nil {
|
|
|
|
t.Errorf("Testcase has failed due to the following:%v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
// Match resource name
|
|
|
|
func TestResourceDescriptionMatch_Name(t *testing.T) {
|
2019-06-07 14:46:18 +03:00
|
|
|
rawResource := []byte(`{
|
2019-08-21 12:03:53 -07:00
|
|
|
"apiVersion": "apps/v1",
|
|
|
|
"kind": "Deployment",
|
|
|
|
"metadata": {
|
|
|
|
"name": "nginx-deployment",
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"replicas": 3,
|
|
|
|
"selector": {
|
|
|
|
"matchLabels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"template": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx:1.7.9",
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"containerPort": 80
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}`)
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := utils.ConvertToUnstructured(rawResource)
|
2019-08-21 12:03:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to convert raw resource to unstructured: %v", err)
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
|
|
|
resourceDescription := kyverno.ResourceDescription{
|
|
|
|
Kinds: []string{"Deployment"},
|
|
|
|
Name: "nginx-deployment",
|
2019-06-07 14:46:18 +03:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: nil,
|
|
|
|
MatchExpressions: nil,
|
|
|
|
},
|
|
|
|
}
|
2019-09-27 16:35:09 -07:00
|
|
|
rule := kyverno.Rule{MatchResources: kyverno.MatchResources{ResourceDescription: resourceDescription}}
|
2019-07-26 07:28:34 -04:00
|
|
|
|
2020-02-07 14:45:43 +05:30
|
|
|
if err := MatchesResourceDescription(*resource, rule, kyverno.RequestInfo{}); err != nil {
|
|
|
|
t.Errorf("Testcase has failed due to the following:%v", err)
|
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
// Match resource regex
|
|
|
|
func TestResourceDescriptionMatch_Name_Regex(t *testing.T) {
|
2019-06-07 14:46:18 +03:00
|
|
|
rawResource := []byte(`{
|
2019-08-21 12:03:53 -07:00
|
|
|
"apiVersion": "apps/v1",
|
|
|
|
"kind": "Deployment",
|
|
|
|
"metadata": {
|
|
|
|
"name": "nginx-deployment",
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"replicas": 3,
|
|
|
|
"selector": {
|
|
|
|
"matchLabels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"template": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx:1.7.9",
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"containerPort": 80
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}`)
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := utils.ConvertToUnstructured(rawResource)
|
2019-08-21 12:03:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to convert raw resource to unstructured: %v", err)
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
|
|
|
resourceDescription := kyverno.ResourceDescription{
|
|
|
|
Kinds: []string{"Deployment"},
|
|
|
|
Name: "nginx-*",
|
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: nil,
|
|
|
|
MatchExpressions: nil,
|
|
|
|
},
|
|
|
|
}
|
2019-09-27 16:35:09 -07:00
|
|
|
rule := kyverno.Rule{MatchResources: kyverno.MatchResources{ResourceDescription: resourceDescription}}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2020-02-07 14:45:43 +05:30
|
|
|
if err := MatchesResourceDescription(*resource, rule, kyverno.RequestInfo{}); err != nil {
|
|
|
|
t.Errorf("Testcase has failed due to the following:%v", err)
|
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
// Match expressions for labels to not match
|
|
|
|
func TestResourceDescriptionMatch_Label_Expression_NotMatch(t *testing.T) {
|
|
|
|
rawResource := []byte(`{
|
|
|
|
"apiVersion": "apps/v1",
|
|
|
|
"kind": "Deployment",
|
|
|
|
"metadata": {
|
|
|
|
"name": "nginx-deployment",
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"replicas": 3,
|
|
|
|
"selector": {
|
|
|
|
"matchLabels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"template": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx:1.7.9",
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"containerPort": 80
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}`)
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := utils.ConvertToUnstructured(rawResource)
|
2019-08-21 12:03:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to convert raw resource to unstructured: %v", err)
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
|
|
|
resourceDescription := kyverno.ResourceDescription{
|
|
|
|
Kinds: []string{"Deployment"},
|
|
|
|
Name: "nginx-*",
|
2019-06-07 14:46:18 +03:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: nil,
|
|
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
|
|
|
metav1.LabelSelectorRequirement{
|
|
|
|
Key: "label2",
|
|
|
|
Operator: "NotIn",
|
|
|
|
Values: []string{
|
|
|
|
"sometest1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-09-27 16:35:09 -07:00
|
|
|
rule := kyverno.Rule{MatchResources: kyverno.MatchResources{ResourceDescription: resourceDescription}}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2020-02-07 14:45:43 +05:30
|
|
|
if err := MatchesResourceDescription(*resource, rule, kyverno.RequestInfo{}); err != nil {
|
|
|
|
t.Errorf("Testcase has failed due to the following:%v", err)
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
// Match label expression in matching set
|
|
|
|
func TestResourceDescriptionMatch_Label_Expression_Match(t *testing.T) {
|
2019-06-07 14:46:18 +03:00
|
|
|
rawResource := []byte(`{
|
2019-08-21 12:03:53 -07:00
|
|
|
"apiVersion": "apps/v1",
|
|
|
|
"kind": "Deployment",
|
|
|
|
"metadata": {
|
|
|
|
"name": "nginx-deployment",
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"replicas": 3,
|
|
|
|
"selector": {
|
|
|
|
"matchLabels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"template": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx:1.7.9",
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"containerPort": 80
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}`)
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := utils.ConvertToUnstructured(rawResource)
|
2019-08-21 12:03:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to convert raw resource to unstructured: %v", err)
|
2019-06-07 14:46:18 +03:00
|
|
|
|
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
resourceDescription := kyverno.ResourceDescription{
|
|
|
|
Kinds: []string{"Deployment"},
|
|
|
|
Name: "nginx-*",
|
2019-06-07 14:46:18 +03:00
|
|
|
Selector: &metav1.LabelSelector{
|
2019-08-21 12:03:53 -07:00
|
|
|
MatchLabels: nil,
|
2019-06-07 14:46:18 +03:00
|
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
|
|
|
metav1.LabelSelectorRequirement{
|
2019-08-21 12:03:53 -07:00
|
|
|
Key: "app",
|
|
|
|
Operator: "NotIn",
|
2019-06-07 14:46:18 +03:00
|
|
|
Values: []string{
|
2019-08-21 12:03:53 -07:00
|
|
|
"nginx1",
|
|
|
|
"nginx2",
|
2019-06-07 14:46:18 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-09-27 16:35:09 -07:00
|
|
|
rule := kyverno.Rule{MatchResources: kyverno.MatchResources{ResourceDescription: resourceDescription}}
|
2019-08-21 12:03:53 -07:00
|
|
|
|
2020-02-07 14:45:43 +05:30
|
|
|
if err := MatchesResourceDescription(*resource, rule, kyverno.RequestInfo{}); err != nil {
|
|
|
|
t.Errorf("Testcase has failed due to the following:%v", err)
|
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
// check for exclude conditions
|
|
|
|
func TestResourceDescriptionExclude_Label_Expression_Match(t *testing.T) {
|
2019-06-07 14:46:18 +03:00
|
|
|
rawResource := []byte(`{
|
2019-08-21 12:03:53 -07:00
|
|
|
"apiVersion": "apps/v1",
|
|
|
|
"kind": "Deployment",
|
|
|
|
"metadata": {
|
|
|
|
"name": "nginx-deployment",
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx",
|
|
|
|
"block": "true"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"replicas": 3,
|
|
|
|
"selector": {
|
|
|
|
"matchLabels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"template": {
|
|
|
|
"metadata": {
|
|
|
|
"labels": {
|
|
|
|
"app": "nginx"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx:1.7.9",
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"containerPort": 80
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
2019-08-21 12:03:53 -07:00
|
|
|
}`)
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := utils.ConvertToUnstructured(rawResource)
|
2019-08-21 12:03:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to convert raw resource to unstructured: %v", err)
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
}
|
|
|
|
resourceDescription := kyverno.ResourceDescription{
|
|
|
|
Kinds: []string{"Deployment"},
|
|
|
|
Name: "nginx-*",
|
2019-06-07 14:46:18 +03:00
|
|
|
Selector: &metav1.LabelSelector{
|
2019-08-21 12:03:53 -07:00
|
|
|
MatchLabels: nil,
|
2019-06-07 14:46:18 +03:00
|
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
|
|
|
metav1.LabelSelectorRequirement{
|
2019-08-21 12:03:53 -07:00
|
|
|
Key: "app",
|
2019-06-07 14:46:18 +03:00
|
|
|
Operator: "NotIn",
|
|
|
|
Values: []string{
|
2019-08-21 12:03:53 -07:00
|
|
|
"nginx1",
|
|
|
|
"nginx2",
|
2019-06-07 14:46:18 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:03:53 -07:00
|
|
|
resourceDescriptionExclude := kyverno.ResourceDescription{
|
2019-06-07 14:46:18 +03:00
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: map[string]string{
|
2019-08-21 12:03:53 -07:00
|
|
|
"block": "true",
|
2019-06-07 14:46:18 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:35:09 -07:00
|
|
|
rule := kyverno.Rule{MatchResources: kyverno.MatchResources{ResourceDescription: resourceDescription},
|
|
|
|
ExcludeResources: kyverno.ExcludeResources{ResourceDescription: resourceDescriptionExclude}}
|
2019-06-07 14:46:18 +03:00
|
|
|
|
2020-02-07 14:45:43 +05:30
|
|
|
if err := MatchesResourceDescription(*resource, rule, kyverno.RequestInfo{}); err == nil {
|
|
|
|
t.Errorf("Testcase has failed due to the following:\n Function has returned no error, even though it was suposed to fail")
|
|
|
|
}
|
2019-06-07 14:46:18 +03:00
|
|
|
}
|
2020-01-10 13:34:45 -08:00
|
|
|
|
2020-01-10 17:15:44 -08:00
|
|
|
func Test_validateGeneralRuleInfoVariables(t *testing.T) {
|
|
|
|
rawResource := []byte(`
|
|
|
|
{
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"kind": "Pod",
|
|
|
|
"metadata": {
|
|
|
|
"name": "image-with-hostpath",
|
|
|
|
"labels": {
|
|
|
|
"app.type": "prod",
|
|
|
|
"namespace": "my-namespace"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "image-with-hostpath",
|
|
|
|
"image": "docker.io/nautiker/curl",
|
|
|
|
"volumeMounts": [
|
|
|
|
{
|
|
|
|
"name": "var-lib-etcd",
|
|
|
|
"mountPath": "/var/lib"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"volumes": [
|
|
|
|
{
|
|
|
|
"name": "var-lib-etcd",
|
|
|
|
"emptyDir": {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
2020-01-10 13:34:45 -08:00
|
|
|
|
2020-01-10 17:15:44 -08:00
|
|
|
policyRaw := []byte(`{
|
|
|
|
"apiVersion": "kyverno.io/v1",
|
|
|
|
"kind": "ClusterPolicy",
|
|
|
|
"metadata": {
|
|
|
|
"name": "test-validate-variables"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "test-match",
|
|
|
|
"match": {
|
|
|
|
"Subjects": [
|
|
|
|
{
|
|
|
|
"kind": "User",
|
|
|
|
"name": "{{request.userInfo.username1}}}"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"resources": {
|
|
|
|
"kind": "{{request.object.kind}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test-exclude",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"namespaces": [
|
|
|
|
"{{request.object.namespace}}"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "test-condition",
|
|
|
|
"preconditions": [
|
|
|
|
{
|
|
|
|
"key": "{{serviceAccountName}}",
|
|
|
|
"operator": "NotEqual",
|
|
|
|
"value": "testuser"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
2020-01-10 13:34:45 -08:00
|
|
|
|
2020-01-10 17:15:44 -08:00
|
|
|
userReqInfo := kyverno.RequestInfo{
|
|
|
|
AdmissionUserInfo: authenticationv1.UserInfo{
|
|
|
|
Username: "user1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var policy kyverno.ClusterPolicy
|
|
|
|
assert.NilError(t, json.Unmarshal(policyRaw, &policy))
|
2020-01-10 13:34:45 -08:00
|
|
|
|
2020-01-10 17:15:44 -08:00
|
|
|
ctx := context.NewContext()
|
2020-01-24 09:37:12 -08:00
|
|
|
var err error
|
|
|
|
err = ctx.AddResource(rawResource)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = ctx.AddUserInfo(userReqInfo)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = ctx.AddSA("system:serviceaccount:test:testuser")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-01-10 13:34:45 -08:00
|
|
|
|
2020-01-10 17:15:44 -08:00
|
|
|
expectPaths := []string{"request.userInfo.username1", "request.object.namespace", ""}
|
|
|
|
|
|
|
|
for i, rule := range policy.Spec.Rules {
|
|
|
|
invalidPaths := validateGeneralRuleInfoVariables(ctx, rule)
|
|
|
|
assert.Assert(t, invalidPaths == expectPaths[i], fmt.Sprintf("result not match, got invalidPaths %s", invalidPaths))
|
|
|
|
}
|
|
|
|
}
|