provider.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. func init() {
  77. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  78. Kubernetes: &esv1.KubernetesProvider{},
  79. }, esv1.MaintenanceStatusMaintained)
  80. }
  81. // Capabilities returns the provider's supported capabilities (ReadWrite).
  82. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  83. return esv1.SecretStoreReadWrite
  84. }
  85. // NewClient constructs a Kubernetes Provider.
  86. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  87. restCfg, err := ctrlcfg.GetConfig()
  88. if err != nil {
  89. return nil, err
  90. }
  91. clientset, err := kubernetes.NewForConfig(restCfg)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return p.newClient(ctx, store, kube, clientset, namespace)
  96. }
  97. func (p *Provider) newClient(ctx context.Context, store esv1.GenericStore, ctrlClient kclient.Client, ctrlClientset kubernetes.Interface, namespace string) (esv1.SecretsClient, error) {
  98. storeSpec := store.GetSpec()
  99. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Kubernetes == nil {
  100. return nil, errors.New("no store type or wrong store type")
  101. }
  102. storeSpecKubernetes := storeSpec.Provider.Kubernetes
  103. client := &Client{
  104. ctrlClientset: ctrlClientset.CoreV1(),
  105. ctrlClient: ctrlClient,
  106. store: storeSpecKubernetes,
  107. namespace: namespace,
  108. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  109. }
  110. // allow SecretStore controller validation to pass
  111. // when using referent namespace.
  112. if client.storeKind == esv1.ClusterSecretStoreKind && client.namespace == "" && isReferentSpec(storeSpecKubernetes) {
  113. return client, nil
  114. }
  115. cfg, err := client.getAuth(ctx)
  116. if err != nil {
  117. return nil, fmt.Errorf("failed to prepare auth: %w", err)
  118. }
  119. userClientset, err := kubernetes.NewForConfig(cfg)
  120. if err != nil {
  121. return nil, fmt.Errorf("error configuring clientset: %w", err)
  122. }
  123. client.userSecretClient = userClientset.CoreV1().Secrets(client.store.RemoteNamespace)
  124. client.userReviewClient = userClientset.AuthorizationV1().SelfSubjectRulesReviews()
  125. client.userAccessReviewClient = userClientset.AuthorizationV1().SelfSubjectAccessReviews()
  126. return client, nil
  127. }
  128. func isReferentSpec(prov *esv1.KubernetesProvider) bool {
  129. if prov.Auth == nil {
  130. return false
  131. }
  132. if prov.Auth.Cert != nil {
  133. if prov.Auth.Cert.ClientCert.Namespace == nil {
  134. return true
  135. }
  136. if prov.Auth.Cert.ClientKey.Namespace == nil {
  137. return true
  138. }
  139. }
  140. if prov.Auth.ServiceAccount != nil {
  141. if prov.Auth.ServiceAccount.Namespace == nil {
  142. return true
  143. }
  144. }
  145. if prov.Auth.Token != nil {
  146. if prov.Auth.Token.BearerToken.Namespace == nil {
  147. return true
  148. }
  149. }
  150. return false
  151. }
  152. // Close cleans up any resources used by the Kubernetes provider.
  153. func (p *Provider) Close(_ context.Context) error {
  154. return nil
  155. }