-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[Core] Fix get_token() issue in msi login and expiresIn key error in cloud shell login credentials for track 2 SDK related commands
#14187
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
Changes from all commits
d8a71bf
7acdefa
e9afbbf
9f2be7a
87aa599
83c1654
141f00d
6c41d3c
b93e244
0f54593
f95b978
d872b65
936ab6d
e9f5441
e751ece
55b350a
41b2ae1
4cf1fcb
5bdfe74
8a5985e
2ab696b
00e01aa
6085e2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import adal | ||
|
|
||
| from msrest.authentication import Authentication | ||
| from msrestazure.azure_active_directory import MSIAuthentication | ||
| from azure.core.credentials import AccessToken | ||
| from azure.cli.core.util import in_cloud_console | ||
|
|
||
|
|
@@ -60,8 +61,10 @@ def _get_token(self): | |
| # This method is exposed for Azure Core. | ||
| def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument | ||
| _, token, full_token, _ = self._get_token() | ||
|
|
||
| return AccessToken(token, int(full_token['expiresIn'] + time.time())) | ||
| try: | ||
| return AccessToken(token, int(full_token['expiresIn'] + time.time())) | ||
| except KeyError: # needed to deal with differing unserialized MSI token payload | ||
| return AccessToken(token, int(full_token['expires_on'])) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is for cloud shell? If yes, can we add comments so that others can understand the purpose?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
|
|
||
| # This method is exposed for msrest. | ||
| def signed_session(self, session=None): # pylint: disable=arguments-differ | ||
|
|
@@ -83,3 +86,10 @@ def _log_hostname(): | |
| logger = get_logger(__name__) | ||
| logger.warning("A Cloud Shell credential problem occurred. When you report the issue with the error " | ||
| "below, please mention the hostname '%s'", socket.gethostname()) | ||
|
|
||
|
|
||
| class MSIAuthenticationWrapper(MSIAuthentication): | ||
| # This method is exposed for Azure Core. | ||
| def get_token(self): | ||
| self.set_token() | ||
| return AccessToken(self.token['access_token'], int(self.token['expires_on'])) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -645,7 +645,7 @@ def test_get_login_credentials_aux_tenants(self, mock_get_token, mock_read_cred_ | |
| aux_tenants=[test_tenant_id2]) | ||
|
|
||
| @mock.patch('azure.cli.core._profile._load_tokens_from_file', autospec=True) | ||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change necessary? Won't it be nice to have
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| def test_get_login_credentials_msi_system_assigned(self, mock_msi_auth, mock_read_cred_file): | ||
| mock_read_cred_file.return_value = [] | ||
|
|
||
|
|
@@ -676,7 +676,7 @@ def test_get_login_credentials_msi_system_assigned(self, mock_msi_auth, mock_rea | |
| self.assertTrue(cred.token_read_count) | ||
|
|
||
| @mock.patch('azure.cli.core._profile._load_tokens_from_file', autospec=True) | ||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| def test_get_login_credentials_msi_user_assigned_with_client_id(self, mock_msi_auth, mock_read_cred_file): | ||
| mock_read_cred_file.return_value = [] | ||
|
|
||
|
|
@@ -707,7 +707,7 @@ def test_get_login_credentials_msi_user_assigned_with_client_id(self, mock_msi_a | |
| self.assertTrue(cred.client_id, test_client_id) | ||
|
|
||
| @mock.patch('azure.cli.core._profile._load_tokens_from_file', autospec=True) | ||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| def test_get_login_credentials_msi_user_assigned_with_object_id(self, mock_msi_auth, mock_read_cred_file): | ||
| mock_read_cred_file.return_value = [] | ||
|
|
||
|
|
@@ -738,7 +738,7 @@ def test_get_login_credentials_msi_user_assigned_with_object_id(self, mock_msi_a | |
| self.assertTrue(cred.object_id, test_object_id) | ||
|
|
||
| @mock.patch('azure.cli.core._profile._load_tokens_from_file', autospec=True) | ||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| def test_get_login_credentials_msi_user_assigned_with_res_id(self, mock_msi_auth, mock_read_cred_file): | ||
| mock_read_cred_file.return_value = [] | ||
|
|
||
|
|
@@ -849,7 +849,7 @@ def test_get_raw_token_for_sp(self, mock_get_token, mock_read_cred_file): | |
| self.assertEqual(tenant, self.tenant_id) | ||
|
|
||
| @mock.patch('azure.cli.core._profile._load_tokens_from_file', autospec=True) | ||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| def test_get_raw_token_msi_system_assigned(self, mock_msi_auth, mock_read_cred_file): | ||
| mock_read_cred_file.return_value = [] | ||
|
|
||
|
|
@@ -884,7 +884,7 @@ def test_get_raw_token_msi_system_assigned(self, mock_msi_auth, mock_read_cred_f | |
|
|
||
| @mock.patch('azure.cli.core._profile.in_cloud_console', autospec=True) | ||
| @mock.patch('azure.cli.core._profile._load_tokens_from_file', autospec=True) | ||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| def test_get_raw_token_in_cloud_console(self, mock_msi_auth, mock_read_cred_file, mock_in_cloud_console): | ||
| mock_read_cred_file.return_value = [] | ||
| mock_in_cloud_console.return_value = True | ||
|
|
@@ -1037,7 +1037,7 @@ def test_find_subscriptions_thru_username_non_password(self, mock_auth_context): | |
| # assert | ||
| self.assertEqual([], subs) | ||
|
|
||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| @mock.patch('azure.cli.core.profiles._shared.get_client_class', autospec=True) | ||
| @mock.patch('azure.cli.core._profile._get_cloud_console_token_endpoint', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.SubscriptionFinder', autospec=True) | ||
|
|
@@ -1186,7 +1186,7 @@ def __init__(self, *args, **kwargs): | |
| self.assertEqual(s['id'], self.id1.split('/')[-1]) | ||
| self.assertEqual(s['tenantId'], '54826b22-38d6-4fb2-bad9-b7b93a3e9c5a') | ||
|
|
||
| @mock.patch('msrestazure.azure_active_directory.MSIAuthentication', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.MSIAuthenticationWrapper', autospec=True) | ||
| @mock.patch('azure.cli.core.profiles._shared.get_client_class', autospec=True) | ||
| @mock.patch('azure.cli.core._profile.SubscriptionFinder', autospec=True) | ||
| def test_find_subscriptions_in_vm_with_msi_user_assigned_with_object_id(self, mock_subscription_finder, mock_get_client_class, | ||
|
|
||
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.
Another problem with
mock.patchis for this form of import, it won't work. After importingMSIAuthenticationWrapper, it becomes a local referenceazure.cli.core._profile.MSIAuthenticationWrapper. See Where to patch.