externalsecret_validator.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package v1
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  19. )
  20. type ExternalSecretValidator struct{}
  21. func (esv *ExternalSecretValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
  22. return validateExternalSecret(obj)
  23. }
  24. func (esv *ExternalSecretValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
  25. return validateExternalSecret(newObj)
  26. }
  27. func (esv *ExternalSecretValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
  28. return nil, nil
  29. }
  30. func validateExternalSecret(obj runtime.Object) (admission.Warnings, error) {
  31. es, ok := obj.(*ExternalSecret)
  32. if !ok {
  33. return nil, errors.New("unexpected type")
  34. }
  35. var errs error
  36. if err := validatePolicies(es); err != nil {
  37. errs = errors.Join(errs, err)
  38. }
  39. if len(es.Spec.Data) == 0 && len(es.Spec.DataFrom) == 0 {
  40. errs = errors.Join(errs, errors.New("either data or dataFrom should be specified"))
  41. }
  42. for _, ref := range es.Spec.DataFrom {
  43. if err := validateExtractFindGenerator(ref); err != nil {
  44. errs = errors.Join(errs, err)
  45. }
  46. if err := validateFindExtractSourceRef(ref); err != nil {
  47. errs = errors.Join(errs, err)
  48. }
  49. if err := validateSourceRef(ref); err != nil {
  50. errs = errors.Join(errs, err)
  51. }
  52. }
  53. errs = validateDuplicateKeys(es, errs)
  54. return nil, errs
  55. }
  56. func validateSourceRef(ref ExternalSecretDataFromRemoteRef) error {
  57. if ref.SourceRef != nil && ref.SourceRef.GeneratorRef == nil && ref.SourceRef.SecretStoreRef == nil {
  58. return errors.New("generatorRef or storeRef must be set when using sourceRef in dataFrom")
  59. }
  60. return nil
  61. }
  62. func validateFindExtractSourceRef(ref ExternalSecretDataFromRemoteRef) error {
  63. if ref.Find == nil && ref.Extract == nil && ref.SourceRef == nil {
  64. return errors.New("either extract, find, or sourceRef must be set to dataFrom")
  65. }
  66. return nil
  67. }
  68. func validateExtractFindGenerator(ref ExternalSecretDataFromRemoteRef) error {
  69. generatorRef := ref.SourceRef != nil && ref.SourceRef.GeneratorRef != nil
  70. if (ref.Find != nil && (ref.Extract != nil || generatorRef)) || (ref.Extract != nil && (ref.Find != nil || generatorRef)) || (generatorRef && (ref.Find != nil || ref.Extract != nil)) {
  71. return errors.New("extract, find, or generatorRef cannot be set at the same time")
  72. }
  73. return nil
  74. }
  75. func validatePolicies(es *ExternalSecret) error {
  76. var errs error
  77. if (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyMerge) ||
  78. (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyNone) {
  79. errs = errors.Join(errs, errors.New("deletionPolicy=Delete must not be used when the controller doesn't own the secret. Please set creationPolicy=Owner"))
  80. }
  81. if es.Spec.Target.DeletionPolicy == DeletionPolicyMerge && es.Spec.Target.CreationPolicy == CreatePolicyNone {
  82. errs = errors.Join(errs, errors.New("deletionPolicy=Merge must not be used with creationPolicy=None. There is no Secret to merge with"))
  83. }
  84. return errs
  85. }
  86. func validateDuplicateKeys(es *ExternalSecret, errs error) error {
  87. if es.Spec.Target.DeletionPolicy == DeletionPolicyRetain {
  88. seenKeys := make(map[string]struct{})
  89. for _, data := range es.Spec.Data {
  90. secretKey := data.SecretKey
  91. if _, exists := seenKeys[secretKey]; exists {
  92. errs = errors.Join(errs, fmt.Errorf("duplicate secretKey found: %s", secretKey))
  93. }
  94. seenKeys[secretKey] = struct{}{}
  95. }
  96. }
  97. return errs
  98. }