Repository navigation
Expand file tree
/
Copy pathtests.ps1
More file actions
180 lines (168 loc) · 6.78 KB
/
Copy pathtests.ps1
File metadata and controls
180 lines (168 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#region generic sections for all tests
#Requires -Modules Az.Accounts, Az.PolicyInsights, Az.Resources
#Requires -Version 7.0
using module AzResourceTest
$helperFunctionScriptPath = (resolve-path -relativeBasePath $PSScriptRoot -path '../../../scripts/pipelines/helper/helper-functions.ps1').Path
#load helper
. $helperFunctionScriptPath
#Run initiate-test script to set environment variables for test configuration and deployment
$globalConfigFilePath = (resolve-path -RelativeBasePath $PSScriptRoot -path '../.shared/policy_integration_test_config.jsonc').Path
$TestDirectory = $PSScriptRoot
Write-Output "Initiating test with global config file: $globalConfigFilePath and test directory: $TestDirectory"
$initiateTestScriptPath = (resolve-path -RelativeBasePath $PSScriptRoot -path '../.shared/initiate-test.ps1').Path
. $initiateTestScriptPath -globalConfigFilePath $globalConfigFilePath -TestDirectory $TestDirectory
# Refer to the ../../docs/policy-integration-test-get-started.md for details on the expected variables to be set by the initiate-test script and the structure of those variables.
#endregion
#region test specific configuration and tests
$actionGroupName = 'ag01'
$monitorAssignmentId = $script:LocalConfig_policyAssignmentIds | Where-Object { $_ -imatch "$script:LocalConfig_assignmentName`$" }
$actionGroupViolatingResourceContent = @{
properties = @{
smsReceivers = @(
@{
countryCode = '7' #Country code for Russia, should violate policy MON-002
phoneNumber = '2345678901'
}
)
emailReceivers = @(
@{
emailAddress = 'test.user1@outlook.com' #violate policy MON-001
}
)
automationRunbookReceivers = @(
@{
automationAccountId = '/subscriptions/62740b7e-8b53-4411-a353-14e023983d78/resourceGroups/rg-mon3-01/providers/Microsoft.Automation/automationAccounts/automationAccount1/webhooks/alert1' #violate policy MON-003
}
)
eventHubReceivers = @(
@{
eventHubNameSpace = '/subscriptions/62740b7e-8b53-4411-a353-14e023983d78/resourceGroups/rg-mon3-01/providers/Microsoft.EventHub/namespaces/eventHub1' #violate policy MON-004
}
)
azureFunctionReceivers = @(
@{
functionAppResourceId = '/subscriptions/62740b7e-8b53-4411-a353-14e023983d78/resourceGroups/rg-mon3-01/providers/Microsoft.Web/sites/functionApp1' #violate policy MON-005
}
)
logicAppReceivers = @(
@{
resourceId = '/subscriptions/62740b7e-8b53-4411-a353-14e023983d78/resourceGroups/rg-mon3-01/providers/Microsoft.Logic/workflows/logicApp1' #violate policy MON-006
}
)
webhookReceivers = @(
@{
serviceUri = 'http://webhookuri1.com' #violate policy MON-007 and MON-008
}
)
}
} | ConvertTo-Json -Depth 99
Write-Output "Action Group Violating Resource Content: `n $actionGroupViolatingResourceContent"
$actionGroupViolatingResourceConfig = @{
resourceName = $actionGroupName
resourceType = 'Microsoft.Insights/actionGroups'
apiVersion = '2024-10-01-preview'
resourceContent = $actionGroupViolatingResourceContent
location = 'global'
includeAuditEffect = $true
}
$policyRestrictionViolatingPolicies = @(
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-001'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-002'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-003'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-004'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-005'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-006'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-007'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-008'
resourceReference = $actionGroupName
policyEffect = 'Deny'
}
)
$whatIfViolatingPolicies = @(
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-001'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-002'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-003'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-004'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-005'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-006'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-007'
}
@{
policyAssignmentId = $monitorAssignmentId
policyDefinitionReferenceId = 'MON-008'
}
)
#define tests
$tests = @()
$tests += New-ARTWhatIfDeploymentTestConfig 'Policy abiding deployment should succeed' $script:token $script:whatIfComplyBicepTemplatePath $script:testResourceGroupId 'Succeeded' -maxRetry $script:GlobalConfig_whatIfMaxRetry
$tests += New-ARTWhatIfDeploymentTestConfig 'Policy violating deployment should fail' $script:token $script:whatIfViolateBicepTemplatePath $script:testResourceGroupId 'Failed' $whatIfViolatingPolicies -maxRetry $script:GlobalConfig_whatIfMaxRetry
$tests += New-ARTArmPolicyRestrictionTestConfig -testName 'Action Group Configuration should violate deny policies' -token $script:token -deploymentTargetResourceId $script:testResourceGroupId -resourceConfig $actionGroupViolatingResourceConfig -policyViolation $policyRestrictionViolatingPolicies
#endregion
#region Invoke tests - do not modify
$params = @{
tests = $tests
testTitle = $script:testTitle
contextTitle = $script:contextTitle
testSuiteName = $script:testSuiteName
OutputFile = $script:outputFilePath
OutputFormat = $script:GlobalConfig_testOutputFormat
}
Test-ARTResourceConfiguration @params
#endregion