-
Notifications
You must be signed in to change notification settings - Fork 3k
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
{Profile} az account get-access-token
: Show expiresOn
for managed identity
#20219
Conversation
Profile |
@@ -78,7 +78,7 @@ def get_access_token(cmd, subscription=None, resource=None, scopes=None, resourc | |||
'tokenType': creds[0], | |||
'accessToken': creds[1], | |||
# 'expires_on': creds[2].get('expires_on', None), | |||
'expiresOn': creds[2].get('expiresOn', None), |
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.
Why do you need to change this line?
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.
We need to guarantee that expiresOn
always exists in creds[2]
- CLI should fail if expiresOn
is not set, instead of returning None
which will cause more trouble.
from .auth.util import scopes_to_resource | ||
msi_creds = MsiAccountTypes.msi_auth_factory(identity_type, identity_id, | ||
scopes_to_resource(scopes)) | ||
sdk_token = msi_creds.get_token(*scopes) |
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 old implementation of ADAL-based Azure CLI returns token_entry
containing inconsistent fields:
expires_on
for managed identityexpiresOn
for ADAL credential
They are unified later at command module level:
azure-cli/src/azure-cli/azure/cli/command_modules/profile/custom.py
Lines 82 to 85 in df737ed
if 'expires_on' in token_entry: | |
# https://docs.python.org/3.8/library/datetime.html#strftime-and-strptime-format-codes | |
token_entry['expiresOn'] = _fromtimestamp(int(token_entry['expires_on']))\ | |
.strftime("%Y-%m-%d %H:%M:%S.%f") |
We use get_token
to unify them to epoch int expires_on
in core instead.
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.
Just out of curiosity, will token_entry['expiresOn']
has its own value in some cases before it is overwritten by _fromtimestamp(int(token_entry['expires_on'])).strftime("%Y-%m-%d %H:%M:%S.%f")
?
May I ask their values should be the same in all cases, but only in different formats, right?
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. get_token
only returns AccessToken
which must have int epoch expires_on
.
any update on when this issue will be merged and released? getting similar issue when trying to use azure app service with custom container. |
az account get-access-token
show expiresOn
for managed identityaz account get-access-token
: Show expiresOn
for managed identity
@waylew-lexis your issue has been resolved by #20215 and will be released in Azure CLI 2.31.0. |
# Conflicts: # src/azure-cli-core/azure/cli/core/tests/test_profile.py
@@ -63,7 +61,6 @@ def __init__(self, *args, **kwargs): | |||
self.object_id = kwargs.get('object_id') | |||
self.msi_res_id = kwargs.get('msi_res_id') | |||
self.resource = kwargs.get('resource') | |||
MSRestAzureAuthStub.return_value = self |
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.
Don't save the instance as class attribute so that tests won't interfere with each other.
msi_creds.set_token() | ||
token_entry = msi_creds.token | ||
creds = (token_entry['token_type'], token_entry['access_token'], token_entry) | ||
raise CLIError("Tenant shouldn't be specified for managed identity account") |
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.
Could we use a specific error type (such as ArgumentUsageError
) instead of CLIError
?
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.
There are so many places in core that don't comply with the error handling rule. Let's refine them together later.
Hello, do you know when this merged modification will be integrated to a release ?
That should be fixed with this modification ;) Regards Marc. |
Build to Cloud Shell: 12/03/2021 Official Release: 12/07/2021 |
Fix #20211
Fix getporter/azure-plugins#39
Fix microsoft/AzureTRE#1067
Fix Azure/azure-cli-extensions#4076
Description
Issue: During the migration to MSAL, for managed identity, the
expiresOn
property inaz account get-access-token
's output is lost.This is due to the complexity and inconsistency of
expiresOn
/expires_on
properties across different services and tools.There are 4 forms of
expiresOn
/expires_on
:"expires_on": "1605238724"
"expires_on": 1605238724
"expiresOn": "2020-11-12 13:50:47.114324"
"expires_on": "11/05/2021 15:18:31 +00:00"
This PR unified these 4 types:
expires_on
is returned.expires_on
is unified to epoch int ([App Service] Support managed identity in App Service container #20215).get_raw_token
returnsexpiresOn
: datetime string for backward compatibility, like"2020-11-12 13:50:47.114324"
expires_on
: epoch int, like1605238724
(az account get-access-token
: Use epochexpiresOn
/expires_on
#19700)Testing Guide