Skip to content

Commit a1b1df0

Browse files
committed
fix: check credential expiration timestamp when generating tokens
What this PR does / why we need it: There are two expirations which must be considered when using a signed EKS token: * 15 minutes after the point in time when the AWS STS request has been signed * The underlying AWS credentials can expire at which point the token won't be accepted The second case is particularly common when making frequent requests while using AssumeRole or AssumeRoleWithWebRequest as mentioned in #590 as the default session timeout is 1 hour. This PR adds an additional check fetching the AWS credential expiration and using that as the returned expiration if it is before the 15 minute token expiration.
1 parent 2596d17 commit a1b1df0

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pkg/token/token.go

+8
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ func (g generator) GetWithSTS(clusterID string, stsAPI stsiface.STSAPI) (Token,
338338
request, _ := stsAPI.GetCallerIdentityRequest(&sts.GetCallerIdentityInput{})
339339
request.HTTPRequest.Header.Add(clusterIDHeader, clusterID)
340340

341+
// Fetch the timestamp when the credentials we're going to use for signing will not be valid anymore
342+
// This operation is potentially racey, but the worst case is that we expire a token early
343+
// Not all credential providers support this, so we ignore any returned errors
344+
credentialsExpiration, _ := request.Config.Credentials.ExpiresAt()
345+
341346
// Sign the request. The expires parameter (sets the x-amz-expires header) is
342347
// currently ignored by STS, and the token expires 15 minutes after the x-amz-date
343348
// timestamp regardless. We set it to 60 seconds for backwards compatibility (the
@@ -351,6 +356,9 @@ func (g generator) GetWithSTS(clusterID string, stsAPI stsiface.STSAPI) (Token,
351356

352357
// Set token expiration to 1 minute before the presigned URL expires for some cushion
353358
tokenExpiration := time.Now().Local().Add(presignedURLExpiration - 1*time.Minute)
359+
if !credentialsExpiration.IsZero() && credentialsExpiration.Before(tokenExpiration) {
360+
tokenExpiration = credentialsExpiration
361+
}
354362
// TODO: this may need to be a constant-time base64 encoding
355363
return Token{v1Prefix + base64.RawURLEncoding.EncodeToString([]byte(presignedURLString)), tokenExpiration}, nil
356364
}

0 commit comments

Comments
 (0)