forked from kartben/thethingsstack-on-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-cloudinit.bicep
193 lines (179 loc) · 5.49 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
param location string = 'eastus' // resourceGroup().location
param resourcesPrefix string
param adminEmail string
param adminPassword string {
secure: true
}
param networkName string {
default: 'The Things Stack'
}
param fqdn string {
metadata: {
description: 'The fully qualified domain name of the server hosting the stack. Ex: \'myttn.francecentral.cloudapp.azure.com\''
}
}
param redisHost string
param redisPort int
param redisPassword string {
secure: true
}
param psqlHost string
param psqlPort int
param psqlLogin string
param psqlPassword string {
secure: true
}
param psqlDatabase string
var scriptName = 'generateCloudInit'
var identityName = 'scratch'
var customRoleName = 'deployment-script-minimum-privilege-for-deployment-principal'
var keyVaultSecretOfficerRoleDefinitionId = resourceId('Microsoft.Authorization/roleDefinitions', 'b86a8fe4-44ce-4948-aee5-eccb2c155cd7')
var keyVaultSecretOfficerRoleDefinitionName = guid(identityName, keyVaultSecretOfficerRoleDefinitionId)
resource mi 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: identityName
location: location
}
resource deploymentScriptCustomRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = {
name: guid(customRoleName)
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, subscription().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.0.80'
retentionInterval: 'P1D'
primaryScriptUri: 'https://raw.githubusercontent.com/kartben/thethingsstack-on-azure/master/generate-cloudinit.sh'
environmentVariables: [
{
name: 'KEYVAULT_NAME'
value: keyvault.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