provider.go 5.3 KB

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