secretstore_validator.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright © The ESO Authors
  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. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. )
  21. // Ensures ExternalSecretValidator implements the admission.CustomValidator interface correctly.
  22. var _ admission.Validator[*SecretStore] = &GenericStoreValidator{}
  23. var _ admission.Validator[*ClusterSecretStore] = &GenericClusterStoreValidator{}
  24. const (
  25. warnStoreUnmaintained = "store %s isn't currently maintained. Please plan and prepare accordingly."
  26. warnStoreDeprecated = "store %s is deprecated and will stop working on the next major version. Please plan and prepare accordingly."
  27. )
  28. // GenericStoreValidator implements webhook validation for SecretStore resources.
  29. type GenericStoreValidator struct{}
  30. // GenericClusterStoreValidator implements webhook validation for ClusterSecretStore resources.
  31. type GenericClusterStoreValidator struct{}
  32. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  33. func (r *GenericStoreValidator) ValidateCreate(_ context.Context, obj *SecretStore) (admission.Warnings, error) {
  34. return validateStore(obj)
  35. }
  36. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  37. func (r *GenericStoreValidator) ValidateUpdate(_ context.Context, _, newObj *SecretStore) (admission.Warnings, error) {
  38. return validateStore(newObj)
  39. }
  40. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  41. func (r *GenericStoreValidator) ValidateDelete(_ context.Context, _ *SecretStore) (admission.Warnings, error) {
  42. return nil, nil
  43. }
  44. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  45. func (r *GenericClusterStoreValidator) ValidateCreate(_ context.Context, obj *ClusterSecretStore) (admission.Warnings, error) {
  46. return validateStore(obj)
  47. }
  48. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  49. func (r *GenericClusterStoreValidator) ValidateUpdate(_ context.Context, _, newObj *ClusterSecretStore) (admission.Warnings, error) {
  50. return validateStore(newObj)
  51. }
  52. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  53. func (r *GenericClusterStoreValidator) ValidateDelete(_ context.Context, _ *ClusterSecretStore) (admission.Warnings, error) {
  54. return nil, nil
  55. }
  56. func validateStore(store GenericStore) (admission.Warnings, error) {
  57. if err := validateConditions(store); err != nil {
  58. return nil, err
  59. }
  60. provider, err := GetProvider(store)
  61. if err != nil {
  62. return nil, err
  63. }
  64. status, err := GetMaintenanceStatus(store)
  65. if err != nil {
  66. return nil, err
  67. }
  68. warns, err := provider.ValidateStore(store)
  69. switch status {
  70. case MaintenanceStatusNotMaintained:
  71. warns = append(warns, fmt.Sprintf(warnStoreUnmaintained, store.GetName()))
  72. case MaintenanceStatusDeprecated:
  73. warns = append(warns, fmt.Sprintf(warnStoreDeprecated, store.GetName()))
  74. case MaintenanceStatusMaintained:
  75. default:
  76. // no warnings
  77. }
  78. return warns, err
  79. }
  80. func validateConditions(store GenericStore) error {
  81. var errs error
  82. for ci, condition := range store.GetSpec().Conditions {
  83. for ri, r := range condition.NamespaceRegexes {
  84. if _, err := regexp.Compile(r); err != nil {
  85. errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
  86. }
  87. }
  88. }
  89. return errs
  90. }