provider.go 5.5 KB

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