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"
|
2020-02-07 18:11:47 +05:30
|
|
|
"reflect"
|
2019-08-19 18:57:19 -07:00
|
|
|
"time"
|
2019-03-06 13:01:17 +02:00
|
|
|
|
2020-02-09 13:12:27 +05:30
|
|
|
"github.com/nirmata/kyverno/pkg/utils"
|
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
2020-02-03 18:51:18 +05:30
|
|
|
|
2019-07-23 23:34:03 -04:00
|
|
|
"github.com/golang/glog"
|
|
|
|
|
2019-05-14 18:10:25 +03:00
|
|
|
"github.com/minio/minio/pkg/wildcard"
|
2019-11-13 13:41:08 -08:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
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"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
if resourceNameSpace == namespace {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSelector(labelSelector *metav1.LabelSelector, resourceLabels map[string]string) (bool, error) {
|
|
|
|
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if selector.Matches(labels.Set(resourceLabels)) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 13:12:27 +05:30
|
|
|
func doesResourceMatchConditionBlock(conditionBlock kyverno.ResourceDescription, userInfo kyverno.UserInfo, admissionInfo kyverno.RequestInfo, resource unstructured.Unstructured) []error {
|
2020-02-19 10:25:51 +05:30
|
|
|
var errs []error
|
|
|
|
if len(conditionBlock.Kinds) > 0 {
|
|
|
|
if !checkKind(conditionBlock.Kinds, resource.GetKind()) {
|
|
|
|
errs = append(errs, fmt.Errorf("resource kind does not match conditionBlock"))
|
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()) {
|
|
|
|
errs = append(errs, fmt.Errorf("resource name does not match conditionBlock"))
|
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()) {
|
|
|
|
errs = append(errs, fmt.Errorf("resource namespace does not match conditionBlock"))
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
|
|
|
if conditionBlock.Selector != nil {
|
|
|
|
hasPassed, err := checkSelector(conditionBlock.Selector, resource.GetLabels())
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("could not parse selector block of the policy in conditionBlock: %v", err))
|
|
|
|
} else {
|
|
|
|
if !hasPassed {
|
|
|
|
errs = append(errs, fmt.Errorf("resource does not match selector of given conditionBlock"))
|
2020-02-06 22:32:50 +05:30
|
|
|
}
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
}
|
|
|
|
if len(userInfo.Roles) > 0 {
|
|
|
|
if !doesSliceContainsAnyOfTheseValues(userInfo.Roles, admissionInfo.Roles...) {
|
|
|
|
errs = append(errs, 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
|
|
|
}
|
|
|
|
if len(userInfo.ClusterRoles) > 0 {
|
|
|
|
if !doesSliceContainsAnyOfTheseValues(userInfo.ClusterRoles, admissionInfo.ClusterRoles...) {
|
|
|
|
errs = append(errs, 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
|
|
|
}
|
|
|
|
if len(userInfo.Subjects) > 0 {
|
|
|
|
if !matchSubjects(userInfo.Subjects, admissionInfo.AdmissionUserInfo) {
|
|
|
|
errs = append(errs, 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
|
|
|
}
|
|
|
|
|
2020-02-19 10:25:51 +05:30
|
|
|
return errs
|
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
|
|
|
|
func matchSubjects(ruleSubjects []rbacv1.Subject, userInfo authenticationv1.UserInfo) bool {
|
|
|
|
const SaPrefix = "system:serviceaccount:"
|
|
|
|
|
|
|
|
userGroups := append(userInfo.Groups, userInfo.Username)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func doesSliceContainsAnyOfTheseValues(slice []string, values ...string) bool {
|
|
|
|
|
|
|
|
var sliceElementsMap = make(map[string]bool, len(slice))
|
|
|
|
for _, sliceElement := range slice {
|
|
|
|
sliceElementsMap[sliceElement] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
if sliceElementsMap[value] {
|
|
|
|
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-02-09 20:52:45 +05:30
|
|
|
func MatchesResourceDescription(resourceRef unstructured.Unstructured, ruleRef kyverno.Rule, admissionInfoRef kyverno.RequestInfo) error {
|
|
|
|
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-02-19 10:25:51 +05:30
|
|
|
if !reflect.DeepEqual(rule.MatchResources.ResourceDescription, kyverno.ResourceDescription{}) {
|
|
|
|
matchErrs := doesResourceMatchConditionBlock(rule.MatchResources.ResourceDescription, rule.MatchResources.UserInfo, admissionInfo, resource)
|
|
|
|
for _, matchErr := range matchErrs {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, matchErr)
|
2020-02-06 23:35:50 +05:30
|
|
|
}
|
2020-02-19 10:25:51 +05:30
|
|
|
} else {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("match block in rule cannot be empty"))
|
|
|
|
}
|
2020-02-07 18:11:47 +05:30
|
|
|
|
|
|
|
// checking if resource has been excluded
|
2020-02-19 10:25:51 +05:30
|
|
|
if !reflect.DeepEqual(rule.ExcludeResources.ResourceDescription, kyverno.ResourceDescription{}) {
|
|
|
|
excludeErrs := doesResourceMatchConditionBlock(rule.ExcludeResources.ResourceDescription, rule.ExcludeResources.UserInfo, admissionInfo, resource)
|
|
|
|
if excludeErrs == nil {
|
|
|
|
reasonsForFailure = append(reasonsForFailure, fmt.Errorf("resource has been excluded since it matches the exclude block"))
|
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-02-07 14:45:43 +05:30
|
|
|
var errorMessage = "rule has failed to match resource for the following reasons:"
|
2020-02-07 18:11:47 +05:30
|
|
|
for i, reasonForFailure := range reasonsForFailure {
|
|
|
|
if reasonForFailure != nil {
|
|
|
|
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
|
|
|
}
|