1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/policy/validation.go

248 lines
6.5 KiB
Go
Raw Normal View History

2020-01-23 18:03:37 +05:30
package policy
import (
2020-01-24 18:53:51 +05:30
"compress/gzip"
2020-01-23 18:03:37 +05:30
"fmt"
2020-01-24 18:53:51 +05:30
"net/http"
2020-01-23 18:03:37 +05:30
"strconv"
"strings"
"sync"
2020-01-24 18:53:51 +05:30
"github.com/golang/glog"
2020-01-23 20:19:58 +05:30
"github.com/nirmata/kyverno/pkg/engine"
"github.com/nirmata/kyverno/pkg/engine/context"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
2020-01-23 18:03:37 +05:30
openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
"github.com/googleapis/gnostic/compiler"
"k8s.io/kube-openapi/pkg/util/proto"
"k8s.io/kube-openapi/pkg/util/proto/validation"
"gopkg.in/yaml.v2"
)
var validationGlobalState struct {
2020-01-24 20:22:33 +05:30
document *openapi_v2.Document
definitions map[string]*openapi_v2.Schema
kindToDefinitionName map[string]string
models proto.Models
isSet bool
2020-01-23 18:03:37 +05:30
}
2020-01-24 18:53:51 +05:30
func init() {
err := setValidationGlobalState()
if err != nil {
panic(err)
}
}
2020-01-23 20:19:58 +05:30
func ValidatePolicyMutation(policy v1.ClusterPolicy) error {
2020-01-24 20:59:14 +05:30
if !validationGlobalState.isSet {
2020-01-24 18:53:51 +05:30
glog.V(4).Info("Cannot Validate policy: Validation global state not set")
return nil
2020-01-23 20:19:58 +05:30
}
var kindToRules = make(map[string][]v1.Rule)
2020-01-23 20:19:58 +05:30
for _, rule := range policy.Spec.Rules {
rule.MatchResources.Selector = nil
2020-01-23 20:19:58 +05:30
if rule.HasMutate() {
for _, kind := range rule.MatchResources.Kinds {
kindToRules[kind] = append(kindToRules[kind], rule)
2020-01-23 20:19:58 +05:30
}
}
}
2020-01-23 18:03:37 +05:30
for kind, rules := range kindToRules {
newPolicy := policy
newPolicy.Spec.Rules = rules
2020-01-24 20:22:33 +05:30
resource, _ := generateEmptyResource(validationGlobalState.definitions[validationGlobalState.kindToDefinitionName[kind]]).(map[string]interface{})
2020-01-23 20:19:58 +05:30
newResource := unstructured.Unstructured{Object: resource}
2020-01-24 09:51:40 +05:30
newResource.SetKind(kind)
2020-01-23 20:19:58 +05:30
policyContext := engine.PolicyContext{
Policy: newPolicy,
2020-01-23 20:19:58 +05:30
NewResource: newResource,
Context: context.NewContext(),
}
resp := engine.Mutate(policyContext)
if len(resp.GetSuccessRules()) != len(rules) {
var errMessages []string
for _, rule := range resp.PolicyResponse.Rules {
2020-01-24 20:59:14 +05:30
if !rule.Success {
errMessages = append(errMessages, fmt.Sprintf("Invalid rule : %v, %v", rule.Name, rule.Message))
}
}
return fmt.Errorf(strings.Join(errMessages, "\n"))
}
2020-01-23 20:19:58 +05:30
err := ValidateResource(resp.PatchedResource.UnstructuredContent(), kind)
if err != nil {
return err
}
}
return nil
}
func ValidateResource(patchedResource interface{}, kind string) error {
2020-01-24 20:59:14 +05:30
if !validationGlobalState.isSet {
2020-01-24 18:53:51 +05:30
glog.V(4).Info("Cannot Validate resource: Validation global state not set")
return nil
2020-01-23 18:03:37 +05:30
}
2020-01-24 18:53:51 +05:30
2020-01-24 20:22:33 +05:30
kind = validationGlobalState.kindToDefinitionName[kind]
2020-01-23 18:03:37 +05:30
2020-01-23 20:19:58 +05:30
schema := validationGlobalState.models.LookupModel(kind)
if schema == nil {
return fmt.Errorf("pre-validation: couldn't find model %s", kind)
2020-01-23 18:03:37 +05:30
}
2020-01-23 20:19:58 +05:30
if errs := validation.ValidateModel(patchedResource, schema, kind); len(errs) > 0 {
var errorMessages []string
for i := range errs {
errorMessages = append(errorMessages, errs[i].Error())
}
return fmt.Errorf(strings.Join(errorMessages, "\n\n"))
2020-01-23 18:03:37 +05:30
}
2020-01-23 20:19:58 +05:30
return nil
2020-01-23 18:03:37 +05:30
}
func setValidationGlobalState() error {
2020-01-24 21:01:56 +05:30
if !validationGlobalState.isSet {
2020-01-24 18:53:51 +05:30
var err error
validationGlobalState.document, err = getSchemaDocument()
if err != nil {
return err
}
2020-01-23 18:03:37 +05:30
2020-01-24 18:53:51 +05:30
validationGlobalState.definitions = make(map[string]*openapi_v2.Schema)
2020-01-24 20:22:33 +05:30
validationGlobalState.kindToDefinitionName = make(map[string]string)
2020-01-24 18:53:51 +05:30
for _, definition := range validationGlobalState.document.GetDefinitions().AdditionalProperties {
validationGlobalState.definitions[definition.GetName()] = definition.GetValue()
2020-01-24 20:22:33 +05:30
path := strings.Split(definition.GetName(), ".")
validationGlobalState.kindToDefinitionName[path[len(path)-1]] = definition.GetName()
2020-01-24 18:53:51 +05:30
}
2020-01-23 18:03:37 +05:30
2020-01-24 18:53:51 +05:30
validationGlobalState.models, err = proto.NewOpenAPIData(validationGlobalState.document)
if err != nil {
return err
}
2020-01-23 18:03:37 +05:30
2020-01-24 18:53:51 +05:30
validationGlobalState.isSet = true
}
2020-01-23 18:03:37 +05:30
return nil
}
2020-01-24 18:53:51 +05:30
func getSchemaDocument() (*openapi_v2.Document, error) {
docReq, _ := http.NewRequest("GET", "https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json", nil)
docReq.Header.Set("accept-encoding", "gzip")
doc, err := http.DefaultClient.Do(docReq)
2020-01-23 18:03:37 +05:30
if err != nil {
2020-01-24 18:53:51 +05:30
return nil, fmt.Errorf("Could not fetch openapi document from the internet, underlying error : %v", err)
2020-01-23 18:03:37 +05:30
}
2020-01-24 18:53:51 +05:30
gzipReader, err := gzip.NewReader(doc.Body)
defer func() {
err := gzipReader.Close()
if err != nil {
glog.V(4).Info("Could not close gzip reader")
}
}()
2020-01-23 18:03:37 +05:30
if err != nil {
return nil, err
}
2020-01-24 18:53:51 +05:30
2020-01-23 18:03:37 +05:30
var spec yaml.MapSlice
2020-01-24 18:53:51 +05:30
err = yaml.NewDecoder(gzipReader).Decode(&spec)
2020-01-23 18:03:37 +05:30
if err != nil {
return nil, err
}
return openapi_v2.NewDocument(spec, compiler.NewContext("$root", nil))
}
func generateEmptyResource(kindSchema *openapi_v2.Schema) interface{} {
types := kindSchema.GetType().GetValue()
if kindSchema.GetXRef() != "" {
return generateEmptyResource(validationGlobalState.definitions[strings.TrimPrefix(kindSchema.GetXRef(), "#/definitions/")])
}
if len(types) != 1 {
return nil
2020-01-23 18:03:37 +05:30
}
switch types[0] {
case "object":
var props = make(map[string]interface{})
2020-01-23 18:03:37 +05:30
properties := kindSchema.GetProperties().GetAdditionalProperties()
if len(properties) == 0 {
return props
2020-01-23 18:03:37 +05:30
}
var wg sync.WaitGroup
var mutex sync.Mutex
wg.Add(len(properties))
for _, property := range properties {
go func(property *openapi_v2.NamedSchema) {
prop := generateEmptyResource(property.GetValue())
mutex.Lock()
props[property.GetName()] = prop
mutex.Unlock()
wg.Done()
}(property)
}
wg.Wait()
return props
case "array":
var array []interface{}
for _, schema := range kindSchema.GetItems().GetSchema() {
array = append(array, generateEmptyResource(schema))
}
return array
case "string":
if kindSchema.GetDefault() != nil {
return string(kindSchema.GetDefault().Value.Value)
}
if kindSchema.GetExample() != nil {
return string(kindSchema.GetExample().GetValue().Value)
}
return ""
case "integer":
if kindSchema.GetDefault() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetDefault().Value.Value))
return val
}
if kindSchema.GetExample() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetExample().GetValue().Value))
return val
}
return 0
case "number":
if kindSchema.GetDefault() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetDefault().Value.Value))
return val
}
if kindSchema.GetExample() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetExample().GetValue().Value))
return val
}
return 0
case "boolean":
if kindSchema.GetDefault() != nil {
2020-01-24 21:09:04 +05:30
return string(kindSchema.GetDefault().Value.Value) == "true"
2020-01-23 18:03:37 +05:30
}
if kindSchema.GetExample() != nil {
2020-01-24 21:09:04 +05:30
return string(kindSchema.GetExample().GetValue().Value) == "true"
2020-01-23 18:03:37 +05:30
}
return false
}
return nil
}