provider.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. authv1 "k8s.io/api/authorization/v1"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/client-go/kubernetes"
  22. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  23. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  24. ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
  25. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. )
  27. // https://github.com/external-secrets/external-secrets/issues/644
  28. var _ esv1.SecretsClient = &Client{}
  29. var _ esv1.Provider = &Provider{}
  30. type KClient interface {
  31. Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Secret, error)
  32. List(ctx context.Context, opts metav1.ListOptions) (*v1.SecretList, error)
  33. Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
  34. Create(ctx context.Context, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
  35. Update(ctx context.Context, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
  36. }
  37. type RClient interface {
  38. Create(ctx context.Context, selfSubjectRulesReview *authv1.SelfSubjectRulesReview, opts metav1.CreateOptions) (*authv1.SelfSubjectRulesReview, error)
  39. }
  40. type AClient interface {
  41. Create(ctx context.Context, selfSubjectAccessReview *authv1.SelfSubjectAccessReview, opts metav1.CreateOptions) (*authv1.SelfSubjectAccessReview, error)
  42. }
  43. // Provider implements Secret Provider interface
  44. // for Kubernetes.
  45. type Provider struct{}
  46. // Client implements Secret Client interface
  47. // for Kubernetes.
  48. type Client struct {
  49. // ctrlClient is a controller-runtime client
  50. // with RBAC scope of the controller (privileged!)
  51. ctrlClient kclient.Client
  52. // ctrlClientset is a client-go CoreV1() client
  53. // with RBAC scope of the controller (privileged!)
  54. ctrlClientset typedcorev1.CoreV1Interface
  55. // userSecretClient is a client-go CoreV1().Secrets() client
  56. // with user-defined scope.
  57. userSecretClient KClient
  58. // userReviewClient is a SelfSubjectRulesReview client with
  59. // user-defined scope.
  60. userReviewClient RClient
  61. // userAccessReviewClient is a SelfSubjectAccessReview client with
  62. // user-defined scope.
  63. userAccessReviewClient AClient
  64. // store is the Kubernetes Provider spec
  65. // which contains the configuration for this provider.
  66. store *esv1.KubernetesProvider
  67. storeKind string
  68. // namespace is the namespace of the
  69. // ExternalSecret referencing this provider.
  70. namespace string
  71. }
  72. func init() {
  73. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  74. Kubernetes: &esv1.KubernetesProvider{},
  75. }, esv1.MaintenanceStatusMaintained)
  76. }
  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. func (p *Provider) Close(_ context.Context) error {
  148. return nil
  149. }