-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
token refresh offset #12136
Merged
Merged
token refresh offset #12136
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
dc1a9b2
token refresh offset
xiangyan99 bd7ae61
update
xiangyan99 6a58f5c
update
xiangyan99 2a94b3d
rename is_refresh to should_refresh
xiangyan99 51fbed8
update
xiangyan99 9ed71f1
update
xiangyan99 618c226
Merge branch 'master' into identity_token_refresh_offset
xiangyan99 e6ab928
updates
xiangyan99 e235ad2
updates
xiangyan99 6d51768
updates
xiangyan99 beb9027
updates
xiangyan99 08fa31e
expose token_refresh_offset
xiangyan99 2bec84f
update
xiangyan99 66fe30b
pylint fix
xiangyan99 b2b4b43
updates
xiangyan99 4eb71a0
pylint fix
xiangyan99 e060268
update
xiangyan99 0d9e63e
use option bags
xiangyan99 9ba3910
pylint fix
xiangyan99 10499c4
updates
xiangyan99 d93fdba
updates
xiangyan99 78518cd
updates
xiangyan99 82ad53a
updates
xiangyan99 429ca27
update
xiangyan99 99e8921
updates
xiangyan99 a57f7fa
updates
xiangyan99 e08692f
updates
xiangyan99 101fa22
updates
xiangyan99 a38294a
updates
xiangyan99 173e26d
update
xiangyan99 533ab8c
update
xiangyan99 b6e1cc7
update
xiangyan99 04dad9d
updates
xiangyan99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
token refresh offset
- Loading branch information
commit dc1a9b2687521cad623e6044bbf0d7f940d63b2b
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
sdk/identity/azure-identity/tests/test_token_refresh_offset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
import time | ||
from azure.identity._internal.aad_client import AadClient | ||
from azure.core.credentials import AccessToken | ||
import pytest | ||
|
||
try: | ||
from unittest import mock | ||
except ImportError: # python < 3.3 | ||
import mock | ||
|
||
|
||
def test_if_refresh(): | ||
client = AadClient("test", "test") | ||
xiangyan99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
now = int(time.time()) | ||
|
||
# do not need refresh | ||
token = AccessToken("token", now + 500) | ||
is_refresh = client.is_refresh(token) | ||
assert not is_refresh | ||
|
||
# need refresh | ||
token = AccessToken("token", now + 100) | ||
client._last_refresh_time = now - 500 | ||
is_refresh = client.is_refresh(token) | ||
assert is_refresh | ||
|
||
# not exceed cool down time, do not refresh | ||
token = AccessToken("token", now + 100) | ||
client._last_refresh_time = now - 5 | ||
is_refresh = client.is_refresh(token) | ||
assert not is_refresh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
or is there some rationale for always using 30 seconds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not _token_refresh_timeout.
We don't have a clear design for this value but it must be less than _token_refresh_offset (default to 120). Or it will hide the auto refresh feature.
The old one 300 does not meet the requirement so I updated it to 30.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether we need an explicit margin here. The 1s margin in
if expires_on > int(time.time())
seems okay to me. My reasoning:token_refresh_offset=300
None
, prompting the caller to acquire a new tokentoken_refresh_offset
will now be observed by callers of this methodOne bad outcome that could follow is the caller using a token that expires in flight. That request will fail, but the caller's other option was to raise without sending the request at all, because it couldn't acquire a new token. It seems better to try the request, which could after all succeed.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is when it is still in token_refresh_retry_timeout time frame.
Extreme case: user gets a token from us which expires in 1s. It is still in token_refresh_retry_timeout time frame so it does not get refreshed.
vs
They get None from us so it forces a refresh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if the credential is waiting on the retry timeout, it won't try to get a new token, regardless of what it gets back from the cache. Returning
None
in that case only guarantees the current request will fail, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. if there is no valid token (it returns None), no matter it is in retry timeout window or not, we will try to get one.
Retry timeout only applies to there is A valid token but it is within the refresh offset window.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I overlooked this behavior. Credentials should observe the retry timeout when the cache is empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is the behavior we want. If we have no access_token and the first attempt to get one failed, do we really want to hold all requests for 30 seconds before attempting to get one? I think we need to clarify this more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My opinion is if there is no one available, every time user calls our library to get one, we will try it w/o a cool down time.