Loading...

Change Azure Policy assignment's system assigned managed identity location

Change Azure Policy assignment's system assigned managed identity location

When Azure Policy starts a template deployment when evaluating deployIfNotExists policies or modifies a resource when evaluating modify policies, it does so using a managed identity that is associated with the policy assignment. Policy assignments use managed identities for Azure resource authorization. You can use either a system-assigned managed identity that is created by the policy service or a user-assigned identity provided by the user.

 

Each Azure Policy assignment can be associated with only one managed identity and, after adding a managed identity to policy assignment, it is possible to edit only some managed identity related settings of the policy assignment. For instance, the type of managed identity can be switched from system assigned and user assigned, but if a system assigned managed identity has been selected and created before, its location can’t be changed. E.g.:

 

Edit policy assignmentEdit policy assignment

 

Azure Portal, CLI or PowerShell rely on the resource providers’ REST APIs and, in this case, on Policy Assignments - Update - REST API (Azure Policy) that allows the update of the identity property type, but does not allow the change of a system assigned managed identity location, for instance.

 

Therefore, to change the system assigned managed identity location, a new policy assignment should be created. As a suggestion, the policy assignment can be duplicated, and the system assigned managed identity location can be specified on the remediation section (that triggers the creation of a new system assigned managed identity) from the Azure Portal. Alternatively, a custom script (using CLI or PowerShell, for instance) can be used that gets the existing policy assignment’s properties and creates a new policy assignment with the same properties’ values except for the system assigned managed identity location. E.g.:

 

<# Disclaimer: This script is not supported under any Microsoft standard support program or service. This script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the current script or documentation, even if Microsoft has been advised of the possibility of such damages. Additional note: The following script generates a new policy assignment id. Please take into consideration that any existing solution(s) built around the policy assignment id might be impacted.#> $subscriptionId = "subscriptionId" $policyAssignmentId = "policyAssignmentId" $newSystemAssignedManagedIdentityLocation = "location" Connect-AzAccount Set-AzContext -Subscription $subscriptionId # Check modules requirements # Check if Az.Accounts module version 2.13.2 (or higher) is installed if (-not((Get-Module -ListAvailable -Name Az.Accounts | Select-Object -ExpandProperty Version -First 1) -ge ([System.Version]"2.13.2"))) { # Install Az module if not installed Install-Module -Name Az.Accounts -Force } # Check if Az.Resources module version 6.12.1 (or higher) is installed if (-not((Get-Module -ListAvailable -Name Az.Resources | Select-Object -ExpandProperty Version -First 1) -ge ([System.Version]"6.12.1"))) { # Install Az.Resources module if not installed Install-Module -Name Az.Resources -Force } Import-Module -Name Az.Accounts -RequiredVersion ([System.Version]"2.13.2") Import-Module -Name Az.Resources -RequiredVersion ([System.Version]"6.12.1") # Get policy assignment $policyAssignment = Get-AzPolicyAssignment -Id $policyAssignmentId # Get policy assignment's policy definition $policyDefinition = Get-AzPolicyDefinition -Id $policyAssignment.Properties.PolicyDefinitionId # Get policy assignment's managed identity $policyIdentity = $policyAssignment.Identity # Get policy's managed identity role assignments $policyIdentityRoleAssignments = Get-AzRoleAssignment -ObjectId $policyIdentity.PrincipalId # Create new policy assignment's parameters from previous assignment $newPolicyAssignmentParameters = @{} $policyAssignmentParametersObject = $policyAssignment.Properties.Parameters.psobject.Properties | Select-Object -ExpandProperty Value -Property Name $policyAssignmentParametersObject | ForEach-Object { $newPolicyAssignmentParameters[$_.Name] = $_.Value } # Generate a 24 character long alphanumeric string to be used on the new policy assignment as id $newPolicyAssignmentName = -join ((48..57) + (97..122) | Get-Random -Count 24 | % {[char]$_}) # Create new policy assignment $newPolicyAssignment = New-AzPolicyAssignment -Name $newPolicyAssignmentName -DisplayName $policyAssignment.Properties.DisplayName -PolicyDefinition $policyDefinition -Scope $policyAssignment.Properties.Scope -PolicyParameterObject $newPolicyAssignmentParameters -IdentityType SystemAssigned -Location $newSystemAssignedManagedIdentityLocation # Get new policy assignment's managed identity $newPolicyIdentity = $newPolicyAssignment.Identity if($newPolicyAssignment -ne $null) { # Create new policy's managed identity role assignments foreach ($roleAssignment in $policyIdentityRoleAssignments) { New-AzRoleAssignment -ObjectId $newPolicyIdentity.PrincipalId -ObjectType "ServicePrincipal" -Scope $roleAssignment.Scope -RoleDefinitionName $roleAssignment.RoleDefinitionName } # Delete previous policy assignment Remove-AzPolicyAssignment -InputObject $policyAssignment }

 

 

In the case of a policy initiative assignment, it is not possible to duplicate the policy assignment from the Azure Portal. Again, a custom script can be used that gets the existing policy assignment’s properties and creates a new policy assignment with the same properties’ values except for the system assigned managed identity location. E.g.:

 

<# Disclaimer: This script is not supported under any Microsoft standard support program or service. This script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the current script or documentation, even if Microsoft has been advised of the possibility of such damages. Additional note: The following script generates a new policy assignment id. Please take into consideration that any existing solution(s) built around the policy assignment id might be impacted.#> $subscriptionId = "subscriptionId" $policyAssignmentId = "policyAssignmentId" $newSystemAssignedManagedIdentityLocation = "location" Connect-AzAccount Set-AzContext -Subscription $subscriptionId # Check modules requirements # Check if Az.Accounts module version 2.13.2 (or higher) is installed if (-not((Get-Module -ListAvailable -Name Az.Accounts | Select-Object -ExpandProperty Version -First 1) -ge ([System.Version]"2.13.2"))) { # Install Az module if not installed Install-Module -Name Az.Accounts -Force } # Check if Az.Resources module version 6.12.1 (or higher) is installed if (-not((Get-Module -ListAvailable -Name Az.Resources | Select-Object -ExpandProperty Version -First 1) -ge ([System.Version]"6.12.1"))) { # Install Az.Resources module if not installed Install-Module -Name Az.Resources -Force } Import-Module -Name Az.Accounts -RequiredVersion ([System.Version]"2.13.2") Import-Module -Name Az.Resources -RequiredVersion ([System.Version]"6.12.1") # Get policy assignment $policyAssignment = Get-AzPolicyAssignment -Id $policyAssignmentId # Get policy assignment's policy definition $policySetDefinition = Get-AzPolicySetDefinition -Id $policyAssignment.Properties.PolicyDefinitionId # Get policy assignment's managed identity $policyIdentity = $policyAssignment.Identity # Get policy's managed identity role assignments $policyIdentityRoleAssignments = Get-AzRoleAssignment -ObjectId $policyIdentity.PrincipalId # Create new policy assignment's parameters from previous assignment $newPolicyAssignmentParameters = @{} $policyAssignmentParametersObject = $policyAssignment.Properties.Parameters.psobject.Properties | Select-Object -ExpandProperty Value -Property Name $policyAssignmentParametersObject | ForEach-Object { $newPolicyAssignmentParameters[$_.Name] = $_.Value } # Generate a 24 character long lower case alphanumeric string to be used on the new policy assignment as id $newPolicyAssignmentName = -join ((48..57) + (97..122) | Get-Random -Count 24 | % {[char]$_}) # Create new policy assignment $newPolicyAssignment = New-AzPolicyAssignment -Name $newPolicyAssignmentName -DisplayName $policyAssignment.Properties.DisplayName -PolicySetDefinition $policySetDefinition -Scope $policyAssignment.Properties.Scope -PolicyParameterObject $newPolicyAssignmentParameters -IdentityType SystemAssigned -Location $newSystemAssignedManagedIdentityLocation if($newPolicyAssignment -ne $null) { # Create new policy's managed identity role assignments foreach ($roleAssignment in $policyIdentityRoleAssignments) { New-AzRoleAssignment -ObjectId $newPolicyIdentity.PrincipalId -ObjectType "ServicePrincipal" -Scope $roleAssignment.Scope -RoleDefinitionName $roleAssignment.RoleDefinitionName } # Delete previous policy assignment Remove-AzPolicyAssignment -InputObject $policyAssignment }

 

Published on:

Learn more
Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Azure Storage - TLS 1.0 and 1.1 retirement

Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...

1 year ago

Efficient Management of Append and Page Blobs Using Azure Storage Actions

  Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...

1 year ago

[Azure AI Search] Internal Server Error when creating CMK encrypted objects

Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...

1 year ago

Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#

Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...

1 year ago

Performing simple Azure Table Storage REST API operations using curl command.

The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...

1 year ago

Bulk delete all the old jobs from the batch account

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...

1 year ago

Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases

In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...

2 years ago

[Azure Service Bus] JMS messages getting dead-lettered

The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.