@@ -220,6 +220,10 @@ def login(self,
220220 return deepcopy (consolidated )
221221
222222 def login_with_managed_identity (self , identity_id = None , allow_no_subscriptions = None ):
223+ if _use_msal_for_managed_identity ():
224+ return self .login_with_managed_identity_msal (identity_id = identity_id ,
225+ allow_no_subscriptions = allow_no_subscriptions )
226+
223227 import jwt
224228 from azure .mgmt .core .tools import is_valid_resource_id
225229 from azure .cli .core .auth .adal_authentication import MSIAuthenticationWrapper
@@ -282,6 +286,34 @@ def login_with_managed_identity(self, identity_id=None, allow_no_subscriptions=N
282286 self ._set_subscriptions (consolidated )
283287 return deepcopy (consolidated )
284288
289+ def login_with_managed_identity_msal (self , identity_id = None , allow_no_subscriptions = None ):
290+ import jwt
291+ # TODO: Support user-assigned identity
292+ identity_type = MsiAccountTypes .system_assigned
293+ from .auth .msal_credentials import ManagedIdentityCredential
294+
295+ cred = ManagedIdentityCredential ()
296+ token = cred .get_token (* self ._arm_scope ).token
297+ logger .info ('Managed identity: token was retrieved. Now trying to initialize local accounts...' )
298+ decode = jwt .decode (token , algorithms = ['RS256' ], options = {"verify_signature" : False })
299+ tenant = decode ['tid' ]
300+
301+ subscription_finder = SubscriptionFinder (self .cli_ctx )
302+ subscriptions = subscription_finder .find_using_specific_tenant (tenant , cred )
303+ base_name = ('{}-{}' .format (identity_type , identity_id ) if identity_id else identity_type )
304+ user = _USER_ASSIGNED_IDENTITY if identity_id else _SYSTEM_ASSIGNED_IDENTITY
305+ if not subscriptions :
306+ if allow_no_subscriptions :
307+ subscriptions = self ._build_tenant_level_accounts ([tenant ])
308+ else :
309+ raise CLIError ('No access was configured for the managed identity, hence no subscriptions were found. '
310+ "If this is expected, use '--allow-no-subscriptions' to have tenant level access." )
311+
312+ consolidated = self ._normalize_properties (user , subscriptions , is_service_principal = True ,
313+ user_assigned_identity_id = base_name )
314+ self ._set_subscriptions (consolidated )
315+ return deepcopy (consolidated )
316+
285317 def login_in_cloud_shell (self ):
286318 import jwt
287319 from .auth .msal_credentials import CloudShellCredential
@@ -354,13 +386,18 @@ def get_login_credentials(self, resource=None, client_id=None, subscription_id=N
354386 # Cloud Shell
355387 from .auth .msal_credentials import CloudShellCredential
356388 from azure .cli .core .auth .credential_adaptor import CredentialAdaptor
357- cs_cred = CloudShellCredential ()
358- # The cloud shell credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
359- cred = CredentialAdaptor (cs_cred , resource = resource )
389+ # The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
390+ cred = CredentialAdaptor (CloudShellCredential (), resource = resource )
360391
361392 elif managed_identity_type :
362393 # managed identity
363- cred = MsiAccountTypes .msi_auth_factory (managed_identity_type , managed_identity_id , resource )
394+ if _use_msal_for_managed_identity ():
395+ from .auth .msal_credentials import ManagedIdentityCredential
396+ from azure .cli .core .auth .credential_adaptor import CredentialAdaptor
397+ # The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
398+ cred = CredentialAdaptor (ManagedIdentityCredential (), resource = resource )
399+ else :
400+ cred = MsiAccountTypes .msi_auth_factory (managed_identity_type , managed_identity_id , resource )
364401
365402 else :
366403 # user and service principal
@@ -415,9 +452,13 @@ def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=No
415452 # managed identity
416453 if tenant :
417454 raise CLIError ("Tenant shouldn't be specified for managed identity account" )
418- from .auth .util import scopes_to_resource
419- cred = MsiAccountTypes .msi_auth_factory (managed_identity_type , managed_identity_id ,
420- scopes_to_resource (scopes ))
455+ if _use_msal_for_managed_identity ():
456+ from .auth .msal_credentials import ManagedIdentityCredential
457+ cred = ManagedIdentityCredential ()
458+ else :
459+ from .auth .util import scopes_to_resource
460+ cred = MsiAccountTypes .msi_auth_factory (managed_identity_type , managed_identity_id ,
461+ scopes_to_resource (scopes ))
421462
422463 else :
423464 cred = self ._create_credential (account , tenant )
@@ -918,3 +959,8 @@ def _create_identity_instance(cli_ctx, *args, **kwargs):
918959 return Identity (* args , encrypt = encrypt , use_msal_http_cache = use_msal_http_cache ,
919960 enable_broker_on_windows = enable_broker_on_windows ,
920961 instance_discovery = instance_discovery , ** kwargs )
962+
963+
964+ def _use_msal_for_managed_identity ():
965+ from msal .managed_identity import get_managed_identity_source , AZURE_ARC
966+ return get_managed_identity_source () == AZURE_ARC
0 commit comments