Skip to content

Commit cd00c8a

Browse files
authored
[Profile] BREAKING CHANGE: az login: Drop --username for managed identity authentication (#31015)
1 parent ebf6ddf commit cd00c8a

File tree

4 files changed

+6
-69
lines changed

4 files changed

+6
-69
lines changed

src/azure-cli-core/azure/cli/core/_profile.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -220,27 +220,24 @@ def login(self,
220220
self._set_subscriptions(consolidated)
221221
return deepcopy(consolidated)
222222

223-
def login_with_managed_identity(self, identity_id=None, client_id=None, object_id=None, resource_id=None,
223+
def login_with_managed_identity(self, client_id=None, object_id=None, resource_id=None,
224224
allow_no_subscriptions=None):
225225
if _use_msal_managed_identity(self.cli_ctx):
226-
if identity_id:
227-
raise CLIError('--username is not supported by MSAL managed identity. '
228-
'Use --client-id, --object-id or --resource-id instead.')
229226
return self.login_with_managed_identity_msal(
230227
client_id=client_id, object_id=object_id, resource_id=resource_id,
231228
allow_no_subscriptions=allow_no_subscriptions)
232229

233230
import jwt
234-
from azure.mgmt.core.tools import is_valid_resource_id
235231
from azure.cli.core.auth.adal_authentication import MSIAuthenticationWrapper
236232
resource = self.cli_ctx.cloud.endpoints.active_directory_resource_id
237233

238-
id_arg_count = len([arg for arg in (client_id, object_id, resource_id, identity_id) if arg])
234+
id_arg_count = len([arg for arg in (client_id, object_id, resource_id) if arg])
239235
if id_arg_count > 1:
240-
raise CLIError('Usage error: Provide only one of --client-id, --object-id, --resource-id, or --username.')
236+
raise CLIError('Usage error: Provide only one of --client-id, --object-id, --resource-id.')
241237

242238
if id_arg_count == 0:
243239
identity_type = MsiAccountTypes.system_assigned
240+
identity_id = None
244241
msi_creds = MSIAuthenticationWrapper(resource=resource)
245242
elif client_id:
246243
identity_type = MsiAccountTypes.user_assigned_client_id
@@ -254,37 +251,6 @@ def login_with_managed_identity(self, identity_id=None, client_id=None, object_i
254251
identity_type = MsiAccountTypes.user_assigned_resource_id
255252
identity_id = resource_id
256253
msi_creds = MSIAuthenticationWrapper(resource=resource, msi_res_id=resource_id)
257-
# The old way of re-using the same --username for 3 types of ID
258-
elif identity_id:
259-
if is_valid_resource_id(identity_id):
260-
msi_creds = MSIAuthenticationWrapper(resource=resource, msi_res_id=identity_id)
261-
identity_type = MsiAccountTypes.user_assigned_resource_id
262-
else:
263-
authenticated = False
264-
from azure.cli.core.azclierror import AzureResponseError
265-
try:
266-
msi_creds = MSIAuthenticationWrapper(resource=resource, client_id=identity_id)
267-
identity_type = MsiAccountTypes.user_assigned_client_id
268-
authenticated = True
269-
except AzureResponseError as ex:
270-
if 'http error: 400, reason: Bad Request' in ex.error_msg:
271-
logger.info('Sniff: not an MSI client id')
272-
else:
273-
raise
274-
275-
if not authenticated:
276-
try:
277-
identity_type = MsiAccountTypes.user_assigned_object_id
278-
msi_creds = MSIAuthenticationWrapper(resource=resource, object_id=identity_id)
279-
authenticated = True
280-
except AzureResponseError as ex:
281-
if 'http error: 400, reason: Bad Request' in ex.error_msg:
282-
logger.info('Sniff: not an MSI object id')
283-
else:
284-
raise
285-
286-
if not authenticated:
287-
raise CLIError('Failed to connect to MSI, check your managed service identity id.')
288254

289255
token_entry = msi_creds.token
290256
token = token_entry['access_token']

src/azure-cli-core/azure/cli/core/tests/test_profile.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -636,19 +636,6 @@ def test_login_with_mi_user_assigned_client_id(self, create_subscription_client_
636636
self.assertEqual(s['user']['type'], 'servicePrincipal')
637637
self.assertEqual(s['user']['assignedIdentityInfo'], 'MSIClient-{}'.format(test_client_id))
638638

639-
# Old way of using identity_id
640-
subscriptions = profile.login_with_managed_identity(identity_id=test_client_id)
641-
642-
self.assertEqual(len(subscriptions), 1)
643-
s = subscriptions[0]
644-
self.assertEqual(s['name'], self.display_name1)
645-
self.assertEqual(s['id'], self.id1.split('/')[-1])
646-
self.assertEqual(s['tenantId'], self.test_mi_tenant)
647-
648-
self.assertEqual(s['user']['name'], 'userAssignedIdentity')
649-
self.assertEqual(s['user']['type'], 'servicePrincipal')
650-
self.assertEqual(s['user']['assignedIdentityInfo'], 'MSIClient-{}'.format(test_client_id))
651-
652639
@mock.patch('azure.cli.core.auth.adal_authentication.MSIAuthenticationWrapper', autospec=True)
653640
@mock.patch('azure.cli.core._profile.SubscriptionFinder._create_subscription_client', autospec=True)
654641
def test_login_with_mi_user_assigned_object_id(self, create_subscription_client_mock,
@@ -689,14 +676,6 @@ def set_token(self):
689676
self.assertEqual(s['user']['type'], 'servicePrincipal')
690677
self.assertEqual(s['user']['assignedIdentityInfo'], 'MSIObject-{}'.format(test_object_id))
691678

692-
# Old way of using identity_id
693-
subscriptions = profile.login_with_managed_identity(identity_id=test_object_id)
694-
695-
s = subscriptions[0]
696-
self.assertEqual(s['user']['name'], 'userAssignedIdentity')
697-
self.assertEqual(s['user']['type'], 'servicePrincipal')
698-
self.assertEqual(s['user']['assignedIdentityInfo'], 'MSIObject-{}'.format(test_object_id))
699-
700679
@mock.patch('requests.get', autospec=True)
701680
@mock.patch('azure.cli.core._profile.SubscriptionFinder._create_subscription_client', autospec=True)
702681
def test_login_with_mi_user_assigned_resource_id(self, create_subscription_client_mock,
@@ -730,14 +709,6 @@ def test_login_with_mi_user_assigned_resource_id(self, create_subscription_clien
730709
self.assertEqual(s['user']['type'], 'servicePrincipal')
731710
self.assertEqual(subscriptions[0]['user']['assignedIdentityInfo'], 'MSIResource-{}'.format(test_res_id))
732711

733-
# Old way of using identity_id
734-
subscriptions = profile.login_with_managed_identity(identity_id=test_res_id)
735-
736-
s = subscriptions[0]
737-
self.assertEqual(s['user']['name'], 'userAssignedIdentity')
738-
self.assertEqual(s['user']['type'], 'servicePrincipal')
739-
self.assertEqual(subscriptions[0]['user']['assignedIdentityInfo'], 'MSIResource-{}'.format(test_res_id))
740-
741712
@mock.patch('azure.cli.core._profile.SubscriptionFinder._create_subscription_client', autospec=True)
742713
@mock.patch('azure.cli.core.auth.msal_credentials.ManagedIdentityCredential', ManagedIdentityCredentialStub)
743714
@mock.patch.dict('os.environ', {'AZURE_CORE_USE_MSAL_MANAGED_IDENTITY': 'true'})

src/azure-cli/azure/cli/command_modules/profile/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def load_arguments(self, command):
4545

4646
with self.argument_context('login') as c:
4747
c.argument('username', options_list=['--username', '-u'],
48-
help='User name, service principal client ID, or managed identity ID.')
48+
help='User name or service principal client ID.')
4949
c.argument('password', options_list=['--password', '-p'],
5050
help='User password or service principal secret. Will prompt if not given.')
5151
c.argument('tenant', options_list=['--tenant', '-t'], validator=validate_tenant,

src/azure-cli/azure/cli/command_modules/profile/custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
156156
from azure.cli.core.breaking_change import print_conditional_breaking_change
157157
print_conditional_breaking_change(cmd.cli_ctx, tag='ManagedIdentityUsernameBreakingChange')
158158
return profile.login_with_managed_identity(
159-
identity_id=username, client_id=client_id, object_id=object_id, resource_id=resource_id,
159+
client_id=client_id, object_id=object_id, resource_id=resource_id,
160160
allow_no_subscriptions=allow_no_subscriptions)
161161
if in_cloud_console(): # tell users they might not need login
162162
logger.warning(_CLOUD_CONSOLE_LOGIN_WARNING)

0 commit comments

Comments
 (0)