externalsecret_validator.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 v1beta1
  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, fmt.Errorf("unexpected type")
  34. }
  35. var errs error
  36. if (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyMerge) ||
  37. (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyNone) {
  38. errs = errors.Join(errs, fmt.Errorf("deletionPolicy=Delete must not be used when the controller doesn't own the secret. Please set creationPolicy=Owner"))
  39. }
  40. if es.Spec.Target.DeletionPolicy == DeletionPolicyMerge && es.Spec.Target.CreationPolicy == CreatePolicyNone {
  41. errs = errors.Join(errs, fmt.Errorf("deletionPolicy=Merge must not be used with creationPolicy=None. There is no Secret to merge with"))
  42. }
  43. if len(es.Spec.Data) == 0 && len(es.Spec.DataFrom) == 0 {
  44. errs = errors.Join(errs, fmt.Errorf("either data or dataFrom should be specified"))
  45. }
  46. for _, ref := range es.Spec.DataFrom {
  47. findOrExtract := ref.Find != nil || ref.Extract != nil
  48. if findOrExtract && ref.SourceRef != nil && ref.SourceRef.GeneratorRef != nil {
  49. errs = errors.Join(errs, fmt.Errorf("generator can not be used with find or extract"))
  50. }
  51. }
  52. errs = validateDuplicateKeys(es, errs)
  53. return nil, errs
  54. }
  55. func validateDuplicateKeys(es *ExternalSecret, errs error) error {
  56. if es.Spec.Target.DeletionPolicy == DeletionPolicyRetain {
  57. seenKeys := make(map[string]struct{})
  58. for _, data := range es.Spec.Data {
  59. secretKey := data.SecretKey
  60. if _, exists := seenKeys[secretKey]; exists {
  61. errs = errors.Join(errs, fmt.Errorf("duplicate secretKey found: %s", secretKey))
  62. }
  63. seenKeys[secretKey] = struct{}{}
  64. }
  65. }
  66. return errs
  67. }