auth.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. "golang.org/x/oauth2"
  17. "golang.org/x/oauth2/google"
  18. v1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. )
  23. func NewTokenSource(ctx context.Context, auth esv1beta1.GCPSMAuth, projectID string, isClusterKind bool, kube kclient.Client, namespace string) (oauth2.TokenSource, error) {
  24. ts, err := serviceAccountTokenSource(ctx, auth, isClusterKind, kube, namespace)
  25. if ts != nil || err != nil {
  26. return ts, err
  27. }
  28. wi, err := newWorkloadIdentity(ctx, projectID)
  29. if err != nil {
  30. useMu.Unlock()
  31. return nil, fmt.Errorf("unable to initialize workload identity")
  32. }
  33. ts, err = wi.TokenSource(ctx, auth, isClusterKind, kube, namespace)
  34. if ts != nil || err != nil {
  35. return ts, err
  36. }
  37. return google.DefaultTokenSource(ctx, CloudPlatformRole)
  38. }
  39. func serviceAccountTokenSource(ctx context.Context, auth esv1beta1.GCPSMAuth, isClusterKind bool, kube kclient.Client, namespace string) (oauth2.TokenSource, error) {
  40. sr := auth.SecretRef
  41. if sr == nil {
  42. return nil, nil
  43. }
  44. credentialsSecret := &v1.Secret{}
  45. credentialsSecretName := sr.SecretAccessKey.Name
  46. objectKey := types.NamespacedName{
  47. Name: credentialsSecretName,
  48. Namespace: namespace,
  49. }
  50. // only ClusterStore is allowed to set namespace (and then it's required)
  51. if isClusterKind {
  52. if credentialsSecretName != "" && sr.SecretAccessKey.Namespace == nil {
  53. return nil, fmt.Errorf(errInvalidClusterStoreMissingSAKNamespace)
  54. } else if credentialsSecretName != "" {
  55. objectKey.Namespace = *sr.SecretAccessKey.Namespace
  56. }
  57. }
  58. err := kube.Get(ctx, objectKey, credentialsSecret)
  59. if err != nil {
  60. return nil, fmt.Errorf(errFetchSAKSecret, err)
  61. }
  62. credentials := credentialsSecret.Data[sr.SecretAccessKey.Key]
  63. if (credentials == nil) || (len(credentials) == 0) {
  64. return nil, fmt.Errorf(errMissingSAK)
  65. }
  66. config, err := google.JWTConfigFromJSON(credentials, CloudPlatformRole)
  67. if err != nil {
  68. return nil, fmt.Errorf(errUnableProcessJSONCredentials, err)
  69. }
  70. return config.TokenSource(ctx), nil
  71. }