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

New-AzBlueprintAssignment forbidden error using service principal

Open dankwok opened this issue 7 years ago • 3 comments

Description

I am experiencing an error while running this command in an Azure Automation runbook. I am first connecting to Azure using the Run As account.

The Run As service principal is a Contributor is part of a custom role where I created to assign Blueprint.

Steps to reproduce

Clear-AzContext -Force
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
I first run these commands before the rest of my script is executed. I then build some parameters to be passed to New-AzBlueprintAssignment and execute it.

Module versions

Az.Blueprint 0.1.0

Debug output

New-AzBlueprintAssignment : Operation returned an invalid status code 'Forbidden'
At line:75 char:17
+ ... ssignment = New-AzBlueprintAssignment -Name $assignmentName -Blueprin ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzBlueprintAssignment], GraphErrorException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Blueprint.Cmdlets.NewAzureRmBlueprintAssignment

dankwok avatar Apr 03 '19 18:04 dankwok

@filizt Hey Filiz, would you mind taking a look at the above issue? Are there any additional permissions needed by the user to run the Blueprint cmdlets?

cormacpayne avatar Apr 03 '19 21:04 cormacpayne

@dankwok The error is expected when running the New-AzBlueprintAssingment cmdlet as a service principal with system-assigned identity (which is the default for the New- cmdlet). This is due to some limitations we have querying AAD as the SP, however we're currently working on a fix which should be in the next release of Blueprint AZ module. In the mean time, one way to run this cmdlet with a service principal is to provide a user-assigned identity through the -UserAssignedIdentity parameter. Here's an New-AzBlueprintAssignment example using a user-assigned identity:

New-AzBlueprintAssignment -Name "myAssignment" -Blueprint $blueprintObject -SubscriptionId 00000000-1111-0000-1111-000000000000 -Location "West US" -Parameter @{P1="v1"; P2="v2"} -UserAssignedIdentity "/sub scriptions/00000000-1111-0000-1111-000000000000/resourceGroups/my-resource-group/providers/Microsoft.ManagedIdentit y/userAssignedIdentities/my-user-defined-identity

filizt avatar Apr 03 '19 23:04 filizt

Thanks @filizt! After some hiccups with my runbook, I was able to run the command you provided to resolve my issue.

dankwok avatar Apr 04 '19 17:04 dankwok