secretstore_validator.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 v1beta1
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "regexp"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. )
  21. var _ admission.Validator[*SecretStore] = &GenericStoreValidator{}
  22. var _ admission.Validator[*ClusterSecretStore] = &GenericClusterStoreValidator{}
  23. // GenericStoreValidator provides validation for SecretStore resources.
  24. type GenericStoreValidator struct{}
  25. // GenericClusterStoreValidator provides validation for ClusterSecretStore resources.
  26. type GenericClusterStoreValidator struct{}
  27. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  28. func (r *GenericClusterStoreValidator) ValidateCreate(_ context.Context, obj *ClusterSecretStore) (admission.Warnings, error) {
  29. return validateStore(obj)
  30. }
  31. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  32. func (r *GenericClusterStoreValidator) ValidateUpdate(_ context.Context, _, newObj *ClusterSecretStore) (admission.Warnings, error) {
  33. return validateStore(newObj)
  34. }
  35. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  36. func (r *GenericClusterStoreValidator) ValidateDelete(_ context.Context, _ *ClusterSecretStore) (admission.Warnings, error) {
  37. return nil, nil
  38. }
  39. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  40. func (r *GenericStoreValidator) ValidateCreate(_ context.Context, obj *SecretStore) (admission.Warnings, error) {
  41. return validateStore(obj)
  42. }
  43. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  44. func (r *GenericStoreValidator) ValidateUpdate(_ context.Context, _, newObj *SecretStore) (admission.Warnings, error) {
  45. return validateStore(newObj)
  46. }
  47. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  48. func (r *GenericStoreValidator) ValidateDelete(_ context.Context, _ *SecretStore) (admission.Warnings, error) {
  49. return nil, nil
  50. }
  51. func validateStore(store GenericStore) (admission.Warnings, error) {
  52. if err := validateConditions(store); err != nil {
  53. return nil, err
  54. }
  55. provider, err := GetProvider(store)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return provider.ValidateStore(store)
  60. }
  61. func validateConditions(store GenericStore) error {
  62. var errs error
  63. for ci, condition := range store.GetSpec().Conditions {
  64. for ri, r := range condition.NamespaceRegexes {
  65. if _, err := regexp.Compile(r); err != nil {
  66. errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
  67. }
  68. }
  69. }
  70. return errs
  71. }