provider.go 5.1 KB

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