| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package internal | 5 package internal |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/http" | 9 "net/http" |
| 10 | 10 |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 "golang.org/x/oauth2" | 12 "golang.org/x/oauth2" |
| 13 "google.golang.org/api/googleapi" | 13 "google.golang.org/api/googleapi" |
| 14 | 14 |
| 15 "github.com/luci/luci-go/common/errors" | |
| 16 "github.com/luci/luci-go/common/gcloud/googleoauth" | 15 "github.com/luci/luci-go/common/gcloud/googleoauth" |
| 17 "github.com/luci/luci-go/common/gcloud/iam" | 16 "github.com/luci/luci-go/common/gcloud/iam" |
| 17 "github.com/luci/luci-go/common/retry" |
| 18 ) | 18 ) |
| 19 | 19 |
| 20 type iamTokenProvider struct { | 20 type iamTokenProvider struct { |
| 21 actAs string | 21 actAs string |
| 22 scopes []string | 22 scopes []string |
| 23 transport http.RoundTripper | 23 transport http.RoundTripper |
| 24 cacheKey CacheKey | 24 cacheKey CacheKey |
| 25 } | 25 } |
| 26 | 26 |
| 27 // NewIAMTokenProvider returns TokenProvider that uses SignBlob IAM API to | 27 // NewIAMTokenProvider returns TokenProvider that uses SignBlob IAM API to |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 }, | 63 }, |
| 64 }, | 64 }, |
| 65 }) | 65 }) |
| 66 if err == nil { | 66 if err == nil { |
| 67 return tok, nil | 67 return tok, nil |
| 68 } | 68 } |
| 69 // Any 4** HTTP response is a fatal error. Everything else is transient. | 69 // Any 4** HTTP response is a fatal error. Everything else is transient. |
| 70 if apiErr, _ := err.(*googleapi.Error); apiErr != nil && apiErr.Code < 5
00 { | 70 if apiErr, _ := err.(*googleapi.Error); apiErr != nil && apiErr.Code < 5
00 { |
| 71 return nil, err | 71 return nil, err |
| 72 } | 72 } |
| 73 » return nil, errors.WrapTransient(err) | 73 » return nil, retry.Tag.Apply(err) |
| 74 } | 74 } |
| 75 | 75 |
| 76 func (p *iamTokenProvider) RefreshToken(ctx context.Context, prev, base *oauth2.
Token) (*oauth2.Token, error) { | 76 func (p *iamTokenProvider) RefreshToken(ctx context.Context, prev, base *oauth2.
Token) (*oauth2.Token, error) { |
| 77 // Service account tokens are self sufficient, there's no need for refre
sh | 77 // Service account tokens are self sufficient, there's no need for refre
sh |
| 78 // token. Minting a token and "refreshing" it is a same thing. | 78 // token. Minting a token and "refreshing" it is a same thing. |
| 79 return p.MintToken(ctx, base) | 79 return p.MintToken(ctx, base) |
| 80 } | 80 } |
| 81 | 81 |
| 82 //////////////////////////////////////////////////////////////////////////////// | 82 //////////////////////////////////////////////////////////////////////////////// |
| 83 | 83 |
| 84 type tokenInjectingTransport struct { | 84 type tokenInjectingTransport struct { |
| 85 transport http.RoundTripper | 85 transport http.RoundTripper |
| 86 token *oauth2.Token | 86 token *oauth2.Token |
| 87 } | 87 } |
| 88 | 88 |
| 89 func (t *tokenInjectingTransport) RoundTrip(req *http.Request) (*http.Response,
error) { | 89 func (t *tokenInjectingTransport) RoundTrip(req *http.Request) (*http.Response,
error) { |
| 90 clone := *req | 90 clone := *req |
| 91 clone.Header = make(http.Header, len(req.Header)+1) | 91 clone.Header = make(http.Header, len(req.Header)+1) |
| 92 for k, v := range req.Header { | 92 for k, v := range req.Header { |
| 93 clone.Header[k] = v | 93 clone.Header[k] = v |
| 94 } | 94 } |
| 95 t.token.SetAuthHeader(&clone) | 95 t.token.SetAuthHeader(&clone) |
| 96 return t.transport.RoundTrip(&clone) | 96 return t.transport.RoundTrip(&clone) |
| 97 } | 97 } |
| OLD | NEW |