validation.go 2.2 KB

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