secretstore_validator.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "regexp"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  21. )
  22. // Ensures ExternalSecretValidator implements the admission.CustomValidator interface correctly.
  23. var _ admission.CustomValidator = &GenericStoreValidator{}
  24. const (
  25. errInvalidStore = "invalid store"
  26. warnStoreUnmaintained = "store %s isn't currently maintained. Please plan and prepare accordingly."
  27. )
  28. // GenericStoreValidator implements webhook validation for SecretStore and ClusterSecretStore resources.
  29. type GenericStoreValidator struct{}
  30. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  31. func (r *GenericStoreValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
  32. st, ok := obj.(GenericStore)
  33. if !ok {
  34. return nil, errors.New(errInvalidStore)
  35. }
  36. return validateStore(st)
  37. }
  38. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  39. func (r *GenericStoreValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
  40. st, ok := newObj.(GenericStore)
  41. if !ok {
  42. return nil, errors.New(errInvalidStore)
  43. }
  44. return validateStore(st)
  45. }
  46. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  47. func (r *GenericStoreValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
  48. return nil, nil
  49. }
  50. func validateStore(store GenericStore) (admission.Warnings, error) {
  51. if err := validateConditions(store); err != nil {
  52. return nil, err
  53. }
  54. provider, err := GetProvider(store)
  55. if err != nil {
  56. return nil, err
  57. }
  58. isMaintained, err := GetMaintenanceStatus(store)
  59. if err != nil {
  60. return nil, err
  61. }
  62. warns, err := provider.ValidateStore(store)
  63. if !isMaintained {
  64. warns = append(warns, fmt.Sprintf(warnStoreUnmaintained, store.GetName()))
  65. }
  66. return warns, err
  67. }
  68. func validateConditions(store GenericStore) error {
  69. var errs error
  70. for ci, condition := range store.GetSpec().Conditions {
  71. for ri, r := range condition.NamespaceRegexes {
  72. if _, err := regexp.Compile(r); err != nil {
  73. errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
  74. }
  75. }
  76. }
  77. return errs
  78. }