-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate-cloudinit.bicep
198 lines (180 loc) · 5.68 KB
/
generate-cloudinit.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
param location string = 'eastus' // resourceGroup().location
param resourcesPrefix string
param vmName string
param adminEmail string
@secure()
param adminPassword string
param networkName string = 'The Things Stack'
@description('The fully qualified domain name of the server hosting the stack. Ex: \'myttn.francecentral.cloudapp.azure.com\'')
param fqdn string
param redisHost string
param redisPort int
@secure()
param redisPassword string
param psqlHost string
param psqlPort int
param psqlLogin string
@secure()
param psqlPassword string
param psqlDatabase string
var scriptName = 'generateCloudInit'
var identityName = 'scratch'
var customRoleName = '${resourcesPrefix}-deployment-script-minimum-privilege-for-deployment-principal'
var keyVaultSecretOfficerRoleDefinitionId = resourceId('Microsoft.Authorization/roleDefinitions', 'b86a8fe4-44ce-4948-aee5-eccb2c155cd7')
var keyVaultSecretOfficerRoleDefinitionName = guid(identityName, keyVaultSecretOfficerRoleDefinitionId, resourceGroup().id)
resource mi 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: identityName
location: location
}
resource deploymentScriptCustomRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = {
name: guid(customRoleName, resourceGroup().id)
properties: {
roleName: customRoleName
description: 'Configure least privilege for the deployment principal in deployment script'
permissions: [
{
actions: [
'Microsoft.Storage/storageAccounts/*'
'Microsoft.ContainerInstance/containerGroups/*'
'Microsoft.Resources/deployments/*'
'Microsoft.Resources/deploymentScripts/*'
'Microsoft.Storage/register/action'
'Microsoft.ContainerInstance/register/action'
]
}
]
assignableScopes: [
resourceGroup().id
]
}
}
resource miCustomRoleAssign 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: guid(customRoleName, identityName, resourceGroup().id)
properties: {
roleDefinitionId: deploymentScriptCustomRole.id
principalId: mi.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource miKeyVaultSecretOfficerRoleAssign 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: keyVaultSecretOfficerRoleDefinitionName
properties: {
roleDefinitionId: keyVaultSecretOfficerRoleDefinitionId
principalId: mi.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource keyvault 'Microsoft.KeyVault/vaults@2019-09-01' = {
name: '${resourcesPrefix}-kv'
location: location
properties: {
tenantId: subscription().tenantId
sku: {
family: 'A'
name: 'standard'
}
enableRbacAuthorization: true
}
}
resource adminPasswordKeyVaultEntry 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = {
name: '${keyvault.name}/ADMIN-PASSWORD'
properties: {
value: adminPassword
}
}
resource redisPasswordKeyVaultEntry 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = {
name: '${keyvault.name}/REDIS-PASSWORD'
properties: {
value: redisPassword
}
}
resource psqlPasswordKeyVaultEntry 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = {
name: '${keyvault.name}/PSQL-PASSWORD'
properties: {
value: psqlPassword
}
}
resource generateCloudInitDeploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: scriptName
location: location
kind: 'AzureCLI'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${mi.id}': { }
}
}
properties: {
azCliVersion: '2.24.0'
retentionInterval: 'P1D'
primaryScriptUri: 'https://raw.githubusercontent.com/kartben/thethingsstack-on-azure/master/generate-cloudinit.sh'
environmentVariables: [
{
name: 'KEYVAULT_NAME'
value: keyvault.name
}
{
name: 'VM_NAME'
value: vmName
}
{
name: 'RG_NAME'
value: resourceGroup().name
}
{
name: 'NETWORK_NAME'
value: networkName
}
{
name: 'ADMIN_EMAIL'
value: adminEmail
}
{
name: 'ADMIN_PASSWORD'
secureValue: adminPassword
}
{
name: 'FQDN'
value: fqdn
}
{
name: 'REDIS_HOST'
value: redisHost
}
{
name: 'REDIS_PORT'
value: string(redisPort)
}
{
name: 'REDIS_PASSWORD'
secureValue: redisPassword
}
{
name: 'PSQL_HOST'
value: psqlHost
}
{
name: 'PSQL_PORT'
value: string(psqlPort)
}
{
name: 'PSQL_LOGIN'
value: psqlLogin
}
{
name: 'PSQL_PASSWORD'
secureValue: psqlPassword
}
{
name: 'PSQL_DATABASE'
value: psqlDatabase
}
]
supportingScriptUris: [
'https://raw.githubusercontent.com/kartben/thethingsstack-on-azure/master/cloud-init-template'
]
cleanupPreference: 'OnSuccess'
timeout: 'PT30M'
}
}
output cloudInitFileAsBase64 string = generateCloudInitDeploymentScript.properties.outputs.cloudInitFileAsBase64