provider.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "crypto/tls"
  17. "crypto/x509"
  18. "errors"
  19. "github.com/DelineaXPM/tss-sdk-go/v3/server"
  20. kubeClient "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. "github.com/external-secrets/external-secrets/runtime/esutils"
  24. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  25. )
  26. var (
  27. errEmptyUserName = errors.New("username must not be empty")
  28. errEmptyPassword = errors.New("password must be set")
  29. errEmptyServerURL = errors.New("serverURL must be set")
  30. errSecretRefAndValueConflict = errors.New("cannot specify both secret reference and value")
  31. errSecretRefAndValueMissing = errors.New("must specify either secret reference or direct value")
  32. errMissingStore = errors.New("missing store specification")
  33. errInvalidSpec = errors.New("invalid specification for secret server provider")
  34. errClusterStoreRequiresNamespace = errors.New("when using a ClusterSecretStore, namespaces must be explicitly set")
  35. errMissingSecretName = errors.New("must specify a secret name")
  36. errMissingSecretKey = errors.New("must specify a secret key")
  37. )
  38. // Provider struct that implements the ESO esv1.Provider.
  39. type Provider struct{}
  40. var _ esv1.Provider = &Provider{}
  41. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  42. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  43. return esv1.SecretStoreReadOnly
  44. }
  45. // NewClient creates a new secrets client based on provided store.
  46. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kubeClient.Client, namespace string) (esv1.SecretsClient, error) {
  47. cfg, err := getConfig(store)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if store.GetKind() == esv1.ClusterSecretStoreKind && doesConfigDependOnNamespace(cfg) {
  52. // we are not attached to a specific namespace, but some config values are dependent on it
  53. return nil, errClusterStoreRequiresNamespace
  54. }
  55. username, err := loadConfigSecret(ctx, store.GetKind(), cfg.Username, kube, namespace)
  56. if err != nil {
  57. return nil, err
  58. }
  59. password, err := loadConfigSecret(ctx, store.GetKind(), cfg.Password, kube, namespace)
  60. if err != nil {
  61. return nil, err
  62. }
  63. ssConfig := server.Configuration{
  64. Credentials: server.UserCredential{
  65. Username: username,
  66. Password: password,
  67. Domain: cfg.Domain,
  68. },
  69. ServerURL: cfg.ServerURL,
  70. }
  71. if len(cfg.CABundle) > 0 || cfg.CAProvider != nil {
  72. cert, err := esutils.FetchCACertFromSource(ctx, esutils.CreateCertOpts{
  73. StoreKind: store.GetKind(),
  74. Client: kube,
  75. Namespace: namespace,
  76. CABundle: cfg.CABundle,
  77. CAProvider: cfg.CAProvider,
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. caCertPool := x509.NewCertPool()
  83. if !caCertPool.AppendCertsFromPEM(cert) {
  84. return nil, errors.New("failed to append caBundle")
  85. }
  86. ssConfig.TLSClientConfig = &tls.Config{
  87. RootCAs: caCertPool,
  88. MinVersion: tls.VersionTLS12,
  89. }
  90. }
  91. secretServer, err := server.New(ssConfig)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return &client{
  96. api: secretServer,
  97. }, nil
  98. }
  99. func loadConfigSecret(
  100. ctx context.Context,
  101. storeKind string,
  102. ref *esv1.SecretServerProviderRef,
  103. kube kubeClient.Client,
  104. namespace string) (string, error) {
  105. if ref.SecretRef == nil {
  106. return ref.Value, nil
  107. }
  108. if err := validateSecretRef(ref); err != nil {
  109. return "", err
  110. }
  111. return resolvers.SecretKeyRef(ctx, kube, storeKind, namespace, ref.SecretRef)
  112. }
  113. func validateStoreSecretRef(store esv1.GenericStore, ref *esv1.SecretServerProviderRef) error {
  114. if ref.SecretRef != nil {
  115. if err := esutils.ValidateReferentSecretSelector(store, *ref.SecretRef); err != nil {
  116. return err
  117. }
  118. }
  119. return validateSecretRef(ref)
  120. }
  121. func validateSecretRef(ref *esv1.SecretServerProviderRef) error {
  122. if ref.SecretRef != nil {
  123. if ref.Value != "" {
  124. return errSecretRefAndValueConflict
  125. }
  126. if ref.SecretRef.Name == "" {
  127. return errMissingSecretName
  128. }
  129. if ref.SecretRef.Key == "" {
  130. return errMissingSecretKey
  131. }
  132. } else if ref.Value == "" {
  133. return errSecretRefAndValueMissing
  134. }
  135. return nil
  136. }
  137. func doesConfigDependOnNamespace(cfg *esv1.SecretServerProvider) bool {
  138. if cfg.Username.SecretRef != nil && cfg.Username.SecretRef.Namespace == nil {
  139. return true
  140. }
  141. if cfg.Password.SecretRef != nil && cfg.Password.SecretRef.Namespace == nil {
  142. return true
  143. }
  144. return false
  145. }
  146. func getConfig(store esv1.GenericStore) (*esv1.SecretServerProvider, error) {
  147. if store == nil {
  148. return nil, errMissingStore
  149. }
  150. storeSpec := store.GetSpec()
  151. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.SecretServer == nil {
  152. return nil, errInvalidSpec
  153. }
  154. cfg := storeSpec.Provider.SecretServer
  155. if cfg.Username == nil {
  156. return nil, errEmptyUserName
  157. }
  158. if cfg.Password == nil {
  159. return nil, errEmptyPassword
  160. }
  161. if cfg.ServerURL == "" {
  162. return nil, errEmptyServerURL
  163. }
  164. err := validateStoreSecretRef(store, cfg.Username)
  165. if err != nil {
  166. return nil, err
  167. }
  168. err = validateStoreSecretRef(store, cfg.Password)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return cfg, nil
  173. }
  174. // ValidateStore validates the store's configuration and returns warnings or error.
  175. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  176. _, err := getConfig(store)
  177. return nil, err
  178. }
  179. // NewProvider creates a new Provider instance.
  180. func NewProvider() esv1.Provider {
  181. return &Provider{}
  182. }
  183. // ProviderSpec returns the provider specification for registration.
  184. func ProviderSpec() *esv1.SecretStoreProvider {
  185. return &esv1.SecretStoreProvider{
  186. SecretServer: &esv1.SecretServerProvider{},
  187. }
  188. }
  189. // MaintenanceStatus returns the maintenance status of the provider.
  190. func MaintenanceStatus() esv1.MaintenanceStatus {
  191. return esv1.MaintenanceStatusMaintained
  192. }