Skip to content

Commit

Permalink
fix: handling existing tokens in the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce-Soghigian committed Jun 27, 2024
1 parent 89444b2 commit fe3c4cb
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/auth/cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import (
"knative.dev/pkg/logging"
)


// expireEarlyTokenCredential is a wrapper around the azcore.TokenCredential that
// returns an earlier ExpiresOn timestamp to avoid conditions like clockSkew, or a race
// condition during polling.
// expireEarlyTokenCredential is a wrapper around the azcore.TokenCredential that
// returns an earlier ExpiresOn timestamp to avoid conditions like clockSkew, or a race
// condition during polling.
// See: https://github.com/hashicorp/terraform-provider-azurerm/issues/20834 for more details
type expireEarlyTokenCredential struct {
cred azcore.TokenCredential
Expand All @@ -48,8 +47,17 @@ func (w *expireEarlyTokenCredential) GetToken(ctx context.Context, options polic
if err != nil {
return azcore.AccessToken{}, err
}

logging.FromContext(ctx).Debug("adjusting token ExpiresOn")
token.ExpiresOn = time.Now().Add(2 * time.Hour)
twoHoursFromNow := time.Now().Add(2 * time.Hour)
// IMDS may have the MI token already, and have an expiration of less than 2h when we receive the token. We don't want to set that value beyond the ExpiresOn time and potentially miss a refresh
// So we just return earlier here. See discussion here: https://github.com/Azure/karpenter-provider-azure/pull/391/files#r1648633051
if token.ExpiresOn.Before(twoHoursFromNow) {
return token, nil
}
// If the token expires in more than 2 hours, this means we are taking in a new token with a fresh 24h expiration time or one already in the cache that hasn't been modified by us, so we want to set that to two hours so
// we can refresh it early to avoid the polling bugs mentioned in the above issue
token.ExpiresOn = twoHoursFromNow
return token, nil
}

Expand Down

0 comments on commit fe3c4cb

Please sign in to comment.