Skip to content

Have GetSigningCredentialsAsync refresh its key earlier - #359

Open
j3parker wants to merge 2 commits into
mainfrom
refresh-fix
Open

Have GetSigningCredentialsAsync refresh its key earlier#359
j3parker wants to merge 2 commits into
mainfrom
refresh-fix

Conversation

@j3parker

@j3parker j3parker commented Feb 7, 2024

Copy link
Copy Markdown
Member

Only refreshing after the key is expired will cause us to sign tokens that might not validate for their entire lifetime.

The spirit was to have GetSigningCredentialsAsync call RefreshAsync even if there was no background job calling it -- so this change makes it call it at the same cadence/time we expect a background service to.

This hasn't been a problem in production for various reasons but it was for a specific use-case in the dev environment, but regardless its a bug.

Only refreshing after the key is expired will cause us to sign tokens
that might not validate for their entire lifetime.

The spirit was to have GetSigningCredentialsAsync call RefreshAsync even
if there was no background job calling it -- so this change makes it
call it at the same cadence/time we expect a background service to.

This hasn't been a problem in production for various reasons but it was
for a specific use-case in the dev environment, but regardless its a
bug.
@j3parker
j3parker marked this pull request as ready for review February 7, 2024 18:10
@j3parker
j3parker requested a review from omsmith as a code owner February 7, 2024 18:10
Comment thread src/D2L.Security.OAuth2/Keys/KeyManagementService.cs Outdated
// do a foreground Refresh
private static readonly TimeSpan GetSigningCredentialsRefreshGracePeriod
= BackgroundRefreshDelay
+ BackgroundRefreshRetryDelay + BackgroundRefreshRetryDelay;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

can't do 2* 😒


if ( current == null || current.ValidTo <= now ) {
if ( current == null
|| ExpectedTimeOfNewUsableKey( current ) + GetSigningCredentialsRefreshGracePeriod < now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The implication here is that once this period is passed every call to GetSigningCredentials is going to fight to update that key.

That basically matches how it worked before the background refresh anyway. The only things without background refresh for us are Lambda functions though, which are single threaded anyway.

) {
// Slow path: RefreshKeyAsync() wasn't called on boot and/or it
// isn't being called in a background job.
await RefreshKeyAsync( now )

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i thought about listening to the response from RefreshKeyAsync here, but the approach I went with was "stateless" and doesn't require having previously called RefreshKeyAsync 🤷 the logic is all in this file and shared so it doesn't seem too bad to me?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If key rotation fails for 3 minutes I think the results are pretty bad here? Taking a look now at whether dev key rotation ever gets delayed...

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

dev dynamo metrics show writes every 3 hours in the first 5 minutes of the hour steady for the past two months. I think we're still in pretty rough shape if cert generation gets delayed, but hopefully that won't happen too often.

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.

3 participants