I have an Azure DevOps CI/CD wherein I have a PowerShell script to create a new Azure Ad App. Although the Service Principle has permissions granted against the following Graph APIs, yet, it fails with the following exception.
2020-12-14T11:49:55.0146669Z ##[error]Error occurred while executing GetApplications Code: Authorization_RequestDenied Message: Insufficient privileges to complete the operation. RequestId: 1130fbec-3eec-4c8e-a2dd-e134f2c4621f DateTimeStamp: Mon, 14 Dec 2020 11:49:54 GMT HttpStatusCode: Forbidden HttpStatusDescription: Forbidden HttpResponseStatus: Completed
The API permissions that I have for the Service Principle are:
I use the following script to connect to Azure AD from the pipeline and create the Azure Ad App using PowerShell
Install-Module AzureAD -Force -Verbose -Scope CurrentUser
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.microsoft.com").AccessToken
$aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken
Connect-AzureAD -MsAccessToken $graphToken -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id
$ApiAppGuid = New-Guid
$ApiAppStartDate = Get-Date
$vvApiAppName = "dev0-vv-api"
# >>>>>> it must be failing here when trying to get the applications...
$appExists = ($vvApiApp = Get-AzureADApplication -Filter "DisplayName eq '$($vvApiAppName)'" -ErrorAction SilentlyContinue)
#... rest of the code that call New-AzureADApplication
Also, when I put the $graphToken
in jwt.io
, it correctly shows the API permissions I've assigned to the Service Principle from Azure Portal.
I have no idea why it keeps failing despite the permissions consents granted! Any ideas highly appreciated - this is now almost taking a day.
Get-AzureADMSApplication
instead? It's a bit dumb but there is a chance the cmdlet is using AAD Graph API and those app permissions don't work there :\ – juunasApplication.ReadWrite.OwnedBy
to see if it still works fine – AliAppRoleAssignment.ReadWrite.All
is an very privileged permission. Consider using a custom role which includes the permission to grant permissions subject to an app consent policy, and use a custom consent policy which only includes the permissions you really need your CI/CD pipeline to be able to grant. – Philippe Signoret