Skip to content

Commit 0f29369

Browse files
committed
google: add some metadata to GCE tokens to identify the token's source
This is required for the direct path feature, which only works with this token source. It's not currently possible to determine the token source type from the return value of FindDefaultCredentials. Another option is to add another field to the Credentials struct, which we could still do later, but direct path is currently pretty experimental and whitelisted/opt-in, so I don't want to add to the public API surface unnecessarily. This CL functionally blocks https://code-review.googlesource.com/c/google-api-go-client/+/40950 Change-Id: Ifb5fe9c6e5c6b33eebb87b45d3c70eebfca691b3 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/175877 Reviewed-by: Chris Broadfoot <[email protected]>
1 parent aaccbc9 commit 0f29369

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

google/google.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,16 @@ func (cs computeSource) Token() (*oauth2.Token, error) {
194194
if res.ExpiresInSec == 0 || res.AccessToken == "" {
195195
return nil, fmt.Errorf("oauth2/google: incomplete token received from metadata")
196196
}
197-
return &oauth2.Token{
197+
tok := &oauth2.Token{
198198
AccessToken: res.AccessToken,
199199
TokenType: res.TokenType,
200200
Expiry: time.Now().Add(time.Duration(res.ExpiresInSec) * time.Second),
201-
}, nil
201+
}
202+
// NOTE(cbro): add hidden metadata about where the token is from.
203+
// This is needed for detection by client libraries to know that credentials come from the metadata server.
204+
// This may be removed in a future version of this library.
205+
return tok.WithExtra(map[string]interface{}{
206+
"oauth2.google.tokenSource": "compute-metadata",
207+
"oauth2.google.serviceAccount": acct,
208+
}), nil
202209
}

0 commit comments

Comments
 (0)