creds.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2017 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package internal
  5. import (
  6. "context"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "golang.org/x/oauth2"
  12. "google.golang.org/api/internal/impersonate"
  13. "golang.org/x/oauth2/google"
  14. )
  15. // Creds returns credential information obtained from DialSettings, or if none, then
  16. // it returns default credential information.
  17. func Creds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
  18. creds, err := baseCreds(ctx, ds)
  19. if err != nil {
  20. return nil, err
  21. }
  22. if ds.ImpersonationConfig != nil {
  23. return impersonateCredentials(ctx, creds, ds)
  24. }
  25. return creds, nil
  26. }
  27. func baseCreds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
  28. if ds.Credentials != nil {
  29. return ds.Credentials, nil
  30. }
  31. if ds.CredentialsJSON != nil {
  32. return credentialsFromJSON(ctx, ds.CredentialsJSON, ds)
  33. }
  34. if ds.CredentialsFile != "" {
  35. data, err := ioutil.ReadFile(ds.CredentialsFile)
  36. if err != nil {
  37. return nil, fmt.Errorf("cannot read credentials file: %v", err)
  38. }
  39. return credentialsFromJSON(ctx, data, ds)
  40. }
  41. if ds.TokenSource != nil {
  42. return &google.Credentials{TokenSource: ds.TokenSource}, nil
  43. }
  44. cred, err := google.FindDefaultCredentials(ctx, ds.GetScopes()...)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if len(cred.JSON) > 0 {
  49. return credentialsFromJSON(ctx, cred.JSON, ds)
  50. }
  51. // For GAE and GCE, the JSON is empty so return the default credentials directly.
  52. return cred, nil
  53. }
  54. // JSON key file type.
  55. const (
  56. serviceAccountKey = "service_account"
  57. )
  58. // credentialsFromJSON returns a google.Credentials from the JSON data
  59. //
  60. // - A self-signed JWT flow will be executed if the following conditions are
  61. // met:
  62. // (1) At least one of the following is true:
  63. // (a) No scope is provided
  64. // (b) Scope for self-signed JWT flow is enabled
  65. // (c) Audiences are explicitly provided by users
  66. // (2) No service account impersontation
  67. //
  68. // - Otherwise, executes standard OAuth 2.0 flow
  69. // More details: google.aip.dev/auth/4111
  70. func credentialsFromJSON(ctx context.Context, data []byte, ds *DialSettings) (*google.Credentials, error) {
  71. // By default, a standard OAuth 2.0 token source is created
  72. cred, err := google.CredentialsFromJSON(ctx, data, ds.GetScopes()...)
  73. if err != nil {
  74. return nil, err
  75. }
  76. // Override the token source to use self-signed JWT if conditions are met
  77. isJWTFlow, err := isSelfSignedJWTFlow(data, ds)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if isJWTFlow {
  82. ts, err := selfSignedJWTTokenSource(data, ds)
  83. if err != nil {
  84. return nil, err
  85. }
  86. cred.TokenSource = ts
  87. }
  88. return cred, err
  89. }
  90. func isSelfSignedJWTFlow(data []byte, ds *DialSettings) (bool, error) {
  91. if (ds.EnableJwtWithScope || ds.HasCustomAudience()) &&
  92. ds.ImpersonationConfig == nil {
  93. // Check if JSON is a service account and if so create a self-signed JWT.
  94. var f struct {
  95. Type string `json:"type"`
  96. // The rest JSON fields are omitted because they are not used.
  97. }
  98. if err := json.Unmarshal(data, &f); err != nil {
  99. return false, err
  100. }
  101. return f.Type == serviceAccountKey, nil
  102. }
  103. return false, nil
  104. }
  105. func selfSignedJWTTokenSource(data []byte, ds *DialSettings) (oauth2.TokenSource, error) {
  106. if len(ds.GetScopes()) > 0 && !ds.HasCustomAudience() {
  107. // Scopes are preferred in self-signed JWT unless the scope is not available
  108. // or a custom audience is used.
  109. return google.JWTAccessTokenSourceWithScope(data, ds.GetScopes()...)
  110. } else if ds.GetAudience() != "" {
  111. // Fallback to audience if scope is not provided
  112. return google.JWTAccessTokenSourceFromJSON(data, ds.GetAudience())
  113. } else {
  114. return nil, errors.New("neither scopes or audience are available for the self-signed JWT")
  115. }
  116. }
  117. // QuotaProjectFromCreds returns the quota project from the JSON blob in the provided credentials.
  118. //
  119. // NOTE(cbro): consider promoting this to a field on google.Credentials.
  120. func QuotaProjectFromCreds(cred *google.Credentials) string {
  121. var v struct {
  122. QuotaProject string `json:"quota_project_id"`
  123. }
  124. if err := json.Unmarshal(cred.JSON, &v); err != nil {
  125. return ""
  126. }
  127. return v.QuotaProject
  128. }
  129. func impersonateCredentials(ctx context.Context, creds *google.Credentials, ds *DialSettings) (*google.Credentials, error) {
  130. if len(ds.ImpersonationConfig.Scopes) == 0 {
  131. ds.ImpersonationConfig.Scopes = ds.GetScopes()
  132. }
  133. ts, err := impersonate.TokenSource(ctx, creds.TokenSource, ds.ImpersonationConfig)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return &google.Credentials{
  138. TokenSource: ts,
  139. ProjectID: creds.ProjectID,
  140. }, nil
  141. }