provider.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "github.com/external-secrets/external-secrets/pkg/esutils"
  22. oClient "github.com/external-secrets/external-secrets/pkg/provider/onboardbase/client"
  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. // Capabilities returns the provider's supported capabilities.
  40. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  41. return esv1.SecretStoreReadOnly
  42. }
  43. // NewClient creates a new Onboardbase client with the provided store configuration.
  44. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  45. storeSpec := store.GetSpec()
  46. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Onboardbase == nil {
  47. return nil, errors.New(errOnboardbaseStore)
  48. }
  49. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  50. client := &Client{
  51. kube: kube,
  52. store: onboardbaseStoreSpec,
  53. namespace: namespace,
  54. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  55. }
  56. if err := client.setAuth(ctx); err != nil {
  57. return nil, err
  58. }
  59. onboardbaseClient, err := oClient.NewOnboardbaseClient(client.onboardbaseAPIKey, client.onboardbasePasscode)
  60. if err != nil {
  61. return nil, fmt.Errorf(errNewClient, err)
  62. }
  63. client.onboardbase = onboardbaseClient
  64. client.project = client.store.Project
  65. client.environment = client.store.Environment
  66. return client, nil
  67. }
  68. // ValidateStore validates the Onboardbase SecretStore configuration.
  69. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  70. storeSpec := store.GetSpec()
  71. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  72. onboardbaseAPIKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbaseAPIKeyRef
  73. if err := esutils.ValidateSecretSelector(store, onboardbaseAPIKeySecretRef); err != nil {
  74. return nil, fmt.Errorf(errInvalidStore, err)
  75. }
  76. if onboardbaseAPIKeySecretRef.Name == "" {
  77. return nil, fmt.Errorf(errInvalidStore, "onboardbaseAPIKey.name cannot be empty")
  78. }
  79. onboardbasePasscodeKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbasePasscodeRef
  80. if err := esutils.ValidateSecretSelector(store, onboardbasePasscodeKeySecretRef); err != nil {
  81. return nil, fmt.Errorf(errInvalidStore, err)
  82. }
  83. if onboardbasePasscodeKeySecretRef.Name == "" {
  84. return nil, fmt.Errorf(errInvalidStore, "onboardbasePasscode.name cannot be empty")
  85. }
  86. return nil, nil
  87. }