secretstore_validator.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Reject an unparseable refreshInterval duration string at admission. The
  61. // x-kubernetes-int-or-string schema accepts any string, so the duration
  62. // format can only be validated here (or at reconcile).
  63. if _, err := store.GetSpec().GetRefreshInterval(); err != nil {
  64. return nil, err
  65. }
  66. provider, err := GetProvider(store)
  67. if err != nil {
  68. return nil, err
  69. }
  70. status, err := GetMaintenanceStatus(store)
  71. if err != nil {
  72. return nil, err
  73. }
  74. warns, err := provider.ValidateStore(store)
  75. switch status {
  76. case MaintenanceStatusNotMaintained:
  77. warns = append(warns, fmt.Sprintf(warnStoreUnmaintained, store.GetName()))
  78. case MaintenanceStatusDeprecated:
  79. warns = append(warns, fmt.Sprintf(warnStoreDeprecated, store.GetName()))
  80. case MaintenanceStatusMaintained:
  81. default:
  82. // no warnings
  83. }
  84. return warns, err
  85. }
  86. func validateConditions(store GenericStore) error {
  87. var errs error
  88. for ci, condition := range store.GetSpec().Conditions {
  89. for ri, r := range condition.NamespaceRegexes {
  90. if _, err := regexp.Compile(r); err != nil {
  91. errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
  92. }
  93. }
  94. }
  95. return errs
  96. }