Skip to content

Commit e4057a4

Browse files
committed
Add object_id, mi_res_id support
1 parent 2bd068f commit e4057a4

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

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

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,45 @@ def login_with_managed_identity(self, identity_id=None, allow_no_subscriptions=N
196196
from azure.cli.core.auth.msal_authentication import ManagedIdentityCredential
197197
import jwt
198198

199-
# TODO: support object_id and msi_res_id like the old days
199+
cred = None
200+
200201
if identity_id:
201-
cred = ManagedIdentityCredential(client_id=identity_id)
202-
identity_type = ManagedIdentityAuth.user_assigned_client_id
202+
from azure.mgmt.core.tools import is_valid_resource_id
203+
if is_valid_resource_id(identity_id):
204+
cred = ManagedIdentityCredential(mi_res_id=identity_id)
205+
identity_type = ManagedIdentityAuth.id_type_resource_id
206+
else:
207+
authenticated = False
208+
# Use trial and error approach to determine the ID type - client ID or object ID
209+
from azure.cli.core.azclierror import AuthenticationError
210+
try:
211+
cred = ManagedIdentityCredential(client_id=identity_id)
212+
cred.get_token(*self._arm_scope)
213+
identity_type = ManagedIdentityAuth.id_type_client_id
214+
authenticated = True
215+
except AuthenticationError as ex:
216+
if 'Identity not found' in str(ex):
217+
logger.info('Sniff: not a client ID')
218+
else:
219+
raise
220+
221+
if not authenticated:
222+
try:
223+
cred = ManagedIdentityCredential(object_id=identity_id)
224+
cred.get_token(*self._arm_scope)
225+
identity_type = ManagedIdentityAuth.id_type_object_id
226+
authenticated = True
227+
except AuthenticationError as ex:
228+
if 'Identity not found' in str(ex):
229+
logger.info('Sniff: not an object ID')
230+
else:
231+
raise
232+
233+
if not authenticated:
234+
raise CLIError('Failed to connect to managed identity, check your managed identity ID.')
203235
else:
204236
cred = ManagedIdentityCredential()
205-
identity_type = ManagedIdentityAuth.system_assigned
237+
identity_type = ManagedIdentityAuth.id_type_no_id
206238

207239
access_token = cred.get_token(*self._arm_scope)
208240

@@ -683,29 +715,26 @@ def get_installation_id(self):
683715

684716

685717
class ManagedIdentityAuth:
686-
# pylint: disable=no-method-argument,no-self-argument
687-
system_assigned = 'MSI' # Not necessarily system-assigned. It merely means no ID is provided.
688-
user_assigned_client_id = 'MSIClient'
689-
user_assigned_object_id = 'MSIObject'
690-
user_assigned_resource_id = 'MSIResource'
691718

692-
@staticmethod
693-
def valid_account_types():
694-
return [ManagedIdentityAuth.system_assigned, ManagedIdentityAuth.user_assigned_client_id,
695-
ManagedIdentityAuth.user_assigned_object_id, ManagedIdentityAuth.user_assigned_resource_id]
719+
# String constants defined in this class are saved to azureProfile.json, so this class shouldn't be put
720+
# under auth/identity.py
721+
id_type_no_id = 'MSI' # Not necessarily system-assigned. It merely means no ID is provided.
722+
id_type_client_id = 'MSIClient'
723+
id_type_object_id = 'MSIObject'
724+
id_type_resource_id = 'MSIResource'
696725

697726
@staticmethod
698727
def credential_factory(identity_type, identity_id):
699728
from azure.cli.core.auth.msal_authentication import ManagedIdentityCredential
700-
if identity_type == ManagedIdentityAuth.system_assigned:
729+
if identity_type == ManagedIdentityAuth.id_type_no_id:
701730
return ManagedIdentityCredential()
702-
if identity_type == ManagedIdentityAuth.user_assigned_client_id:
731+
if identity_type == ManagedIdentityAuth.id_type_client_id:
703732
return ManagedIdentityCredential(client_id=identity_id)
704-
if identity_type == ManagedIdentityAuth.user_assigned_object_id:
733+
if identity_type == ManagedIdentityAuth.id_type_object_id:
705734
return ManagedIdentityCredential(object_id=identity_id)
706-
if identity_type == ManagedIdentityAuth.user_assigned_resource_id:
735+
if identity_type == ManagedIdentityAuth.id_type_resource_id:
707736
return ManagedIdentityCredential(msi_res_id=identity_id)
708-
raise ValueError("unrecognized managed identity account name '{}'".format(identity_type))
737+
raise ValueError("Unrecognized managed identity account type '{}'".format(identity_type))
709738

710739

711740
class SubscriptionFinder:

src/azure-cli-core/azure/cli/core/auth/msal_authentication.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ def get_token(self, *scopes, **kwargs):
144144

145145
class ManagedIdentityCredential(ManagedIdentity):
146146

147-
def __init__(self, client_id=None):
147+
def __init__(self, client_id=None, object_id=None, mi_res_id=None):
148148
import requests
149-
super().__init__(requests.Session(), client_id=client_id)
149+
super().__init__(requests.Session(), client_id=client_id, object_id=object_id, mi_res_id=mi_res_id)
150150

151151
def get_token(self, *scopes, **kwargs):
152152
logger.debug("ManagedIdentityCredential.get_token: scopes=%r, kwargs=%r", scopes, kwargs)

0 commit comments

Comments
 (0)