Skip to content

Commit 490fb25

Browse files
committed
reduce token expiration if credentials will expire
Fixes kubernetes-sigs#590 This comes up when using a long-lived `token.Generator` instance where the underlying assume-role session might expire, invalidating the token.
1 parent b037258 commit 490fb25

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/token/token.go

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4242
"k8s.io/client-go/pkg/apis/clientauthentication"
4343
clientauthv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
44+
4445
"sigs.k8s.io/aws-iam-authenticator/pkg"
4546
"sigs.k8s.io/aws-iam-authenticator/pkg/arn"
4647
"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
@@ -337,6 +338,14 @@ func (g generator) GetWithSTS(clusterID string, stsAPI stsiface.STSAPI) (Token,
337338

338339
// Set token expiration to 1 minute before the presigned URL expires for some cushion
339340
tokenExpiration := time.Now().Local().Add(presignedURLExpiration - 1*time.Minute)
341+
// If the STS client is using temporary credentials, use the expiration time of those credentials.
342+
// https://github.com/kubernetes-sigs/aws-iam-authenticator/issues/590
343+
if realSts, ok := stsAPI.(*sts.STS); ok {
344+
// if expiresAt is in the past, it's probably not valid so don't use it
345+
if expiresAt, err := realSts.Config.Credentials.ExpiresAt(); err == nil && expiresAt.Before(tokenExpiration) && expiresAt.After(time.Now()) {
346+
tokenExpiration = expiresAt
347+
}
348+
}
340349
// TODO: this may need to be a constant-time base64 encoding
341350
return Token{v1Prefix + base64.RawURLEncoding.EncodeToString([]byte(presignedURLString)), tokenExpiration}, nil
342351
}

0 commit comments

Comments
 (0)