externalsecret_validator.go 4.5 KB

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