provider.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package delinea
  13. import (
  14. "context"
  15. "errors"
  16. "github.com/DelineaXPM/dsv-sdk-go/v2/vault"
  17. kubeClient "sigs.k8s.io/controller-runtime/pkg/client"
  18. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  19. "github.com/external-secrets/external-secrets/pkg/utils"
  20. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  21. )
  22. var (
  23. errEmptyTenant = errors.New("tenant must not be empty")
  24. errEmptyClientID = errors.New("clientID must be set")
  25. errEmptyClientSecret = errors.New("clientSecret must be set")
  26. errSecretRefAndValueConflict = errors.New("cannot specify both secret reference and value")
  27. errSecretRefAndValueMissing = errors.New("must specify either secret reference or direct value")
  28. errMissingStore = errors.New("missing store specification")
  29. errInvalidSpec = errors.New("invalid specification for delinea provider")
  30. errMissingSecretName = errors.New("must specify a secret name")
  31. errMissingSecretKey = errors.New("must specify a secret key")
  32. errClusterStoreRequiresNamespace = errors.New("when using a ClusterSecretStore, namespaces must be explicitly set")
  33. )
  34. type Provider struct{}
  35. var _ esv1beta1.Provider = &Provider{}
  36. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  37. func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
  38. return esv1beta1.SecretStoreReadOnly
  39. }
  40. func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kubeClient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  41. cfg, err := getConfig(store)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if store.GetKind() == esv1beta1.ClusterSecretStoreKind && doesConfigDependOnNamespace(cfg) {
  46. // we are not attached to a specific namespace, but some config values are dependent on it
  47. return nil, errClusterStoreRequiresNamespace
  48. }
  49. clientID, err := loadConfigSecret(ctx, store.GetKind(), cfg.ClientID, kube, namespace)
  50. if err != nil {
  51. return nil, err
  52. }
  53. clientSecret, err := loadConfigSecret(ctx, store.GetKind(), cfg.ClientSecret, kube, namespace)
  54. if err != nil {
  55. return nil, err
  56. }
  57. dsvClient, err := vault.New(vault.Configuration{
  58. Credentials: vault.ClientCredential{
  59. ClientID: clientID,
  60. ClientSecret: clientSecret,
  61. },
  62. Tenant: cfg.Tenant,
  63. TLD: cfg.TLD,
  64. URLTemplate: cfg.URLTemplate,
  65. })
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &client{
  70. api: dsvClient,
  71. }, nil
  72. }
  73. func loadConfigSecret(
  74. ctx context.Context,
  75. storeKind string,
  76. ref *esv1beta1.DelineaProviderSecretRef,
  77. kube kubeClient.Client,
  78. namespace string) (string, error) {
  79. if ref.SecretRef == nil {
  80. return ref.Value, nil
  81. }
  82. if err := validateSecretRef(ref); err != nil {
  83. return "", err
  84. }
  85. return resolvers.SecretKeyRef(ctx, kube, storeKind, namespace, ref.SecretRef)
  86. }
  87. func validateStoreSecretRef(store esv1beta1.GenericStore, ref *esv1beta1.DelineaProviderSecretRef) error {
  88. if ref.SecretRef != nil {
  89. if err := utils.ValidateReferentSecretSelector(store, *ref.SecretRef); err != nil {
  90. return err
  91. }
  92. }
  93. return validateSecretRef(ref)
  94. }
  95. func validateSecretRef(ref *esv1beta1.DelineaProviderSecretRef) error {
  96. if ref.SecretRef != nil {
  97. if ref.Value != "" {
  98. return errSecretRefAndValueConflict
  99. }
  100. if ref.SecretRef.Name == "" {
  101. return errMissingSecretName
  102. }
  103. if ref.SecretRef.Key == "" {
  104. return errMissingSecretKey
  105. }
  106. } else if ref.Value == "" {
  107. return errSecretRefAndValueMissing
  108. }
  109. return nil
  110. }
  111. func doesConfigDependOnNamespace(cfg *esv1beta1.DelineaProvider) bool {
  112. if cfg.ClientID.SecretRef != nil && cfg.ClientID.SecretRef.Namespace == nil {
  113. return true
  114. }
  115. if cfg.ClientSecret.SecretRef != nil && cfg.ClientSecret.SecretRef.Namespace == nil {
  116. return true
  117. }
  118. return false
  119. }
  120. func getConfig(store esv1beta1.GenericStore) (*esv1beta1.DelineaProvider, error) {
  121. if store == nil {
  122. return nil, errMissingStore
  123. }
  124. storeSpec := store.GetSpec()
  125. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Delinea == nil {
  126. return nil, errInvalidSpec
  127. }
  128. cfg := storeSpec.Provider.Delinea
  129. if cfg.Tenant == "" {
  130. return nil, errEmptyTenant
  131. }
  132. if cfg.ClientID == nil {
  133. return nil, errEmptyClientID
  134. }
  135. if cfg.ClientSecret == nil {
  136. return nil, errEmptyClientSecret
  137. }
  138. err := validateStoreSecretRef(store, cfg.ClientID)
  139. if err != nil {
  140. return nil, err
  141. }
  142. err = validateStoreSecretRef(store, cfg.ClientSecret)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return cfg, nil
  147. }
  148. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) error {
  149. _, err := getConfig(store)
  150. return err
  151. }
  152. func init() {
  153. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  154. Delinea: &esv1beta1.DelineaProvider{},
  155. })
  156. }