Skip to content

Commit dc0fdc3

Browse files
Update Microsoft.PolicyInsights module (#16832)
* Update Microsoft.PolicyInsights module - New version (1.5.0) of the Microsoft.PolicyInsights module - Update .NET SDK version - Update Policy States and Policy Events commands to use the new paged API - Update remediation commands to include new properties + improved paging behavior - Generated help, added examples - Move cancellation token implementation to base class so it can be used in multiple places - Fixed excessive polling of jobs due to test mode always being "playback" * Updated Microsoft.PolicyInsights tests - Created a script for setting up the test environment from scratch - Common.ps1 is used for defining the names of the test resources and it used by both the env setup and the tests. - Updated policy states and policy events tests, add paging tests - Updated remediation tests to test new properties + paging improvements - Fixed indentation * Add test recordings * Fix cancellation issue Cancellation source was disposed at the beginning, causing the cmdlets to ignore it when the user press ctrl+C * Updated ChangeLog.md * Changed 'ParallelDeployments' param to 'ParallelDeploymentCount' Regenerated help and re-recorded tests. * Update EnvironmentSetupHelper.cs * Update RMTestBase.cs * Update RMTestBase.cs * Update ChangeLog.md Co-authored-by: Yunchi Wang <[email protected]>
1 parent faa7c97 commit dc0fdc3

File tree

95 files changed

+16092
-123403
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+16092
-123403
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"resourceCount": {
6+
"type": "int"
7+
},
8+
"resourceNamePrefix": {
9+
"type": "string"
10+
}
11+
},
12+
"variables": {
13+
"locations": [ "westus", "eastus", "northcentralus", "southcentralus", "centralus", "eastus2", "westcentralus", "westus2", "westus3" ]
14+
},
15+
"resources": [
16+
{
17+
"type": "Microsoft.Network/networkSecurityGroups",
18+
"apiVersion": "2020-11-01",
19+
"name": "[format('{0}{1}', parameters('resourceNamePrefix'), copyIndex('nsgCopy'))]",
20+
"location": "[variables('locations')[mod(copyIndex('nsgCopy'), length(variables('locations')))]]",
21+
"properties": {},
22+
"copy": {
23+
"name": "nsgCopy",
24+
"count": "[parameters('resourceCount')]",
25+
"mode": "Serial",
26+
"batchSize": 4
27+
}
28+
}
29+
]
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"remediationCount": {
6+
"type": "int"
7+
},
8+
"assignmentId": {
9+
"type": "string"
10+
}
11+
},
12+
"resources": [
13+
{
14+
"type": "Microsoft.PolicyInsights/Remediations",
15+
"apiVersion": "2021-10-01",
16+
"name": "[format('testRemediation{0}', copyIndex('remediationCopy'))]",
17+
"properties": {
18+
"policyAssignmentId": "[parameters('assignmentId')]",
19+
"resourceCount": 1
20+
},
21+
"copy": {
22+
"name": "remediationCopy",
23+
"count": "[parameters('remediationCount')]",
24+
"mode": "Serial"
25+
}
26+
}
27+
]
28+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
# This script will set up the necessary resources and policies to record the policy insights tests.
16+
# This makes things a lot easier, since the tests require specific policies to be present in various scopes
17+
# as well as large number of non-compliant resources in order to test things like paging.
18+
# Both the tests and this script use the resource details defined in the common.ps1 script.
19+
# Before running env setup, make sure you have access to the MG and Subscription defined in common.ps1 (or change them to MG\subscription you do have acess to)
20+
. "..\ScenarioTests\Common.ps1"
21+
22+
# Note: Once the script is finished, wait for full compliance results before running the tests.
23+
$subscriptionId = $(Get-TestSubscriptionId)
24+
$managementGroupId = $(Get-TestManagementGroupName)
25+
$emptyResourceGroupName = $(Get-EmptyTestResourceGroupName)
26+
$resourceGroup1 = $(Get-FirstTestResourceGroupName)
27+
$resourceGroup2 = $(Get-SecondTestResourceGroupName)
28+
29+
# Connect
30+
Connect-AzAccount -DeviceCode
31+
Set-AzContext -SubscriptionId $subscriptionId
32+
33+
#Create an empty RG
34+
Get-AzResourceGroup -Name $emptyResourceGroupName -ErrorVariable rgNotPresent -ErrorAction SilentlyContinue
35+
if ($rgNotPresent) {
36+
New-AzResourceGroup -Name $emptyResourceGroupName -Location "eastus"
37+
}
38+
39+
# Create 2 RGs
40+
foreach ($resourceGroupName in @($resourceGroup1, $resourceGroup2)) {
41+
Get-AzResourceGroup -Name $resourceGroupName -ErrorVariable rgNotPresent -ErrorAction SilentlyContinue
42+
if ($rgNotPresent) {
43+
New-AzResourceGroup -Name $resourceGroupName -Location "northcentralus"
44+
}
45+
}
46+
47+
# Create DINE and modify definitions (MG-level)
48+
$deployIfNotExistsPolicyDefinition = New-AzPolicyDefinition -Name $(Get-TestDINEPolicyDefinitionName) -Policy "$PSScriptRoot/NSG_DINE_neverCompliant_policyDefinition.json" -DisplayName "PS cmdlet tests: never compliant DINE policy" -Mode Indexed -ManagementGroupName $managementGroupId
49+
$modifyPolicyDefinition = New-AzPolicyDefinition -Name $(Get-TestModifyPolicyDefinitionName) -Policy "$PSScriptRoot/NSG_modify_neverCompliant_policyDefinition.json" -DisplayName "PS cmdlet tests: never compliant modify policy" -Mode Indexed -ManagementGroupName $managementGroupId
50+
51+
# Assign the DINE policy in both MG and subscription level
52+
$mgDINEAssignment = New-AzPolicyAssignment -Name $(Get-TestManagementGroupDINEAssignmentName) -Scope "/providers/microsoft.management/managementgroups/$managementGroupId" -DisplayName "PS cmdlet tests: never compliant DINE policy (MG)" -PolicyDefinition $deployIfNotExistsPolicyDefinition -AssignIdentity -Location "westus2"
53+
$subDINEAssignment = New-AzPolicyAssignment -Name $(Get-TestSubscriptionDINEAssignmentName) -Scope "/subscriptions/$subscriptionId" -DisplayName "PS cmdlet tests: never compliant DINE policy (Sub)" -PolicyDefinition $deployIfNotExistsPolicyDefinition -AssignIdentity -Location "westus2"
54+
55+
# Assign the modify policy to the subscription
56+
$subModifyAssignment = New-AzPolicyAssignment -Name $(Get-TestSubscriptionModifyAssignmentName) -Scope "/subscriptions/$subscriptionId" -DisplayName "PS cmdlet tests: never compliant modify policy" -PolicyDefinition $modifyPolicyDefinition -AssignIdentity -Location "westus2"
57+
58+
# Give the assignments permissions to perform remediations
59+
Start-Sleep -Seconds 60
60+
New-AzRoleAssignment -Scope "/providers/microsoft.management/managementgroups/$managementGroupId" -ObjectId $mgDINEAssignment.Identity.principalId -RoleDefinitionName "Key Vault Contributor"
61+
New-AzRoleAssignment -Scope "/subscriptions/$subscriptionId" -ObjectId $subDINEAssignment.Identity.principalId -RoleDefinitionName "Key Vault Contributor"
62+
New-AzRoleAssignment -Scope "/subscriptions/$subscriptionId" -ObjectId $subModifyAssignment.Identity.principalId -RoleDefinitionName "Tag Contributor"
63+
64+
# Trigger 101 modify remediations with different names (don't care about the outcome, just want to have 101 remediation entities we can query)
65+
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroup1 -TemplateFile "$PSScriptRoot/CreateRemediationsTemplate.json" -remediationCount 101 -assignmentId $subModifyAssignment.ResourceId
66+
67+
# Create a subscription-level audit policy definition
68+
$partiallyCompliantAuditPolicyDefinition = New-AzPolicyDefinition -Name $(Get-TestAuditPolicyDefinitionName) -Policy "$PSScriptRoot/NSG_audit_partiallyCompliant_policyDefinition.json" -DisplayName "PS cmdlet tests: partially compliant audit policy" -Mode Indexed -SubscriptionId $subscriptionId
69+
70+
# Assign the audit policy to subscription and RG levels
71+
New-AzPolicyAssignment -Name $(Get-TestSubscriptionAuditAssignmentName) -Scope "/subscriptions/$subscriptionId" -DisplayName "PS cmdlet tests: partially compliant audit policy (Sub)" -PolicyDefinition $partiallyCompliantAuditPolicyDefinition
72+
New-AzPolicyAssignment -Name $(Get-TestResourceGroupAuditAssignmentName) -Scope "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup1" -DisplayName "PS cmdlet tests: partially compliant audit policy (RG)" -PolicyDefinition $partiallyCompliantAuditPolicyDefinition
73+
74+
# Create an initiative for the audit policy
75+
$policyDefinitions = @"
76+
[
77+
{
78+
"policyDefinitionId": "$($partiallyCompliantAuditPolicyDefinition.ResourceId)"
79+
}
80+
]
81+
"@
82+
83+
$policySetDefinition = New-AzPolicySetDefinition -Name $(Get-TestPolicySetDefinitionName) -DisplayName "PS cmdlet tests: test initiative" -PolicyDefinition $policyDefinitions -SubscriptionId $subscriptionId
84+
85+
# Assign the initiative to the subscription
86+
New-AzPolicyAssignment -Name $(Get-TestSubscriptionAuditInitiativeAssignmentName) -Scope "/subscriptions/$subscriptionId" -DisplayName "PS cmdlet tests: initiative with audit policy (Sub)" -PolicySetDefinition $policySetDefinition
87+
88+
Start-Sleep -Seconds 60
89+
90+
# In each RG, create 510 NSGs (will take a while)
91+
foreach ($resourceGroupName in @($resourceGroup1, $resourceGroup2)) {
92+
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile "$PSScriptRoot/CreateNSGsTemplate.json" -resourceCount 510 -resourceNamePrefix $(Get-TestResourceNamePrefix)
93+
}

src/PolicyInsights/PolicyInsights.Test/EnvSetup/emptyDeployment_KeyVault_policyDefinition.json renamed to src/PolicyInsights/PolicyInsights.Test/EnvSetup/NSG_DINE_neverCompliant_policyDefinition.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{
22
"if": {
3-
"field": "type",
4-
"equals": "Microsoft.KeyVault/vaults"
3+
"allOf": [
4+
{
5+
"field": "type",
6+
"equals": "Microsoft.Network/networkSecurityGroups"
7+
},
8+
{
9+
"field": "name",
10+
"like": "pstests*"
11+
}
12+
]
513
},
614
"then": {
715
"effect": "deployIfNotExists",
@@ -29,4 +37,4 @@
2937
}
3038
}
3139
}
32-
}
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"if": {
3+
"allOf": [
4+
{
5+
"field": "type",
6+
"equals": "Microsoft.Network/networkSecurityGroups"
7+
},
8+
{
9+
"field": "name",
10+
"like": "pstests1*"
11+
}
12+
]
13+
},
14+
"then": {
15+
"effect": "audit"
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"if": {
3+
"allOf": [
4+
{
5+
"field": "type",
6+
"equals": "Microsoft.Network/networkSecurityGroups"
7+
},
8+
{
9+
"field": "name",
10+
"like": "pstests*"
11+
}
12+
]
13+
},
14+
"then": {
15+
"effect": "modify",
16+
"details": {
17+
"roleDefinitionIds": [
18+
"/providers/microsoft.authorization/roleDefinitions/4a9ae827-6dc8-4573-8ac7-8239d42aa03f"
19+
],
20+
"operations": [
21+
{
22+
"operation": "addOrReplace",
23+
"field": "tags.pstests",
24+
"value": "testValue"
25+
}
26+
]
27+
}
28+
}
29+
}

src/PolicyInsights/PolicyInsights.Test/EnvSetup/RemediationSetup.ps1

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/PolicyInsights/PolicyInsights.Test/PolicyInsights.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.PolicyInsights" Version="3.1.0" />
14+
<PackageReference Include="Microsoft.Azure.Management.PolicyInsights" Version="5.0.0" />
1515
</ItemGroup>
1616

1717
</Project>

0 commit comments

Comments
 (0)