2019-05-15 14:25:32 +03:00
|
|
|
package engine
|
2019-03-06 13:01:17 +02:00
|
|
|
|
|
|
|
import (
|
2020-02-07 14:45:43 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-01-29 17:38:23 -08:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-12-01 22:48:56 -08:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
2020-12-04 09:28:30 -08:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/wildcards"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
2021-06-10 07:46:26 +05:30
|
|
|
"github.com/minio/pkg/wildcard"
|
2020-02-09 13:12:27 +05:30
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
2021-03-02 10:01:06 +05:30
|
|
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
2019-05-14 18:10:25 +03:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-12-30 17:08:50 -08:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2019-03-06 13:01:17 +02:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2020-12-01 22:48:56 -08:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-03-06 13:01:17 +02:00
|
|
|
)
|
|
|
|
|
2019-08-19 18:57:19 -07:00
|
|
|
//EngineStats stores in the statistics for a single application of resource
|
|
|
|
type EngineStats struct {
|
|
|
|
// average time required to process the policy rules on a resource
|
|
|
|
ExecutionTime time.Duration
|
2020-01-24 12:05:53 -08:00
|
|
|
// Count of rules that were applied successfully
|
2019-08-19 18:57:19 -07:00
|
|
|
RulesAppliedCount int
|
2019-08-14 15:18:46 -07:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:15:52 +05:30
|
|
|
func checkKind(kinds []string, resource unstructured.Unstructured) bool {
|
2020-02-06 23:35:50 +05:30
|
|
|
for _, kind := range kinds {
|
2021-03-05 06:15:52 +05:30
|
|
|
SplitGVK := strings.Split(kind, "/")
|
|
|
|
if len(SplitGVK) == 1 {
|
|
|
|
if resource.GetKind() == kind {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else if len(SplitGVK) == 2 {
|
|
|
|
if resource.GroupVersionKind().Kind == SplitGVK[1] && resource.GroupVersionKind().Version == SplitGVK[0] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if resource.GroupVersionKind().Group == SplitGVK[0] && resource.GroupVersionKind().Kind == SplitGVK[2] && (resource.GroupVersionKind().Version == SplitGVK[1] || resource.GroupVersionKind().Version == "*") {
|
|
|
|
return true
|
|
|
|
}
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkName(name, resourceName string) bool {
|
2020-02-06 23:55:46 +05:30
|
|
|
return wildcard.Match(name, resourceName)
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
|
|
|
|
2020-12-08 22:17:53 -08:00
|
|
|
func checkNameSpace(namespaces []string, resource unstructured.Unstructured) bool {
|
|
|
|
resourceNameSpace := resource.GetNamespace()
|
|
|
|
if resource.GetKind() == "Namespace" {
|
|
|
|
resourceNameSpace = resource.GetName()
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:35:50 +05:30
|
|
|
for _, namespace := range namespaces {
|
2020-05-26 10:36:56 -07:00
|
|
|
if wildcard.Match(namespace, resourceNameSpace) {
|
2020-02-06 23:35:50 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2020-02-06 23:35:50 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:42:27 +05:30
|
|
|
func checkAnnotations(annotations map[string]string, resourceAnnotations map[string]string) bool {
|
2020-12-02 12:25:56 -08:00
|
|
|
if len(annotations) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:42:27 +05:30
|
|
|
for k, v := range annotations {
|
2020-12-02 12:25:56 -08:00
|
|
|
match := false
|
|
|
|
for k1, v1 := range resourceAnnotations {
|
|
|
|
if wildcard.Match(k, k1) && wildcard.Match(v, v1) {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
2020-08-18 05:42:27 +05:30
|
|
|
}
|
2020-12-02 12:25:56 -08:00
|
|
|
|
|
|
|
if match == false {
|
2020-08-18 05:42:27 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 12:25:56 -08:00
|
|
|
|
2020-08-18 05:42:27 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:35:50 +05:30
|
|
|
func checkSelector(labelSelector *metav1.LabelSelector, resourceLabels map[string]string) (bool, error) {
|
2020-12-04 09:28:30 -08:00
|
|
|
wildcards.ReplaceInSelector(labelSelector, resourceLabels)
|
2020-02-06 23:35:50 +05:30
|
|
|
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
|
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
log.Log.Error(err, "failed to build label selector")
|
2020-02-06 23:35:50 +05:30
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if selector.Matches(labels.Set(resourceLabels)) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:23:30 -07:00
|
|
|
// doesResourceMatchConditionBlock filters the resource with defined conditions
|
|
|
|
// for a match / exclude block, it has the following attributes:
|
|
|
|
// ResourceDescription:
|
|
|
|
// Kinds []string
|
|
|
|
// Name string
|
|
|
|
// Namespaces []string
|
|
|
|
// Selector
|
|
|
|
// UserInfo:
|
|
|
|
// Roles []string
|
|
|
|
// ClusterRoles []string
|
|
|
|
// Subjects []rbacv1.Subject
|
|
|
|
// To filter out the targeted resources with ResourceDescription, the check
|
2020-12-16 12:29:16 -08:00
|
|
|
// should be: AND across attributes but an OR inside attributes that of type list
|
2020-07-14 20:23:30 -07:00
|
|
|
// To filter out the targeted resources with UserInfo, the check
|
2020-11-17 12:01:01 -08:00
|
|
|
// should be: OR (across & inside) attributes
|
2021-02-04 02:39:42 +05:30
|
|
|
func doesResourceMatchConditionBlock(conditionBlock kyverno.ResourceDescription, userInfo kyverno.UserInfo, admissionInfo kyverno.RequestInfo, resource unstructured.Unstructured, dynamicConfig []string, namespaceLabels map[string]string) []error {
|
2020-02-19 10:25:51 +05:30
|
|
|
var errs []error
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
if len(conditionBlock.Kinds) > 0 {
|
2021-03-05 06:15:52 +05:30
|
|
|
if !checkKind(conditionBlock.Kinds, resource) {
|
2020-12-04 15:59:15 -08:00
|
|
|
errs = append(errs, fmt.Errorf("kind does not match %v", conditionBlock.Kinds))
|
2020-02-07 14:45:43 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
if conditionBlock.Name != "" {
|
|
|
|
if !checkName(conditionBlock.Name, resource.GetName()) {
|
2020-05-26 10:36:56 -07:00
|
|
|
errs = append(errs, fmt.Errorf("name does not match"))
|
2019-08-09 12:59:37 -07:00
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2021-06-29 11:01:22 +05:30
|
|
|
if len(conditionBlock.Names) > 0 {
|
|
|
|
noneMatch := true
|
|
|
|
for i := range conditionBlock.Names {
|
|
|
|
if checkName(conditionBlock.Names[i], resource.GetName()) {
|
|
|
|
noneMatch = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if noneMatch {
|
|
|
|
errs = append(errs, fmt.Errorf("none of the names match"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
if len(conditionBlock.Namespaces) > 0 {
|
2020-12-08 22:17:53 -08:00
|
|
|
if !checkNameSpace(conditionBlock.Namespaces, resource) {
|
2020-05-26 10:36:56 -07:00
|
|
|
errs = append(errs, fmt.Errorf("namespace does not match"))
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2020-08-18 05:42:27 +05:30
|
|
|
if len(conditionBlock.Annotations) > 0 {
|
|
|
|
if !checkAnnotations(conditionBlock.Annotations, resource.GetAnnotations()) {
|
|
|
|
errs = append(errs, fmt.Errorf("annotations does not match"))
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
if conditionBlock.Selector != nil {
|
|
|
|
hasPassed, err := checkSelector(conditionBlock.Selector, resource.GetLabels())
|
|
|
|
if err != nil {
|
2020-05-26 10:36:56 -07:00
|
|
|
errs = append(errs, fmt.Errorf("failed to parse selector: %v", err))
|
2020-02-19 10:25:51 +05:30
|
|
|
} else {
|
|
|
|
if !hasPassed {
|
2020-05-26 10:36:56 -07:00
|
|
|
errs = append(errs, fmt.Errorf("selector does not match"))
|
2020-02-06 22:32:50 +05:30
|
|
|
}
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-05-19 00:14:23 -07:00
|
|
|
|
2021-02-09 00:06:39 +05:30
|
|
|
if conditionBlock.NamespaceSelector != nil && resource.GetKind() != "Namespace" && resource.GetKind() != "" {
|
2021-02-04 02:39:42 +05:30
|
|
|
hasPassed, err := checkSelector(conditionBlock.NamespaceSelector, namespaceLabels)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to parse namespace selector: %v", err))
|
|
|
|
} else {
|
|
|
|
if !hasPassed {
|
|
|
|
errs = append(errs, fmt.Errorf("namespace selector does not match"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 00:14:23 -07:00
|
|
|
keys := append(admissionInfo.AdmissionUserInfo.Groups, admissionInfo.AdmissionUserInfo.Username)
|
2020-07-14 20:23:30 -07:00
|
|
|
var userInfoErrors []error
|
|
|
|
var checkedItem int
|
2020-08-07 17:09:24 -07:00
|
|
|
if len(userInfo.Roles) > 0 && !utils.SliceContains(keys, dynamicConfig...) {
|
2020-07-14 20:23:30 -07:00
|
|
|
checkedItem++
|
2020-05-19 00:14:23 -07:00
|
|
|
|
2020-05-19 13:04:06 -07:00
|
|
|
if !utils.SliceContains(userInfo.Roles, admissionInfo.Roles...) {
|
2020-07-14 20:23:30 -07:00
|
|
|
userInfoErrors = append(userInfoErrors, fmt.Errorf("user info does not match roles for the given conditionBlock"))
|
|
|
|
} else {
|
|
|
|
return errs
|
2020-02-09 19:11:25 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-07-14 20:23:30 -07:00
|
|
|
|
2020-08-07 17:09:24 -07:00
|
|
|
if len(userInfo.ClusterRoles) > 0 && !utils.SliceContains(keys, dynamicConfig...) {
|
2020-07-14 20:23:30 -07:00
|
|
|
checkedItem++
|
|
|
|
|
2020-05-19 13:04:06 -07:00
|
|
|
if !utils.SliceContains(userInfo.ClusterRoles, admissionInfo.ClusterRoles...) {
|
2020-07-14 20:23:30 -07:00
|
|
|
userInfoErrors = append(userInfoErrors, fmt.Errorf("user info does not match clustersRoles for the given conditionBlock"))
|
|
|
|
} else {
|
|
|
|
return errs
|
2020-02-09 19:11:25 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-07-14 20:23:30 -07:00
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
if len(userInfo.Subjects) > 0 {
|
2020-07-14 20:23:30 -07:00
|
|
|
checkedItem++
|
|
|
|
|
2020-08-14 12:21:06 -07:00
|
|
|
if !matchSubjects(userInfo.Subjects, admissionInfo.AdmissionUserInfo, dynamicConfig) {
|
2020-07-14 20:23:30 -07:00
|
|
|
userInfoErrors = append(userInfoErrors, fmt.Errorf("user info does not match subject for the given conditionBlock"))
|
|
|
|
} else {
|
|
|
|
return errs
|
2020-02-09 19:11:25 +05:30
|
|
|
}
|
2020-02-07 18:11:47 +05:30
|
|
|
}
|
|
|
|
|
2020-07-14 20:23:30 -07:00
|
|
|
if checkedItem != len(userInfoErrors) {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(errs, userInfoErrors...)
|
2020-02-07 18:11:47 +05:30
|
|
|
}
|
|
|
|
|
2020-02-09 13:12:27 +05:30
|
|
|
// matchSubjects return true if one of ruleSubjects exist in userInfo
|
2020-08-14 12:21:06 -07:00
|
|
|
func matchSubjects(ruleSubjects []rbacv1.Subject, userInfo authenticationv1.UserInfo, dynamicConfig []string) bool {
|
2020-02-09 13:12:27 +05:30
|
|
|
const SaPrefix = "system:serviceaccount:"
|
|
|
|
|
|
|
|
userGroups := append(userInfo.Groups, userInfo.Username)
|
2020-05-18 20:01:20 -07:00
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
// TODO: see issue https://github.com/kyverno/kyverno/issues/861
|
2020-08-14 12:21:06 -07:00
|
|
|
for _, e := range dynamicConfig {
|
2020-08-07 17:09:24 -07:00
|
|
|
ruleSubjects = append(ruleSubjects,
|
|
|
|
rbacv1.Subject{Kind: "Group", Name: e},
|
|
|
|
)
|
|
|
|
}
|
2020-05-18 20:01:20 -07:00
|
|
|
|
2020-02-09 13:12:27 +05:30
|
|
|
for _, subject := range ruleSubjects {
|
|
|
|
switch subject.Kind {
|
|
|
|
case "ServiceAccount":
|
|
|
|
if len(userInfo.Username) <= len(SaPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
subjectServiceAccount := subject.Namespace + ":" + subject.Name
|
|
|
|
if userInfo.Username[len(SaPrefix):] == subjectServiceAccount {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case "User", "Group":
|
|
|
|
if utils.ContainsString(userGroups, subject.Name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:11:47 +05:30
|
|
|
//MatchesResourceDescription checks if the resource matches resource description of the rule or not
|
2021-02-04 02:39:42 +05:30
|
|
|
func MatchesResourceDescription(resourceRef unstructured.Unstructured, ruleRef kyverno.Rule, admissionInfoRef kyverno.RequestInfo, dynamicConfig []string, namespaceLabels map[string]string) error {
|
2020-08-07 17:09:24 -07:00
|
|
|
|
2020-02-09 20:52:45 +05:30
|
|
|
rule := *ruleRef.DeepCopy()
|
|
|
|
resource := *resourceRef.DeepCopy()
|
|
|
|
admissionInfo := *admissionInfoRef.DeepCopy()
|
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
var reasonsForFailure []error
|
2021-07-29 01:29:53 +05:30
|
|
|
if len(rule.MatchResources.Any) > 0 {
|
|
|
|
// inlcude object if ANY of the criterias match
|
|
|
|
// so if one matches then break from loop
|
|
|
|
oneMatched := false
|
|
|
|
for _, rmr := range rule.MatchResources.Any {
|
|
|
|
// if there are no errors it means it was a match
|
|
|
|
if len(matchesResourceDescriptionMatchHelper(rmr, admissionInfo, resource, dynamicConfig, namespaceLabels)) == 0 {
|
|
|
|
oneMatched = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !oneMatched {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("no resource matched"))
|
|
|
|
}
|
|
|
|
} else if len(rule.MatchResources.All) > 0 {
|
|
|
|
// include object if ALL of the criterias match
|
|
|
|
for _, rmr := range rule.MatchResources.All {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionMatchHelper(rmr, admissionInfo, resource, dynamicConfig, namespaceLabels)...)
|
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
} else {
|
2021-07-29 01:29:53 +05:30
|
|
|
rmr := kyverno.ResourceFilter{UserInfo: rule.MatchResources.UserInfo, ResourceDescription: rule.MatchResources.ResourceDescription}
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionMatchHelper(rmr, admissionInfo, resource, dynamicConfig, namespaceLabels)...)
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-02-07 18:11:47 +05:30
|
|
|
|
2021-07-29 01:29:53 +05:30
|
|
|
if len(rule.ExcludeResources.Any) > 0 {
|
|
|
|
// exclude the object if ANY of the criterias match
|
|
|
|
for _, rer := range rule.ExcludeResources.Any {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionExcludeHelper(rer, admissionInfo, resource, dynamicConfig, namespaceLabels)...)
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
2021-07-29 01:29:53 +05:30
|
|
|
} else if len(rule.ExcludeResources.All) > 0 {
|
|
|
|
// exlcude the object if ALL the criterias match
|
|
|
|
excludedByAll := true
|
|
|
|
for _, rer := range rule.ExcludeResources.All {
|
|
|
|
// we got no errors inplying a resource did NOT exclude it
|
|
|
|
// "matchesResourceDescriptionExcludeHelper" returns errors if resource is excluded by a filter
|
|
|
|
if len(matchesResourceDescriptionExcludeHelper(rer, admissionInfo, resource, dynamicConfig, namespaceLabels)) == 0 {
|
|
|
|
excludedByAll = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if excludedByAll {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("resource excluded since the combination of all criterias exclude it"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rer := kyverno.ResourceFilter{UserInfo: rule.ExcludeResources.UserInfo, ResourceDescription: rule.ExcludeResources.ResourceDescription}
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionExcludeHelper(rer, admissionInfo, resource, dynamicConfig, namespaceLabels)...)
|
2020-02-06 22:32:50 +05:30
|
|
|
}
|
2019-05-01 14:48:50 -07:00
|
|
|
|
2020-02-07 18:11:47 +05:30
|
|
|
// creating final error
|
2020-11-03 15:31:58 -08:00
|
|
|
var errorMessage = fmt.Sprintf("rule %s not matched:", ruleRef.Name)
|
2020-02-07 18:11:47 +05:30
|
|
|
for i, reasonForFailure := range reasonsForFailure {
|
|
|
|
if reasonForFailure != nil {
|
2020-05-26 10:36:56 -07:00
|
|
|
errorMessage += "\n " + fmt.Sprint(i+1) + ". " + reasonForFailure.Error()
|
2020-02-07 14:45:43 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:11:47 +05:30
|
|
|
if len(reasonsForFailure) > 0 {
|
2020-02-07 14:45:43 +05:30
|
|
|
return errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-05-01 14:48:50 -07:00
|
|
|
}
|
2021-02-01 12:59:13 -08:00
|
|
|
|
2021-07-29 01:29:53 +05:30
|
|
|
func matchesResourceDescriptionMatchHelper(rmr kyverno.ResourceFilter, admissionInfo kyverno.RequestInfo, resource unstructured.Unstructured, dynamicConfig []string, namespaceLabels map[string]string) []error {
|
|
|
|
var errs []error
|
|
|
|
if reflect.DeepEqual(admissionInfo, kyverno.RequestInfo{}) {
|
|
|
|
rmr.UserInfo = kyverno.UserInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checking if resource matches the rule
|
|
|
|
if !reflect.DeepEqual(rmr.ResourceDescription, kyverno.ResourceDescription{}) ||
|
|
|
|
!reflect.DeepEqual(rmr.UserInfo, kyverno.UserInfo{}) {
|
|
|
|
matchErrs := doesResourceMatchConditionBlock(rmr.ResourceDescription, rmr.UserInfo, admissionInfo, resource, dynamicConfig, namespaceLabels)
|
|
|
|
errs = append(errs, matchErrs...)
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("match cannot be empty"))
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchesResourceDescriptionExcludeHelper(rer kyverno.ResourceFilter, admissionInfo kyverno.RequestInfo, resource unstructured.Unstructured, dynamicConfig []string, namespaceLabels map[string]string) []error {
|
|
|
|
var errs []error
|
|
|
|
// checking if resource matches the rule
|
|
|
|
if !reflect.DeepEqual(rer.ResourceDescription, kyverno.ResourceDescription{}) ||
|
|
|
|
!reflect.DeepEqual(rer.UserInfo, kyverno.UserInfo{}) {
|
|
|
|
excludeErrs := doesResourceMatchConditionBlock(rer.ResourceDescription, rer.UserInfo, admissionInfo, resource, dynamicConfig, namespaceLabels)
|
|
|
|
// it was a match so we want to exclude it
|
|
|
|
if len(excludeErrs) == 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("resource excluded since one of the criterias excluded it"))
|
|
|
|
errs = append(errs, excludeErrs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// len(errs) != 0 if the filter excluded the resource
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:01:06 +05:30
|
|
|
func copyAnyAllConditions(original kyverno.AnyAllConditions) kyverno.AnyAllConditions {
|
|
|
|
if reflect.DeepEqual(original, kyverno.AnyAllConditions{}) {
|
|
|
|
return kyverno.AnyAllConditions{}
|
|
|
|
}
|
|
|
|
return *original.DeepCopy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// backwards compatibility
|
|
|
|
func copyOldConditions(original []kyverno.Condition) []kyverno.Condition {
|
2020-12-23 15:10:07 -08:00
|
|
|
if original == nil || len(original) == 0 {
|
|
|
|
return []kyverno.Condition{}
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:59:13 -08:00
|
|
|
var copies []kyverno.Condition
|
2020-02-04 12:13:41 -08:00
|
|
|
for _, condition := range original {
|
2021-02-01 12:59:13 -08:00
|
|
|
copies = append(copies, *condition.DeepCopy())
|
2020-01-10 11:59:05 -08:00
|
|
|
}
|
2020-12-23 15:10:07 -08:00
|
|
|
|
2021-02-01 12:59:13 -08:00
|
|
|
return copies
|
2020-01-10 11:59:05 -08:00
|
|
|
}
|
2020-06-24 10:26:04 -07:00
|
|
|
|
2021-07-30 22:07:01 +03:00
|
|
|
func transformConditions(original apiextensions.JSON) (interface{}, error) {
|
2021-03-02 10:01:06 +05:30
|
|
|
// conditions are currently in the form of []interface{}
|
|
|
|
kyvernoOriginalConditions, err := utils.ApiextensionsJsonToKyvernoConditions(original)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch typedValue := kyvernoOriginalConditions.(type) {
|
|
|
|
case kyverno.AnyAllConditions:
|
|
|
|
return copyAnyAllConditions(typedValue), nil
|
|
|
|
case []kyverno.Condition: // backwards compatibility
|
|
|
|
return copyOldConditions(typedValue), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("wrongfully configured data")
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:11:20 -07:00
|
|
|
// excludeResource checks if the resource has ownerRef set
|
2021-07-09 02:14:38 -07:00
|
|
|
func excludeResource(podControllers string, resource unstructured.Unstructured) bool {
|
2020-09-01 09:11:20 -07:00
|
|
|
kind := resource.GetKind()
|
2021-07-09 02:14:38 -07:00
|
|
|
hasOwner := false
|
2020-09-01 09:11:20 -07:00
|
|
|
if kind == "Pod" || kind == "Job" {
|
2021-07-09 02:14:38 -07:00
|
|
|
for _, owner := range resource.GetOwnerReferences() {
|
|
|
|
hasOwner = true
|
|
|
|
if owner.Kind != "ReplicaSet" && !strings.Contains(podControllers, owner.Kind) {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-24 10:26:04 -07:00
|
|
|
}
|
2021-07-09 02:14:38 -07:00
|
|
|
return hasOwner
|
2020-06-24 10:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2020-09-01 09:11:20 -07:00
|
|
|
|
2020-12-23 15:10:07 -08:00
|
|
|
// ManagedPodResource returns true:
|
2020-09-01 09:11:20 -07:00
|
|
|
// - if the policy has auto-gen annotation && resource == Pod
|
|
|
|
// - if the auto-gen contains cronJob && resource == Job
|
2020-12-23 15:10:07 -08:00
|
|
|
func ManagedPodResource(policy kyverno.ClusterPolicy, resource unstructured.Unstructured) bool {
|
2021-07-09 02:14:38 -07:00
|
|
|
podControllers, ok := policy.GetAnnotations()[PodControllersAnnotation]
|
|
|
|
if !ok || strings.ToLower(podControllers) == "none" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if excludeResource(podControllers, resource) {
|
2020-09-01 09:11:20 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-07-09 02:14:38 -07:00
|
|
|
if strings.Contains(podControllers, "CronJob") && excludeResource(podControllers, resource) {
|
|
|
|
return true
|
2020-09-01 09:11:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|