externalsecret_validator.go 4.1 KB

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