Skip to content

Commit

Permalink
fix(aks): check parent scopes role assignments in pod identity assign…
Browse files Browse the repository at this point in the history
…ment (#3889)
  • Loading branch information
bcho authored Sep 15, 2021
1 parent 0401646 commit 875d584
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/aks-preview/azext_aks_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4184,15 +4184,25 @@ def _ensure_managed_identity_operator_permission(cli_ctx, instance, scope):

factory = get_auth_management_client(cli_ctx, scope)
assignments_client = factory.role_assignments
cluster_identity_object_id = cluster_identity_object_id.lower()
scope = scope.lower()

for i in assignments_client.list_for_scope(scope=scope, filter='atScope()'):
if i.scope.lower() != scope.lower():
continue
# list all assignments of the target identity (scope) that assigned to the cluster identity
filter_query = "atScope() and assignedTo('{}')".format(cluster_identity_object_id)
for i in assignments_client.list_for_scope(scope=scope, filter=filter_query):
if not i.role_definition_id.lower().endswith(CONST_MANAGED_IDENTITY_OPERATOR_ROLE_ID):
continue
if i.principal_id.lower() != cluster_identity_object_id.lower():

# sanity checks to make sure we see the correct assignments
if i.principal_id.lower() != cluster_identity_object_id:
# assignedTo() should return the assignment to cluster identity
continue
if not scope.startswith(i.scope.lower()):
# atScope() should return the assignments in subscription / resource group / resource level
continue

# already assigned
logger.debug('Managed Identity Opereator role has been assigned to {}'.format(i.scope))
return

if not _add_role_assignment(cli_ctx, CONST_MANAGED_IDENTITY_OPERATOR_ROLE, cluster_identity_object_id,
Expand Down

0 comments on commit 875d584

Please sign in to comment.