Skip to content

Commit 7388caa

Browse files
authored
Merge pull request Azure#10714 from pilor/piRegComp
Update PolicyInsights nuget reference to 3.0.0
2 parents 49f3c1b + 8058070 commit 7388caa

File tree

80 files changed

+85270
-45177
lines changed

Some content is hidden

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

80 files changed

+85270
-45177
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 in a subscription to record the Remediation tests
16+
# You'll need to update the policy assignment IDs, resource IDs, etc... in the test to match the env your SPN has access to
17+
$subscriptionId = "f67cc918-f64f-4c3f-aa24-a855465f9d41"
18+
$managementGroupId = "AzGovPerfTest" # should be a parent of the subscription
19+
$resourceGroupName = "PSTestRG"
20+
21+
# Create the resoure group
22+
Select-AzSubscription $subscriptionId
23+
$rg = Get-AzResourceGroup -Name $resourceGroupName -ErrorVariable rgNotPresent -ErrorAction SilentlyContinue
24+
if ($rgNotPresent)
25+
{
26+
$rg = New-AzResourceGroup -Name $resourceGroupName -Location "northcentralus"
27+
}
28+
29+
New-AzKeyVault -Name "PSTestKV1" -ResourceGroupName $rg.ResourceGroupName -Location "northcentralus"
30+
New-AzKeyVault -Name "PSTestKV2" -ResourceGroupName $rg.ResourceGroupName -Location "northcentralus"
31+
New-AzKeyVault -Name "PSTestKV3" -ResourceGroupName $rg.ResourceGroupName -Location "westus2"
32+
33+
$policyDef = New-AzPolicyDefinition -Name "PSTestDeployDefinition" -Policy "$PSScriptRoot/emptyDeployment_KeyVault_policyDefinition.json" -DisplayName "Empty deployment on each KeyVault resource" -Mode Indexed -ManagementGroupName $managementGroupId
34+
35+
$mgAssignment = New-AzPolicyAssignment -Name "PSTestDeployAssignmentMG" -Scope "/providers/microsoft.management/managementgroups/$managementGroupId" -DisplayName "Empty deployment on each KeyVault resource (MG)" -PolicyDefinition $policyDef -AssignIdentity -Location "westus2"
36+
$subAssignment = New-AzPolicyAssignment -Name "PSTestDeployAssignmentSub" -Scope "/subscriptions/$subscriptionId" -DisplayName "Empty deployment on each KeyVault resource (SUB)" -PolicyDefinition $policyDef -AssignIdentity -Location "westus2"
37+
38+
Start-Sleep -Seconds 60
39+
40+
New-AzRoleAssignment -Scope "/providers/microsoft.management/managementgroups/$managementGroupId" -ObjectId $mgAssignment.Identity.principalId -RoleDefinitionName "Key Vault Contributor"
41+
New-AzRoleAssignment -Scope "/subscriptions/$subscriptionId" -ObjectId $subAssignment.Identity.principalId -RoleDefinitionName "Key Vault Contributor"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"if": {
3+
"field": "type",
4+
"equals": "Microsoft.KeyVault/vaults"
5+
},
6+
"then": {
7+
"effect": "deployIfNotExists",
8+
"details": {
9+
"type": "Microsoft.Authorization/policyAssignments",
10+
"name": "notExists",
11+
"roleDefinitionIds": [
12+
"/providers/Microsoft.Authorization/roleDefinitions/f25e0fa2-a7c8-4377-a976-54943a77a395"
13+
],
14+
"deployment": {
15+
"properties": {
16+
"mode": "incremental",
17+
"template": {
18+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
19+
"contentVersion": "1.0.0.0",
20+
"resources": [],
21+
"outputs": {
22+
"constantOutput": {
23+
"type": "string",
24+
"value": "someConstantValue"
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}

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="2.0.2" />
14+
<PackageReference Include="Microsoft.Azure.Management.PolicyInsights" Version="3.0.0" />
1515
</ItemGroup>
1616

1717
</Project>

src/PolicyInsights/PolicyInsights.Test/ScenarioTests/Common.ps1

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gets test resource group group name
2727
#>
2828
function Get-TestResourceGroupName
2929
{
30-
"jilimpolicytest2"
30+
"cleanupservice"
3131
}
3232

3333
<#
@@ -36,7 +36,7 @@ Gets test resource id
3636
#>
3737
function Get-TestResourceId
3838
{
39-
"/subscriptions/d0610b27-9663-4c05-89f8-5b4be01e86a5/resourcegroups/govintpolicyrp/providers/microsoft.network/trafficmanagerprofiles/gov-int-policy-rp"
39+
"/subscriptions/0b40be06-04e8-4f70-abde-a502b2646fe8/resourcegroups/cleanupservice/providers/Microsoft.KeyVault/vaults/kv12-cleanupservice"
4040
}
4141

4242
<#
@@ -45,7 +45,7 @@ Gets test policy set definition name
4545
#>
4646
function Get-TestPolicySetDefinitionName
4747
{
48-
"875cf75e-49c3-47f8-ab8d-89ba3d2311a0"
48+
"716ef2c9-4404-4673-8609-f984d58c417d"
4949
}
5050

5151
<#
@@ -54,7 +54,7 @@ Gets test policy definition name
5454
#>
5555
function Get-TestPolicyDefinitionName
5656
{
57-
"24813039-7534-408a-9842-eb99f45721b1"
57+
"policy2"
5858
}
5959

6060
<#
@@ -63,7 +63,7 @@ Gets test policy assignment name
6363
#>
6464
function Get-TestPolicyAssignmentName
6565
{
66-
"f54e881207924ca8b2e39f6a"
66+
"SecurityCenterBuiltIn"
6767
}
6868

6969
<#
@@ -72,7 +72,7 @@ Gets test resource group group name for resource group level policy assignment (
7272
#>
7373
function Get-TestResourceGroupNameForPolicyAssignmentEvents
7474
{
75-
"jilimpolicytest2"
75+
"cleanupservice"
7676
}
7777

7878
<#
@@ -81,7 +81,16 @@ Gets test policy assignment name (resource group level) (for event tests)
8181
#>
8282
function Get-TestPolicyAssignmentNameResourceGroupLevelEvents
8383
{
84-
"e9860612d8ec4a469f59af06"
84+
"568e500984414fbe8a4f10d0"
85+
}
86+
87+
<#
88+
.SYNOPSIS
89+
Gets test policy definition name for events
90+
#>
91+
function Get-TestPolicyDefinitionNameForEvents
92+
{
93+
"d994a92a-7669-4735-9950-bdcc326f32e4"
8594
}
8695

8796
<#
@@ -90,7 +99,7 @@ Gets test resource group group name for resource group level policy assignment (
9099
#>
91100
function Get-TestResourceGroupNameForPolicyAssignmentStates
92101
{
93-
"jilimpolicytest2"
102+
"cleanupservice"
94103
}
95104

96105
<#
@@ -99,7 +108,7 @@ Gets test policy assignment name (resource group level) (for state tests)
99108
#>
100109
function Get-TestPolicyAssignmentNameResourceGroupLevelStates
101110
{
102-
"e9860612d8ec4a469f59af06"
111+
"147b065f2a0f409ca31325c7"
103112
}
104113

105114
<#
@@ -108,7 +117,7 @@ Gets test query interval start
108117
#>
109118
function Get-TestQueryIntervalStart
110119
{
111-
"2019-01-20 00:00:00Z"
120+
"2019-12-01 00:00:00Z"
112121
}
113122

114123
<#
@@ -117,7 +126,7 @@ Gets test query interval end
117126
#>
118127
function Get-TestQueryIntervalEnd
119128
{
120-
"2019-04-15 00:00:00Z"
129+
"2019-12-06 00:00:00Z"
121130
}
122131

123132
<#
@@ -126,7 +135,7 @@ Gets the policy assignment used in remediation tests at subscription level and b
126135
#>
127136
function Get-TestRemediationSubscriptionPolicyAssignmentId
128137
{
129-
"/subscriptions/d0610b27-9663-4c05-89f8-5b4be01e86a5/providers/Microsoft.Authorization/policyAssignments/2deae24764b447c29af7c309"
138+
"/subscriptions/f67cc918-f64f-4c3f-aa24-a855465f9d41/providers/Microsoft.Authorization/policyAssignments/fcddeb6113ec43798567dce2"
130139
}
131140

132141
<#
@@ -135,7 +144,7 @@ Gets the policy assignment used in remediation tests at management group scope
135144
#>
136145
function Get-TestRemediationMgPolicyAssignmentId
137146
{
138-
"/providers/Microsoft.Management/managementGroups/PolicyUIMG/providers/Microsoft.Authorization/policyAssignments/326b090398a649e3858e3f23"
147+
"/providers/Microsoft.Management/managementGroups/AzGovPerfTest/providers/Microsoft.Authorization/policyAssignments/d80d743b97874fd3bfd1d539"
139148
}
140149

141150
<#
@@ -249,8 +258,10 @@ function Validate-PolicyStateSummary
249258
Assert-NotNullOrEmpty $policyAssignmentSummary.PolicyAssignmentId
250259

251260
Assert-NotNull $policyAssignmentSummary.Results
252-
Assert-NotNull $policyAssignmentSummary.Results.NonCompliantResources
253-
Assert-NotNull $policyAssignmentSummary.Results.NonCompliantPolicies
261+
Validate-SummaryResults -results:$policyAssignmentSummary.Results -nonCompliantPoliciesAssertNull:$false
262+
Assert-NotNull $policyAssignmentSummary.PolicyDefinitions
263+
Assert-NotNull $policyAssignmentSummary.PolicyGroups
264+
Assert-True { $policyAssignmentSummary.PolicyGroups.Count -gt 0 }
254265

255266
Assert-NotNull $policyAssignmentSummary.PolicyDefinitions
256267
if ($policyAssignmentSummary.PolicyDefinitions.Count -gt 0)
@@ -260,18 +271,42 @@ function Validate-PolicyStateSummary
260271
Foreach($policyDefinitionSummary in $policyAssignmentSummary.PolicyDefinitions)
261272
{
262273
Assert-NotNull $policyDefinitionSummary
263-
264274
Assert-NotNullOrEmpty $policyDefinitionSummary.PolicyDefinitionId
265275
Assert-NotNullOrEmpty $policyDefinitionSummary.Effect
266276

277+
Assert-NotNull $policyDefinitionSummary.PolicyDefinitionGroupNames
267278
Assert-NotNull $policyDefinitionSummary.Results
268-
Assert-NotNull $policyDefinitionSummary.Results.NonCompliantResources
269-
Assert-Null $policyDefinitionSummary.Results.NonCompliantPolicies
279+
Validate-SummaryResults -results:$policyDefinitionSummary.Results
270280
}
271281
}
272282
}
273283
}
274284

285+
<#
286+
.SYNOPSIS
287+
Validates a summary results
288+
#>
289+
function Validate-SummaryResults
290+
{
291+
param([Microsoft.Azure.Commands.PolicyInsights.Models.SummaryResults] $results,
292+
[switch]$nonCompliantPoliciesAssertNull = $true
293+
)
294+
295+
Assert-NotNull $results.NonCompliantResources
296+
if($nonCompliantPoliciesAssertNull)
297+
{
298+
Assert-Null $results.NonCompliantPolicies
299+
}
300+
else
301+
{
302+
Assert-NotNull $results.NonCompliantPolicies
303+
}
304+
Assert-NotNull $results.ResourceDetails
305+
Assert-NotNull $results.PolicyDetails
306+
Assert-True { $results.PolicyDetails.Count -gt 0 }
307+
Assert-NotNull $results.PolicyGroupDetails
308+
}
309+
275310
<#
276311
.SYNOPSIS
277312
Validates a remediation
@@ -309,6 +344,32 @@ function Validate-RemediationDeployment
309344
Assert-NotNullOrEmpty $deployment.ResourceLocation
310345
}
311346

347+
<#
348+
.SYNOPSIS
349+
Validates a policy metadata resource
350+
#>
351+
function Validate-PolicyMetadata
352+
{
353+
param([Microsoft.Azure.Commands.PolicyInsights.Models.PSPolicyMetadata]$policyMetadata,
354+
[switch]$validateExtendedProperties = $false)
355+
356+
Assert-NotNull $policyMetadata
357+
358+
Assert-NotNull $policyMetadata.Name
359+
Assert-AreEqual "Microsoft.PolicyInsights/policyMetadata" $policyMetadata.Type
360+
Assert-True { $policyMetadata.Id -like "/providers/Microsoft.PolicyInsights/policyMetadata/" + $policyMetadata.Name }
361+
362+
Assert-NotNull $policyMetadata.Owner
363+
Assert-NotNull $policyMetadata.Title
364+
Assert-NotNull $policyMetadata.Category
365+
Assert-NotNull $policyMetadata.MetadataId
366+
if($validateExtendedProperties)
367+
{
368+
Assert-NotNull $policyMetadata.Requirements
369+
Assert-NotNull $policyMetadata.Description
370+
}
371+
}
372+
312373
<#
313374
.SYNOPSIS
314375
Validates a string is not null or empty

0 commit comments

Comments
 (0)