Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: renew the session token when the token expires #683

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ func (sc *SessionCache) newSession(ctx context.Context, opts Options) (*session.
WithLogger(sdkLogger{})
}

awsCfg.Retryer = newCustomRetryer(opts.MaxRetries)
awsCfg.Retryer = newCustomRetryer(sc, opts.MaxRetries)

useSharedConfig := session.SharedConfigEnable
{
Expand Down Expand Up @@ -1276,7 +1276,7 @@ func (sc *SessionCache) newSession(ctx context.Context, opts Options) (*session.
return sess, nil
}

func (sc *SessionCache) clear() {
func (sc *SessionCache) Clear() {
sc.Lock()
defer sc.Unlock()
sc.sessions = map[Options]*session.Session{}
Expand Down Expand Up @@ -1324,10 +1324,12 @@ func setSessionRegion(ctx context.Context, sess *session.Session, bucket string)
// error codes. Such as, retry for S3 InternalError code.
type customRetryer struct {
client.DefaultRetryer
sc *SessionCache
}

func newCustomRetryer(maxRetries int) *customRetryer {
func newCustomRetryer(sc *SessionCache, maxRetries int) *customRetryer {
return &customRetryer{
sc: sc,
DefaultRetryer: client.DefaultRetryer{
NumMaxRetries: maxRetries,
},
Expand All @@ -1337,13 +1339,22 @@ func newCustomRetryer(maxRetries int) *customRetryer {
// ShouldRetry overrides SDK's built in DefaultRetryer, adding custom retry
// logics that are not included in the SDK.
func (c *customRetryer) ShouldRetry(req *request.Request) bool {
shouldRetry := errHasCode(req.Error, "InternalError") || errHasCode(req.Error, "RequestTimeTooSkewed") || errHasCode(req.Error, "SlowDown") || strings.Contains(req.Error.Error(), "connection reset") || strings.Contains(req.Error.Error(), "connection timed out")
shouldRetry := errHasCode(req.Error, "InternalError") || errHasCode(req.Error, "RequestTimeTooSkewed") || errHasCode(req.Error, "SlowDown") || strings.Contains(req.Error.Error(), "connection reset") || strings.Contains(req.Error.Error(), "connection timed out") || errHasCode(req.Error, "ExpiredToken") || errHasCode(req.Error, "ExpiredTokenException")

if errHasCode(req.Error, "ExpiredToken") || errHasCode(req.Error, "ExpiredTokenException") {
log.Debug(log.DebugMessage{
Err: "Clearing the token",
})

c.sc.Clear()
}

if !shouldRetry {
shouldRetry = c.DefaultRetryer.ShouldRetry(req)
}

// Errors related to tokens
if errHasCode(req.Error, "ExpiredToken") || errHasCode(req.Error, "ExpiredTokenException") || errHasCode(req.Error, "InvalidToken") {
if errHasCode(req.Error, "InvalidToken") {
return false
}

Expand Down
20 changes: 12 additions & 8 deletions storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestNewSessionPathStyle(t *testing.T) {
}

func TestNewSessionWithRegionSetViaEnv(t *testing.T) {
globalSessionCache.clear()
globalSessionCache.Clear()

const expectedRegion = "us-west-2"

Expand All @@ -116,7 +116,7 @@ func TestNewSessionWithRegionSetViaEnv(t *testing.T) {
}

func TestNewSessionWithNoSignRequest(t *testing.T) {
globalSessionCache.clear()
globalSessionCache.Clear()

sess, err := globalSessionCache.newSession(context.Background(), Options{
NoSignRequest: true,
Expand Down Expand Up @@ -190,7 +190,7 @@ aws_secret_access_key = p2_profile_access_key`
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
globalSessionCache.clear()
globalSessionCache.Clear()
sess, err := globalSessionCache.newSession(context.Background(), Options{
Profile: tc.profileName,
CredentialFile: tc.fileName,
Expand Down Expand Up @@ -489,12 +489,12 @@ func TestS3Retry(t *testing.T) {
{
name: "ExpiredToken",
err: awserr.New("ExpiredToken", "expired token", nil),
expectedRetry: 0,
expectedRetry: 5,
},
{
name: "ExpiredTokenException",
err: awserr.New("ExpiredTokenException", "expired token exception", nil),
expectedRetry: 0,
expectedRetry: 5,
},

// Invalid Token errors
Expand Down Expand Up @@ -538,8 +538,12 @@ func TestS3Retry(t *testing.T) {
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
sessionCache := &SessionCache{
sessions: map[Options]*session.Session{},
}

sess := unit.Session
sess.Config.Retryer = newCustomRetryer(expectedRetry)
sess.Config.Retryer = newCustomRetryer(sessionCache, expectedRetry)

mockAPI := s3.New(sess)
mockS3 := &S3{
Expand Down Expand Up @@ -1041,7 +1045,7 @@ func TestSessionRegionDetection(t *testing.T) {
opts.bucket = tc.bucket
}

globalSessionCache.clear()
globalSessionCache.Clear()

sess, err := globalSessionCache.newSession(context.Background(), opts)
if err != nil {
Expand Down Expand Up @@ -1241,7 +1245,7 @@ func TestAWSLogLevel(t *testing.T) {

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
globalSessionCache.clear()
globalSessionCache.Clear()
sess, err := globalSessionCache.newSession(context.Background(), Options{
LogLevel: log.LevelFromString(tc.level),
})
Expand Down
Loading