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: Lazy refresh should refresh tokens 4 minutes before expiration. #2063

Merged
merged 1 commit into from
Sep 5, 2024

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented Sep 5, 2024

Added a 4 minute buffer to refreshing tokens and certificates to avoid creating race condition that would allow the connector to create an ephemeral certificate with an expired auth token.

Now, IAM auth tokens are now refreshed 4 minutes before they token expire. Also, the Lazy Refresh Strategy will refresh the client certificate 4 minutes before the expiration of the certificate and the IAM auth token.

This should mitigate some of the strange certificate expiration errors commonly found in Cloud Run, see: #2059

@jackwotherspoon
Copy link
Collaborator

This may mitigate some token expiration problems in Cloud Run.

Fixes #2059

We don't know for sure that this will fix the above issue right? We should probably mark this as "Related to #2509" and have it tested in the wild first before marking as fixed. Thoughts?

@hessjcg
Copy link
Collaborator Author

hessjcg commented Sep 5, 2024

This may mitigate some token expiration problems in Cloud Run.
Fixes #2059

We don't know for sure that this will fix the above issue right? We should probably mark this as "Related to #2509" and have it tested in the wild first before marking as fixed. Thoughts?

Good point. I set this as "related to."

Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One small nit.

.getAccessToken()
.getExpirationTime()
.toInstant()
.minus(4, ChronoUnit.MINUTES)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a constant for this value somewhere? I think we should use it to avoid future mistakes in buffering.

Copy link
Collaborator

@jackwotherspoon jackwotherspoon Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➕ 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, yes. RefreshCalculator.DEFAULT_REFRESH_BUFFER

Copy link

@sanmahapatra sanmahapatra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

|| "".equals(credentials.getAccessToken().getTokenValue());
}

private void refreshIfNearExpiration(GoogleCredentials credentials) throws IOException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since we also refresh for reasons other than expiry maybe a better name is refreshIfRequired or refreshIfNecessary or something similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@hessjcg hessjcg enabled auto-merge (squash) September 5, 2024 19:35
@hessjcg hessjcg merged commit 286051d into main Sep 5, 2024
17 checks passed
@hessjcg hessjcg deleted the gh-2059-fix-token-refresh branch September 5, 2024 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants