Skip to content

Commit 1916969

Browse files
authored
Merge pull request #5802 from willie-yao/fix-aks-mgmt-assign
Dont give contributor access to byo identity in aks mgmt
2 parents 4a082ca + e1c881f commit 1916969

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

scripts/aks-as-mgmt.sh

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,25 @@ create_aks_cluster() {
226226
export MANAGED_IDENTITY_RG
227227
echo "mgmt resource identity resource group: ${MANAGED_IDENTITY_RG}"
228228

229-
230-
echo "assigning contributor role to managed identity over the $AZURE_SUBSCRIPTION_ID subscription"
231-
# Note: Even though --assignee-principal-type ServicePrincipal is specified, this does not mean that the role assignment is for a secret of type service principal.
232-
# Creating a role assignment for a managed identity using other assignee-principal-type from (Group, User, ForeignGroup) will lead to RBAC error.
233-
# To avoid RBAC error, we need to assign the role to the managed identity using the --assignee-principal-type ServicePrincipal.
234-
# refer: https://learn.microsoft.com/en-us/azure/role-based-access-control/troubleshooting?tabs=bicep#symptom---assigning-a-role-to-a-new-principal-sometimes-fails
235-
until az role assignment create --assignee-object-id "${AKS_MI_OBJECT_ID}" --role "Contributor" \
236-
--scope "/subscriptions/${AZURE_SUBSCRIPTION_ID}" --assignee-principal-type ServicePrincipal --output none \
237-
--only-show-errors; do
238-
echo "retrying to assign contributor role"
239-
sleep 5
240-
done
229+
# Only assign contributor role if using AKS-created managed identity (not user-provided)
230+
# Check if all user-provided identity variables are set - if so, skip role assignment
231+
if [[ -n "${AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY:-}" ]] && \
232+
[[ -n "${AZURE_OBJECT_ID_USER_ASSIGNED_IDENTITY:-}" ]] && \
233+
[[ -n "${AZURE_USER_ASSIGNED_IDENTITY_RESOURCE_ID:-}" ]]; then
234+
echo "skipping contributor role assignment for user-provided managed identity (assuming it already has necessary permissions)"
235+
else
236+
echo "assigning contributor role to managed identity over the $AZURE_SUBSCRIPTION_ID subscription"
237+
# Note: Even though --assignee-principal-type ServicePrincipal is specified, this does not mean that the role assignment is for a secret of type service principal.
238+
# Creating a role assignment for a managed identity using other assignee-principal-type from (Group, User, ForeignGroup) will lead to RBAC error.
239+
# To avoid RBAC error, we need to assign the role to the managed identity using the --assignee-principal-type ServicePrincipal.
240+
# refer: https://learn.microsoft.com/en-us/azure/role-based-access-control/troubleshooting?tabs=bicep#symptom---assigning-a-role-to-a-new-principal-sometimes-fails
241+
until az role assignment create --assignee-object-id "${AKS_MI_OBJECT_ID}" --role "Contributor" \
242+
--scope "/subscriptions/${AZURE_SUBSCRIPTION_ID}" --assignee-principal-type ServicePrincipal --output none \
243+
--only-show-errors; do
244+
echo "retrying to assign contributor role"
245+
sleep 5
246+
done
247+
fi
241248

242249
# Set the ASO_CREDENTIAL_SECRET_MODE to podidentity to
243250
# use the client ID of the managed identity created by AKS for authentication

0 commit comments

Comments
 (0)