forked from mspnp/aks-baseline-regulated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-cluster-stamp.bicep
381 lines (346 loc) · 11 KB
/
pre-cluster-stamp.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
380
381
targetScope = 'resourceGroup'
/*** PARAMETERS ***/
@description('The regional network spoke VNet Resource ID that the cluster will be joined to')
@minLength(79)
param targetVnetResourceId string
@allowed([
'usgovarizona'
'usgovvirginia'
])
@description('AKS Service, Node Pools, and supporting services (KeyVault, App Gateway, etc) region. This needs to be the same region as the vnet provided in these parameters.')
@minLength(4)
param location string = 'usgovarizona'
@allowed([
'usgovarizona'
'usgovvirginia'
'usgovtexas'
])
@description('For Azure resources that support native geo-redunancy, provide the location the redundant service will have its secondary. Should be different than the location parameter and ideally should be a paired region - https://learn.microsoft.com/azure/best-practices-availability-paired-regions. This region does not need to support availability zones.')
@minLength(4)
param geoRedundancyLocation string = 'usgovtexas'
@description('The Base64 encoded AKS ingress controller\'s public certificate (as .crt or .cer) to be stored in Azure Key Vault as secret and referenced by Azure Application Gateway as a trusted root certificate.')
@minLength(35)
param aksIngressControllerCertificate string
/*** VARIABLES ***/
var subRgUniqueString = uniqueString('aks', subscription().subscriptionId, resourceGroup().id)
var clusterName = 'aks-${subRgUniqueString}'
var acrName = 'acraks${subRgUniqueString}'
/*** EXISTING RESOURCES ***/
@description('Built-in Azure RBAC role that is applied to a Key Vault to grant with secrets content read privileges. Granted to both Key Vault and our workload\'s identity.')
resource keyVaultSecretsUserRole 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
name: '4633458b-17de-408a-b874-0445c86b69e6'
scope: subscription()
}
@description('Built-in Azure RBAC role that is applied a Key Vault to grant with metadata, certificates, keys and secrets read privileges. Granted to App Gateway\'s managed identity.')
resource keyVaultReaderRole 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
name: '21090545-7ca7-4776-b22c-e363652d74d2'
scope: subscription()
}
@description('Spoke resource group')
resource spokeResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = {
scope: subscription()
name: split(targetVnetResourceId, '/')[4]
}
@description('The Spoke virtual network')
resource vnetSpoke 'Microsoft.Network/virtualNetworks@2022-01-01' existing = {
scope: spokeResourceGroup
name: last(split(targetVnetResourceId, '/'))
// Spoke virutual network's subnet for all private endpoints
resource snetPrivatelinkendpoints 'subnets' existing = {
name: 'snet-privatelinkendpoints'
}
// spoke virtual network's subnet for managment acr agent pools
resource snetManagmentCrAgents 'subnets' existing = {
name: 'snet-management-acragents'
}
}
resource pdzCr 'Microsoft.Network/privateDnsZones@2020-06-01' existing = {
scope: spokeResourceGroup
name: 'privatelink.azurecr.us'
}
resource pdzKv 'Microsoft.Network/privateDnsZones@2020-06-01' existing = {
scope: spokeResourceGroup
name: 'privatelink.vaultcore.usgovcloudapi.net'
}
/*** RESOURCES ***/
@description('The AKS cluster and related resources log analytics workspace.')
resource la 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: 'la-${clusterName}'
location: location
properties: {
sku: {
name: 'PerGB2018'
}
retentionInDays: 90
publicNetworkAccessForIngestion: 'Enabled'
publicNetworkAccessForQuery: 'Enabled'
}
}
@description('The private container registry for the aks regulated cluster.')
resource acr 'Microsoft.ContainerRegistry/registries@2022-02-01-preview' = {
name: acrName
location: location
sku: {
name: 'Premium'
}
properties: {
adminUserEnabled: false
networkRuleSet: {
defaultAction: 'Deny'
ipRules: []
}
policies: {
quarantinePolicy: {
status: 'disabled'
}
trustPolicy: {
type: 'Notary'
status: 'enabled'
}
retentionPolicy: {
days: 15
status: 'enabled'
}
}
publicNetworkAccess: 'Disabled'
encryption: {
status: 'disabled'
}
dataEndpointEnabled: true
networkRuleBypassOptions: 'AzureServices'
zoneRedundancy: 'Disabled'
}
resource grl 'replications' = {
name: geoRedundancyLocation
location: geoRedundancyLocation
}
resource ap 'agentPools@2019-06-01-preview' = {
name: 'acragent'
location: location
properties: {
count: 1
os: 'Linux'
tier: 'S1'
virtualNetworkSubnetResourceId: vnetSpoke::snetManagmentCrAgents.id
}
}
}
resource cr_diagnosticSettings 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
scope: acr
name: 'default'
properties: {
workspaceId: la.id
metrics: [
{
timeGrain: 'PT1M'
category: 'AllMetrics'
enabled: true
}
]
logs: [
{
category: 'ContainerRegistryRepositoryEvents'
enabled: true
}
{
category: 'ContainerRegistryLoginEvents'
enabled: true
}
]
}
}
@description('The network interface in the spoke vnet that enables privately connecting the AKS cluster with Container Registry.')
resource peCr 'Microsoft.Network/privateEndpoints@2022-05-01' = {
name: 'pe-${acr.name}'
location: location
properties: {
subnet: {
id: vnetSpoke::snetPrivatelinkendpoints.id
}
privateLinkServiceConnections: [
{
name: 'to-${vnetSpoke.name}'
properties: {
privateLinkServiceId: acr.id
groupIds: [
'registry'
]
}
}
]
}
dependsOn: [
acr::grl
]
resource pdzg 'privateDnsZoneGroups' = {
name: 'for-${acr.name}'
properties: {
privateDnsZoneConfigs: [
{
name: 'privatelink-azurecr-io'
properties: {
privateDnsZoneId: pdzCr.id
}
}
]
}
}
}
@description('The scheduled query that returns images being imported from repositories different than quarantine/')
resource sqrNonQuarantineImportedImgesToCr 'Microsoft.Insights/scheduledQueryRules@2022-06-15' = {
name: 'Image Imported into ACR from ${acr.name} source other than approved Quarantine'
location: location
properties: {
description: 'The only images we want in live/ are those that came from this ACR instance, but from the quarantine/ repository.'
actions: {
actionGroups: []
}
criteria: {
allOf: [
{
operator: 'GreaterThan'
query: 'ContainerRegistryRepositoryEvents\r\n| where OperationName == "importImage" and Repository startswith "live/" and MediaType !startswith strcat(_ResourceId, "/quarantine")'
threshold: 0
timeAggregation: 'Count'
dimensions: []
failingPeriods: {
minFailingPeriodsToAlert: 1
numberOfEvaluationPeriods: 1
}
resourceIdColumn: ''
}
]
}
enabled: true
evaluationFrequency: 'PT10M'
scopes: [
acr.id
]
severity: 3
windowSize: 'PT10M'
muteActionsDuration: null
overrideQueryTimeRange: null
}
dependsOn: [
cr_diagnosticSettings
]
}
@description('The secret storage management resource for the AKS regulated cluster.')
resource kv 'Microsoft.KeyVault/vaults@2022-07-01' = {
name: 'kv-${clusterName}'
location: location
properties: {
accessPolicies: [] // Azure RBAC is used instead
sku: {
family: 'A'
name: 'standard'
}
tenantId: subscription().tenantId
networkAcls: {
bypass: 'AzureServices' // Required for AppGW communication
defaultAction: 'Deny'
ipRules: []
virtualNetworkRules: []
}
enableRbacAuthorization: true
enabledForDeployment: false
enabledForDiskEncryption: false
enabledForTemplateDeployment: false
enableSoftDelete: true
softDeleteRetentionInDays: 7
createMode: 'default'
}
}
@description('The in-cluster ingress controller identity used by the pod identity agent to acquire access tokens to read SSL certs from Azure Key Vault.')
resource miIngressController 'Microsoft.ManagedIdentity/userAssignedIdentities@2022-01-31-preview' = {
name: 'mi-${clusterName}-ingresscontroller'
location: location
}
@description('Grant the ingress controller\'s managed identity with Key Vault secrets user role permissions; this allows pulling secrets from Key Vault.')
resource kvMiIngressControllerSecretsUserRole_roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: kv
name: guid(resourceGroup().id, miIngressController.name, keyVaultSecretsUserRole.id)
properties: {
roleDefinitionId: keyVaultSecretsUserRole.id
principalId: miIngressController.properties.principalId
principalType: 'ServicePrincipal'
}
}
@description('Grant the ingress controller\'s managed identity with Key Vault reader role permissions; this allows pulling frontend and backend certificates.')
resource kvMiIngressControllerKeyVaultReader_roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: kv
name: guid(resourceGroup().id, miIngressController.name, keyVaultReaderRole.id)
properties: {
roleDefinitionId: keyVaultReaderRole.id
principalId: miIngressController.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource kvsAppGwIngressInternalAksIngressTls 'Microsoft.KeyVault/vaults/secrets@2021-11-01-preview' = {
parent: kv
name: 'agw-ingress-internal-aks-ingress-daveinci-com-tls'
properties: {
value: aksIngressControllerCertificate
}
dependsOn: [
miIngressController
]
}
resource kv_diagnosticSettings 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
scope: kv
name: 'default'
properties: {
workspaceId: la.id
logs: [
{
category: 'AuditEvent'
enabled: true
}
]
metrics: [
{
category: 'AllMetrics'
enabled: true
}
]
}
}
@description('The network interface in the spoke vnet that enables privately connecting the AKS cluster with Key Vault.')
resource peKv 'Microsoft.Network/privateEndpoints@2022-01-01' = {
name: 'pe-${kv.name}'
location: location
properties: {
subnet: {
id: vnetSpoke::snetPrivatelinkendpoints.id
}
privateLinkServiceConnections: [
{
name: 'to-${vnetSpoke.name}'
properties: {
privateLinkServiceId: kv.id
groupIds: [
'vault'
]
}
}
]
}
resource pdzg 'privateDnsZoneGroups' = {
name: 'for-${kv.name}'
properties: {
privateDnsZoneConfigs: [
{
name: 'privatelink-akv-net'
properties: {
privateDnsZoneId: pdzKv.id
}
}
]
}
}
}
/*** OUTPUTS ***/
output quarantineContainerRegistryName string = acr.name
output containerRegistryName string = acr.name
output keyVaultName string = kv.name
output ingressClientid string = miIngressController.properties.clientId