Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from azure.cli.core.azclierror import ValidationError
from knack.log import get_logger

from ._agent_hosting_preflight import build_request, preflight, _missing_identity_fields, _storage_parameters
from ._agent_hosting_preflight import (
build_request, preflight, _accepted_role_ids, _missing_identity_fields,
_resource_name, _role_guid, _storage_parameters,
)
from ._client_factory import get_agent_hosting_clients


Expand All @@ -28,8 +31,7 @@ def prepare_agent_hosting(cmd, resource_group_name, account_name, location, tags
try:
return provision(cmd.cli_ctx, checked, tags, request=request)
except (AzureError, ValidationError):
logger.warning('BYO AKS dependency preparation failed. Resources and role assignments created before the '
'failure were not deleted. Review the preceding messages before retrying.')
logger.warning('BYO AKS dependency preparation failed; resources created before the failure were not deleted.')
raise


Expand All @@ -38,25 +40,15 @@ def provision(cli_ctx, checked, tags, request=None):
tags = dict(tags or {})
account = parse_resource_id(resources.account_id)
clients = get_agent_hosting_clients(cli_ctx, account['subscription'])
reused_dependencies = tuple(
(name, arm_id)
for name, arm_id, supplied, existing in (
('Hosting-management identity', resources.management_identity,
request.management_identity if request else None, checked.management_identity),
('Workload identity', resources.workload_identity,
request.workload_identity if request else None, checked.workload_identity),
('Storage account', resources.storage, request.storage if request else None, checked.storage),
)
if request is not None and supplied is None and existing is not None
)
if reused_dependencies:
details = '\n'.join(f' {name}: {arm_id}' for name, arm_id in reused_dependencies)
logger.warning('Using existing automatically generated BYO AKS dependencies:\n%s', details)
management = _ensure_identity(clients.identity, resources.management_identity, checked.management_identity,
resources.location, tags)
workload = _ensure_identity(clients.identity, resources.workload_identity, checked.workload_identity,
resources.location, tags, workload=True)
_ensure_storage(clients.storage, resources.storage, checked.storage, resources.location, tags, checked.storage_sku)
management = _ensure_identity(
clients.identity, resources.management_identity, checked.management_identity,
resources.location, tags, warn_reuse=bool(request and request.management_identity is None))
workload = _ensure_identity(
clients.identity, resources.workload_identity, checked.workload_identity,
resources.location, tags, workload=True, warn_reuse=bool(request and request.workload_identity is None))
_ensure_storage(
clients.storage, resources.storage, checked.storage, resources.location, tags, checked.storage_sku,
warn_reuse=bool(request and request.storage is None))
principals = {
resources.management_identity: management.principal_id, resources.workload_identity: workload.principal_id,
}
Expand All @@ -66,68 +58,64 @@ def provision(cli_ctx, checked, tags, request=None):
if subscription.lower() not in authorization_clients:
authorization_clients[subscription.lower()] = get_agent_hosting_clients(cli_ctx, subscription).authorization
_ensure_grant(authorization_clients[subscription.lower()], grant, principals[grant.identity_id])
logger.info('BYO AKS dependencies are ready:\n'
' Hosting-management identity: %s\n'
' Workload identity: %s\n'
' Storage account: %s\n'
'These resources are not automatically deleted if Account creation fails.',
resources.management_identity, resources.workload_identity, resources.storage)
return {'storage': resources.storage, 'management_identity': resources.management_identity,
'workload_identity': resources.workload_identity}


def _ensure_identity(client, arm_id, existing, location, tags, workload=False):
def _ensure_identity(client, arm_id, existing, location, tags, workload=False, warn_reuse=False):
from azure.mgmt.msi.models import Identity
identity_type = 'workload' if workload else 'hosting-management'
name = _resource_name(arm_id)
if existing is not None:
logger.info('Using existing %s identity: %s', identity_type, arm_id)
if warn_reuse:
logger.warning("Using existing %s identity '%s'.", identity_type, name)
return existing
parts = parse_resource_id(arm_id)
logger.warning('Creating %s identity: %s', identity_type, arm_id)
logger.warning("Creating %s identity '%s' ...", identity_type, name)
identity = client.user_assigned_identities.create_or_update(
parts['resource_group'], parts['name'], Identity(location=location, tags=tags))
missing = _missing_identity_fields(identity, workload)
if missing:
raise ValidationError(f'Created identity {arm_id} has missing metadata: {", ".join(missing)}. '
raise ValidationError(f"Created identity '{name}' has missing metadata: {', '.join(missing)}. "
'Wait for identity provisioning to complete and inspect it before retrying.')
return identity


def _ensure_storage(client, arm_id, existing, location, tags, sku):
def _ensure_storage(client, arm_id, existing, location, tags, sku, warn_reuse=False):
name = _resource_name(arm_id)
if existing is not None:
logger.info('Using existing Storage account: %s', arm_id)
if warn_reuse:
logger.warning("Using existing storage account '%s'.", name)
return existing
if sku is None:
raise ValidationError(f'Storage SKU was not resolved for {arm_id}. Run preflight again.')
raise ValidationError(f"Storage SKU was not resolved for '{name}'. Run preflight again.")
parts = parse_resource_id(arm_id)
logger.warning('Creating Storage account: %s', arm_id)
logger.warning("Creating storage account '%s' (%s) ...", name, sku)
return client.storage_accounts.begin_create(
parts['resource_group'], parts['name'], _storage_parameters(location, tags, sku)).result()


def _find_grant(client, grant, principal, exact_scope=False):
accepted = {_role_guid(grant.role_id)} if exact_scope else _accepted_role_ids(grant.name)
for assignment in client.role_assignments.list_for_scope(grant.scope, filter='atScope()'):
properties = assignment.properties
if not properties or not properties.principal_id or not properties.role_definition_id:
continue
if (properties.principal_id.lower() == principal.lower() and
properties.role_definition_id.lower() == grant.role_id.lower() and not properties.condition):
_role_guid(properties.role_definition_id) in accepted and not properties.condition):
if not exact_scope or (properties.scope and
properties.scope.rstrip('/').lower() == grant.scope.rstrip('/').lower()):
return assignment
return None


def _ensure_grant(client, grant, principal):
existing = _find_grant(client, grant, principal)
if existing is not None:
logger.info("Using existing role assignment for role '%s': %s", grant.name, existing.id)
if _find_grant(client, grant, principal) is not None:
return
identity_name = _resource_name(grant.identity_id)
scope_name = _resource_name(grant.scope)
name = str(uuid.uuid5(uuid.NAMESPACE_URL, f'{grant.scope}/{principal}/{grant.role_id}'.lower()))
arm_id = f'{grant.scope}/providers/Microsoft.Authorization/roleAssignments/{name}'
logger.warning("Assigning role '%s':\n Identity: %s\n Scope: %s",
grant.name, grant.identity_id, grant.scope)
logger.info('Role assignment resource ID: %s', arm_id)
logger.info("Assigning role '%s' to '%s' on '%s'", grant.name, identity_name, scope_name)
try:
client.role_assignments.create(grant.scope, name, {'properties': {
'principalId': principal, 'roleDefinitionId': grant.role_id, 'principalType': 'ServicePrincipal'}})
Expand All @@ -136,11 +124,9 @@ def _ensure_grant(client, grant, principal):
try:
existing = _find_grant(client, grant, principal, exact_scope=True)
except AzureError as read_error:
logger.warning('Could not verify a conflicting role assignment:\n'
' Assignment: %s\n'
' Error: %s', arm_id, read_error)
logger.info("Could not verify conflicting '%s' role assignment for '%s' on '%s': %s",
grant.name, identity_name, scope_name, read_error)
existing = None
if existing is not None:
logger.info("Using existing role assignment for role '%s': %s", grant.name, existing.id)
return
raise
Loading
Loading