provider.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package kubernetes
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. authv1 "k8s.io/api/authorization/v1"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/client-go/kubernetes"
  21. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  22. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  23. ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. )
  26. // https://github.com/external-secrets/external-secrets/issues/644
  27. var _ esv1.SecretsClient = &Client{}
  28. var _ esv1.Provider = &Provider{}
  29. type KClient interface {
  30. Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Secret, error)
  31. List(ctx context.Context, opts metav1.ListOptions) (*v1.SecretList, error)
  32. Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
  33. Create(ctx context.Context, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
  34. Update(ctx context.Context, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
  35. }
  36. type RClient interface {
  37. Create(ctx context.Context, selfSubjectRulesReview *authv1.SelfSubjectRulesReview, opts metav1.CreateOptions) (*authv1.SelfSubjectRulesReview, error)
  38. }
  39. type AClient interface {
  40. Create(ctx context.Context, selfSubjectAccessReview *authv1.SelfSubjectAccessReview, opts metav1.CreateOptions) (*authv1.SelfSubjectAccessReview, error)
  41. }
  42. // Provider implements Secret Provider interface
  43. // for Kubernetes.
  44. type Provider struct{}
  45. // Client implements Secret Client interface
  46. // for Kubernetes.
  47. type Client struct {
  48. // ctrlClient is a controller-runtime client
  49. // with RBAC scope of the controller (privileged!)
  50. ctrlClient kclient.Client
  51. // ctrlClientset is a client-go CoreV1() client
  52. // with RBAC scope of the controller (privileged!)
  53. ctrlClientset typedcorev1.CoreV1Interface
  54. // userSecretClient is a client-go CoreV1().Secrets() client
  55. // with user-defined scope.
  56. userSecretClient KClient
  57. // userReviewClient is a SelfSubjectRulesReview client with
  58. // user-defined scope.
  59. userReviewClient RClient
  60. // userAccessReviewClient is a SelfSubjectAccessReview client with
  61. // user-defined scope.
  62. userAccessReviewClient AClient
  63. // store is the Kubernetes Provider spec
  64. // which contains the configuration for this provider.
  65. store *esv1.KubernetesProvider
  66. storeKind string
  67. // namespace is the namespace of the
  68. // ExternalSecret referencing this provider.
  69. namespace string
  70. }
  71. func init() {
  72. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  73. Kubernetes: &esv1.KubernetesProvider{},
  74. }, esv1.MaintenanceStatusMaintained)
  75. }
  76. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  77. return esv1.SecretStoreReadWrite
  78. }
  79. // NewClient constructs a Kubernetes Provider.
  80. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  81. restCfg, err := ctrlcfg.GetConfig()
  82. if err != nil {
  83. return nil, err
  84. }
  85. clientset, err := kubernetes.NewForConfig(restCfg)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return p.newClient(ctx, store, kube, clientset, namespace)
  90. }
  91. func (p *Provider) newClient(ctx context.Context, store esv1.GenericStore, ctrlClient kclient.Client, ctrlClientset kubernetes.Interface, namespace string) (esv1.SecretsClient, error) {
  92. storeSpec := store.GetSpec()
  93. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Kubernetes == nil {
  94. return nil, errors.New("no store type or wrong store type")
  95. }
  96. storeSpecKubernetes := storeSpec.Provider.Kubernetes
  97. client := &Client{
  98. ctrlClientset: ctrlClientset.CoreV1(),
  99. ctrlClient: ctrlClient,
  100. store: storeSpecKubernetes,
  101. namespace: namespace,
  102. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  103. }
  104. // allow SecretStore controller validation to pass
  105. // when using referent namespace.
  106. if client.storeKind == esv1.ClusterSecretStoreKind && client.namespace == "" && isReferentSpec(storeSpecKubernetes) {
  107. return client, nil
  108. }
  109. cfg, err := client.getAuth(ctx)
  110. if err != nil {
  111. return nil, fmt.Errorf("failed to prepare auth: %w", err)
  112. }
  113. userClientset, err := kubernetes.NewForConfig(cfg)
  114. if err != nil {
  115. return nil, fmt.Errorf("error configuring clientset: %w", err)
  116. }
  117. client.userSecretClient = userClientset.CoreV1().Secrets(client.store.RemoteNamespace)
  118. client.userReviewClient = userClientset.AuthorizationV1().SelfSubjectRulesReviews()
  119. client.userAccessReviewClient = userClientset.AuthorizationV1().SelfSubjectAccessReviews()
  120. return client, nil
  121. }
  122. func isReferentSpec(prov *esv1.KubernetesProvider) bool {
  123. if prov.Auth.Cert != nil {
  124. if prov.Auth.Cert.ClientCert.Namespace == nil {
  125. return true
  126. }
  127. if prov.Auth.Cert.ClientKey.Namespace == nil {
  128. return true
  129. }
  130. }
  131. if prov.Auth.ServiceAccount != nil {
  132. if prov.Auth.ServiceAccount.Namespace == nil {
  133. return true
  134. }
  135. }
  136. if prov.Auth.Token != nil {
  137. if prov.Auth.Token.BearerToken.Namespace == nil {
  138. return true
  139. }
  140. }
  141. return false
  142. }
  143. func (p *Provider) Close(_ context.Context) error {
  144. return nil
  145. }