validation.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2016 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. "fmt"
  16. "k8s.io/apimachinery/pkg/util/validation/field"
  17. "k8s.io/kubernetes/pkg/apis/certificates"
  18. apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
  19. )
  20. // validateCSR validates the signature and formatting of a base64-wrapped,
  21. // PEM-encoded PKCS#10 certificate signing request. If this is invalid, we must
  22. // not accept the CSR for further processing.
  23. func validateCSR(obj *certificates.CertificateSigningRequest) error {
  24. csr, err := certificates.ParseCSR(obj)
  25. if err != nil {
  26. return err
  27. }
  28. // check that the signature is valid
  29. err = csr.CheckSignature()
  30. if err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. // We don't care what you call your certificate requests.
  36. func ValidateCertificateRequestName(name string, prefix bool) []string {
  37. return nil
  38. }
  39. func ValidateCertificateSigningRequest(csr *certificates.CertificateSigningRequest) field.ErrorList {
  40. isNamespaced := false
  41. allErrs := apivalidation.ValidateObjectMeta(&csr.ObjectMeta, isNamespaced, ValidateCertificateRequestName, field.NewPath("metadata"))
  42. err := validateCSR(csr)
  43. specPath := field.NewPath("spec")
  44. if err != nil {
  45. allErrs = append(allErrs, field.Invalid(specPath.Child("request"), csr.Spec.Request, fmt.Sprintf("%v", err)))
  46. }
  47. if len(csr.Spec.Usages) == 0 {
  48. allErrs = append(allErrs, field.Required(specPath.Child("usages"), "usages must be provided"))
  49. }
  50. return allErrs
  51. }
  52. func ValidateCertificateSigningRequestUpdate(newCSR, oldCSR *certificates.CertificateSigningRequest) field.ErrorList {
  53. validationErrorList := ValidateCertificateSigningRequest(newCSR)
  54. metaUpdateErrorList := apivalidation.ValidateObjectMetaUpdate(&newCSR.ObjectMeta, &oldCSR.ObjectMeta, field.NewPath("metadata"))
  55. return append(validationErrorList, metaUpdateErrorList...)
  56. }