provider.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/esutils"
  22. "github.com/external-secrets/external-secrets/pkg/esutils/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. // Provider implements the External Secrets provider for Delinea Secret Server.
  37. type Provider struct{}
  38. var _ esv1.Provider = &Provider{}
  39. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  40. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  41. return esv1.SecretStoreReadOnly
  42. }
  43. // NewClient creates a new Delinea Secret Server client.
  44. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kubeClient.Client, namespace string) (esv1.SecretsClient, error) {
  45. cfg, err := getConfig(store)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if store.GetKind() == esv1.ClusterSecretStoreKind && doesConfigDependOnNamespace(cfg) {
  50. // we are not attached to a specific namespace, but some config values are dependent on it
  51. return nil, errClusterStoreRequiresNamespace
  52. }
  53. clientID, err := loadConfigSecret(ctx, store.GetKind(), cfg.ClientID, kube, namespace)
  54. if err != nil {
  55. return nil, err
  56. }
  57. clientSecret, err := loadConfigSecret(ctx, store.GetKind(), cfg.ClientSecret, kube, namespace)
  58. if err != nil {
  59. return nil, err
  60. }
  61. dsvClient, err := vault.New(vault.Configuration{
  62. Credentials: vault.ClientCredential{
  63. ClientID: clientID,
  64. ClientSecret: clientSecret,
  65. },
  66. Tenant: cfg.Tenant,
  67. TLD: cfg.TLD,
  68. URLTemplate: cfg.URLTemplate,
  69. })
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &client{
  74. api: dsvClient,
  75. }, nil
  76. }
  77. func loadConfigSecret(
  78. ctx context.Context,
  79. storeKind string,
  80. ref *esv1.DelineaProviderSecretRef,
  81. kube kubeClient.Client,
  82. namespace string) (string, error) {
  83. if ref.SecretRef == nil {
  84. return ref.Value, nil
  85. }
  86. if err := validateSecretRef(ref); err != nil {
  87. return "", err
  88. }
  89. return resolvers.SecretKeyRef(ctx, kube, storeKind, namespace, ref.SecretRef)
  90. }
  91. func validateStoreSecretRef(store esv1.GenericStore, ref *esv1.DelineaProviderSecretRef) error {
  92. if ref.SecretRef != nil {
  93. if err := esutils.ValidateReferentSecretSelector(store, *ref.SecretRef); err != nil {
  94. return err
  95. }
  96. }
  97. return validateSecretRef(ref)
  98. }
  99. func validateSecretRef(ref *esv1.DelineaProviderSecretRef) error {
  100. if ref.SecretRef != nil {
  101. if ref.Value != "" {
  102. return errSecretRefAndValueConflict
  103. }
  104. if ref.SecretRef.Name == "" {
  105. return errMissingSecretName
  106. }
  107. if ref.SecretRef.Key == "" {
  108. return errMissingSecretKey
  109. }
  110. } else if ref.Value == "" {
  111. return errSecretRefAndValueMissing
  112. }
  113. return nil
  114. }
  115. func doesConfigDependOnNamespace(cfg *esv1.DelineaProvider) bool {
  116. if cfg.ClientID.SecretRef != nil && cfg.ClientID.SecretRef.Namespace == nil {
  117. return true
  118. }
  119. if cfg.ClientSecret.SecretRef != nil && cfg.ClientSecret.SecretRef.Namespace == nil {
  120. return true
  121. }
  122. return false
  123. }
  124. func getConfig(store esv1.GenericStore) (*esv1.DelineaProvider, error) {
  125. if store == nil {
  126. return nil, errMissingStore
  127. }
  128. storeSpec := store.GetSpec()
  129. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Delinea == nil {
  130. return nil, errInvalidSpec
  131. }
  132. cfg := storeSpec.Provider.Delinea
  133. if cfg.Tenant == "" {
  134. return nil, errEmptyTenant
  135. }
  136. if cfg.ClientID == nil {
  137. return nil, errEmptyClientID
  138. }
  139. if cfg.ClientSecret == nil {
  140. return nil, errEmptyClientSecret
  141. }
  142. err := validateStoreSecretRef(store, cfg.ClientID)
  143. if err != nil {
  144. return nil, err
  145. }
  146. err = validateStoreSecretRef(store, cfg.ClientSecret)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return cfg, nil
  151. }
  152. // ValidateStore validates the Delinea SecretStore configuration.
  153. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  154. _, err := getConfig(store)
  155. return nil, err
  156. }
  157. func init() {
  158. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  159. Delinea: &esv1.DelineaProvider{},
  160. }, esv1.MaintenanceStatusMaintained)
  161. }