provider.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 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/providers/v1/onboardbase/client"
  22. "github.com/external-secrets/external-secrets/runtime/esutils"
  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. // Capabilities returns the provider's supported capabilities.
  35. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  36. return esv1.SecretStoreReadOnly
  37. }
  38. // NewClient creates a new Onboardbase client with the provided store configuration.
  39. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  40. storeSpec := store.GetSpec()
  41. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Onboardbase == nil {
  42. return nil, errors.New(errOnboardbaseStore)
  43. }
  44. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  45. client := &Client{
  46. kube: kube,
  47. store: onboardbaseStoreSpec,
  48. namespace: namespace,
  49. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  50. }
  51. if err := client.setAuth(ctx); err != nil {
  52. return nil, err
  53. }
  54. onboardbaseClient, err := oClient.NewOnboardbaseClient(client.onboardbaseAPIKey, client.onboardbasePasscode)
  55. if err != nil {
  56. return nil, fmt.Errorf(errNewClient, err)
  57. }
  58. client.onboardbase = onboardbaseClient
  59. client.project = client.store.Project
  60. client.environment = client.store.Environment
  61. return client, nil
  62. }
  63. // ValidateStore validates the Onboardbase SecretStore configuration.
  64. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  65. storeSpec := store.GetSpec()
  66. onboardbaseStoreSpec := storeSpec.Provider.Onboardbase
  67. onboardbaseAPIKeySecretRef := onboardbaseStoreSpec.Auth.OnboardbaseAPIKeyRef
  68. if err := esutils.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 := esutils.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. }
  83. // NewProvider creates a new Provider instance.
  84. func NewProvider() esv1.Provider {
  85. return &Provider{}
  86. }
  87. // ProviderSpec returns the provider specification for registration.
  88. func ProviderSpec() *esv1.SecretStoreProvider {
  89. return &esv1.SecretStoreProvider{
  90. Onboardbase: &esv1.OnboardbaseProvider{},
  91. }
  92. }
  93. // MaintenanceStatus returns the maintenance status of the provider.
  94. func MaintenanceStatus() esv1.MaintenanceStatus {
  95. return esv1.MaintenanceStatusMaintained
  96. }