Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{Graph} Drop azure-graphrbac SDK in keyvault module #22203

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/azure-cli/azure/cli/command_modules/keyvault/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
MutuallyExclusiveArgumentError
from azure.cli.core.profiles import ResourceType, AZURE_API_PROFILES, SDKProfile
from azure.cli.core.util import sdk_no_wait
from azure.graphrbac.models import GraphErrorException

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, ec
Expand Down Expand Up @@ -310,15 +309,15 @@ def list_vault(client, resource_group_name=None):
# pylint: disable=inconsistent-return-statements
def _get_current_user_object_id(graph_client):
try:
current_user = graph_client.signed_in_user.get()
if current_user and current_user.object_id: # pylint:disable=no-member
return current_user.object_id # pylint:disable=no-member
except CloudError:
current_user = graph_client.signed_in_user_get()
if current_user and current_user.get('id', None): # pylint:disable=no-member
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think id property should always be there... Any case it won't?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case id doesn't exist. But yes, we might never meet such case😂

return current_user['id'] # pylint:disable=no-member
except (CloudError, CLIError):
pass


def _get_object_id_by_spn(graph_client, spn):
accounts = list(graph_client.service_principals.list(
accounts = list(graph_client.service_principal_list(
filter="servicePrincipalNames/any(c:c eq '{}')".format(spn)))
if not accounts:
logger.warning("Unable to find user with spn '%s'", spn)
Expand All @@ -327,19 +326,19 @@ def _get_object_id_by_spn(graph_client, spn):
logger.warning("Multiple service principals found with spn '%s'. "
"You can avoid this by specifying object id.", spn)
return None
return accounts[0].object_id
return accounts[0]['id']


def _get_object_id_by_upn(graph_client, upn):
accounts = list(graph_client.users.list(filter="userPrincipalName eq '{}'".format(upn)))
accounts = list(graph_client.user_list(filter="userPrincipalName eq '{}'".format(upn)))
if not accounts:
logger.warning("Unable to find user with upn '%s'", upn)
return None
if len(accounts) > 1:
logger.warning("Multiple users principals found with upn '%s'. "
"You can avoid this by specifying object id.", upn)
return None
return accounts[0].object_id
return accounts[0]['id']


def _get_object_id_from_subscription(graph_client, subscription):
Expand Down Expand Up @@ -665,7 +664,7 @@ def create_vault(cmd, client, # pylint: disable=too-many-locals, too-many-state
# just continue the normal creation process
pass
from azure.cli.core._profile import Profile
from azure.graphrbac import GraphRbacManagementClient
from azure.cli.command_modules.role._graph_client import GraphClient

VaultCreateOrUpdateParameters = cmd.get_models('VaultCreateOrUpdateParameters',
resource_type=ResourceType.MGMT_KEYVAULT)
Expand All @@ -682,10 +681,7 @@ def create_vault(cmd, client, # pylint: disable=too-many-locals, too-many-state
cred, _, tenant_id = profile.get_login_credentials(
resource=cmd.cli_ctx.cloud.endpoints.active_directory_graph_resource_id)

graph_client = GraphRbacManagementClient(
cred,
tenant_id,
base_url=cmd.cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
graph_client = GraphClient(cmd.cli_ctx)
subscription = profile.get_subscription()

# if bypass or default_action was specified create a NetworkRuleSet
Expand Down Expand Up @@ -751,7 +747,7 @@ def create_vault(cmd, client, # pylint: disable=too-many-locals, too-many-state

try:
object_id = _get_current_user_object_id(graph_client)
except GraphErrorException:
except CLIError:
object_id = _get_object_id(graph_client, subscription=subscription)
if not object_id:
raise CLIError('Cannot create vault.\nUnable to query active directory for information '
Expand Down Expand Up @@ -900,14 +896,12 @@ def update_hsm(cmd, instance,
def _object_id_args_helper(cli_ctx, object_id, spn, upn):
if not object_id:
from azure.cli.core._profile import Profile
from azure.graphrbac import GraphRbacManagementClient
from azure.cli.command_modules.role._graph_client import GraphClient

profile = Profile(cli_ctx=cli_ctx)
cred, _, tenant_id = profile.get_login_credentials(
resource=cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
graph_client = GraphRbacManagementClient(cred,
tenant_id,
base_url=cli_ctx.cloud.endpoints.active_directory_graph_resource_id)
graph_client = GraphClient(cli_ctx)
object_id = _get_object_id(graph_client, spn=spn, upn=upn)
if not object_id:
raise CLIError('Unable to get object id from principal name.')
Expand Down Expand Up @@ -2006,7 +2000,7 @@ def _get_principal_dics(cli_ctx, role_assignments):
principals = _get_object_stubs(graph_client, principal_ids)
return {i.object_id: (_get_displayable_name(i), i.object_type) for i in principals}

except (CloudError, GraphErrorException) as ex:
except (CloudError, CLIError) as ex:
# failure on resolving principal due to graph permission should not fail the whole thing
logger.info("Failed to resolve graph object information per error '%s'", ex)

Expand Down
Loading