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

280 lines
7.3 KiB
Go
Raw Normal View History

2020-03-04 18:56:59 +05:30
package openapi
import (
2020-03-19 20:45:30 +05:30
"encoding/json"
"errors"
2020-03-04 18:56:59 +05:30
"fmt"
"strconv"
"strings"
"sync"
2020-03-19 20:45:30 +05:30
"github.com/nirmata/kyverno/pkg/engine/utils"
2020-03-04 18:56:59 +05:30
"github.com/nirmata/kyverno/data"
"github.com/golang/glog"
"github.com/nirmata/kyverno/pkg/engine"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
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 openApiGlobalState struct {
mutex sync.RWMutex
document *openapi_v2.Document
definitions map[string]*openapi_v2.Schema
kindToDefinitionName map[string]string
2020-03-06 01:09:38 +05:30
crdList []string
2020-03-04 18:56:59 +05:30
models proto.Models
}
func init() {
2020-03-06 01:09:38 +05:30
defaultDoc, err := getSchemaDocument()
if err != nil {
panic(err)
}
2020-03-04 18:56:59 +05:30
2020-03-06 01:09:38 +05:30
err = useOpenApiDocument(defaultDoc)
if err != nil {
panic(err)
2020-03-04 18:56:59 +05:30
}
}
func ValidatePolicyFields(policyRaw []byte) error {
2020-03-04 18:56:59 +05:30
openApiGlobalState.mutex.RLock()
defer openApiGlobalState.mutex.RUnlock()
var policy v1.ClusterPolicy
err := json.Unmarshal(policyRaw, &policy)
2020-03-19 20:45:30 +05:30
if err != nil {
return err
}
policyUnst, err := utils.ConvertToUnstructured(policyRaw)
if err != nil {
return err
}
err = ValidateResource(*policyUnst.DeepCopy(), "ClusterPolicy")
if err != nil {
return err
}
return validatePolicyMutation(policy)
}
func ValidateResource(patchedResource unstructured.Unstructured, kind string) error {
openApiGlobalState.mutex.RLock()
defer openApiGlobalState.mutex.RUnlock()
var err error
kind = openApiGlobalState.kindToDefinitionName[kind]
schema := openApiGlobalState.models.LookupModel(kind)
if schema == nil {
2020-03-25 02:15:56 +05:30
// Check if kind is a CRD
schema, err = getSchemaFromDefinitions(kind)
if err != nil || schema == nil {
return fmt.Errorf("pre-validation: couldn't find model %s", kind)
}
delete(patchedResource.Object, "kind")
}
if errs := validation.ValidateModel(patchedResource.UnstructuredContent(), 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"))
}
return nil
}
func GetDefinitionNameFromKind(kind string) string {
openApiGlobalState.mutex.RLock()
defer openApiGlobalState.mutex.RUnlock()
return openApiGlobalState.kindToDefinitionName[kind]
}
2020-03-19 20:45:30 +05:30
func validatePolicyMutation(policy v1.ClusterPolicy) error {
2020-03-04 18:56:59 +05:30
var kindToRules = make(map[string][]v1.Rule)
for _, rule := range policy.Spec.Rules {
if rule.HasMutate() {
for _, kind := range rule.MatchResources.Kinds {
kindToRules[kind] = append(kindToRules[kind], rule)
}
}
}
for kind, rules := range kindToRules {
2020-03-06 01:09:38 +05:30
newPolicy := *policy.DeepCopy()
2020-03-04 18:56:59 +05:30
newPolicy.Spec.Rules = rules
resource, _ := generateEmptyResource(openApiGlobalState.definitions[openApiGlobalState.kindToDefinitionName[kind]]).(map[string]interface{})
2020-03-05 22:50:32 +05:30
if resource == nil {
glog.V(4).Infof("Cannot Validate policy: openApi definition now found for %v", kind)
return nil
}
2020-03-04 18:56:59 +05:30
newResource := unstructured.Unstructured{Object: resource}
newResource.SetKind(kind)
2020-03-06 01:52:03 +05:30
patchedResource, err := engine.ForceMutate(nil, *newPolicy.DeepCopy(), newResource)
2020-03-06 01:09:38 +05:30
if err != nil {
return err
2020-03-04 18:56:59 +05:30
}
2020-03-06 01:09:38 +05:30
err = ValidateResource(*patchedResource.DeepCopy(), kind)
2020-03-04 18:56:59 +05:30
if err != nil {
return err
}
}
return nil
}
2020-03-05 22:50:32 +05:30
func useOpenApiDocument(customDoc *openapi_v2.Document) error {
2020-03-04 18:56:59 +05:30
openApiGlobalState.mutex.Lock()
defer openApiGlobalState.mutex.Unlock()
openApiGlobalState.document = customDoc
openApiGlobalState.definitions = make(map[string]*openapi_v2.Schema)
openApiGlobalState.kindToDefinitionName = make(map[string]string)
for _, definition := range openApiGlobalState.document.GetDefinitions().AdditionalProperties {
openApiGlobalState.definitions[definition.GetName()] = definition.GetValue()
path := strings.Split(definition.GetName(), ".")
openApiGlobalState.kindToDefinitionName[path[len(path)-1]] = definition.GetName()
}
var err error
openApiGlobalState.models, err = proto.NewOpenAPIData(openApiGlobalState.document)
if err != nil {
return err
}
return nil
}
func getSchemaDocument() (*openapi_v2.Document, error) {
var spec yaml.MapSlice
err := yaml.Unmarshal([]byte(data.SwaggerDoc), &spec)
if err != nil {
return nil, err
}
return openapi_v2.NewDocument(spec, compiler.NewContext("$root", nil))
}
2020-03-06 01:09:38 +05:30
// For crd, we do not store definition in document
func getSchemaFromDefinitions(kind string) (proto.Schema, error) {
2020-03-19 20:45:30 +05:30
if kind == "" {
return nil, errors.New("invalid kind")
}
2020-03-06 01:09:38 +05:30
path := proto.NewPath(kind)
2020-03-19 20:45:30 +05:30
definition := openApiGlobalState.definitions[kind]
if definition == nil {
return nil, errors.New("could not find definition")
}
// This was added so crd's can access
// normal definitions from existing schema such as
// `metadata` - this maybe a breaking change.
// Removing this may cause policy validate to stop working
existingDefinitions, _ := openApiGlobalState.models.(*proto.Definitions)
return (existingDefinitions).ParseSchema(definition, &path)
2020-03-06 01:09:38 +05:30
}
2020-03-04 18:56:59 +05:30
func generateEmptyResource(kindSchema *openapi_v2.Schema) interface{} {
types := kindSchema.GetType().GetValue()
if kindSchema.GetXRef() != "" {
return generateEmptyResource(openApiGlobalState.definitions[strings.TrimPrefix(kindSchema.GetXRef(), "#/definitions/")])
}
if len(types) != 1 {
2020-03-05 22:50:32 +05:30
if len(kindSchema.GetProperties().GetAdditionalProperties()) > 0 {
types = []string{"object"}
} else {
return nil
}
2020-03-04 18:56:59 +05:30
}
switch types[0] {
case "object":
var props = make(map[string]interface{})
properties := kindSchema.GetProperties().GetAdditionalProperties()
if len(properties) == 0 {
return props
}
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 int64(val)
2020-03-04 18:56:59 +05:30
}
if kindSchema.GetExample() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetExample().GetValue().Value))
return int64(val)
2020-03-04 18:56:59 +05:30
}
return int64(0)
2020-03-04 18:56:59 +05:30
case "number":
if kindSchema.GetDefault() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetDefault().Value.Value))
return int64(val)
2020-03-04 18:56:59 +05:30
}
if kindSchema.GetExample() != nil {
val, _ := strconv.Atoi(string(kindSchema.GetExample().GetValue().Value))
return int64(val)
2020-03-04 18:56:59 +05:30
}
return int64(0)
2020-03-04 18:56:59 +05:30
case "boolean":
if kindSchema.GetDefault() != nil {
return string(kindSchema.GetDefault().Value.Value) == "true"
}
if kindSchema.GetExample() != nil {
return string(kindSchema.GetExample().GetValue().Value) == "true"
}
return false
}
return nil
}