2019-05-15 14:25:32 +03:00
|
|
|
package engine
|
2019-03-06 13:01:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-04-30 18:54:08 -07:00
|
|
|
"strings"
|
2019-08-19 18:57:19 -07:00
|
|
|
"time"
|
2019-03-06 13:01:17 +02:00
|
|
|
|
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-12-30 17:08:50 -08:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/operator"
|
2019-07-31 17:43:46 -07:00
|
|
|
"github.com/nirmata/kyverno/pkg/utils"
|
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
|
|
|
|
// Count of rules that were applied succesfully
|
|
|
|
RulesAppliedCount int
|
2019-08-14 15:18:46 -07:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:59:37 -07:00
|
|
|
//MatchesResourceDescription checks if the resource matches resource desription of the rule or not
|
2019-08-13 11:32:12 -07:00
|
|
|
func MatchesResourceDescription(resource unstructured.Unstructured, rule kyverno.Rule) bool {
|
2019-08-09 12:59:37 -07:00
|
|
|
matches := rule.MatchResources.ResourceDescription
|
|
|
|
exclude := rule.ExcludeResources.ResourceDescription
|
|
|
|
|
2019-08-13 11:32:12 -07:00
|
|
|
if !findKind(matches.Kinds, resource.GetKind()) {
|
2019-08-09 12:59:37 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
name := resource.GetName()
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-08-09 12:59:37 -07:00
|
|
|
namespace := resource.GetNamespace()
|
|
|
|
|
2019-08-09 16:55:43 -07:00
|
|
|
if matches.Name != "" {
|
2019-08-09 12:59:37 -07:00
|
|
|
// Matches
|
2019-08-09 16:55:43 -07:00
|
|
|
if !wildcard.Match(matches.Name, name) {
|
2019-08-09 12:59:37 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-08-09 12:59:37 -07:00
|
|
|
// Matches
|
2019-09-12 17:11:55 -07:00
|
|
|
// check if the resource namespace is defined in the list of namespace pattern
|
|
|
|
if len(matches.Namespaces) > 0 && !utils.ContainsNamepace(matches.Namespaces, namespace) {
|
2019-08-09 12:59:37 -07:00
|
|
|
return false
|
|
|
|
}
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-08-09 12:59:37 -07:00
|
|
|
// Matches
|
|
|
|
if matches.Selector != nil {
|
|
|
|
selector, err := metav1.LabelSelectorAsSelector(matches.Selector)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !selector.Matches(labels.Set(resource.GetLabels())) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 19:31:42 -07:00
|
|
|
|
|
|
|
excludeName := func(name string) Condition {
|
|
|
|
if exclude.Name == "" {
|
|
|
|
return NotEvaluate
|
|
|
|
}
|
|
|
|
if wildcard.Match(exclude.Name, name) {
|
|
|
|
return Skip
|
|
|
|
}
|
|
|
|
return Process
|
|
|
|
}
|
|
|
|
|
|
|
|
excludeNamespace := func(namespace string) Condition {
|
|
|
|
if len(exclude.Namespaces) == 0 {
|
|
|
|
return NotEvaluate
|
|
|
|
}
|
2019-09-12 17:11:55 -07:00
|
|
|
if utils.ContainsNamepace(exclude.Namespaces, namespace) {
|
2019-09-03 19:31:42 -07:00
|
|
|
return Skip
|
|
|
|
}
|
|
|
|
return Process
|
|
|
|
}
|
|
|
|
|
|
|
|
excludeSelector := func(labelsMap map[string]string) Condition {
|
|
|
|
if exclude.Selector == nil {
|
|
|
|
return NotEvaluate
|
|
|
|
}
|
2019-08-09 12:59:37 -07:00
|
|
|
selector, err := metav1.LabelSelectorAsSelector(exclude.Selector)
|
|
|
|
// if the label selector is incorrect, should be fail or
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
2019-09-03 19:31:42 -07:00
|
|
|
return Skip
|
2019-08-09 12:59:37 -07:00
|
|
|
}
|
2019-09-03 19:31:42 -07:00
|
|
|
if selector.Matches(labels.Set(labelsMap)) {
|
|
|
|
return Skip
|
2019-08-09 12:59:37 -07:00
|
|
|
}
|
2019-09-03 19:31:42 -07:00
|
|
|
return Process
|
2019-08-09 12:59:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:31:42 -07:00
|
|
|
excludeKind := func(kind string) Condition {
|
|
|
|
if len(exclude.Kinds) == 0 {
|
|
|
|
return NotEvaluate
|
|
|
|
}
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-09-03 19:31:42 -07:00
|
|
|
if findKind(exclude.Kinds, kind) {
|
|
|
|
return Skip
|
|
|
|
}
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-09-03 19:31:42 -07:00
|
|
|
return Process
|
|
|
|
}
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-09-03 19:31:42 -07:00
|
|
|
// 0 -> dont check
|
|
|
|
// 1 -> is not to be exclude
|
|
|
|
// 2 -> to be exclude
|
|
|
|
excludeEval := []Condition{}
|
2019-08-17 09:45:57 -07:00
|
|
|
|
2019-09-03 19:31:42 -07:00
|
|
|
if ret := excludeName(resource.GetName()); ret != NotEvaluate {
|
|
|
|
excludeEval = append(excludeEval, ret)
|
|
|
|
}
|
|
|
|
if ret := excludeNamespace(resource.GetNamespace()); ret != NotEvaluate {
|
|
|
|
excludeEval = append(excludeEval, ret)
|
|
|
|
}
|
|
|
|
if ret := excludeSelector(resource.GetLabels()); ret != NotEvaluate {
|
|
|
|
excludeEval = append(excludeEval, ret)
|
|
|
|
}
|
|
|
|
if ret := excludeKind(resource.GetKind()); ret != NotEvaluate {
|
|
|
|
excludeEval = append(excludeEval, ret)
|
|
|
|
}
|
|
|
|
// Filtered NotEvaluate
|
|
|
|
|
2019-09-04 09:56:44 -07:00
|
|
|
if len(excludeEval) == 0 {
|
|
|
|
// nothing to exclude
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return func() bool {
|
2019-09-03 19:31:42 -07:00
|
|
|
for _, ret := range excludeEval {
|
|
|
|
if ret == Process {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Condition int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NotEvaluate Condition = 0
|
|
|
|
Process Condition = 1
|
|
|
|
Skip Condition = 2
|
|
|
|
)
|
2019-03-06 13:01:17 +02:00
|
|
|
|
2019-07-26 15:54:42 -07:00
|
|
|
// ParseResourceInfoFromObject get kind/namepace/name from resource
|
|
|
|
func ParseResourceInfoFromObject(rawResource []byte) string {
|
|
|
|
|
|
|
|
kind := ParseKindFromObject(rawResource)
|
|
|
|
namespace := ParseNamespaceFromObject(rawResource)
|
|
|
|
name := ParseNameFromObject(rawResource)
|
|
|
|
return strings.Join([]string{kind, namespace, name}, "/")
|
|
|
|
}
|
|
|
|
|
2019-06-26 12:19:11 -07:00
|
|
|
//ParseKindFromObject get kind from resource
|
|
|
|
func ParseKindFromObject(bytes []byte) string {
|
2019-05-01 14:48:50 -07:00
|
|
|
var objectJSON map[string]interface{}
|
|
|
|
json.Unmarshal(bytes, &objectJSON)
|
|
|
|
|
|
|
|
return objectJSON["kind"].(string)
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
//ParseNameFromObject extracts resource name from JSON obj
|
2019-05-07 16:50:39 -07:00
|
|
|
func ParseNameFromObject(bytes []byte) string {
|
2019-05-02 11:15:23 -07:00
|
|
|
var objectJSON map[string]interface{}
|
|
|
|
json.Unmarshal(bytes, &objectJSON)
|
2019-07-25 13:14:55 -04:00
|
|
|
meta, ok := objectJSON["metadata"]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
2019-05-02 11:15:23 -07:00
|
|
|
|
2019-07-25 13:14:55 -04:00
|
|
|
metaMap, ok := meta.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if name, ok := metaMap["name"].(string); ok {
|
2019-03-06 13:01:17 +02:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2019-03-21 18:09:58 +02:00
|
|
|
|
2019-06-05 17:43:59 -07:00
|
|
|
// ParseNamespaceFromObject extracts the namespace from the JSON obj
|
2019-05-07 16:50:39 -07:00
|
|
|
func ParseNamespaceFromObject(bytes []byte) string {
|
2019-05-02 11:15:23 -07:00
|
|
|
var objectJSON map[string]interface{}
|
|
|
|
json.Unmarshal(bytes, &objectJSON)
|
2019-07-25 13:14:55 -04:00
|
|
|
meta, ok := objectJSON["metadata"]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
metaMap, ok := meta.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
2019-05-02 11:15:23 -07:00
|
|
|
|
2019-07-25 13:14:55 -04:00
|
|
|
if name, ok := metaMap["namespace"].(string); ok {
|
|
|
|
return name
|
2019-03-21 18:09:58 +02:00
|
|
|
}
|
2019-07-25 13:14:55 -04:00
|
|
|
|
2019-03-21 18:09:58 +02:00
|
|
|
return ""
|
|
|
|
}
|
2019-04-30 17:26:50 -07:00
|
|
|
|
2019-05-21 15:43:43 -07:00
|
|
|
func findKind(kinds []string, kindGVK string) bool {
|
|
|
|
for _, kind := range kinds {
|
|
|
|
if kind == kindGVK {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-06-05 13:43:07 +03:00
|
|
|
|
2019-06-20 18:21:55 +03:00
|
|
|
func isStringIsReference(str string) bool {
|
2019-12-30 17:08:50 -08:00
|
|
|
if len(str) < len(operator.ReferenceSign) {
|
2019-06-20 18:21:55 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return str[0] == '$' && str[1] == '(' && str[len(str)-1] == ')'
|
|
|
|
}
|
|
|
|
|
2019-06-25 22:53:18 -07:00
|
|
|
type resourceInfo struct {
|
2019-07-23 23:34:03 -04:00
|
|
|
Resource unstructured.Unstructured
|
|
|
|
Gvk *metav1.GroupVersionKind
|
2019-06-25 22:53:18 -07:00
|
|
|
}
|