2019-05-15 14:25:32 +03:00
|
|
|
package engine
|
2019-03-06 13:01:17 +02:00
|
|
|
|
|
|
|
import (
|
2020-09-23 02:41:49 +05:30
|
|
|
"encoding/json"
|
2020-02-07 14:45:43 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-22 18:26:52 -07:00
|
|
|
"reflect"
|
|
|
|
"time"
|
2020-09-23 02:41:49 +05:30
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
2020-02-09 13:12:27 +05:30
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
2020-03-17 16:25:34 -07:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2020-10-07 11:12:31 -07:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
2019-05-14 18:10:25 +03:00
|
|
|
"github.com/minio/minio/pkg/wildcard"
|
|
|
|
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-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/resourcecache"
|
2020-09-23 02:41:49 +05:30
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-11-09 20:36:42 +05:30
|
|
|
"strings"
|
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
|
|
|
}
|
|
|
|
|
2020-02-06 23:35:50 +05:30
|
|
|
func checkKind(kinds []string, resourceKind string) bool {
|
|
|
|
for _, kind := range kinds {
|
|
|
|
if resourceKind == kind {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func checkNameSpace(namespaces []string, resourceNameSpace string) bool {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:42:27 +05:30
|
|
|
func checkAnnotations(annotations map[string]string, resourceAnnotations map[string]string) bool {
|
|
|
|
for k, v := range annotations {
|
|
|
|
if len(resourceAnnotations) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if resourceAnnotations[k] != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:35:50 +05:30
|
|
|
func checkSelector(labelSelector *metav1.LabelSelector, resourceLabels map[string]string) (bool, error) {
|
|
|
|
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
|
|
|
|
// should be: AND across attibutes but an OR inside attributes that of type list
|
|
|
|
// To filter out the targeted resources with UserInfo, the check
|
2020-11-17 12:01:01 -08:00
|
|
|
// should be: OR (across & inside) attributes
|
2020-08-14 12:21:06 -07:00
|
|
|
func doesResourceMatchConditionBlock(conditionBlock kyverno.ResourceDescription, userInfo kyverno.UserInfo, admissionInfo kyverno.RequestInfo, resource unstructured.Unstructured, dynamicConfig []string) []error {
|
2020-02-19 10:25:51 +05:30
|
|
|
var errs []error
|
|
|
|
if len(conditionBlock.Kinds) > 0 {
|
|
|
|
if !checkKind(conditionBlock.Kinds, resource.GetKind()) {
|
2020-05-26 10:36:56 -07:00
|
|
|
errs = append(errs, fmt.Errorf("kind does not match"))
|
2020-02-07 14:45:43 +05:30
|
|
|
}
|
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
|
|
|
}
|
|
|
|
if len(conditionBlock.Namespaces) > 0 {
|
|
|
|
if !checkNameSpace(conditionBlock.Namespaces, resource.GetNamespace()) {
|
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-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-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
|
|
|
|
|
|
|
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
|
2020-08-14 12:21:06 -07:00
|
|
|
func MatchesResourceDescription(resourceRef unstructured.Unstructured, ruleRef kyverno.Rule, admissionInfoRef kyverno.RequestInfo, dynamicConfig []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
|
2020-02-07 18:11:47 +05:30
|
|
|
|
2020-02-09 19:11:25 +05:30
|
|
|
if reflect.DeepEqual(admissionInfo, kyverno.RequestInfo{}) {
|
|
|
|
rule.MatchResources.UserInfo = kyverno.UserInfo{}
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:11:47 +05:30
|
|
|
// checking if resource matches the rule
|
2020-05-18 17:10:49 -07:00
|
|
|
if !reflect.DeepEqual(rule.MatchResources.ResourceDescription, kyverno.ResourceDescription{}) ||
|
|
|
|
!reflect.DeepEqual(rule.MatchResources.UserInfo, kyverno.UserInfo{}) {
|
2020-08-14 12:21:06 -07:00
|
|
|
matchErrs := doesResourceMatchConditionBlock(rule.MatchResources.ResourceDescription, rule.MatchResources.UserInfo, admissionInfo, resource, dynamicConfig)
|
2020-02-19 10:49:57 +05:30
|
|
|
reasonsForFailure = append(reasonsForFailure, matchErrs...)
|
2020-02-19 10:25:51 +05:30
|
|
|
} else {
|
2020-05-26 10:36:56 -07:00
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("match cannot be empty"))
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
2020-02-07 18:11:47 +05:30
|
|
|
|
|
|
|
// checking if resource has been excluded
|
2020-05-18 17:10:49 -07:00
|
|
|
if !reflect.DeepEqual(rule.ExcludeResources.ResourceDescription, kyverno.ResourceDescription{}) ||
|
|
|
|
!reflect.DeepEqual(rule.ExcludeResources.UserInfo, kyverno.UserInfo{}) {
|
2020-08-14 12:21:06 -07:00
|
|
|
excludeErrs := doesResourceMatchConditionBlock(rule.ExcludeResources.ResourceDescription, rule.ExcludeResources.UserInfo, admissionInfo, resource, dynamicConfig)
|
2020-02-19 10:25:51 +05:30
|
|
|
if excludeErrs == nil {
|
2020-05-26 10:36:56 -07:00
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("resource excluded"))
|
2020-02-06 23:35:50 +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
|
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
|
|
|
}
|
2020-02-04 12:13:41 -08:00
|
|
|
func copyConditions(original []kyverno.Condition) []kyverno.Condition {
|
|
|
|
var copy []kyverno.Condition
|
|
|
|
for _, condition := range original {
|
2020-02-14 11:59:28 -08:00
|
|
|
copy = append(copy, *condition.DeepCopy())
|
2020-01-10 11:59:05 -08:00
|
|
|
}
|
2020-02-04 12:13:41 -08:00
|
|
|
return copy
|
2020-01-10 11:59:05 -08:00
|
|
|
}
|
2020-06-24 10:26:04 -07:00
|
|
|
|
2020-09-01 09:11:20 -07:00
|
|
|
// excludeResource checks if the resource has ownerRef set
|
|
|
|
func excludeResource(resource unstructured.Unstructured) bool {
|
|
|
|
kind := resource.GetKind()
|
|
|
|
if kind == "Pod" || kind == "Job" {
|
2020-07-09 11:48:34 -07:00
|
|
|
if len(resource.GetOwnerReferences()) > 0 {
|
2020-06-24 10:26:04 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2020-09-01 09:11:20 -07:00
|
|
|
|
|
|
|
// SkipPolicyApplication returns true:
|
|
|
|
// - if the policy has auto-gen annotation && resource == Pod
|
|
|
|
// - if the auto-gen contains cronJob && resource == Job
|
|
|
|
func SkipPolicyApplication(policy kyverno.ClusterPolicy, resource unstructured.Unstructured) bool {
|
|
|
|
if policy.HasAutoGenAnnotation() && excludeResource(resource) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if podControllers, ok := policy.GetAnnotations()[PodControllersAnnotation]; ok {
|
|
|
|
if strings.Contains(podControllers, "CronJob") && excludeResource(resource) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2020-09-23 02:41:49 +05:30
|
|
|
|
|
|
|
// AddResourceToContext - Add the Configmap JSON to Context.
|
2020-10-14 17:39:45 -07:00
|
|
|
// it will read configmaps (can be extended to get other type of resource like secrets, namespace etc)
|
|
|
|
// from the informer cache and add the configmap data to context
|
|
|
|
func AddResourceToContext(logger logr.Logger, contextEntries []kyverno.ContextEntry, resCache resourcecache.ResourceCacheIface, ctx *context.Context) error {
|
|
|
|
if len(contextEntries) == 0 {
|
2020-09-23 02:41:49 +05:30
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 17:39:45 -07:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
// get GVR Cache for "configmaps"
|
|
|
|
// can get cache for other resources if the informers are enabled in resource cache
|
|
|
|
gvrC := resCache.GetGVRCache("configmaps")
|
2020-10-14 17:39:45 -07:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
if gvrC != nil {
|
|
|
|
lister := gvrC.GetLister()
|
2020-10-14 17:39:45 -07:00
|
|
|
for _, context := range contextEntries {
|
2020-09-23 02:41:49 +05:30
|
|
|
contextData := make(map[string]interface{})
|
|
|
|
name := context.ConfigMap.Name
|
|
|
|
namespace := context.ConfigMap.Namespace
|
|
|
|
if namespace == "" {
|
|
|
|
namespace = "default"
|
|
|
|
}
|
2020-10-14 17:39:45 -07:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
key := fmt.Sprintf("%s/%s", namespace, name)
|
|
|
|
obj, err := lister.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, fmt.Sprintf("failed to read configmap %s/%s from cache", namespace, name))
|
|
|
|
continue
|
|
|
|
}
|
2020-10-14 17:39:45 -07:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to convert context runtime object to unstructured")
|
|
|
|
continue
|
|
|
|
}
|
2020-10-14 17:39:45 -07:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
// extract configmap data
|
|
|
|
contextData["data"] = unstructuredObj["data"]
|
|
|
|
contextData["metadata"] = unstructuredObj["metadata"]
|
|
|
|
contextNamedData := make(map[string]interface{})
|
|
|
|
contextNamedData[context.Name] = contextData
|
|
|
|
jdata, err := json.Marshal(contextNamedData)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to unmarshal context data")
|
|
|
|
continue
|
|
|
|
}
|
2020-10-14 17:39:45 -07:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
// add data to context
|
|
|
|
err = ctx.AddJSON(jdata)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to load context json")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("configmaps GVR Cache not found")
|
|
|
|
}
|