provider.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 secretmanager
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "sync"
  19. secretmanager "cloud.google.com/go/secretmanager/apiv1"
  20. "golang.org/x/oauth2"
  21. "google.golang.org/api/option"
  22. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  23. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. "github.com/external-secrets/external-secrets/pkg/esutils"
  26. )
  27. // Provider is a secrets provider for GCP Secret Manager.
  28. // It implements the necessary NewClient() and ValidateStore() funcs.
  29. type Provider struct{}
  30. // https://github.com/external-secrets/external-secrets/issues/644
  31. var _ esv1.SecretsClient = &Client{}
  32. var _ esv1.Provider = &Provider{}
  33. func init() {
  34. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  35. GCPSM: &esv1.GCPSMProvider{},
  36. }, esv1.MaintenanceStatusMaintained)
  37. }
  38. /*
  39. Currently, GCPSM client has a limitation around how concurrent connections work
  40. This limitation causes memory leaks due to random disconnects from living clients
  41. and also payload switches when sending a call (such as using a credential from one
  42. thread to ask secrets from another thread).
  43. A Mutex was implemented to make sure only one connection can be in place at a time.
  44. */
  45. var useMu = sync.Mutex{}
  46. // Capabilities returns the provider's capabilities to read/write secrets.
  47. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  48. return esv1.SecretStoreReadWrite
  49. }
  50. // NewClient constructs a GCP Provider.
  51. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  52. storeSpec := store.GetSpec()
  53. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.GCPSM == nil {
  54. return nil, errors.New(errGCPSMStore)
  55. }
  56. gcpStore := storeSpec.Provider.GCPSM
  57. useMu.Lock()
  58. client := &Client{
  59. kube: kube,
  60. store: gcpStore,
  61. storeKind: store.GetKind(),
  62. namespace: namespace,
  63. }
  64. defer func() {
  65. if client.smClient == nil {
  66. _ = client.Close(ctx)
  67. }
  68. }()
  69. // this project ID is used for authentication (currently only relevant for workload identity)
  70. clusterProjectID, err := clusterProjectID(storeSpec)
  71. if err != nil {
  72. return nil, err
  73. }
  74. isClusterKind := store.GetObjectKind().GroupVersionKind().Kind == esv1.ClusterSecretStoreKind
  75. // allow SecretStore controller validation to pass
  76. // when using referent namespace.
  77. if namespace == "" && isClusterKind && isReferentSpec(gcpStore) {
  78. // placeholder smClient to prevent closing the client twice
  79. client.smClient, _ = secretmanager.NewClient(ctx, option.WithTokenSource(oauth2.StaticTokenSource(&oauth2.Token{})))
  80. return client, nil
  81. }
  82. ts, err := NewTokenSource(ctx, gcpStore.Auth, clusterProjectID, store.GetKind(), kube, namespace)
  83. if err != nil {
  84. return nil, fmt.Errorf(errUnableCreateGCPSMClient, err)
  85. }
  86. // check if we can get credentials
  87. _, err = ts.Token()
  88. if err != nil {
  89. return nil, fmt.Errorf(errUnableGetCredentials, err)
  90. }
  91. var clientGCPSM *secretmanager.Client
  92. if gcpStore.Location != "" {
  93. ep := fmt.Sprintf("secretmanager.%s.rep.googleapis.com:443", gcpStore.Location)
  94. regional := option.WithEndpoint(ep)
  95. clientGCPSM, err = secretmanager.NewClient(ctx, option.WithTokenSource(ts), regional)
  96. if err != nil {
  97. return nil, fmt.Errorf(errUnableCreateGCPSMClient, err)
  98. }
  99. } else {
  100. clientGCPSM, err = secretmanager.NewClient(ctx, option.WithTokenSource(ts))
  101. if err != nil {
  102. return nil, fmt.Errorf(errUnableCreateGCPSMClient, err)
  103. }
  104. }
  105. client.smClient = clientGCPSM
  106. return client, nil
  107. }
  108. // ValidateStore validates the configuration of the secret store.
  109. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  110. if store == nil {
  111. return nil, errors.New(errInvalidStore)
  112. }
  113. spc := store.GetSpec()
  114. if spc == nil {
  115. return nil, errors.New(errInvalidStoreSpec)
  116. }
  117. if spc.Provider == nil {
  118. return nil, errors.New(errInvalidStoreProv)
  119. }
  120. g := spc.Provider.GCPSM
  121. if p == nil {
  122. return nil, errors.New(errInvalidGCPProv)
  123. }
  124. if g.Auth.SecretRef != nil {
  125. if err := esutils.ValidateReferentSecretSelector(store, g.Auth.SecretRef.SecretAccessKey); err != nil {
  126. return nil, fmt.Errorf(errInvalidAuthSecretRef, err)
  127. }
  128. }
  129. if g.Auth.WorkloadIdentity != nil {
  130. if err := esutils.ValidateReferentServiceAccountSelector(store, g.Auth.WorkloadIdentity.ServiceAccountRef); err != nil {
  131. return nil, fmt.Errorf(errInvalidWISARef, err)
  132. }
  133. }
  134. return nil, nil
  135. }
  136. func clusterProjectID(spec *esv1.SecretStoreSpec) (string, error) {
  137. if spec.Provider.GCPSM.Auth.WorkloadIdentity != nil && spec.Provider.GCPSM.Auth.WorkloadIdentity.ClusterProjectID != "" {
  138. return spec.Provider.GCPSM.Auth.WorkloadIdentity.ClusterProjectID, nil
  139. }
  140. if spec.Provider.GCPSM.ProjectID != "" {
  141. return spec.Provider.GCPSM.ProjectID, nil
  142. }
  143. return "", errors.New(errNoProjectID)
  144. }
  145. func isReferentSpec(prov *esv1.GCPSMProvider) bool {
  146. if prov.Auth.SecretRef != nil &&
  147. prov.Auth.SecretRef.SecretAccessKey.Namespace == nil {
  148. return true
  149. }
  150. if prov.Auth.WorkloadIdentity != nil &&
  151. prov.Auth.WorkloadIdentity.ServiceAccountRef.Namespace == nil {
  152. return true
  153. }
  154. return false
  155. }