Skip to content

Commit 0a228cc

Browse files
authored
{Keyvault} Remove useless AAD endpoint and honor user input --subscription to support cross sub operations (#26539)
* Removed unused aad endpoint * honor user input subscription * avoid keyerror * add data plane cross sub support
1 parent 24d0db5 commit 0a228cc

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

src/azure-cli/azure/cli/command_modules/keyvault/_client_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def data_plane_azure_keyvault_secret_client(cli_ctx, command_args):
289289
def _prepare_data_plane_azure_keyvault_client(cli_ctx, command_args, resource_type):
290290
version = str(get_api_version(cli_ctx, resource_type))
291291
profile = Profile(cli_ctx=cli_ctx)
292-
credential, _, _ = profile.get_login_credentials()
292+
credential, _, _ = profile.get_login_credentials(subscription_id=cli_ctx.data.get('subscription_id'))
293293
vault_url = \
294294
command_args.get('hsm_name', None) or \
295295
command_args.get('vault_base_url', None) or \

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,14 @@ def recover_vault_or_hsm(cmd, client, resource_group_name=None, location=None, v
401401

402402

403403
def recover_hsm(cmd, client, hsm_name, resource_group_name, location, no_wait=False):
404-
from azure.cli.core._profile import Profile
404+
from azure.cli.core._profile import Profile, _TENANT_ID
405405

406406
ManagedHsm = cmd.get_models('ManagedHsm', resource_type=ResourceType.MGMT_KEYVAULT)
407407
ManagedHsmSku = cmd.get_models('ManagedHsmSku', resource_type=ResourceType.MGMT_KEYVAULT)
408408

409409
# tenantId and sku shouldn't be required
410410
profile = Profile(cli_ctx=cmd.cli_ctx)
411-
_, _, tenant_id = profile.get_login_credentials(
412-
resource=cmd.cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
411+
tenant_id = profile.get_subscription(subscription=cmd.cli_ctx.data.get('subscription_id', None))[_TENANT_ID]
413412

414413
# Use 'Recover' as 'create_mode' temporarily since it's a bug from service side making 'create_mode' case-sensitive
415414
# Will change it back to CreateMode.recover.value('recover') from SDK definition after service fix
@@ -426,7 +425,7 @@ def recover_hsm(cmd, client, hsm_name, resource_group_name, location, no_wait=Fa
426425

427426

428427
def recover_vault(cmd, client, vault_name, resource_group_name, location, no_wait=False):
429-
from azure.cli.core._profile import Profile
428+
from azure.cli.core._profile import Profile, _TENANT_ID
430429

431430
VaultCreateOrUpdateParameters = cmd.get_models('VaultCreateOrUpdateParameters',
432431
resource_type=ResourceType.MGMT_KEYVAULT)
@@ -436,8 +435,7 @@ def recover_vault(cmd, client, vault_name, resource_group_name, location, no_wai
436435
Sku = cmd.get_models('Sku', resource_type=ResourceType.MGMT_KEYVAULT)
437436
SkuName = cmd.get_models('SkuName', resource_type=ResourceType.MGMT_KEYVAULT)
438437
profile = Profile(cli_ctx=cmd.cli_ctx)
439-
_, _, tenant_id = profile.get_login_credentials(
440-
resource=cmd.cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
438+
tenant_id = profile.get_subscription(subscription=cmd.cli_ctx.data.get('subscription_id', None))[_TENANT_ID]
441439

442440
params = VaultCreateOrUpdateParameters(location=location,
443441
properties={'tenant_id': tenant_id,
@@ -596,7 +594,7 @@ def create_hsm(cmd, client,
596594

597595
administrators = [admin.strip().replace('\r', '').replace('\n', '') for admin in administrators]
598596

599-
from azure.cli.core._profile import Profile
597+
from azure.cli.core._profile import Profile, _TENANT_ID
600598

601599
if not sku:
602600
sku = 'Standard_B1'
@@ -606,8 +604,7 @@ def create_hsm(cmd, client,
606604
ManagedHsmSku = cmd.get_models('ManagedHsmSku', resource_type=ResourceType.MGMT_KEYVAULT)
607605

608606
profile = Profile(cli_ctx=cmd.cli_ctx)
609-
_, _, tenant_id = profile.get_login_credentials(
610-
resource=cmd.cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
607+
tenant_id = profile.get_subscription(subscription=cmd.cli_ctx.data.get('subscription_id', None))[_TENANT_ID]
611608

612609
properties = ManagedHsmProperties(tenant_id=tenant_id,
613610
enable_purge_protection=enable_purge_protection,
@@ -656,7 +653,7 @@ def create_vault(cmd, client, # pylint: disable=too-many-locals, too-many-state
656653
# if client.get raise exception, we can take it as no existing vault found
657654
# just continue the normal creation process
658655
pass
659-
from azure.cli.core._profile import Profile
656+
from azure.cli.core._profile import Profile, _TENANT_ID
660657
from azure.cli.command_modules.role import graph_client_factory, GraphError
661658

662659
VaultCreateOrUpdateParameters = cmd.get_models('VaultCreateOrUpdateParameters',
@@ -671,11 +668,9 @@ def create_vault(cmd, client, # pylint: disable=too-many-locals, too-many-state
671668
VaultProperties = cmd.get_models('VaultProperties', resource_type=ResourceType.MGMT_KEYVAULT)
672669

673670
profile = Profile(cli_ctx=cmd.cli_ctx)
674-
_, _, tenant_id = profile.get_login_credentials(
675-
resource=cmd.cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
676-
677671
graph_client = graph_client_factory(cmd.cli_ctx)
678-
subscription = profile.get_subscription()
672+
subscription = profile.get_subscription(subscription=cmd.cli_ctx.data.get('subscription_id', None))
673+
tenant_id = subscription[_TENANT_ID]
679674

680675
# if bypass or default_action was specified create a NetworkRuleSet
681676
# if neither were specified we will parse it from parameter `--network-acls`

0 commit comments

Comments
 (0)