From c677de5d1a28bbb6ac4576d528515bb058f94ec4 Mon Sep 17 00:00:00 2001 From: Ym <173550318+MaxonT@users.noreply.github.com> Date: Sun, 29 Nov 2026 03:09:48 -0600 Subject: [PATCH 1/2] feat(policy): add Azure.Policy.AssignmentMessage rule Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com> --- .../rules/Azure.Policy.AssignmentMessage.md | 63 +++++++++++++++++++ .../rules/Azure.Policy.Rule.ps1 | 8 +++ .../Azure.Policy.Tests.ps1 | 16 +++++ .../Resources.Policy.json | 8 ++- 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 docs/en/rules/Azure.Policy.AssignmentMessage.md diff --git a/docs/en/rules/Azure.Policy.AssignmentMessage.md b/docs/en/rules/Azure.Policy.AssignmentMessage.md new file mode 100644 index 00000000000..8c1971e6c1e --- /dev/null +++ b/docs/en/rules/Azure.Policy.AssignmentMessage.md @@ -0,0 +1,63 @@ +--- +severity: Awareness +pillar: Operational Excellence +category: OE:04 Tools and processes +resource: Policy +resourceType: Microsoft.Authorization/policyAssignments +online version: https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.Policy.AssignmentMessage/ +--- + +# Set a non-compliance message for policy assignments + +## SYNOPSIS + +Policy assignments should set a default non-compliance message. + +## DESCRIPTION + +Policy assignments can configure non-compliance messages. +A non-compliance message that does not set `policyDefinitionReferenceId` is the default message for the assignment. +When a resource is denied, the message is shown to the user in the portal and in deployment errors. + +Set a default non-compliance message to give a human readable reason for any denied deployments or changes. + +## RECOMMENDATION + +Consider setting a default non-compliance message for each policy assignment. + +## EXAMPLES + +### Azure templates + +To deploy policy assignments that pass this rule: + +- Set the `properties.nonComplianceMessages` property with at least one message. +- Do not set `policyDefinitionReferenceId` on the default message. + +For example: + +```json +{ + "comments": "Initiative assignment", + "name": "assignment-001", + "type": "Microsoft.Authorization/policyAssignments", + "apiVersion": "2022-06-01", + "properties": { + "displayName": "Assignment 001", + "description": "An example policy assignment.", + "policyDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/policySetDefinitions', 'initiative-001')]", + "enforcementMode": "Default", + "nonComplianceMessages": [ + { + "message": "Resources must follow the organization tagging standard." + } + ] + } +} +``` + +## LINKS + +- [OE:04 Tools and processes](https://learn.microsoft.com/azure/well-architected/operational-excellence/tools-processes) +- [Azure Policy assignment structure](https://learn.microsoft.com/azure/governance/policy/concepts/assignment-structure#non-compliance-messages) +- [Azure deployment reference](https://learn.microsoft.com/azure/templates/microsoft.authorization/policyassignments) diff --git a/src/PSRule.Rules.Azure/rules/Azure.Policy.Rule.ps1 b/src/PSRule.Rules.Azure/rules/Azure.Policy.Rule.ps1 index 838e6579574..c95a8804133 100644 --- a/src/PSRule.Rules.Azure/rules/Azure.Policy.Rule.ps1 +++ b/src/PSRule.Rules.Azure/rules/Azure.Policy.Rule.ps1 @@ -18,6 +18,14 @@ Rule 'Azure.Policy.AssignmentDescriptors' -Ref 'AZR-000143' -Type 'Microsoft.Aut $Assert.HasFieldValue($TargetObject, 'properties.description'); } +# Synopsis: Policy assignments should set a default non-compliance message. +Rule 'Azure.Policy.AssignmentMessage' -Ref 'AZR-000546' -Type 'Microsoft.Authorization/policyAssignments' -Tag @{ release = 'GA'; ruleSet = '2026_09'; 'Azure.WAF/pillar' = 'Operational Excellence'; } { + $messages = @($TargetObject.properties.nonComplianceMessages | Where-Object { + -not [string]::IsNullOrWhiteSpace($_.message) -and [string]::IsNullOrEmpty($_.policyDefinitionReferenceId) + }); + $Assert.Greater($messages, '.', 0); +} + # Synopsis: Policy assignments require assignedBy metadata. Rule 'Azure.Policy.AssignmentAssignedBy' -Ref 'AZR-000144' -Type 'Microsoft.Authorization/policyAssignments' -Tag @{ release = 'GA'; ruleSet = '2021_06'; 'Azure.WAF/pillar' = 'Operational Excellence'; } { $Assert.HasFieldValue($TargetObject, 'properties.metadata.assignedBy'); diff --git a/tests/PSRule.Rules.Azure.Tests/Azure.Policy.Tests.ps1 b/tests/PSRule.Rules.Azure.Tests/Azure.Policy.Tests.ps1 index 2f6b835ee78..398dc8de548 100644 --- a/tests/PSRule.Rules.Azure.Tests/Azure.Policy.Tests.ps1 +++ b/tests/PSRule.Rules.Azure.Tests/Azure.Policy.Tests.ps1 @@ -68,6 +68,22 @@ Describe 'Azure.Policy' -Tag Policy { $ruleResult.TargetName | Should -BeIn 'assignment-002'; } + It 'Azure.Policy.AssignmentMessage' { + $filteredResult = $result | Where-Object { $_.RuleName -eq 'Azure.Policy.AssignmentMessage' }; + + # Fail + $ruleResult = @($filteredResult | Where-Object { $_.Outcome -eq 'Fail' }); + $ruleResult | Should -Not -BeNullOrEmpty; + $ruleResult.Length | Should -Be 1; + $ruleResult.TargetName | Should -BeIn 'assignment-001'; + + # Pass + $ruleResult = @($filteredResult | Where-Object { $_.Outcome -eq 'Pass' }); + $ruleResult | Should -Not -BeNullOrEmpty; + $ruleResult.Length | Should -Be 1; + $ruleResult.TargetName | Should -BeIn 'assignment-002'; + } + It 'Azure.Policy.AssignmentAssignedBy' { $filteredResult = $result | Where-Object { $_.RuleName -eq 'Azure.Policy.AssignmentAssignedBy' }; diff --git a/tests/PSRule.Rules.Azure.Tests/Resources.Policy.json b/tests/PSRule.Rules.Azure.Tests/Resources.Policy.json index 4566570d763..3988c237ce6 100644 --- a/tests/PSRule.Rules.Azure.Tests/Resources.Policy.json +++ b/tests/PSRule.Rules.Azure.Tests/Resources.Policy.json @@ -261,7 +261,13 @@ "createdBy": "00000000-0000-0000-0000-000000000000", "updatedBy": "00000000-0000-0000-0000-000000000000" }, - "enforcementMode": "Default" + "enforcementMode": "Default", + "nonComplianceMessages": [ + { + "policyDefinitionReferenceId": "16686347626800927871", + "message": "Resources are required to be deployed in a specific location." + } + ] }, "Sku": { "name": "A1", From 34100a2daa9823f19323b44207e870e7b68c6763 Mon Sep 17 00:00:00 2001 From: Ym <173550318+MaxonT@users.noreply.github.com> Date: Sun, 29 Nov 2026 03:10:41 -0600 Subject: [PATCH 2/2] docs: add changelog entry for Azure.Policy.AssignmentMessage Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com> --- docs/changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index 73d6d60f0ba..dd3b8b4d391 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -36,6 +36,9 @@ What's changed since pre-release v1.48.0-B0228: - Azure Fleet: - Added `Azure.Fleet.SecureBoot` to require Trusted Launch/Confidential VM with Secure Boot for fleet VM profiles by @l46983284-cpu. [#3729](https://github.com/Azure/PSRule.Rules.Azure/issues/3729) + - Azure Policy: + - Added `Azure.Policy.AssignmentMessage` to check policy assignments set a default non-compliance message. + [#727](https://github.com/Azure/PSRule.Rules.Azure/issues/727) - General improvements: - Added support for resolving filtered subnet IDs from existing virtual networks during Bicep expansion. [#2159](https://github.com/Azure/PSRule.Rules.Azure/issues/2159)