schema.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "reflect"
  18. "regexp"
  19. "strings"
  20. "github.com/emicklei/go-restful/swagger"
  21. "github.com/golang/glog"
  22. apiutil "k8s.io/kubernetes/pkg/api/util"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  25. "k8s.io/kubernetes/pkg/util/yaml"
  26. )
  27. type InvalidTypeError struct {
  28. ExpectedKind reflect.Kind
  29. ObservedKind reflect.Kind
  30. FieldName string
  31. }
  32. func (i *InvalidTypeError) Error() string {
  33. return fmt.Sprintf("expected type %s, for field %s, got %s", i.ExpectedKind.String(), i.FieldName, i.ObservedKind.String())
  34. }
  35. func NewInvalidTypeError(expected reflect.Kind, observed reflect.Kind, fieldName string) error {
  36. return &InvalidTypeError{expected, observed, fieldName}
  37. }
  38. // TypeNotFoundError is returned when specified type
  39. // can not found in schema
  40. type TypeNotFoundError string
  41. func (tnfe TypeNotFoundError) Error() string {
  42. return fmt.Sprintf("couldn't find type: %s", string(tnfe))
  43. }
  44. // Schema is an interface that knows how to validate an API object serialized to a byte array.
  45. type Schema interface {
  46. ValidateBytes(data []byte) error
  47. }
  48. type NullSchema struct{}
  49. func (NullSchema) ValidateBytes(data []byte) error { return nil }
  50. type SwaggerSchema struct {
  51. api swagger.ApiDeclaration
  52. delegate Schema // For delegating to other api groups
  53. }
  54. func NewSwaggerSchemaFromBytes(data []byte, factory Schema) (Schema, error) {
  55. schema := &SwaggerSchema{}
  56. err := json.Unmarshal(data, &schema.api)
  57. if err != nil {
  58. return nil, err
  59. }
  60. schema.delegate = factory
  61. return schema, nil
  62. }
  63. // validateList unpacks a list and validate every item in the list.
  64. // It return nil if every item is ok.
  65. // Otherwise it return an error list contain errors of every item.
  66. func (s *SwaggerSchema) validateList(obj map[string]interface{}) []error {
  67. items, exists := obj["items"]
  68. if !exists {
  69. return []error{fmt.Errorf("no items field in %#v", obj)}
  70. }
  71. return s.validateItems(items)
  72. }
  73. func (s *SwaggerSchema) validateItems(items interface{}) []error {
  74. allErrs := []error{}
  75. itemList, ok := items.([]interface{})
  76. if !ok {
  77. return append(allErrs, fmt.Errorf("items isn't a slice"))
  78. }
  79. for i, item := range itemList {
  80. fields, ok := item.(map[string]interface{})
  81. if !ok {
  82. allErrs = append(allErrs, fmt.Errorf("items[%d] isn't a map[string]interface{}", i))
  83. continue
  84. }
  85. groupVersion := fields["apiVersion"]
  86. if groupVersion == nil {
  87. allErrs = append(allErrs, fmt.Errorf("items[%d].apiVersion not set", i))
  88. continue
  89. }
  90. itemVersion, ok := groupVersion.(string)
  91. if !ok {
  92. allErrs = append(allErrs, fmt.Errorf("items[%d].apiVersion isn't string type", i))
  93. continue
  94. }
  95. if len(itemVersion) == 0 {
  96. allErrs = append(allErrs, fmt.Errorf("items[%d].apiVersion is empty", i))
  97. }
  98. kind := fields["kind"]
  99. if kind == nil {
  100. allErrs = append(allErrs, fmt.Errorf("items[%d].kind not set", i))
  101. continue
  102. }
  103. itemKind, ok := kind.(string)
  104. if !ok {
  105. allErrs = append(allErrs, fmt.Errorf("items[%d].kind isn't string type", i))
  106. continue
  107. }
  108. if len(itemKind) == 0 {
  109. allErrs = append(allErrs, fmt.Errorf("items[%d].kind is empty", i))
  110. }
  111. version := apiutil.GetVersion(itemVersion)
  112. errs := s.ValidateObject(item, "", version+"."+itemKind)
  113. if len(errs) >= 1 {
  114. allErrs = append(allErrs, errs...)
  115. }
  116. }
  117. return allErrs
  118. }
  119. func (s *SwaggerSchema) ValidateBytes(data []byte) error {
  120. var obj interface{}
  121. out, err := yaml.ToJSON(data)
  122. if err != nil {
  123. return err
  124. }
  125. data = out
  126. if err := json.Unmarshal(data, &obj); err != nil {
  127. return err
  128. }
  129. fields, ok := obj.(map[string]interface{})
  130. if !ok {
  131. return fmt.Errorf("error in unmarshaling data %s", string(data))
  132. }
  133. groupVersion := fields["apiVersion"]
  134. if groupVersion == nil {
  135. return fmt.Errorf("apiVersion not set")
  136. }
  137. if _, ok := groupVersion.(string); !ok {
  138. return fmt.Errorf("apiVersion isn't string type")
  139. }
  140. kind := fields["kind"]
  141. if kind == nil {
  142. return fmt.Errorf("kind not set")
  143. }
  144. if _, ok := kind.(string); !ok {
  145. return fmt.Errorf("kind isn't string type")
  146. }
  147. if strings.HasSuffix(kind.(string), "List") {
  148. return utilerrors.NewAggregate(s.validateList(fields))
  149. }
  150. version := apiutil.GetVersion(groupVersion.(string))
  151. allErrs := s.ValidateObject(obj, "", version+"."+kind.(string))
  152. if len(allErrs) == 1 {
  153. return allErrs[0]
  154. }
  155. return utilerrors.NewAggregate(allErrs)
  156. }
  157. func (s *SwaggerSchema) ValidateObject(obj interface{}, fieldName, typeName string) []error {
  158. allErrs := []error{}
  159. models := s.api.Models
  160. model, ok := models.At(typeName)
  161. // Verify the api version matches. This is required for nested types with differing api versions because
  162. // s.api only has schema for 1 api version (the parent object type's version).
  163. // e.g. an extensions/v1beta1 Template embedding a /v1 Service requires the schema for the extensions/v1beta1
  164. // api to delegate to the schema for the /v1 api.
  165. // Only do this for !ok objects so that cross ApiVersion vendored types take precedence.
  166. if !ok && s.delegate != nil {
  167. fields, mapOk := obj.(map[string]interface{})
  168. if !mapOk {
  169. return append(allErrs, fmt.Errorf("field %s: expected object of type map[string]interface{}, but the actual type is %T", fieldName, obj))
  170. }
  171. if delegated, err := s.delegateIfDifferentApiVersion(runtime.Unstructured{Object: fields}); delegated {
  172. if err != nil {
  173. allErrs = append(allErrs, err)
  174. }
  175. return allErrs
  176. }
  177. }
  178. if !ok {
  179. return append(allErrs, TypeNotFoundError(typeName))
  180. }
  181. properties := model.Properties
  182. if len(properties.List) == 0 {
  183. // The object does not have any sub-fields.
  184. return nil
  185. }
  186. fields, ok := obj.(map[string]interface{})
  187. if !ok {
  188. return append(allErrs, fmt.Errorf("field %s: expected object of type map[string]interface{}, but the actual type is %T", fieldName, obj))
  189. }
  190. if len(fieldName) > 0 {
  191. fieldName = fieldName + "."
  192. }
  193. // handle required fields
  194. for _, requiredKey := range model.Required {
  195. if _, ok := fields[requiredKey]; !ok {
  196. allErrs = append(allErrs, fmt.Errorf("field %s: is required", requiredKey))
  197. }
  198. }
  199. for key, value := range fields {
  200. details, ok := properties.At(key)
  201. // Special case for runtime.RawExtension and runtime.Objects because they always fail to validate
  202. // This is because the actual values will be of some sub-type (e.g. Deployment) not the expected
  203. // super-type (RawExtension)
  204. if s.isGenericArray(details) {
  205. errs := s.validateItems(value)
  206. if len(errs) > 0 {
  207. allErrs = append(allErrs, errs...)
  208. }
  209. continue
  210. }
  211. if !ok {
  212. allErrs = append(allErrs, fmt.Errorf("found invalid field %s for %s", key, typeName))
  213. continue
  214. }
  215. if details.Type == nil && details.Ref == nil {
  216. allErrs = append(allErrs, fmt.Errorf("could not find the type of %s from object: %v", key, details))
  217. }
  218. var fieldType string
  219. if details.Type != nil {
  220. fieldType = *details.Type
  221. } else {
  222. fieldType = *details.Ref
  223. }
  224. if value == nil {
  225. glog.V(2).Infof("Skipping nil field: %s", key)
  226. continue
  227. }
  228. errs := s.validateField(value, fieldName+key, fieldType, &details)
  229. if len(errs) > 0 {
  230. allErrs = append(allErrs, errs...)
  231. }
  232. }
  233. return allErrs
  234. }
  235. // delegateIfDifferentApiVersion delegates the validation of an object if its ApiGroup does not match the
  236. // current SwaggerSchema.
  237. // First return value is true if the validation was delegated (by a different ApiGroup SwaggerSchema)
  238. // Second return value is the result of the delegated validation if performed.
  239. func (s *SwaggerSchema) delegateIfDifferentApiVersion(obj runtime.Unstructured) (bool, error) {
  240. // Never delegate objects in the same ApiVersion or we will get infinite recursion
  241. if !s.isDifferentApiVersion(obj) {
  242. return false, nil
  243. }
  244. // Convert the object back into bytes so that we can pass it to the ValidateBytes function
  245. m, err := json.Marshal(obj.Object)
  246. if err != nil {
  247. return true, err
  248. }
  249. // Delegate validation of this object to the correct SwaggerSchema for its ApiGroup
  250. return true, s.delegate.ValidateBytes(m)
  251. }
  252. // isDifferentApiVersion Returns true if obj lives in a different ApiVersion than the SwaggerSchema does.
  253. // The SwaggerSchema will not be able to process objects in different ApiVersions unless they are vendored.
  254. func (s *SwaggerSchema) isDifferentApiVersion(obj runtime.Unstructured) bool {
  255. groupVersion := obj.GetAPIVersion()
  256. return len(groupVersion) > 0 && s.api.ApiVersion != groupVersion
  257. }
  258. // isGenericArray Returns true if p is an array of generic Objects - either RawExtension or Object.
  259. func (s *SwaggerSchema) isGenericArray(p swagger.ModelProperty) bool {
  260. return p.DataTypeFields.Type != nil &&
  261. *p.DataTypeFields.Type == "array" &&
  262. p.Items != nil &&
  263. p.Items.Ref != nil &&
  264. (*p.Items.Ref == "runtime.RawExtension" || *p.Items.Ref == "runtime.Object")
  265. }
  266. // This matches type name in the swagger spec, such as "v1.Binding".
  267. var versionRegexp = regexp.MustCompile(`^(v.+|unversioned)\..*`)
  268. func (s *SwaggerSchema) validateField(value interface{}, fieldName, fieldType string, fieldDetails *swagger.ModelProperty) []error {
  269. allErrs := []error{}
  270. if reflect.TypeOf(value) == nil {
  271. return append(allErrs, fmt.Errorf("unexpected nil value for field %v", fieldName))
  272. }
  273. // TODO: caesarxuchao: because we have multiple group/versions and objects
  274. // may reference objects in other group, the commented out way of checking
  275. // if a filedType is a type defined by us is outdated. We use a hacky way
  276. // for now.
  277. // TODO: the type name in the swagger spec is something like "v1.Binding",
  278. // and the "v1" is generated from the package name, not the groupVersion of
  279. // the type. We need to fix go-restful to embed the group name in the type
  280. // name, otherwise we couldn't handle identically named types in different
  281. // groups correctly.
  282. if versionRegexp.MatchString(fieldType) {
  283. // if strings.HasPrefix(fieldType, apiVersion) {
  284. return s.ValidateObject(value, fieldName, fieldType)
  285. }
  286. switch fieldType {
  287. case "string":
  288. // Be loose about what we accept for 'string' since we use IntOrString in a couple of places
  289. _, isString := value.(string)
  290. _, isNumber := value.(float64)
  291. _, isInteger := value.(int)
  292. if !isString && !isNumber && !isInteger {
  293. return append(allErrs, NewInvalidTypeError(reflect.String, reflect.TypeOf(value).Kind(), fieldName))
  294. }
  295. case "array":
  296. arr, ok := value.([]interface{})
  297. if !ok {
  298. return append(allErrs, NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName))
  299. }
  300. var arrType string
  301. if fieldDetails.Items.Ref == nil && fieldDetails.Items.Type == nil {
  302. return append(allErrs, NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName))
  303. }
  304. if fieldDetails.Items.Ref != nil {
  305. arrType = *fieldDetails.Items.Ref
  306. } else {
  307. arrType = *fieldDetails.Items.Type
  308. }
  309. for ix := range arr {
  310. errs := s.validateField(arr[ix], fmt.Sprintf("%s[%d]", fieldName, ix), arrType, nil)
  311. if len(errs) > 0 {
  312. allErrs = append(allErrs, errs...)
  313. }
  314. }
  315. case "uint64":
  316. case "int64":
  317. case "integer":
  318. _, isNumber := value.(float64)
  319. _, isInteger := value.(int)
  320. if !isNumber && !isInteger {
  321. return append(allErrs, NewInvalidTypeError(reflect.Int, reflect.TypeOf(value).Kind(), fieldName))
  322. }
  323. case "float64":
  324. if _, ok := value.(float64); !ok {
  325. return append(allErrs, NewInvalidTypeError(reflect.Float64, reflect.TypeOf(value).Kind(), fieldName))
  326. }
  327. case "boolean":
  328. if _, ok := value.(bool); !ok {
  329. return append(allErrs, NewInvalidTypeError(reflect.Bool, reflect.TypeOf(value).Kind(), fieldName))
  330. }
  331. // API servers before release 1.3 produce swagger spec with `type: "any"` as the fallback type, while newer servers produce spec with `type: "object"`.
  332. // We have both here so that kubectl can work with both old and new api servers.
  333. case "object":
  334. case "any":
  335. default:
  336. return append(allErrs, fmt.Errorf("unexpected type: %v", fieldType))
  337. }
  338. return allErrs
  339. }