provider.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 kubernetes implements a provider for Kubernetes secrets, allowing
  14. // External Secrets to read from and write to Kubernetes Secrets.
  15. package kubernetes
  16. import (
  17. "context"
  18. "errors"
  19. "fmt"
  20. authv1 "k8s.io/api/authorization/v1"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/client-go/kubernetes"
  24. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  25. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  26. ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
  27. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  28. )
  29. // https://github.com/external-secrets/external-secrets/issues/644
  30. var _ esv1.SecretsClient = &Client{}
  31. var _ esv1.Provider = &Provider{}
  32. // KClient defines the interface for interacting with Kubernetes Secrets.
  33. type KClient interface {
  34. Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Secret, error)
  35. List(ctx context.Context, opts metav1.ListOptions) (*v1.SecretList, error)
  36. Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
  37. Create(ctx context.Context, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
  38. Update(ctx context.Context, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
  39. }
  40. // RClient defines the interface for performing self subject rules reviews.
  41. type RClient interface {
  42. Create(ctx context.Context, selfSubjectRulesReview *authv1.SelfSubjectRulesReview, opts metav1.CreateOptions) (*authv1.SelfSubjectRulesReview, error)
  43. }
  44. // AClient defines the interface for performing self subject access reviews.
  45. type AClient interface {
  46. Create(ctx context.Context, selfSubjectAccessReview *authv1.SelfSubjectAccessReview, opts metav1.CreateOptions) (*authv1.SelfSubjectAccessReview, error)
  47. }
  48. // Provider implements the SecretStore Provider interface for Kubernetes.
  49. type Provider struct{}
  50. // Client implements Secret Client interface
  51. // for Kubernetes.
  52. type Client struct {
  53. // ctrlClient is a controller-runtime client
  54. // with RBAC scope of the controller (privileged!)
  55. ctrlClient kclient.Client
  56. // ctrlClientset is a client-go CoreV1() client
  57. // with RBAC scope of the controller (privileged!)
  58. ctrlClientset typedcorev1.CoreV1Interface
  59. // userSecretClient is a client-go CoreV1().Secrets() client
  60. // with user-defined scope.
  61. userSecretClient KClient
  62. // userReviewClient is a SelfSubjectRulesReview client with
  63. // user-defined scope.
  64. userReviewClient RClient
  65. // userAccessReviewClient is a SelfSubjectAccessReview client with
  66. // user-defined scope.
  67. userAccessReviewClient AClient
  68. // store is the Kubernetes Provider spec
  69. // which contains the configuration for this provider.
  70. store *esv1.KubernetesProvider
  71. storeKind string
  72. // namespace is the namespace of the
  73. // ExternalSecret referencing this provider.
  74. namespace string
  75. }
  76. // Capabilities returns the provider's supported capabilities (ReadWrite).
  77. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  78. return esv1.SecretStoreReadWrite
  79. }
  80. // NewClient constructs a Kubernetes Provider.
  81. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  82. restCfg, err := ctrlcfg.GetConfig()
  83. if err != nil {
  84. return nil, err
  85. }
  86. clientset, err := kubernetes.NewForConfig(restCfg)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return p.newClient(ctx, store, kube, clientset, namespace)
  91. }
  92. func (p *Provider) newClient(ctx context.Context, store esv1.GenericStore, ctrlClient kclient.Client, ctrlClientset kubernetes.Interface, namespace string) (esv1.SecretsClient, error) {
  93. storeSpec := store.GetSpec()
  94. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Kubernetes == nil {
  95. return nil, errors.New("no store type or wrong store type")
  96. }
  97. storeSpecKubernetes := storeSpec.Provider.Kubernetes
  98. client := &Client{
  99. ctrlClientset: ctrlClientset.CoreV1(),
  100. ctrlClient: ctrlClient,
  101. store: storeSpecKubernetes,
  102. namespace: namespace,
  103. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  104. }
  105. // allow SecretStore controller validation to pass
  106. // when using referent namespace.
  107. if client.storeKind == esv1.ClusterSecretStoreKind && client.namespace == "" && isReferentSpec(storeSpecKubernetes) {
  108. return client, nil
  109. }
  110. cfg, err := client.getAuth(ctx)
  111. if err != nil {
  112. return nil, fmt.Errorf("failed to prepare auth: %w", err)
  113. }
  114. userClientset, err := kubernetes.NewForConfig(cfg)
  115. if err != nil {
  116. return nil, fmt.Errorf("error configuring clientset: %w", err)
  117. }
  118. client.userSecretClient = userClientset.CoreV1().Secrets(client.store.RemoteNamespace)
  119. client.userReviewClient = userClientset.AuthorizationV1().SelfSubjectRulesReviews()
  120. client.userAccessReviewClient = userClientset.AuthorizationV1().SelfSubjectAccessReviews()
  121. return client, nil
  122. }
  123. func isReferentSpec(prov *esv1.KubernetesProvider) bool {
  124. if prov.Auth == nil {
  125. return false
  126. }
  127. if prov.Auth.Cert != nil {
  128. if prov.Auth.Cert.ClientCert.Namespace == nil {
  129. return true
  130. }
  131. if prov.Auth.Cert.ClientKey.Namespace == nil {
  132. return true
  133. }
  134. }
  135. if prov.Auth.ServiceAccount != nil {
  136. if prov.Auth.ServiceAccount.Namespace == nil {
  137. return true
  138. }
  139. }
  140. if prov.Auth.Token != nil {
  141. if prov.Auth.Token.BearerToken.Namespace == nil {
  142. return true
  143. }
  144. }
  145. return false
  146. }
  147. // Close cleans up any resources used by the Kubernetes provider.
  148. func (p *Provider) Close(_ context.Context) error {
  149. return nil
  150. }
  151. // NewProvider creates a new Provider instance.
  152. func NewProvider() esv1.Provider {
  153. return &Provider{}
  154. }
  155. // ProviderSpec returns the provider specification for registration.
  156. func ProviderSpec() *esv1.SecretStoreProvider {
  157. return &esv1.SecretStoreProvider{
  158. Kubernetes: &esv1.KubernetesProvider{},
  159. }
  160. }
  161. // MaintenanceStatus returns the maintenance status of the provider.
  162. func MaintenanceStatus() esv1.MaintenanceStatus {
  163. return esv1.MaintenanceStatusMaintained
  164. }