azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

Service principal role assignment fails with "can't find" error

Open OmpahDev opened this issue 6 years ago • 12 comments

Describe the bug When attempting to assign permissions to my AKS service principal, it fails with "Cannot find user or service principal in graph database for 'msi'. If the assignee is an appId, make sure the corresponding service principal is created with 'az ad sp create --id msi'" - even though I have confirmed that this is the correct service principal ID.

To Reproduce Run the following: CLIENT_ID=$(az aks show --resource-group aks-rg-name --name aks-name --query "servicePrincipalProfile.clientId" --output tsv) echo $CLIENT_ID #to make sure the above command worked az role assignment create --assignee $CLIENT_ID --role "Network Contributor" --scope /subscriptions/mysubID/resourceGroups/rg-name/providers/Microsoft.Network/virtualNetworks/myvnetname

Expected behavior The role assignment to complete.

Environment summary Running PowerShell on Windows with latest CLI version (2.11).

Additional context I'm attempting to assign Network Contributor permissions to my Azure CNI configured AKS cluster so I can create an internal load balancer, as documented here https://docs.microsoft.com/en-us/azure/aks/internal-lb#use-private-networks

OmpahDev avatar Sep 16 '20 16:09 OmpahDev

@tdevopsottawa Why is this closed? I encountered the same issue, apparently the servicePrincipalProfile.clientId is not even registered in AAD App registrations!

using this cmd as instructed in lab... aks create --resource-group akshandsonlab --name --enable-addons monitoring --kubernetes-version $version --generate-ssh-keys --location

wezfaas avatar Dec 15 '20 05:12 wezfaas

Experiencing the same. @tdevopsottawa why did you close the issue?

Skybladev2 avatar Jan 16 '21 20:01 Skybladev2

Same issue, why is this closed?

JamieSandell avatar Feb 28 '21 10:02 JamieSandell

Same issue, why is this closed?

sithumc avatar Mar 01 '21 05:03 sithumc

@tdevopsottawa

yonzhan avatar Mar 01 '21 06:03 yonzhan

I just had this same error. You could try reversing the order and create the ACR First, then attach it to the AKS on creation as shown here: https://docs.microsoft.com/en-us/azure/aks/cluster-container-registry-integration

The set of the principle still errs out, but you should be able to then leverage the acr from your aks cluster.

blgorman avatar Mar 18 '21 04:03 blgorman

I have this same error, attempting

az ad sp create-for-rbac -n "my-service-principle-name-svp" --role="Contributor" --scopes="$(az group show --name my-rg-northcentralus --query id -o tsv)" az role assignment create --assignee "$(az ad sp list --display-name "my-service-principle-name-svp" --query '[0].appId')" --role "Contributor" --scope "$(az group show --name other-rg-northcentralus --query id -o tsv)"

Cannot find user or service principal in graph database for '"7b5fddf2-722c-4186-bd3c-110ae5a4569b"'. If the assignee is an appId, make sure the corresponding service principal is created with 'az ad sp create --id "7b5fddf2-722c-4186-bd3c-110ae5a4569b"'.

7b5fddf2-722c-4186-bd3c-110ae5a4569b is "my-service-principle-name-svp", it does exist I was able make the role assignment successfully using Azure portal but want CLI for this

arthg avatar Jan 10 '22 20:01 arthg

I'm running into the same problem: I want to create a role assignment via a yaml pipeline. When creating the role assignment, or querying for the id of the principal using az ad sp show --id thename I get this error, although the pipeline has just created a resource (Azure Function) with the name 'thename'.

fgheysels avatar Feb 22 '22 12:02 fgheysels

Same.

This appears to be a timing issue ... when I try the same command from the command line, about a minute or so after I get the error, the command runs successfully.

I'd rather not live with a sleep 60 in my script, so I'd be interested in a solution.

chrislalos avatar Sep 02 '22 16:09 chrislalos

I had the same problem when using the normal login email address (like [email protected]) as the CLIENT_ID or AAD-ENTITY-ID for --assignee parameter. After using user principal name (like demo_email.com#EXT#@demoemail.onmicrosoft.com) it was successful. You can find the user principal name in azure portal in the users list.

wibram avatar Sep 30 '22 07:09 wibram

Why is this closed? I am consistently experiencing the same issue when trying to assign a role directly after creating an App Registration and its Service Principal using the Azure CLI.

Is there possibly any way to wait until the Service Principal of the App Registration is available for role assignments or comparable actions?

manuelfuchs avatar Nov 16 '22 18:11 manuelfuchs

@blgorman

Thanks I also had the same issue until I put all the role assignments after

subscriptionId=$(az account show --query id --output tsv) echo subscriptionId: $subscriptionId

subscriptionName=$(az account show --query name --output tsv) echo subscriptionName: $subscriptionName

tenantId=$(az account show --query tenantId --output tsv) echo tenantId: $tenantId

DO ROLE ASSIGNMENT HERE NOT BEFORE THE ABOVE

az role assignment create --assignee $PRINCIPAL --role "AcrPull" --resource-group $ResourceGroupName
az role assignment create --assignee $PRINCIPAL --role "AcrPush" --resource-group $ResourceGroupName

I just had this same error. You could try reversing the order and create the ACR First, then attach it to the AKS on creation as shown here: https://docs.microsoft.com/en-us/azure/aks/cluster-container-registry-integration

The set of the principle still errs out, but you should be able to then leverage the acr from your aks cluster.

scheung38 avatar Dec 31 '22 04:12 scheung38