validate.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 openbao
  14. import (
  15. "errors"
  16. "fmt"
  17. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  18. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. "github.com/external-secrets/external-secrets/runtime/esutils"
  20. )
  21. const (
  22. errInvalidStore = "invalid store"
  23. errInvalidStoreSpec = "invalid store spec"
  24. errInvalidStoreProv = "invalid store provider"
  25. errInvalidOpenBaoProv = "invalid OpenBao provider"
  26. errInvalidTokenRef = "invalid Auth.TokenSecretRef: %w"
  27. errInvalidUserPassSecretRef = "invalid Auth.UserPass.SecretRef: %w"
  28. )
  29. // ValidateStore validates the OpenBao provider configuration in the SecretStore.
  30. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  31. if store == nil {
  32. return nil, errors.New(errInvalidStore)
  33. }
  34. spc := store.GetSpec()
  35. if spc == nil {
  36. return nil, errors.New(errInvalidStoreSpec)
  37. }
  38. if spc.Provider == nil {
  39. return nil, errors.New(errInvalidStoreProv)
  40. }
  41. baoProvider := spc.Provider.OpenBao
  42. if baoProvider == nil {
  43. return nil, errors.New(errInvalidOpenBaoProv)
  44. }
  45. if baoProvider.Auth != nil {
  46. auth := baoProvider.Auth
  47. if auth.TokenSecretRef != nil {
  48. if err := esutils.ValidateReferentSecretSelector(store, *auth.TokenSecretRef); err != nil {
  49. return nil, fmt.Errorf(errInvalidTokenRef, err)
  50. }
  51. }
  52. if auth.UserPass != nil {
  53. if err := esutils.ValidateReferentSecretSelector(store, auth.UserPass.SecretRef); err != nil {
  54. return nil, fmt.Errorf(errInvalidUserPassSecretRef, err)
  55. }
  56. }
  57. }
  58. return nil, nil
  59. }