provider.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 onboardbase
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. oClient "github.com/external-secrets/external-secrets/pkg/provider/onboardbase/client"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. )
  24. const (
  25. errNewClient = "unable to create OnboardbaseClient : %s"
  26. errInvalidStore = "invalid store: %s"
  27. errOnboardbaseStore = "missing or invalid Onboardbase SecretStore"
  28. )
  29. // Provider is a Onboardbase secrets provider implementing NewClient and ValidateStore for the esv1.Provider interface.
  30. type Provider struct{}
  31. // https://github.com/external-secrets/external-secrets/issues/644
  32. var _ esv1.SecretsClient = &Client{}
  33. var _ esv1.Provider = &Provider{}
  34. func init() {
  35. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  36. Onboardbase: &esv1.OnboardbaseProvider{},
  37. }, esv1.MaintenanceStatusMaintained)
  38. }
  39. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  40. return esv1.SecretStoreReadOnly
  41. }
  42. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  43. storeSpec := store.GetSpec()
  44. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Onboardbase == nil {
  45. return nil, errors.New(errOnboardbaseStore)
  46. }
  47. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  48. client := &Client{
  49. kube: kube,
  50. store: onboardbaseStoreSpec,
  51. namespace: namespace,
  52. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  53. }
  54. if err := client.setAuth(ctx); err != nil {
  55. return nil, err
  56. }
  57. onboardbaseClient, err := oClient.NewOnboardbaseClient(client.onboardbaseAPIKey, client.onboardbasePasscode)
  58. if err != nil {
  59. return nil, fmt.Errorf(errNewClient, err)
  60. }
  61. client.onboardbase = onboardbaseClient
  62. client.project = client.store.Project
  63. client.environment = client.store.Environment
  64. return client, nil
  65. }
  66. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  67. storeSpec := store.GetSpec()
  68. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  69. onboardbaseAPIKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbaseAPIKeyRef
  70. if err := utils.ValidateSecretSelector(store, onboardbaseAPIKeySecretRef); err != nil {
  71. return nil, fmt.Errorf(errInvalidStore, err)
  72. }
  73. if onboardbaseAPIKeySecretRef.Name == "" {
  74. return nil, fmt.Errorf(errInvalidStore, "onboardbaseAPIKey.name cannot be empty")
  75. }
  76. onboardbasePasscodeKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbasePasscodeRef
  77. if err := utils.ValidateSecretSelector(store, onboardbasePasscodeKeySecretRef); err != nil {
  78. return nil, fmt.Errorf(errInvalidStore, err)
  79. }
  80. if onboardbasePasscodeKeySecretRef.Name == "" {
  81. return nil, fmt.Errorf(errInvalidStore, "onboardbasePasscode.name cannot be empty")
  82. }
  83. return nil, nil
  84. }