2014-11-20 05:25:24 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-11-20 05:25:24 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/emicklei/go-restful/swagger"
|
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/errors"
|
|
|
|
errs "k8s.io/kubernetes/pkg/util/fielderrors"
|
|
|
|
"k8s.io/kubernetes/pkg/util/yaml"
|
2014-11-20 05:25:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InvalidTypeError struct {
|
|
|
|
ExpectedKind reflect.Kind
|
|
|
|
ObservedKind reflect.Kind
|
|
|
|
FieldName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InvalidTypeError) Error() string {
|
|
|
|
return fmt.Sprintf("expected type %s, for field %s, got %s", i.ExpectedKind.String(), i.FieldName, i.ObservedKind.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInvalidTypeError(expected reflect.Kind, observed reflect.Kind, fieldName string) error {
|
|
|
|
return &InvalidTypeError{expected, observed, fieldName}
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:50:31 +00:00
|
|
|
// Schema is an interface that knows how to validate an API object serialized to a byte array.
|
|
|
|
type Schema interface {
|
|
|
|
ValidateBytes(data []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type NullSchema struct{}
|
|
|
|
|
|
|
|
func (NullSchema) ValidateBytes(data []byte) error { return nil }
|
|
|
|
|
|
|
|
type SwaggerSchema struct {
|
2014-11-20 05:25:24 +00:00
|
|
|
api swagger.ApiDeclaration
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:50:31 +00:00
|
|
|
func NewSwaggerSchemaFromBytes(data []byte) (Schema, error) {
|
|
|
|
schema := &SwaggerSchema{}
|
2014-11-20 05:25:24 +00:00
|
|
|
err := json.Unmarshal(data, &schema.api)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return schema, nil
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:50:31 +00:00
|
|
|
func (s *SwaggerSchema) ValidateBytes(data []byte) error {
|
2014-11-20 05:25:24 +00:00
|
|
|
var obj interface{}
|
2015-03-17 03:43:59 +00:00
|
|
|
out, err := yaml.ToJSON(data)
|
2014-11-20 05:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-17 03:43:59 +00:00
|
|
|
data = out
|
|
|
|
if err := json.Unmarshal(data, &obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-14 18:31:12 +00:00
|
|
|
fields, ok := obj.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("error in unmarshaling data %s", string(data))
|
|
|
|
}
|
2015-06-07 13:02:27 +00:00
|
|
|
apiVersion := fields["apiVersion"]
|
|
|
|
if apiVersion == nil {
|
2015-06-10 19:45:10 +00:00
|
|
|
return fmt.Errorf("apiVersion not set")
|
2015-06-07 13:02:27 +00:00
|
|
|
}
|
|
|
|
kind := fields["kind"]
|
|
|
|
if kind == nil {
|
2015-06-10 19:45:10 +00:00
|
|
|
return fmt.Errorf("kind not set")
|
2015-06-07 13:02:27 +00:00
|
|
|
}
|
2015-06-18 06:16:37 +00:00
|
|
|
allErrs := s.ValidateObject(obj, apiVersion.(string), "", apiVersion.(string)+"."+kind.(string))
|
|
|
|
if len(allErrs) == 1 {
|
|
|
|
return allErrs[0]
|
|
|
|
}
|
|
|
|
return errors.NewAggregate(allErrs)
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 06:16:37 +00:00
|
|
|
func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, typeName string) errs.ValidationErrorList {
|
|
|
|
allErrs := errs.ValidationErrorList{}
|
2014-11-20 05:25:24 +00:00
|
|
|
models := s.api.Models
|
|
|
|
// TODO: handle required fields here too.
|
2015-05-28 23:43:13 +00:00
|
|
|
model, ok := models.At(typeName)
|
2014-11-20 05:25:24 +00:00
|
|
|
if !ok {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, fmt.Errorf("couldn't find type: %s", typeName))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
properties := model.Properties
|
2015-05-28 23:43:13 +00:00
|
|
|
if len(properties.List) == 0 {
|
2015-04-14 18:31:12 +00:00
|
|
|
// The object does not have any sub-fields.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fields, ok := obj.(map[string]interface{})
|
|
|
|
if !ok {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, fmt.Errorf("field %s: expected object of type map[string]interface{}, but the actual type is %T", fieldName, obj))
|
2015-04-14 18:31:12 +00:00
|
|
|
}
|
2014-11-20 05:25:24 +00:00
|
|
|
if len(fieldName) > 0 {
|
|
|
|
fieldName = fieldName + "."
|
|
|
|
}
|
2015-07-28 00:29:46 +00:00
|
|
|
// handle required fields
|
2015-06-18 06:16:37 +00:00
|
|
|
for _, requiredKey := range model.Required {
|
|
|
|
if _, ok := fields[requiredKey]; !ok {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("field %s: is required", requiredKey))
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 05:25:24 +00:00
|
|
|
for key, value := range fields {
|
2015-05-28 23:43:13 +00:00
|
|
|
details, ok := properties.At(key)
|
2014-11-20 05:25:24 +00:00
|
|
|
if !ok {
|
2015-07-28 00:29:46 +00:00
|
|
|
allErrs = append(allErrs, fmt.Errorf("found invalid field %s for %s", key, typeName))
|
2014-11-20 05:25:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-04-14 18:31:12 +00:00
|
|
|
if details.Type == nil && details.Ref == nil {
|
2015-06-18 06:16:37 +00:00
|
|
|
allErrs = append(allErrs, fmt.Errorf("could not find the type of %s from object: %v", key, details))
|
2015-04-14 18:31:12 +00:00
|
|
|
}
|
|
|
|
var fieldType string
|
|
|
|
if details.Type != nil {
|
|
|
|
fieldType = *details.Type
|
|
|
|
} else {
|
|
|
|
fieldType = *details.Ref
|
2015-03-27 22:47:40 +00:00
|
|
|
}
|
2014-11-20 05:25:24 +00:00
|
|
|
if value == nil {
|
|
|
|
glog.V(2).Infof("Skipping nil field: %s", key)
|
|
|
|
continue
|
|
|
|
}
|
2015-06-18 06:16:37 +00:00
|
|
|
errs := s.validateField(value, apiVersion, fieldName+key, fieldType, &details)
|
|
|
|
if len(errs) > 0 {
|
2014-11-20 05:25:24 +00:00
|
|
|
glog.Errorf("Validation failed for: %s, %v", key, value)
|
2015-06-18 06:16:37 +00:00
|
|
|
allErrs = append(allErrs, errs...)
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-18 06:16:37 +00:00
|
|
|
return allErrs
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 06:16:37 +00:00
|
|
|
func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName, fieldType string, fieldDetails *swagger.ModelProperty) errs.ValidationErrorList {
|
2014-11-20 05:25:24 +00:00
|
|
|
if strings.HasPrefix(fieldType, apiVersion) {
|
|
|
|
return s.ValidateObject(value, apiVersion, fieldName, fieldType)
|
|
|
|
}
|
2015-06-18 06:16:37 +00:00
|
|
|
allErrs := errs.ValidationErrorList{}
|
2014-11-20 05:25:24 +00:00
|
|
|
switch fieldType {
|
|
|
|
case "string":
|
|
|
|
// Be loose about what we accept for 'string' since we use IntOrString in a couple of places
|
|
|
|
_, isString := value.(string)
|
|
|
|
_, isNumber := value.(float64)
|
2014-11-26 04:10:21 +00:00
|
|
|
_, isInteger := value.(int)
|
|
|
|
if !isString && !isNumber && !isInteger {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, NewInvalidTypeError(reflect.String, reflect.TypeOf(value).Kind(), fieldName))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
case "array":
|
|
|
|
arr, ok := value.([]interface{})
|
|
|
|
if !ok {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
2015-04-14 18:31:12 +00:00
|
|
|
var arrType string
|
|
|
|
if fieldDetails.Items.Ref == nil && fieldDetails.Items.Type == nil {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName))
|
2015-04-14 18:31:12 +00:00
|
|
|
}
|
|
|
|
if fieldDetails.Items.Ref != nil {
|
|
|
|
arrType = *fieldDetails.Items.Ref
|
|
|
|
} else {
|
|
|
|
arrType = *fieldDetails.Items.Type
|
|
|
|
}
|
2014-11-20 05:25:24 +00:00
|
|
|
for ix := range arr {
|
2015-06-18 06:16:37 +00:00
|
|
|
errs := s.validateField(arr[ix], apiVersion, fmt.Sprintf("%s[%d]", fieldName, ix), arrType, nil)
|
|
|
|
if len(errs) > 0 {
|
|
|
|
allErrs = append(allErrs, errs...)
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "uint64":
|
2015-05-05 16:15:12 +00:00
|
|
|
case "int64":
|
2014-11-20 05:25:24 +00:00
|
|
|
case "integer":
|
2014-11-26 04:10:21 +00:00
|
|
|
_, isNumber := value.(float64)
|
|
|
|
_, isInteger := value.(int)
|
|
|
|
if !isNumber && !isInteger {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, NewInvalidTypeError(reflect.Int, reflect.TypeOf(value).Kind(), fieldName))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
case "float64":
|
|
|
|
if _, ok := value.(float64); !ok {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, NewInvalidTypeError(reflect.Float64, reflect.TypeOf(value).Kind(), fieldName))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
|
|
|
case "boolean":
|
|
|
|
if _, ok := value.(bool); !ok {
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, NewInvalidTypeError(reflect.Bool, reflect.TypeOf(value).Kind(), fieldName))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
2015-04-14 18:31:12 +00:00
|
|
|
case "any":
|
2014-11-20 05:25:24 +00:00
|
|
|
default:
|
2015-06-18 06:16:37 +00:00
|
|
|
return append(allErrs, fmt.Errorf("unexpected type: %v", fieldType))
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|
2015-06-18 06:16:37 +00:00
|
|
|
return allErrs
|
2014-11-20 05:25:24 +00:00
|
|
|
}
|