provider.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright © The ESO Authors
  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. // userCoreV1 is a client-go CoreV1() interface
  60. // with user-defined scope, used for dynamic namespace resolution.
  61. userCoreV1 typedcorev1.CoreV1Interface
  62. // userSecretClient is a client-go CoreV1().Secrets() client
  63. // with user-defined scope.
  64. userSecretClient KClient
  65. // userReviewClient is a SelfSubjectRulesReview client with
  66. // user-defined scope.
  67. userReviewClient RClient
  68. // userAccessReviewClient is a SelfSubjectAccessReview client with
  69. // user-defined scope.
  70. userAccessReviewClient AClient
  71. // store is the Kubernetes Provider spec
  72. // which contains the configuration for this provider.
  73. store *esv1.KubernetesProvider
  74. storeKind string
  75. // namespace is the namespace of the
  76. // ExternalSecret referencing this provider.
  77. namespace string
  78. }
  79. // Capabilities returns the provider's supported capabilities (ReadWrite).
  80. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  81. return esv1.SecretStoreReadWrite
  82. }
  83. // NewClient constructs a Kubernetes Provider.
  84. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  85. restCfg, err := ctrlcfg.GetConfig()
  86. if err != nil {
  87. return nil, err
  88. }
  89. clientset, err := kubernetes.NewForConfig(restCfg)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return p.newClient(ctx, store, kube, clientset, namespace)
  94. }
  95. func (p *Provider) newClient(ctx context.Context, store esv1.GenericStore, ctrlClient kclient.Client, ctrlClientset kubernetes.Interface, namespace string) (esv1.SecretsClient, error) {
  96. storeSpec := store.GetSpec()
  97. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Kubernetes == nil {
  98. return nil, errors.New("no store type or wrong store type")
  99. }
  100. storeSpecKubernetes := storeSpec.Provider.Kubernetes
  101. client := &Client{
  102. ctrlClientset: ctrlClientset.CoreV1(),
  103. ctrlClient: ctrlClient,
  104. store: storeSpecKubernetes,
  105. namespace: namespace,
  106. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  107. }
  108. // allow SecretStore controller validation to pass
  109. // when using referent namespace.
  110. if client.storeKind == esv1.ClusterSecretStoreKind && client.namespace == "" && isReferentSpec(storeSpecKubernetes) {
  111. return client, nil
  112. }
  113. cfg, err := client.getAuth(ctx)
  114. if err != nil {
  115. return nil, fmt.Errorf("failed to prepare auth: %w", err)
  116. }
  117. userClientset, err := kubernetes.NewForConfig(cfg)
  118. if err != nil {
  119. return nil, fmt.Errorf("error configuring clientset: %w", err)
  120. }
  121. client.userCoreV1 = userClientset.CoreV1()
  122. client.userSecretClient = client.userCoreV1.Secrets(client.store.RemoteNamespace)
  123. client.userReviewClient = userClientset.AuthorizationV1().SelfSubjectRulesReviews()
  124. client.userAccessReviewClient = userClientset.AuthorizationV1().SelfSubjectAccessReviews()
  125. return client, nil
  126. }
  127. func (c *Client) secretsClientFor(namespace string) KClient {
  128. if c.userCoreV1 != nil && namespace != "" && namespace != c.store.RemoteNamespace {
  129. return c.userCoreV1.Secrets(namespace)
  130. }
  131. return c.userSecretClient
  132. }
  133. func isReferentSpec(prov *esv1.KubernetesProvider) bool {
  134. if prov.Auth == nil {
  135. return false
  136. }
  137. if prov.Auth.Cert != nil {
  138. if prov.Auth.Cert.ClientCert.Namespace == nil {
  139. return true
  140. }
  141. if prov.Auth.Cert.ClientKey.Namespace == nil {
  142. return true
  143. }
  144. }
  145. if prov.Auth.ServiceAccount != nil {
  146. if prov.Auth.ServiceAccount.Namespace == nil {
  147. return true
  148. }
  149. }
  150. if prov.Auth.Token != nil {
  151. if prov.Auth.Token.BearerToken.Namespace == nil {
  152. return true
  153. }
  154. }
  155. return false
  156. }
  157. // Close cleans up any resources used by the Kubernetes provider.
  158. func (p *Provider) Close(_ context.Context) error {
  159. return nil
  160. }
  161. // NewProvider creates a new Provider instance.
  162. func NewProvider() esv1.Provider {
  163. return &Provider{}
  164. }
  165. // ProviderSpec returns the provider specification for registration.
  166. func ProviderSpec() *esv1.SecretStoreProvider {
  167. return &esv1.SecretStoreProvider{
  168. Kubernetes: &esv1.KubernetesProvider{},
  169. }
  170. }
  171. // MaintenanceStatus returns the maintenance status of the provider.
  172. func MaintenanceStatus() esv1.MaintenanceStatus {
  173. return esv1.MaintenanceStatusMaintained
  174. }