-
Notifications
You must be signed in to change notification settings - Fork 140
/
avnm.bicep
379 lines (364 loc) · 11.8 KB
/
avnm.bicep
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
param location string
param prodNetworkGroupMembers array
param nonProdNetworkGroupMembers array
param hubVnetId string
param deployVpnGateway bool
param deployDefaultDenySecurityAdminRules bool
@description('This is the Azure Virtual Network Manager which will be used to implement the connected group for spoke-to-spoke connectivity.')
resource networkManager 'Microsoft.Network/networkManagers@2022-05-01' = {
name: 'avnm-${location}'
location: location
properties: {
networkManagerScopeAccesses: [
'Connectivity'
'SecurityAdmin'
]
networkManagerScopes: {
subscriptions: [
'/subscriptions/${subscription().subscriptionId}'
]
managementGroups: []
}
}
}
// static network group membership is used to avoid potential conflict in the test environment.
// for production deployments, consider using Azure Policy to dynamically bring VNETs under
// AVNM management. see https://learn.microsoft.com/azure/virtual-network-manager/concept-azure-policy-integration
@description('This is the static network group for the production spoke VNETs.')
resource networkGroupProd 'Microsoft.Network/networkManagers/networkGroups@2023-04-01' = {
name: 'ng-${location}-spokes-prod'
parent: networkManager
properties: {
description: 'Prod Spoke VNETs Network Group'
}
resource staticMembersSpokeOne 'staticMembers@2022-05-01' = {
name: 'sm-${location}-1'
properties: {
resourceId: prodNetworkGroupMembers[0]
}
}
resource staticMembersSpokeTwo 'staticMembers@2022-05-01' = {
name: 'sm-${location}-2'
properties: {
resourceId: prodNetworkGroupMembers[1]
}
}
}
@description('This is the static network group for the non-production spoke VNETs.')
resource networkGroupNonProd 'Microsoft.Network/networkManagers/networkGroups@2023-04-01' = {
name: 'ng-${location}-spokes-nonprod'
parent: networkManager
properties: {
description: 'Non-prod Spoke VNETs Network Group'
}
resource staticMembersSpokeOne 'staticMembers@2022-05-01' = {
name: 'sm-${location}-1'
properties: {
resourceId: nonProdNetworkGroupMembers[0]
}
}
resource staticMembersSpokeTwo 'staticMembers@2022-05-01' = {
name: 'sm-${location}-2'
properties: {
resourceId: nonProdNetworkGroupMembers[1]
}
}
}
@description('This is the static network group for all VNETs.')
resource networkGroupAll 'Microsoft.Network/networkManagers/networkGroups@2023-04-01' = {
name: 'ng-${location}-all'
parent: networkManager
properties: {
description: 'All VNETs Network Group (for Security Configurations)'
}
resource staticMembers1 'staticMembers@2022-05-01' = {
name: 'sm-${location}-0'
properties: {
resourceId: prodNetworkGroupMembers[0]
}
}
resource staticMembers2 'staticMembers@2022-05-01' = {
name: 'sm-${location}-1'
properties: {
resourceId: prodNetworkGroupMembers[1]
}
}
resource staticMembers3 'staticMembers@2022-05-01' = {
name: 'sm-${location}-2'
properties: {
resourceId: nonProdNetworkGroupMembers[0]
}
}
resource staticMembers4 'staticMembers@2022-05-01' = {
name: 'sm-${location}-3'
properties: {
resourceId: nonProdNetworkGroupMembers[1]
}
}
resource staticMembers5 'staticMembers@2022-05-01' = {
name: 'sm-${location}-4'
properties: {
resourceId: hubVnetId
}
}
}
@description('This connectivity configuration defines the connectivity between the spokes.')
resource connectivityConfigurationNonProd 'Microsoft.Network/networkManagers/connectivityConfigurations@2023-04-01' = {
name: 'cc-${location}-spokesnonprod'
parent: networkManager
dependsOn: [
networkGroupNonProd::staticMembersSpokeOne
networkGroupNonProd::staticMembersSpokeTwo
]
properties: {
description: 'Non-prod poke-to-spoke connectivity configuration'
appliesToGroups: [
{
networkGroupId: networkGroupNonProd.id
isGlobal: 'False'
useHubGateway: string(deployVpnGateway)
groupConnectivity: 'DirectlyConnected'
}
]
connectivityTopology: 'HubAndSpoke'
deleteExistingPeering: 'True'
hubs: [
{
resourceId: hubVnetId
resourceType: 'Microsoft.Network/virtualNetworks'
}
]
isGlobal: 'False'
}
}
@description('This connectivity configuration defines the connectivity between the spokes.')
resource connectivityConfigurationProd 'Microsoft.Network/networkManagers/connectivityConfigurations@2023-04-01' = {
name: 'cc-${location}-spokesprod'
parent: networkManager
dependsOn: [
networkGroupProd::staticMembersSpokeOne
networkGroupProd::staticMembersSpokeTwo
]
properties: {
description: 'Prod spoke-to-spoke connectivity configuration (through hub)'
appliesToGroups: [
{
networkGroupId: networkGroupProd.id
isGlobal: 'False'
useHubGateway: string(deployVpnGateway)
groupConnectivity: 'None'
}
]
connectivityTopology: 'HubAndSpoke'
deleteExistingPeering: 'True'
hubs: [
{
resourceId: hubVnetId
resourceType: 'Microsoft.Network/virtualNetworks'
}
]
isGlobal: 'False'
}
}
@description('This user assigned identity is used by the Deployment Script resource to interact with Azure resources.')
resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: 'uai-${location}'
location: location
}
@description('This role assignment grants the user assigned identity the Contributor role on the resource group.')
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(resourceGroup().id, userAssignedIdentity.name)
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c') // Contributor: b24988ac-6180-42a0-ab88-20f7382dd24c
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
@description('This is the securityadmin configuration assigned to the AVNM')
resource securityConfig 'Microsoft.Network/networkManagers/securityAdminConfigurations@2023-04-01' = {
name: 'sg-${location}'
parent: networkManager
properties: {
applyOnNetworkIntentPolicyBasedServices: [ 'None' ]
description: 'Security Group for AVNM'
}
}
@description('This is the rules collection for the security admin config assigned to the AVNM')
resource rulesCollection 'Microsoft.Network/networkManagers/securityAdminConfigurations/ruleCollections@2023-04-01' = {
name: 'rc-${location}'
parent: securityConfig
properties: {
appliesToGroups: [
{
networkGroupId: networkGroupAll.id
}
]
}
}
@description('This example rule contains all denied inbound TCP ports')
resource rule1 'Microsoft.Network/networkManagers/securityAdminConfigurations/ruleCollections/rules@2023-04-01' = if (deployDefaultDenySecurityAdminRules) {
name: 'r-tcp-${location}'
kind: 'Custom'
parent: rulesCollection
properties: {
access: 'Deny'
description: 'Inbound TCP Deny Example Rule'
destinationPortRanges: [ '20', '21', '22', '23', '69', '119', '161', '445', '512', '514', '873', '3389', '5800', '5900' ]
destinations: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
direction: 'Inbound'
priority: 100
protocol: 'TCP'
sourcePortRanges: [ '0-65535' ]
sources: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
}
}
@description('This example rule contains all denied inbound TCP or UDP ports')
resource rule2 'Microsoft.Network/networkManagers/securityAdminConfigurations/ruleCollections/rules@2023-04-01' = {
name: 'r-tcp-udp-${location}'
kind: 'Custom'
parent: rulesCollection
properties: {
access: 'Deny'
description: 'Inbound TCP/UDP Deny Example Rule'
destinationPortRanges: [ '11', '135', '162', '593', '2049' ]
destinations: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
direction: 'Inbound'
priority: 101
protocol: 'TCP,UDP'
sourcePortRanges: [ '0-65535' ]
sources: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
}
}
@description('This example rule contains all denied inbound UDP ports')
resource rule3 'Microsoft.Network/networkManagers/securityAdminConfigurations/ruleCollections/rules@2023-04-01' = {
name: 'r-udp-${location}'
kind: 'Custom'
parent: rulesCollection
properties: {
access: 'Deny'
description: 'Inbound UDP Deny Example Rule'
destinationPortRanges: [ '69', '11211' ]
destinations: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
direction: 'Inbound'
priority: 102
protocol: 'UDP'
sourcePortRanges: [ '0-65535' ]
sources: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
}
}
@description('This example rule always allows outbound traffic to Microsoft Entra ID, overriding NSG outbound restrictions')
resource rule4 'Microsoft.Network/networkManagers/securityAdminConfigurations/ruleCollections/rules@2023-04-01' = {
name: 'r-alwaysallow-${location}'
kind: 'Custom'
parent: rulesCollection
properties: {
access: 'AlwaysAllow'
description: 'Always allow outbound traffic to Microsoft Entra ID'
destinationPortRanges: [ '0-65535' ]
destinations: [
{
addressPrefix: 'AzureActiveDirectory'
addressPrefixType: 'ServiceTag'
}
]
direction: 'Outbound'
priority: 103
protocol: 'Any'
sourcePortRanges: [ '0-65535' ]
sources: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
}
}
@description('This example rule allows outbound traffic to Azure SQL, unless an NSG in the path denies it')
resource rule5 'Microsoft.Network/networkManagers/securityAdminConfigurations/ruleCollections/rules@2023-04-01' = {
name: 'r-allowsql-${location}'
kind: 'Custom'
parent: rulesCollection
properties: {
access: 'Allow'
description: 'Allow outbound traffic to Azure SQL'
destinationPortRanges: [ '0-65535' ]
destinations: [
{
addressPrefix: 'Sql'
addressPrefixType: 'ServiceTag'
}
]
direction: 'Outbound'
priority: 104
protocol: 'Any'
sourcePortRanges: [ '0-65535' ]
sources: [
{
addressPrefix: '*'
addressPrefixType: 'IPPrefix'
}
]
}
}
//
// In order to deploy a Connectivity or Security configruation, the /commit endpoint must be called or a Deployment created in the Portal.
// This DeploymentScript resource executes a PowerShell script which calls the /commit endpoint and monitors the status of the deployment.
//
module deploymentScriptConnectivityConfigs './avnmDeploymentScript.bicep' = {
name: 'ds-${location}-connectivityconfigs'
dependsOn: [
roleAssignment
]
params: {
location: location
userAssignedIdentityId: userAssignedIdentity.id
configurationIds: '${connectivityConfigurationProd.id},${connectivityConfigurationNonProd.id}' // each configuration separated by a comma
configType: 'Connectivity'
networkManagerName: networkManager.name
deploymentScriptName: 'ds-${location}-connectivityconfigs'
}
}
module deploymentScriptSecurityConfigs './avnmDeploymentScript.bicep' = {
name: 'ds-${location}-securityconfigs'
dependsOn: [
roleAssignment
]
params: {
location: location
userAssignedIdentityId: userAssignedIdentity.id
configurationIds: securityConfig.id // each configuration separated by a semicolon
configType: 'SecurityAdmin'
networkManagerName: networkManager.name
deploymentScriptName: 'ds-${location}-securityconfigs'
}
}