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

Strip Monotonic Clock Readings when Comparing Credential Expiry Time #789

Merged
merged 3 commits into from
Oct 5, 2020
Merged
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
4 changes: 3 additions & 1 deletion aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ type Credentials struct {
// Expired returns if the credentials have expired.
func (v Credentials) Expired() bool {
if v.CanExpire {
return !v.Expires.After(sdk.NowTime())
// Calling Round(0) on the current time will truncate the monotonic reading only. Ensures credential expiry
// time is always based on reported wall-clock time.
return !v.Expires.After(sdk.NowTime().Round(0))
}

return false
Expand Down
9 changes: 7 additions & 2 deletions ec2imds/token_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ var timeNow = time.Now

// Expired returns if the token is expired.
func (t *apiToken) Expired() bool {
return timeNow().After(t.expires)
// Calling Round(0) on the current time will truncate the monotonic reading only. Ensures credential expiry
// time is always based on reported wall-clock time.
return timeNow().Round(0).After(t.expires)
}

func (t *tokenProvider) ID() string { return "APITokenProvider" }
Expand Down Expand Up @@ -112,7 +114,7 @@ func (t *tokenProvider) HandleDeserialize(
return out, metadata, fmt.Errorf("expect HTTP transport, got %T", out.RawResponse)
}

if resp.StatusCode == 401 { // unauthorized
if resp.StatusCode == http.StatusUnauthorized { // unauthorized
err = &retryableError{Err: err}
t.enable()
}
Expand Down Expand Up @@ -228,5 +230,8 @@ func (t *tokenProvider) disable() {
// enable enables the token provide to start refreshing tokens, and adding them
// to the pending request.
func (t *tokenProvider) enable() {
t.tokenMux.Lock()
t.token = nil
t.tokenMux.Unlock()
atomic.StoreUint32(&t.disabled, 0)
}