externalsecret_validator.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://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 v1
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. )
  21. // Ensures ExternalSecretValidator implements the admission.CustomValidator interface correctly.
  22. var _ admission.CustomValidator = &ExternalSecretValidator{}
  23. // ExternalSecretValidator implements a validating webhook for ExternalSecrets.
  24. type ExternalSecretValidator struct{}
  25. // ValidateCreate is called on creation of ExternalSecret resource object.
  26. func (esv *ExternalSecretValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
  27. return validateExternalSecret(obj)
  28. }
  29. // ValidateUpdate is called when updating an ExternalSecret resource object.
  30. func (esv *ExternalSecretValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
  31. return validateExternalSecret(newObj)
  32. }
  33. // ValidateDelete is called when deleting an ExternalSecret resource object.
  34. func (esv *ExternalSecretValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
  35. return nil, nil
  36. }
  37. func validateExternalSecret(obj runtime.Object) (admission.Warnings, error) {
  38. es, ok := obj.(*ExternalSecret)
  39. if !ok {
  40. return nil, errors.New("unexpected type")
  41. }
  42. var errs error
  43. if err := validatePolicies(es); err != nil {
  44. errs = errors.Join(errs, err)
  45. }
  46. if len(es.Spec.Data) == 0 && len(es.Spec.DataFrom) == 0 {
  47. errs = errors.Join(errs, errors.New("either data or dataFrom should be specified"))
  48. }
  49. for _, ref := range es.Spec.DataFrom {
  50. if err := validateExtractFindGenerator(ref); err != nil {
  51. errs = errors.Join(errs, err)
  52. }
  53. if err := validateFindExtractSourceRef(ref); err != nil {
  54. errs = errors.Join(errs, err)
  55. }
  56. if err := validateSourceRef(ref); err != nil {
  57. errs = errors.Join(errs, err)
  58. }
  59. }
  60. errs = validateDuplicateKeys(es, errs)
  61. return nil, errs
  62. }
  63. func validateSourceRef(ref ExternalSecretDataFromRemoteRef) error {
  64. if ref.SourceRef != nil && ref.SourceRef.GeneratorRef == nil && ref.SourceRef.SecretStoreRef == nil {
  65. return errors.New("generatorRef or storeRef must be set when using sourceRef in dataFrom")
  66. }
  67. return nil
  68. }
  69. func validateFindExtractSourceRef(ref ExternalSecretDataFromRemoteRef) error {
  70. if ref.Find == nil && ref.Extract == nil && ref.SourceRef == nil {
  71. return errors.New("either extract, find, or sourceRef must be set to dataFrom")
  72. }
  73. return nil
  74. }
  75. func validateExtractFindGenerator(ref ExternalSecretDataFromRemoteRef) error {
  76. generatorRef := ref.SourceRef != nil && ref.SourceRef.GeneratorRef != nil
  77. if (ref.Find != nil && (ref.Extract != nil || generatorRef)) || (ref.Extract != nil && (ref.Find != nil || generatorRef)) || (generatorRef && (ref.Find != nil || ref.Extract != nil)) {
  78. return errors.New("extract, find, or generatorRef cannot be set at the same time")
  79. }
  80. return nil
  81. }
  82. func validatePolicies(es *ExternalSecret) error {
  83. var errs error
  84. if (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyMerge) ||
  85. (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyNone) {
  86. errs = errors.Join(errs, errors.New("deletionPolicy=Delete must not be used when the controller doesn't own the secret. Please set creationPolicy=Owner"))
  87. }
  88. if es.Spec.Target.DeletionPolicy == DeletionPolicyMerge && es.Spec.Target.CreationPolicy == CreatePolicyNone {
  89. errs = errors.Join(errs, errors.New("deletionPolicy=Merge must not be used with creationPolicy=None. There is no Secret to merge with"))
  90. }
  91. return errs
  92. }
  93. func validateDuplicateKeys(es *ExternalSecret, errs error) error {
  94. if es.Spec.Target.DeletionPolicy == DeletionPolicyRetain {
  95. seenKeys := make(map[string]struct{})
  96. for _, data := range es.Spec.Data {
  97. secretKey := data.SecretKey
  98. if _, exists := seenKeys[secretKey]; exists {
  99. errs = errors.Join(errs, fmt.Errorf("duplicate secretKey found: %s", secretKey))
  100. }
  101. seenKeys[secretKey] = struct{}{}
  102. }
  103. }
  104. return errs
  105. }