Skip to content

Commit 4dc4884

Browse files
committed
fixes v1 sdk code leak and go imports
1 parent 1f7e3b4 commit 4dc4884

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

aws/ec2metadata/api_client_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package ec2metadata_test
22

33
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
48
"github.com/aws/aws-sdk-go-v2/aws"
59
"github.com/aws/aws-sdk-go-v2/aws/awserr"
610
"github.com/aws/aws-sdk-go-v2/aws/ec2metadata"
711
"github.com/aws/aws-sdk-go-v2/internal/awstesting"
812
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
9-
"os"
10-
"strings"
11-
"testing"
1213
)
1314

1415
func TestClientDisableIMDS(t *testing.T) {

aws/ec2metadata/token_provider.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package ec2metadata
22

33
import (
4-
request "github.com/aws/aws-sdk-go-v2/aws"
5-
"github.com/aws/aws-sdk-go-v2/aws/awserr"
6-
"github.com/aws/aws-sdk-go/aws/credentials"
74
"net/http"
85
"sync/atomic"
96
"time"
7+
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/aws/awserr"
1010
)
1111

1212
// A tokenProvider struct provides access to EC2Metadata client
@@ -24,7 +24,7 @@ type tokenProvider struct {
2424
// A ec2Token struct helps use of token in EC2 Metadata service ops
2525
type ec2Token struct {
2626
token string
27-
credentials.Expiry
27+
aws.Credentials
2828
}
2929

3030
// newTokenProvider provides a pointer to a tokenProvider instance
@@ -33,24 +33,21 @@ func newTokenProvider(c *Client, duration time.Duration) *tokenProvider {
3333
}
3434

3535
// fetchTokenHandler fetches token for EC2Metadata service client by default.
36-
func (t *tokenProvider) fetchTokenHandler(r *request.Request) {
36+
func (t *tokenProvider) fetchTokenHandler(r *aws.Request) {
3737

3838
// short-circuits to insecure data flow if tokenProvider is disabled.
3939
if v := atomic.LoadUint32(&t.disabled); v == 1 {
4040
return
4141
}
4242

43-
if ec2Token, ok := t.token.Load().(ec2Token); ok && !ec2Token.IsExpired() {
43+
if ec2Token, ok := t.token.Load().(ec2Token); ok && !ec2Token.Expired() {
4444
r.HTTPRequest.Header.Set(tokenHeader, ec2Token.token)
4545
return
4646
}
4747

4848
output, err := t.client.getToken(t.configuredTTL)
49-
5049
if err != nil {
51-
52-
// change the disabled flag on token provider to true,
53-
// when error is request timeout error.
50+
// change the disabled flag on token provider to true, when error is request timeout error.
5451
if requestFailureError, ok := err.(awserr.RequestFailure); ok {
5552
switch requestFailureError.StatusCode() {
5653
case http.StatusForbidden, http.StatusNotFound, http.StatusMethodNotAllowed:
@@ -72,17 +69,17 @@ func (t *tokenProvider) fetchTokenHandler(r *request.Request) {
7269
newToken := ec2Token{
7370
token: output.Token,
7471
}
75-
newToken.SetExpiration(time.Now().Add(output.TTL), ttlExpirationWindow)
72+
newToken.CanExpire = true
73+
newToken.Expires = time.Now().Add(output.TTL).Add(-ttlExpirationWindow)
7674
t.token.Store(newToken)
77-
78-
// Inject token header to the request.
7975
if ec2Token, ok := t.token.Load().(ec2Token); ok {
76+
// Inject token header to the request.
8077
r.HTTPRequest.Header.Set(tokenHeader, ec2Token.token)
8178
}
8279
}
8380

8481
// enableTokenProviderHandler enables the token provider
85-
func (t *tokenProvider) enableTokenProviderHandler(r *request.Request) {
82+
func (t *tokenProvider) enableTokenProviderHandler(r *aws.Request) {
8683
// If the error code status is 401, we enable the token provider
8784
if e, ok := r.Error.(awserr.RequestFailure); ok && e != nil &&
8885
e.StatusCode() == http.StatusUnauthorized {

0 commit comments

Comments
 (0)