impersonate.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package externalaccount
  5. import (
  6. "bytes"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "time"
  14. "golang.org/x/oauth2"
  15. )
  16. // generateAccesstokenReq is used for service account impersonation
  17. type generateAccessTokenReq struct {
  18. Delegates []string `json:"delegates,omitempty"`
  19. Lifetime string `json:"lifetime,omitempty"`
  20. Scope []string `json:"scope,omitempty"`
  21. }
  22. type impersonateTokenResponse struct {
  23. AccessToken string `json:"accessToken"`
  24. ExpireTime string `json:"expireTime"`
  25. }
  26. // ImpersonateTokenSource uses a source credential, stored in Ts, to request an access token to the provided URL.
  27. // Scopes can be defined when the access token is requested.
  28. type ImpersonateTokenSource struct {
  29. // Ctx is the execution context of the impersonation process
  30. // used to perform http call to the URL. Required
  31. Ctx context.Context
  32. // Ts is the source credential used to generate a token on the
  33. // impersonated service account. Required.
  34. Ts oauth2.TokenSource
  35. // URL is the endpoint to call to generate a token
  36. // on behalf the service account. Required.
  37. URL string
  38. // Scopes that the impersonated credential should have. Required.
  39. Scopes []string
  40. // Delegates are the service account email addresses in a delegation chain.
  41. // Each service account must be granted roles/iam.serviceAccountTokenCreator
  42. // on the next service account in the chain. Optional.
  43. Delegates []string
  44. // TokenLifetimeSeconds is the number of seconds the impersonation token will
  45. // be valid for.
  46. TokenLifetimeSeconds int
  47. }
  48. // Token performs the exchange to get a temporary service account token to allow access to GCP.
  49. func (its ImpersonateTokenSource) Token() (*oauth2.Token, error) {
  50. lifetimeString := "3600s"
  51. if its.TokenLifetimeSeconds != 0 {
  52. lifetimeString = fmt.Sprintf("%ds", its.TokenLifetimeSeconds)
  53. }
  54. reqBody := generateAccessTokenReq{
  55. Lifetime: lifetimeString,
  56. Scope: its.Scopes,
  57. Delegates: its.Delegates,
  58. }
  59. b, err := json.Marshal(reqBody)
  60. if err != nil {
  61. return nil, fmt.Errorf("oauth2/google: unable to marshal request: %v", err)
  62. }
  63. client := oauth2.NewClient(its.Ctx, its.Ts)
  64. req, err := http.NewRequest("POST", its.URL, bytes.NewReader(b))
  65. if err != nil {
  66. return nil, fmt.Errorf("oauth2/google: unable to create impersonation request: %v", err)
  67. }
  68. req = req.WithContext(its.Ctx)
  69. req.Header.Set("Content-Type", "application/json")
  70. resp, err := client.Do(req)
  71. if err != nil {
  72. return nil, fmt.Errorf("oauth2/google: unable to generate access token: %v", err)
  73. }
  74. defer resp.Body.Close()
  75. body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
  76. if err != nil {
  77. return nil, fmt.Errorf("oauth2/google: unable to read body: %v", err)
  78. }
  79. if c := resp.StatusCode; c < 200 || c > 299 {
  80. return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
  81. }
  82. var accessTokenResp impersonateTokenResponse
  83. if err := json.Unmarshal(body, &accessTokenResp); err != nil {
  84. return nil, fmt.Errorf("oauth2/google: unable to parse response: %v", err)
  85. }
  86. expiry, err := time.Parse(time.RFC3339, accessTokenResp.ExpireTime)
  87. if err != nil {
  88. return nil, fmt.Errorf("oauth2/google: unable to parse expiry: %v", err)
  89. }
  90. return &oauth2.Token{
  91. AccessToken: accessTokenResp.AccessToken,
  92. Expiry: expiry,
  93. TokenType: "Bearer",
  94. }, nil
  95. }