provider.go 3.4 KB

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