provider.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 secretserver
  14. import (
  15. "context"
  16. "errors"
  17. "github.com/DelineaXPM/tss-sdk-go/v3/server"
  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. errEmptyUserName = errors.New("username must not be empty")
  26. errEmptyPassword = errors.New("password must be set")
  27. errEmptyServerURL = errors.New("serverURL 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 secret server provider")
  32. errClusterStoreRequiresNamespace = errors.New("when using a ClusterSecretStore, namespaces must be explicitly set")
  33. errMissingSecretName = errors.New("must specify a secret name")
  34. errMissingSecretKey = errors.New("must specify a secret key")
  35. )
  36. // Provider struct that implements the ESO esv1.Provider.
  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 secrets client based on provided store.
  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. username, err := loadConfigSecret(ctx, store.GetKind(), cfg.Username, kube, namespace)
  54. if err != nil {
  55. return nil, err
  56. }
  57. password, err := loadConfigSecret(ctx, store.GetKind(), cfg.Password, kube, namespace)
  58. if err != nil {
  59. return nil, err
  60. }
  61. secretServer, err := server.New(server.Configuration{
  62. Credentials: server.UserCredential{
  63. Username: username,
  64. Password: password,
  65. Domain: cfg.Domain,
  66. },
  67. ServerURL: cfg.ServerURL,
  68. })
  69. if err != nil {
  70. return nil, err
  71. }
  72. return &client{
  73. api: secretServer,
  74. }, nil
  75. }
  76. func loadConfigSecret(
  77. ctx context.Context,
  78. storeKind string,
  79. ref *esv1.SecretServerProviderRef,
  80. kube kubeClient.Client,
  81. namespace string) (string, error) {
  82. if ref.SecretRef == nil {
  83. return ref.Value, nil
  84. }
  85. if err := validateSecretRef(ref); err != nil {
  86. return "", err
  87. }
  88. return resolvers.SecretKeyRef(ctx, kube, storeKind, namespace, ref.SecretRef)
  89. }
  90. func validateStoreSecretRef(store esv1.GenericStore, ref *esv1.SecretServerProviderRef) error {
  91. if ref.SecretRef != nil {
  92. if err := esutils.ValidateReferentSecretSelector(store, *ref.SecretRef); err != nil {
  93. return err
  94. }
  95. }
  96. return validateSecretRef(ref)
  97. }
  98. func validateSecretRef(ref *esv1.SecretServerProviderRef) error {
  99. if ref.SecretRef != nil {
  100. if ref.Value != "" {
  101. return errSecretRefAndValueConflict
  102. }
  103. if ref.SecretRef.Name == "" {
  104. return errMissingSecretName
  105. }
  106. if ref.SecretRef.Key == "" {
  107. return errMissingSecretKey
  108. }
  109. } else if ref.Value == "" {
  110. return errSecretRefAndValueMissing
  111. }
  112. return nil
  113. }
  114. func doesConfigDependOnNamespace(cfg *esv1.SecretServerProvider) bool {
  115. if cfg.Username.SecretRef != nil && cfg.Username.SecretRef.Namespace == nil {
  116. return true
  117. }
  118. if cfg.Password.SecretRef != nil && cfg.Password.SecretRef.Namespace == nil {
  119. return true
  120. }
  121. return false
  122. }
  123. func getConfig(store esv1.GenericStore) (*esv1.SecretServerProvider, error) {
  124. if store == nil {
  125. return nil, errMissingStore
  126. }
  127. storeSpec := store.GetSpec()
  128. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.SecretServer == nil {
  129. return nil, errInvalidSpec
  130. }
  131. cfg := storeSpec.Provider.SecretServer
  132. if cfg.Username == nil {
  133. return nil, errEmptyUserName
  134. }
  135. if cfg.Password == nil {
  136. return nil, errEmptyPassword
  137. }
  138. if cfg.ServerURL == "" {
  139. return nil, errEmptyServerURL
  140. }
  141. err := validateStoreSecretRef(store, cfg.Username)
  142. if err != nil {
  143. return nil, err
  144. }
  145. err = validateStoreSecretRef(store, cfg.Password)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return cfg, nil
  150. }
  151. // ValidateStore validates the store's configuration and returns warnings or error.
  152. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  153. _, err := getConfig(store)
  154. return nil, err
  155. }
  156. func init() {
  157. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  158. SecretServer: &esv1.SecretServerProvider{},
  159. }, esv1.MaintenanceStatusMaintained)
  160. }