Skip to content

Commit 75c742f

Browse files
committed
managed-identity-id
1 parent 1c6b3cc commit 75c742f

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060

6161
_AZ_LOGIN_MESSAGE = "Please run 'az login' to setup account."
6262

63+
MANAGED_IDENTITY_ID_WARNING = (
64+
"Passing the managed identity ID with --username is deprecated and will be removed in a future release. "
65+
"Please use --client-id, --object-id or --resource-id instead."
66+
)
67+
6368

6469
def load_subscriptions(cli_ctx, all_clouds=False, refresh=False):
6570
profile = Profile(cli_ctx=cli_ctx)
@@ -219,7 +224,8 @@ def login(self,
219224
self._set_subscriptions(consolidated)
220225
return deepcopy(consolidated)
221226

222-
def login_with_managed_identity(self, identity_id=None, allow_no_subscriptions=None):
227+
def login_with_managed_identity(self, identity_id=None, client_id=None, object_id=None, resource_id=None,
228+
allow_no_subscriptions=None):
223229
if _on_azure_arc():
224230
return self.login_with_managed_identity_azure_arc(
225231
identity_id=identity_id, allow_no_subscriptions=allow_no_subscriptions)
@@ -229,7 +235,24 @@ def login_with_managed_identity(self, identity_id=None, allow_no_subscriptions=N
229235
from azure.cli.core.auth.adal_authentication import MSIAuthenticationWrapper
230236
resource = self.cli_ctx.cloud.endpoints.active_directory_resource_id
231237

232-
if identity_id:
238+
if not any((identity_id, client_id, object_id, resource_id)):
239+
identity_type = MsiAccountTypes.system_assigned
240+
msi_creds = MSIAuthenticationWrapper(resource=resource)
241+
elif client_id:
242+
identity_type = MsiAccountTypes.user_assigned_client_id
243+
identity_id = client_id
244+
msi_creds = MSIAuthenticationWrapper(resource=resource, client_id=client_id)
245+
elif object_id:
246+
identity_type = MsiAccountTypes.user_assigned_object_id
247+
identity_id = object_id
248+
msi_creds = MSIAuthenticationWrapper(resource=resource, object_id=object_id)
249+
elif resource_id:
250+
identity_type = MsiAccountTypes.user_assigned_resource_id
251+
identity_id = resource_id
252+
msi_creds = MSIAuthenticationWrapper(resource=resource, msi_res_id=resource_id)
253+
# The old way of re-using the same --username for 3 types of ID
254+
elif identity_id:
255+
logger.warning(MANAGED_IDENTITY_ID_WARNING)
233256
if is_valid_resource_id(identity_id):
234257
msi_creds = MSIAuthenticationWrapper(resource=resource, msi_res_id=identity_id)
235258
identity_type = MsiAccountTypes.user_assigned_resource_id
@@ -260,10 +283,6 @@ def login_with_managed_identity(self, identity_id=None, allow_no_subscriptions=N
260283
if not authenticated:
261284
raise CLIError('Failed to connect to MSI, check your managed service identity id.')
262285

263-
else:
264-
identity_type = MsiAccountTypes.system_assigned
265-
msi_creds = MSIAuthenticationWrapper(resource=resource)
266-
267286
token_entry = msi_creds.token
268287
token = token_entry['access_token']
269288
logger.info('MSI: token was retrieved. Now trying to initialize local accounts...')

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def load_arguments(self, command):
7575
# Managed identity
7676
c.argument('identity', options_list=('-i', '--identity'), action='store_true',
7777
help="Log in using managed identity", arg_group='Managed Identity')
78+
c.argument('client_id',
79+
help="Client ID of the user-assigned managed identity", arg_group='Managed Identity')
80+
c.argument('object_id',
81+
help="Object ID of the user-assigned managed identity", arg_group='Managed Identity')
82+
c.argument('resource_id',
83+
help="Resource ID of the user-assigned managed identity", arg_group='Managed Identity')
7884

7985
with self.argument_context('logout') as c:
8086
c.argument('username', help='account user, if missing, logout the current active account')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
text: az login --service-principal --username APP_ID --certificate /path/to/cert.pem --tenant TENANT_ID
4444
- name: Log in with a system-assigned managed identity.
4545
text: az login --identity
46-
- name: Log in with a user-assigned managed identity. You must specify the client ID, object ID or resource ID of the user-assigned managed identity with --username.
47-
text: az login --identity --username 00000000-0000-0000-0000-000000000000
46+
- name: Log in with a user-assigned managed identity's client ID.
47+
text: az login --identity --client-id 00000000-0000-0000-0000-000000000000
4848
"""
4949

5050
helps['account'] = """

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
121121
# Service principal
122122
service_principal=None, certificate=None, use_cert_sn_issuer=None, client_assertion=None,
123123
# Managed identity
124-
identity=False):
124+
identity=False, client_id=None, object_id=None, resource_id=None):
125125
"""Log in to access Azure subscriptions"""
126126

127127
# quick argument usage check
@@ -143,7 +143,9 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
143143
if identity:
144144
if in_cloud_console():
145145
return profile.login_in_cloud_shell()
146-
return profile.login_with_managed_identity(username, allow_no_subscriptions)
146+
return profile.login_with_managed_identity(
147+
identity_id=username, client_id=client_id, object_id=object_id, resource_id=resource_id,
148+
allow_no_subscriptions=allow_no_subscriptions)
147149
if in_cloud_console(): # tell users they might not need login
148150
logger.warning(_CLOUD_CONSOLE_LOGIN_WARNING)
149151

0 commit comments

Comments
 (0)