@@ -120,25 +120,54 @@ echo "🔐 Testing Azure Service Bus permissions..."
120120if ! command -v jq & > /dev/null; then
121121 echo " -> ❌ jq is not installed, which is required for this check. Skipping permissions test."
122122else
123- SCOPE=" /subscriptions/$AZURE_SERVICEBUS_SUBSCRIPTION_ID /resourceGroups/$AZURE_SERVICEBUS_RESOURCE_GROUP /providers/Microsoft.ServiceBus/namespaces/$AZURE_SERVICEBUS_NAMESPACE "
123+ # Define the two scopes we will check against
124+ NAMESPACE_SCOPE=" /subscriptions/$AZURE_SERVICEBUS_SUBSCRIPTION_ID /resourceGroups/$AZURE_SERVICEBUS_RESOURCE_GROUP /providers/Microsoft.ServiceBus/namespaces/$AZURE_SERVICEBUS_NAMESPACE "
125+ TOPIC_SCOPE=" $NAMESPACE_SCOPE /topics/$AZURE_SERVICEBUS_DELIVERY_TOPIC "
124126
125127 echo " (Getting Service Principal Object ID...)"
126128 # Note: This command relies on the user being logged into the az CLI
127129 SP_OBJECT_ID=$( az ad sp show --id " $AZURE_SERVICEBUS_CLIENT_ID " --query " id" -o tsv)
130+
128131 if [ -z " $SP_OBJECT_ID " ]; then
129132 echo " -> ❌ Could not retrieve Service Principal Object ID. Please check your Azure login and that the SP exists."
130133 else
134+ permission_found=false
135+ # Function to check for a specific role assignment at a specific scope
131136 check_role () {
132137 local role_name=$1
133- echo " (Checking for role: '$role_name ')..."
134- if az role assignment list --assignee " $SP_OBJECT_ID " --scope " $SCOPE " --query " contains([].roleDefinitionName, '$role_name ')" | grep -q " true" ; then
135- echo " -> ✅ Service principal has the required '$role_name ' role."
138+ local scope=$2
139+ local scope_name=$3 # A friendly name for the scope for logging
140+
141+ echo " (Checking for role: '$role_name ' at $scope_name scope...)"
142+ if az role assignment list --assignee " $SP_OBJECT_ID " --scope " $scope " --query " contains([].roleDefinitionName, '$role_name ')" | grep -q " true" ; then
143+ echo " -> ✅ Service principal has the required '$role_name ' role at the $scope_name scope."
144+ permission_found=true
136145 else
137- echo " -> ❌ Service principal does NOT have the required '$role_name ' role."
138- echo " To fix, run: az role assignment create --assignee \" $SP_OBJECT_ID \" --role \" $role_name \" --scope \" $SCOPE \" "
146+ echo " -> No '$role_name ' role found at $scope_name scope."
139147 fi
140148 }
141- check_role " Azure Service Bus Data Owner"
149+
150+ # 1. Check for Data Owner at the Namespace level (highest privilege)
151+ check_role " Azure Service Bus Data Owner" " $NAMESPACE_SCOPE " " Namespace"
152+
153+ # 2. Check for Data Sender at the Namespace level
154+ check_role " Azure Service Bus Data Sender" " $NAMESPACE_SCOPE " " Namespace"
155+
156+ # 3. Check for Data Sender at the Topic level (most specific)
157+ check_role " Azure Service Bus Data Sender" " $TOPIC_SCOPE " " Topic"
158+
159+ # If none of the checks passed, show a final error
160+ if [ " $permission_found " = false ]; then
161+ echo " "
162+ echo " -> ❌ PERMISSION FAILURE: The Service Principal does NOT have the required permissions to publish to topic '$AZURE_SERVICEBUS_DELIVERY_TOPIC '."
163+ echo " To fix, grant the 'Azure Service Bus Data Sender' role at either the Namespace or the specific Topic scope."
164+ echo " (Alternatively, 'Azure Service Bus Data Owner' at the Namespace level also works)."
165+ echo " Run one of the following commands:"
166+ echo " Namespace Level: az role assignment create --assignee \" $SP_OBJECT_ID \" --role \" Azure Service Bus Data Sender\" --scope \" $NAMESPACE_SCOPE \" "
167+ echo " Topic Level: az role assignment create --assignee \" $SP_OBJECT_ID \" --role \" Azure Service Bus Data Sender\" --scope \" $TOPIC_SCOPE \" "
168+ else
169+ echo " -> ✅ Permissions are sufficient for publishing."
170+ fi
142171 fi
143172fi
144173
0 commit comments