provider.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or impliec.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package onboardbase
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  18. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  19. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. oClient "github.com/external-secrets/external-secrets/pkg/provider/onboardbase/client"
  21. "github.com/external-secrets/external-secrets/pkg/utils"
  22. )
  23. const (
  24. errNewClient = "unable to create OnboardbaseClient : %s"
  25. errInvalidStore = "invalid store: %s"
  26. errOnboardbaseStore = "missing or invalid Onboardbase SecretStore"
  27. )
  28. // Provider is a Onboardbase secrets provider implementing NewClient and ValidateStore for the esv1beta1.Provider interface.
  29. type Provider struct{}
  30. // https://github.com/external-secrets/external-secrets/issues/644
  31. var _ esv1beta1.SecretsClient = &Client{}
  32. var _ esv1beta1.Provider = &Provider{}
  33. func init() {
  34. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  35. Onboardbase: &esv1beta1.OnboardbaseProvider{},
  36. })
  37. }
  38. func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
  39. return esv1beta1.SecretStoreReadOnly
  40. }
  41. func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  42. storeSpec := store.GetSpec()
  43. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Onboardbase == nil {
  44. return nil, errors.New(errOnboardbaseStore)
  45. }
  46. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  47. client := &Client{
  48. kube: kube,
  49. store: onboardbaseStoreSpec,
  50. namespace: namespace,
  51. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  52. }
  53. if err := client.setAuth(ctx); err != nil {
  54. return nil, err
  55. }
  56. onboardbaseClient, err := oClient.NewOnboardbaseClient(client.onboardbaseAPIKey, client.onboardbasePasscode)
  57. if err != nil {
  58. return nil, fmt.Errorf(errNewClient, err)
  59. }
  60. client.onboardbase = onboardbaseClient
  61. client.project = client.store.Project
  62. client.environment = client.store.Environment
  63. return client, nil
  64. }
  65. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
  66. storeSpec := store.GetSpec()
  67. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  68. onboardbaseAPIKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbaseAPIKeyRef
  69. if err := utils.ValidateSecretSelector(store, onboardbaseAPIKeySecretRef); err != nil {
  70. return nil, fmt.Errorf(errInvalidStore, err)
  71. }
  72. if onboardbaseAPIKeySecretRef.Name == "" {
  73. return nil, fmt.Errorf(errInvalidStore, "onboardbaseAPIKey.name cannot be empty")
  74. }
  75. onboardbasePasscodeKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbasePasscodeRef
  76. if err := utils.ValidateSecretSelector(store, onboardbasePasscodeKeySecretRef); err != nil {
  77. return nil, fmt.Errorf(errInvalidStore, err)
  78. }
  79. if onboardbasePasscodeKeySecretRef.Name == "" {
  80. return nil, fmt.Errorf(errInvalidStore, "onboardbasePasscode.name cannot be empty")
  81. }
  82. return nil, nil
  83. }