auth.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright © The ESO Authors
  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. /*
  14. Package secretmanager implements the GCP Secret Manager provider for External Secrets.
  15. It provides functionality to interact with GCP Secret Manager, handle workload identity,
  16. and manage secret operations.
  17. */
  18. package secretmanager
  19. import (
  20. "context"
  21. "fmt"
  22. "golang.org/x/oauth2"
  23. "golang.org/x/oauth2/google"
  24. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  25. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  27. )
  28. // NewTokenSource creates a new OAuth2 token source for GCP Secret Manager authentication.
  29. // It attempts to create a token source using service account credentials, workload identity,
  30. // or workload identity federation in that order.
  31. func NewTokenSource(ctx context.Context, auth esv1.GCPSMAuth, projectID, storeKind string, kube kclient.Client, namespace string) (oauth2.TokenSource, error) {
  32. ts, err := serviceAccountTokenSource(ctx, auth, storeKind, kube, namespace)
  33. if ts != nil || err != nil {
  34. return ts, err
  35. }
  36. wi, err := newWorkloadIdentity(ctx, projectID)
  37. if err != nil {
  38. return nil, fmt.Errorf("unable to initialize workload identity: %w", err)
  39. }
  40. defer func() {
  41. _ = wi.Close()
  42. }()
  43. isClusterKind := storeKind == esv1.ClusterSecretStoreKind
  44. ts, err = wi.TokenSource(ctx, auth, isClusterKind, kube, namespace)
  45. if ts != nil || err != nil {
  46. return ts, err
  47. }
  48. wif, err := newWorkloadIdentityFederation(kube, auth.WorkloadIdentityFederation, isClusterKind, namespace)
  49. if err != nil {
  50. return nil, fmt.Errorf("failed to initialize workload identity federation: %w", err)
  51. }
  52. ts, err = wif.TokenSource(ctx)
  53. if ts != nil || err != nil {
  54. return ts, err
  55. }
  56. return google.DefaultTokenSource(ctx, CloudPlatformRole)
  57. }
  58. func serviceAccountTokenSource(ctx context.Context, auth esv1.GCPSMAuth, storeKind string, kube kclient.Client, namespace string) (oauth2.TokenSource, error) {
  59. sr := auth.SecretRef
  60. if sr == nil {
  61. return nil, nil
  62. }
  63. credentials, err := resolvers.SecretKeyRef(
  64. ctx,
  65. kube,
  66. storeKind,
  67. namespace,
  68. &auth.SecretRef.SecretAccessKey)
  69. if err != nil {
  70. return nil, err
  71. }
  72. config, err := google.JWTConfigFromJSON([]byte(credentials), CloudPlatformRole)
  73. if err != nil {
  74. return nil, fmt.Errorf(errUnableProcessJSONCredentials, err)
  75. }
  76. return config.TokenSource(ctx), nil
  77. }