provider.go 4.2 KB

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