Skip to content

Commit 3a88063

Browse files
committed
mi-msrestazure
1 parent 4370a6f commit 3a88063

File tree

5 files changed

+22
-547
lines changed

5 files changed

+22
-547
lines changed

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

Lines changed: 9 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -220,63 +220,8 @@ def login(self,
220220
self._set_subscriptions(consolidated)
221221
return deepcopy(consolidated)
222222

223-
def login_with_managed_identity_msrestazure(self, client_id=None, object_id=None, resource_id=None,
224-
allow_no_subscriptions=None):
225-
# Old way of using msrestazure for managed identity
226-
import jwt
227-
from azure.cli.core.auth.adal_authentication import MSIAuthenticationWrapper
228-
resource = self.cli_ctx.cloud.endpoints.active_directory_resource_id
229-
230-
id_arg_count = len([arg for arg in (client_id, object_id, resource_id) if arg])
231-
if id_arg_count > 1:
232-
raise CLIError('Usage error: Provide only one of --client-id, --object-id, --resource-id.')
233-
234-
if id_arg_count == 0:
235-
identity_type = MsiAccountTypes.system_assigned
236-
identity_id = None
237-
msi_creds = MSIAuthenticationWrapper(resource=resource)
238-
elif client_id:
239-
identity_type = MsiAccountTypes.user_assigned_client_id
240-
identity_id = client_id
241-
msi_creds = MSIAuthenticationWrapper(resource=resource, client_id=client_id)
242-
elif object_id:
243-
identity_type = MsiAccountTypes.user_assigned_object_id
244-
identity_id = object_id
245-
msi_creds = MSIAuthenticationWrapper(resource=resource, object_id=object_id)
246-
elif resource_id:
247-
identity_type = MsiAccountTypes.user_assigned_resource_id
248-
identity_id = resource_id
249-
msi_creds = MSIAuthenticationWrapper(resource=resource, msi_res_id=resource_id)
250-
251-
token_entry = msi_creds.token
252-
token = token_entry['access_token']
253-
logger.info('MSI: token was retrieved. Now trying to initialize local accounts...')
254-
decode = jwt.decode(token, algorithms=['RS256'], options={"verify_signature": False})
255-
tenant = decode['tid']
256-
257-
subscription_finder = SubscriptionFinder(self.cli_ctx)
258-
subscriptions = subscription_finder.find_using_specific_tenant(tenant, msi_creds)
259-
base_name = ('{}-{}'.format(identity_type, identity_id) if identity_id else identity_type)
260-
user = _USER_ASSIGNED_IDENTITY if identity_id else _SYSTEM_ASSIGNED_IDENTITY
261-
if not subscriptions:
262-
if allow_no_subscriptions:
263-
subscriptions = self._build_tenant_level_accounts([tenant])
264-
else:
265-
raise CLIError('No access was configured for the VM, hence no subscriptions were found. '
266-
"If this is expected, use '--allow-no-subscriptions' to have tenant level access.")
267-
268-
consolidated = self._normalize_properties(user, subscriptions, is_service_principal=True,
269-
user_assigned_identity_id=base_name)
270-
self._set_subscriptions(consolidated)
271-
return deepcopy(consolidated)
272-
273223
def login_with_managed_identity(self, client_id=None, object_id=None, resource_id=None,
274224
allow_no_subscriptions=None):
275-
if not _use_msal_managed_identity(self.cli_ctx):
276-
return self.login_with_managed_identity_msrestazure(
277-
client_id=client_id, object_id=object_id, resource_id=resource_id,
278-
allow_no_subscriptions=allow_no_subscriptions)
279-
280225
import jwt
281226
from .auth.constants import ACCESS_TOKEN
282227

@@ -366,21 +311,14 @@ def get_login_credentials(self, subscription_id=None, aux_subscriptions=None, au
366311
if in_cloud_console() and account[_USER_ENTITY].get(_CLOUD_SHELL_ID):
367312
# Cloud Shell
368313
from .auth.msal_credentials import CloudShellCredential
369-
# The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
314+
# The credential must be wrapped by CredentialAdaptor so that it can work with SDK.
370315
sdk_cred = CredentialAdaptor(CloudShellCredential())
371316

372317
elif managed_identity_type:
373318
# managed identity
374-
if _use_msal_managed_identity(self.cli_ctx):
375-
# The credential must be wrapped by CredentialAdaptor so that it can work with Track 1 SDKs.
376-
cred = MsiAccountTypes.msal_credential_factory(managed_identity_type, managed_identity_id)
377-
sdk_cred = CredentialAdaptor(cred)
378-
else:
379-
# The resource is merely used by msrestazure to get the first access token.
380-
# It is not actually used in an API invocation.
381-
sdk_cred = MsiAccountTypes.msi_auth_factory(
382-
managed_identity_type, managed_identity_id,
383-
self.cli_ctx.cloud.endpoints.active_directory_resource_id)
319+
# The credential must be wrapped by CredentialAdaptor so that it can work with SDK.
320+
cred = MsiAccountTypes.msal_credential_factory(managed_identity_type, managed_identity_id)
321+
sdk_cred = CredentialAdaptor(cred)
384322

385323
else:
386324
# user and service principal
@@ -433,15 +371,10 @@ def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=No
433371
# managed identity
434372
if tenant:
435373
raise CLIError("Tenant shouldn't be specified for managed identity account")
436-
if _use_msal_managed_identity(self.cli_ctx):
437-
cred = MsiAccountTypes.msal_credential_factory(managed_identity_type, managed_identity_id)
438-
if credential_out:
439-
credential_out['credential'] = cred
440-
sdk_cred = CredentialAdaptor(cred)
441-
else:
442-
from .auth.util import scopes_to_resource
443-
sdk_cred = MsiAccountTypes.msi_auth_factory(managed_identity_type, managed_identity_id,
444-
scopes_to_resource(scopes))
374+
cred = MsiAccountTypes.msal_credential_factory(managed_identity_type, managed_identity_id)
375+
if credential_out:
376+
credential_out['credential'] = cred
377+
sdk_cred = CredentialAdaptor(cred)
445378

446379
else:
447380
sdk_cred = CredentialAdaptor(self._create_credential(account, tenant_id=tenant))
@@ -772,19 +705,6 @@ def valid_msi_account_types():
772705
return [MsiAccountTypes.system_assigned, MsiAccountTypes.user_assigned_client_id,
773706
MsiAccountTypes.user_assigned_object_id, MsiAccountTypes.user_assigned_resource_id]
774707

775-
@staticmethod
776-
def msi_auth_factory(cli_account_name, identity, resource):
777-
from azure.cli.core.auth.adal_authentication import MSIAuthenticationWrapper
778-
if cli_account_name == MsiAccountTypes.system_assigned:
779-
return MSIAuthenticationWrapper(resource=resource)
780-
if cli_account_name == MsiAccountTypes.user_assigned_client_id:
781-
return MSIAuthenticationWrapper(resource=resource, client_id=identity)
782-
if cli_account_name == MsiAccountTypes.user_assigned_object_id:
783-
return MSIAuthenticationWrapper(resource=resource, object_id=identity)
784-
if cli_account_name == MsiAccountTypes.user_assigned_resource_id:
785-
return MSIAuthenticationWrapper(resource=resource, msi_res_id=identity)
786-
raise ValueError("unrecognized msi account name '{}'".format(cli_account_name))
787-
788708
@staticmethod
789709
def parse_ids(client_id=None, object_id=None, resource_id=None):
790710
id_arg_count = len([arg for arg in (client_id, object_id, resource_id) if arg])
@@ -931,9 +851,7 @@ def _create_subscription_client(self, credential):
931851
.format(ResourceType.MGMT_RESOURCE_SUBSCRIPTIONS, self.cli_ctx.cloud.profile))
932852
api_version = get_api_version(self.cli_ctx, ResourceType.MGMT_RESOURCE_SUBSCRIPTIONS)
933853

934-
# MSIAuthenticationWrapper already implements get_token, so no need to wrap it with CredentialAdaptor
935-
from azure.cli.core.auth.adal_authentication import MSIAuthenticationWrapper
936-
sdk_cred = credential if isinstance(credential, MSIAuthenticationWrapper) else CredentialAdaptor(credential)
854+
sdk_cred = CredentialAdaptor(credential)
937855
client_kwargs = _prepare_mgmt_client_kwargs_track2(self.cli_ctx, sdk_cred)
938856
client = client_type(sdk_cred, api_version=api_version,
939857
base_url=self.cli_ctx.cloud.endpoints.resource_manager,
@@ -984,11 +902,3 @@ def _create_identity_instance(cli_ctx, authority, tenant_id=None, client_id=None
984902
use_msal_http_cache=use_msal_http_cache,
985903
enable_broker_on_windows=enable_broker_on_windows,
986904
instance_discovery=instance_discovery)
987-
988-
989-
def _use_msal_managed_identity(cli_ctx):
990-
from azure.cli.core.telemetry import set_use_msal_managed_identity
991-
# Use core.use_msal_managed_identity=false to use the old msrestazure implementation
992-
use_msal_managed_identity = cli_ctx.config.getboolean('core', 'use_msal_managed_identity', fallback=True)
993-
set_use_msal_managed_identity(use_msal_managed_identity)
994-
return use_msal_managed_identity

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

Lines changed: 0 additions & 129 deletions
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def __init__(self, correlation_id=None, application=None):
7878
self.enable_broker_on_windows = None
7979
self.msal_telemetry = None
8080
self.login_experience_v2 = None
81-
self.use_msal_managed_identity = None
8281

8382
def add_event(self, name, properties):
8483
for key in self.instrumentation_key:
@@ -235,7 +234,6 @@ def _get_azure_cli_properties(self):
235234
set_custom_properties(result, 'EnableBrokerOnWindows', str(self.enable_broker_on_windows))
236235
set_custom_properties(result, 'MsalTelemetry', self.msal_telemetry)
237236
set_custom_properties(result, 'LoginExperienceV2', str(self.login_experience_v2))
238-
set_custom_properties(result, 'UseMsalManagedIdentity', str(self.use_msal_managed_identity))
239237

240238
return result
241239

@@ -488,11 +486,6 @@ def set_msal_telemetry(msal_telemetry):
488486
@decorators.suppress_all_exceptions()
489487
def set_login_experience_v2(login_experience_v2):
490488
_session.login_experience_v2 = login_experience_v2
491-
492-
493-
@decorators.suppress_all_exceptions()
494-
def set_use_msal_managed_identity(use_msal_managed_identity):
495-
_session.use_msal_managed_identity = use_msal_managed_identity
496489
# endregion
497490

498491

0 commit comments

Comments
 (0)