2023-03-27 10:09:46 +02:00
|
|
|
package utils
|
2019-03-06 13:01:17 +02:00
|
|
|
|
|
|
|
import (
|
2020-02-07 14:45:43 +05:30
|
|
|
"fmt"
|
2023-09-19 14:06:53 +02:00
|
|
|
"slices"
|
2021-10-11 21:44:43 +02:00
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2024-06-20 11:44:43 +02:00
|
|
|
kyvernov2 "github.com/kyverno/kyverno/api/kyverno/v2"
|
2023-10-30 00:59:53 +01:00
|
|
|
"github.com/kyverno/kyverno/ext/wildcard"
|
2022-12-22 07:39:54 +01:00
|
|
|
datautils "github.com/kyverno/kyverno/pkg/utils/data"
|
2023-01-10 21:16:59 +01:00
|
|
|
matchutils "github.com/kyverno/kyverno/pkg/utils/match"
|
2020-02-09 13:12:27 +05:30
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
2019-12-30 17:08:50 -08:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2023-03-22 11:18:11 +01:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2019-03-06 13:01:17 +02:00
|
|
|
)
|
|
|
|
|
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-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:
|
2022-08-24 15:08:24 +02:00
|
|
|
//
|
|
|
|
// Kinds []string
|
|
|
|
// Name string
|
|
|
|
// Namespaces []string
|
|
|
|
// Selector
|
|
|
|
//
|
2020-07-14 20:23:30 -07:00
|
|
|
// UserInfo:
|
2022-08-24 15:08:24 +02:00
|
|
|
//
|
|
|
|
// Roles []string
|
|
|
|
// ClusterRoles []string
|
|
|
|
// Subjects []rbacv1.Subject
|
|
|
|
//
|
2020-07-14 20:23:30 -07:00
|
|
|
// 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
|
2023-03-22 11:18:11 +01:00
|
|
|
func doesResourceMatchConditionBlock(
|
|
|
|
conditionBlock kyvernov1.ResourceDescription,
|
|
|
|
userInfo kyvernov1.UserInfo,
|
2024-06-20 11:44:43 +02:00
|
|
|
admissionInfo kyvernov2.RequestInfo,
|
2023-03-22 11:18:11 +01:00
|
|
|
resource unstructured.Unstructured,
|
|
|
|
namespaceLabels map[string]string,
|
|
|
|
gvk schema.GroupVersionKind,
|
|
|
|
subresource string,
|
2023-03-29 06:22:21 +02:00
|
|
|
operation kyvernov1.AdmissionOperation,
|
2023-03-22 11:18:11 +01:00
|
|
|
) []error {
|
2023-09-19 01:01:49 -07:00
|
|
|
if len(conditionBlock.Operations) > 0 {
|
|
|
|
if !slices.Contains(conditionBlock.Operations, operation) {
|
|
|
|
// if operation does not match, return immediately
|
|
|
|
err := fmt.Errorf("operation does not match")
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 22:17:53 -08:00
|
|
|
|
2023-09-19 01:01:49 -07:00
|
|
|
var errs []error
|
2020-02-19 10:25:51 +05:30
|
|
|
if len(conditionBlock.Kinds) > 0 {
|
2023-01-06 19:10:35 +05:30
|
|
|
// Matching on ephemeralcontainers even when they are not explicitly specified for backward compatibility.
|
2023-03-22 11:18:11 +01:00
|
|
|
if !matchutils.CheckKind(conditionBlock.Kinds, gvk, subresource, true) {
|
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
|
|
|
|
2022-10-13 18:02:01 +02:00
|
|
|
resourceName := resource.GetName()
|
|
|
|
if resourceName == "" {
|
|
|
|
resourceName = resource.GetGenerateName()
|
|
|
|
}
|
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
if conditionBlock.Name != "" {
|
2023-01-10 21:16:59 +01:00
|
|
|
if !matchutils.CheckName(conditionBlock.Name, resourceName) {
|
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 {
|
2023-01-10 21:16:59 +01:00
|
|
|
if matchutils.CheckName(conditionBlock.Names[i], resourceName) {
|
2021-06-29 11:01:22 +05:30
|
|
|
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 {
|
2023-01-10 21:16:59 +01:00
|
|
|
if !matchutils.CheckAnnotations(conditionBlock.Annotations, resource.GetAnnotations()) {
|
2020-08-18 05:42:27 +05:30
|
|
|
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 {
|
2023-01-10 21:16:59 +01:00
|
|
|
hasPassed, err := matchutils.CheckSelector(conditionBlock.Selector, resource.GetLabels())
|
2020-02-19 10:25:51 +05:30
|
|
|
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
|
|
|
|
2023-07-20 18:34:07 +08:00
|
|
|
if conditionBlock.NamespaceSelector != nil {
|
|
|
|
if resource.GetKind() == "Namespace" {
|
|
|
|
errs = append(errs, fmt.Errorf("namespace selector is not applicable for namespace resource"))
|
|
|
|
} else if resource.GetKind() != "" || slices.Contains(conditionBlock.Kinds, "*") && wildcard.Match("*", resource.GetKind()) {
|
|
|
|
hasPassed, err := matchutils.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 labels"))
|
|
|
|
}
|
2021-02-04 02:39:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:23:30 -07:00
|
|
|
var userInfoErrors []error
|
2023-02-23 15:28:52 +01:00
|
|
|
if len(userInfo.Roles) > 0 {
|
2022-12-22 07:39:54 +01:00
|
|
|
if !datautils.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"))
|
2020-02-09 19:11:25 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-07-14 20:23:30 -07:00
|
|
|
|
2023-02-23 15:28:52 +01:00
|
|
|
if len(userInfo.ClusterRoles) > 0 {
|
2022-12-22 07:39:54 +01:00
|
|
|
if !datautils.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"))
|
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 {
|
2023-02-23 15:28:52 +01:00
|
|
|
if !matchSubjects(userInfo.Subjects, admissionInfo.AdmissionUserInfo) {
|
2020-07-14 20:23:30 -07:00
|
|
|
userInfoErrors = append(userInfoErrors, fmt.Errorf("user info does not match subject for the given conditionBlock"))
|
2020-02-09 19:11:25 +05:30
|
|
|
}
|
2020-02-07 18:11:47 +05:30
|
|
|
}
|
2023-03-29 06:22:21 +02:00
|
|
|
|
2020-07-14 20:23:30 -07:00
|
|
|
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
|
2023-02-23 15:28:52 +01:00
|
|
|
func matchSubjects(ruleSubjects []rbacv1.Subject, userInfo authenticationv1.UserInfo) bool {
|
|
|
|
return matchutils.CheckSubjects(ruleSubjects, userInfo)
|
2020-02-09 13:12:27 +05:30
|
|
|
}
|
|
|
|
|
2023-03-22 13:41:16 +01:00
|
|
|
// matchesResourceDescription checks if the resource matches resource description of the rule or not
|
2023-03-27 10:09:46 +02:00
|
|
|
func MatchesResourceDescription(
|
2023-09-19 01:01:49 -07:00
|
|
|
resource unstructured.Unstructured,
|
|
|
|
rule kyvernov1.Rule,
|
2024-06-20 11:44:43 +02:00
|
|
|
admissionInfo kyvernov2.RequestInfo,
|
2023-03-22 11:18:11 +01:00
|
|
|
namespaceLabels map[string]string,
|
|
|
|
policyNamespace string,
|
|
|
|
gvk schema.GroupVersionKind,
|
|
|
|
subresource string,
|
2023-03-29 06:22:21 +02:00
|
|
|
operation kyvernov1.AdmissionOperation,
|
2023-03-22 11:18:11 +01:00
|
|
|
) error {
|
2023-09-19 01:01:49 -07:00
|
|
|
if resource.Object == nil {
|
2023-03-22 13:41:16 +01:00
|
|
|
return fmt.Errorf("resource is empty")
|
|
|
|
}
|
2020-02-09 20:52:45 +05:30
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
var reasonsForFailure []error
|
2023-09-19 01:01:49 -07:00
|
|
|
if policyNamespace != "" && policyNamespace != resource.GetNamespace() {
|
2023-02-20 08:26:10 -08:00
|
|
|
return fmt.Errorf("policy and resource namespaces mismatch")
|
2021-10-01 14:16:33 +05:30
|
|
|
}
|
2022-05-09 18:50:50 -07:00
|
|
|
|
2021-07-29 01:29:53 +05:30
|
|
|
if len(rule.MatchResources.Any) > 0 {
|
2021-10-03 23:07:40 -07:00
|
|
|
// include object if ANY of the criteria match
|
2021-07-29 01:29:53 +05:30
|
|
|
// 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
|
2023-03-29 06:22:21 +02:00
|
|
|
if len(matchesResourceDescriptionMatchHelper(rmr, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)) == 0 {
|
2021-07-29 01:29:53 +05:30
|
|
|
oneMatched = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !oneMatched {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("no resource matched"))
|
|
|
|
}
|
|
|
|
} else if len(rule.MatchResources.All) > 0 {
|
2022-05-16 15:38:57 +02:00
|
|
|
// include object if ALL of the criteria match
|
2021-07-29 01:29:53 +05:30
|
|
|
for _, rmr := range rule.MatchResources.All {
|
2023-03-29 06:22:21 +02:00
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionMatchHelper(rmr, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)...)
|
2021-07-29 01:29:53 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
} else {
|
2022-05-17 13:12:43 +02:00
|
|
|
rmr := kyvernov1.ResourceFilter{UserInfo: rule.MatchResources.UserInfo, ResourceDescription: rule.MatchResources.ResourceDescription}
|
2023-03-29 06:22:21 +02:00
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionMatchHelper(rmr, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)...)
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-02-07 18:11:47 +05:30
|
|
|
|
2023-09-19 01:01:49 -07:00
|
|
|
// check exlude conditions only if match succeeds
|
|
|
|
if len(reasonsForFailure) == 0 {
|
|
|
|
if len(rule.ExcludeResources.Any) > 0 {
|
|
|
|
// exclude the object if ANY of the criteria match
|
|
|
|
for _, rer := range rule.ExcludeResources.Any {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionExcludeHelper(rer, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)...)
|
2021-07-29 01:29:53 +05:30
|
|
|
}
|
2023-09-19 01:01:49 -07:00
|
|
|
} else if len(rule.ExcludeResources.All) > 0 {
|
|
|
|
// exclude the object if ALL the criteria 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, namespaceLabels, gvk, subresource, operation)) == 0 {
|
|
|
|
excludedByAll = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if excludedByAll {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("resource excluded since the combination of all criteria exclude it"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rer := kyvernov1.ResourceFilter{UserInfo: rule.ExcludeResources.UserInfo, ResourceDescription: rule.ExcludeResources.ResourceDescription}
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchesResourceDescriptionExcludeHelper(rer, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)...)
|
2021-07-29 01:29:53 +05:30
|
|
|
}
|
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
|
2023-09-19 01:01:49 -07:00
|
|
|
errorMessage := fmt.Sprintf("rule %s not matched:", rule.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 {
|
2023-02-01 14:38:04 +08:00
|
|
|
return fmt.Errorf(errorMessage)
|
2020-02-07 14:45:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-05-01 14:48:50 -07:00
|
|
|
}
|
2021-02-01 12:59:13 -08:00
|
|
|
|
2023-03-22 11:18:11 +01:00
|
|
|
func matchesResourceDescriptionMatchHelper(
|
|
|
|
rmr kyvernov1.ResourceFilter,
|
2024-06-20 11:44:43 +02:00
|
|
|
admissionInfo kyvernov2.RequestInfo,
|
2023-03-22 11:18:11 +01:00
|
|
|
resource unstructured.Unstructured,
|
|
|
|
namespaceLabels map[string]string,
|
|
|
|
gvk schema.GroupVersionKind,
|
|
|
|
subresource string,
|
2023-03-29 06:22:21 +02:00
|
|
|
operation kyvernov1.AdmissionOperation,
|
2023-03-22 11:18:11 +01:00
|
|
|
) []error {
|
2021-07-29 01:29:53 +05:30
|
|
|
var errs []error
|
2024-06-20 11:44:43 +02:00
|
|
|
if datautils.DeepEqual(admissionInfo, kyvernov2.RequestInfo{}) {
|
2022-05-17 13:12:43 +02:00
|
|
|
rmr.UserInfo = kyvernov1.UserInfo{}
|
2021-07-29 01:29:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// checking if resource matches the rule
|
2023-03-24 11:01:49 +01:00
|
|
|
if !datautils.DeepEqual(rmr.ResourceDescription, kyvernov1.ResourceDescription{}) ||
|
|
|
|
!datautils.DeepEqual(rmr.UserInfo, kyvernov1.UserInfo{}) {
|
2023-03-29 06:22:21 +02:00
|
|
|
matchErrs := doesResourceMatchConditionBlock(rmr.ResourceDescription, rmr.UserInfo, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)
|
2021-07-29 01:29:53 +05:30
|
|
|
errs = append(errs, matchErrs...)
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("match cannot be empty"))
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2023-03-22 11:18:11 +01:00
|
|
|
func matchesResourceDescriptionExcludeHelper(
|
|
|
|
rer kyvernov1.ResourceFilter,
|
2024-06-20 11:44:43 +02:00
|
|
|
admissionInfo kyvernov2.RequestInfo,
|
2023-03-22 11:18:11 +01:00
|
|
|
resource unstructured.Unstructured,
|
|
|
|
namespaceLabels map[string]string,
|
|
|
|
gvk schema.GroupVersionKind,
|
|
|
|
subresource string,
|
2023-03-29 06:22:21 +02:00
|
|
|
operation kyvernov1.AdmissionOperation,
|
2023-03-22 11:18:11 +01:00
|
|
|
) []error {
|
2021-07-29 01:29:53 +05:30
|
|
|
var errs []error
|
|
|
|
// checking if resource matches the rule
|
2023-03-24 11:01:49 +01:00
|
|
|
if !datautils.DeepEqual(rer.ResourceDescription, kyvernov1.ResourceDescription{}) ||
|
|
|
|
!datautils.DeepEqual(rer.UserInfo, kyvernov1.UserInfo{}) {
|
2023-03-29 06:22:21 +02:00
|
|
|
excludeErrs := doesResourceMatchConditionBlock(rer.ResourceDescription, rer.UserInfo, admissionInfo, resource, namespaceLabels, gvk, subresource, operation)
|
2021-07-29 01:29:53 +05:30
|
|
|
// it was a match so we want to exclude it
|
|
|
|
if len(excludeErrs) == 0 {
|
2022-05-16 15:38:57 +02:00
|
|
|
errs = append(errs, fmt.Errorf("resource excluded since one of the criteria excluded it"))
|
2021-07-29 01:29:53 +05:30
|
|
|
errs = append(errs, excludeErrs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// len(errs) != 0 if the filter excluded the resource
|
|
|
|
return errs
|
|
|
|
}
|