-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathcreate-key-vault-and-secrets.sh
304 lines (243 loc) · 12.6 KB
/
create-key-vault-and-secrets.sh
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
#!/bin/bash
# Variables
keyVaultName="KEY_VAULT_NAME"
keyVaultResourceGroupName="KEY_VAULT_RESOURCE_GROUP_NAME"
keyVaultSku="Standard"
location="WestEurope"
storageAccountName="STORAGE_ACCOUNT_NAME"
storageAccountResourceGroupName="ATORAGE_ACCOUNT_RESOURCE_GROUP_NME"
containerName="tfstate"
sshPublicKey="ssh-rsa XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
azureDevOpsUrl="https://dev.azure.com/contoso"
azureDevOpsPat="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
azureDevOpsAgentPoolName="Terraform"
subscriptionName=$(az account show --query name --output tsv)
# Get storage account key
echo "Retrieving the primary key of the [$storageAccountName] storage account..."
storageAccountKey=$(az storage account keys list \
--resource-group $storageAccountResourceGroupName \
--account-name $storageAccountName \
--query [0].value -o tsv)
if [[ -n $storageAccountKey ]]; then
echo "Primary key of the [$storageAccountName] storage account successfully retrieved"
else
echo "Failed to retrieve the primary key of the [$storageAccountName] storage account"
exit
fi
# Check if the resource group already exists
echo "Checking if [$keyVaultResourceGroupName] resource group actually exists in the [$subscriptionName] subscription..."
az group show --name $keyVaultResourceGroupName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$keyVaultResourceGroupName] resource group actually exists in the [$subscriptionName] subscription"
echo "Creating [$keyVaultResourceGroupName] resource group in the [$subscriptionName] subscription..."
# create the resource group
az group create --name $keyVaultResourceGroupName --location $location 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$keyVaultResourceGroupName] resource group successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$keyVaultResourceGroupName] resource group in the [$subscriptionName] subscription"
exit
fi
else
echo "[$keyVaultResourceGroupName] resource group already exists in the [$subscriptionName] subscription"
fi
# Check if the key vault already exists
echo "Checking if [$keyVaultName] key vault actually exists in the [$subscriptionName] subscription..."
az keyvault show --name $keyVaultName --resource-group $keyVaultResourceGroupName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$keyVaultName] key vault actually exists in the [$subscriptionName] subscription"
echo "Creating [$keyVaultName] key vault in the [$subscriptionName] subscription..."
# create the key vault
az keyvault create \
--name $keyVaultName \
--resource-group $keyVaultResourceGroupName \
--location $location \
--enabled-for-deployment \
--enabled-for-disk-encryption \
--enabled-for-template-deployment \
--sku $keyVaultSku 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$keyVaultName] key vault successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$keyVaultName] key vault in the [$subscriptionName] subscription"
exit
fi
else
echo "[$keyVaultName] key vault already exists in the [$subscriptionName] subscription"
fi
# Check if the secret already exists
terraformBackendResourceGroupNameSecretName="terraformBackendResourceGroupName"
terraformBackendResourceGroupNameSecretValue=$storageAccountResourceGroupName
echo "Checking if [$terraformBackendResourceGroupNameSecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $terraformBackendResourceGroupNameSecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$terraformBackendResourceGroupNameSecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$terraformBackendResourceGroupNameSecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $terraformBackendResourceGroupNameSecretName \
--vault-name $keyVaultName \
--value $terraformBackendResourceGroupNameSecretValue 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$terraformBackendResourceGroupNameSecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$terraformBackendResourceGroupNameSecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$terraformBackendResourceGroupNameSecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
terraformBackendStorageAccountNameSecretName="terraformBackendStorageAccountName"
terraformBackendStorageAccountNameSecretValue=$storageAccountName
echo "Checking if [$terraformBackendStorageAccountNameSecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $terraformBackendStorageAccountNameSecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$terraformBackendStorageAccountNameSecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$terraformBackendStorageAccountNameSecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $terraformBackendStorageAccountNameSecretName \
--vault-name $keyVaultName \
--value $terraformBackendStorageAccountNameSecretValue 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$terraformBackendStorageAccountNameSecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$terraformBackendStorageAccountNameSecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$terraformBackendStorageAccountNameSecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
terraformBackendStorageAccountKeySecretName="terraformBackendStorageAccountKey"
terraformBackendStorageAccountKeySecretValue=$storageAccountKey
echo "Checking if [$terraformBackendStorageAccountKeySecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $terraformBackendStorageAccountKeySecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$terraformBackendStorageAccountKeySecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$terraformBackendStorageAccountKeySecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $terraformBackendStorageAccountKeySecretName \
--vault-name $keyVaultName \
--value $terraformBackendStorageAccountKeySecretValue 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$terraformBackendStorageAccountKeySecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$terraformBackendStorageAccountKeySecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$terraformBackendStorageAccountKeySecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
terraformBackendContainerNameSecretName="terraformBackendContainerName"
terraformBackendContainerNameSecretValue=$containerName
echo "Checking if [$terraformBackendContainerNameSecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $terraformBackendContainerNameSecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$terraformBackendContainerNameSecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$terraformBackendContainerNameSecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $terraformBackendContainerNameSecretName \
--vault-name $keyVaultName \
--value $terraformBackendContainerNameSecretValue 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$terraformBackendContainerNameSecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$terraformBackendContainerNameSecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$terraformBackendContainerNameSecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
sshPublicKeySecretName="sshPublicKey"
sshPublicKeySecretValue=$sshPublicKey
echo "Checking if [$sshPublicKeySecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $sshPublicKeySecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$sshPublicKeySecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$sshPublicKeySecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $sshPublicKeySecretName \
--vault-name $keyVaultName \
--value "$sshPublicKeySecretValue" 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$sshPublicKeySecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$sshPublicKeySecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$sshPublicKeySecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
azureDevOpsUrlSecretName="azureDevOpsUrl"
azureDevOpsUrlSecretValue=$azureDevOpsUrl
echo "Checking if [$azureDevOpsUrlSecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $azureDevOpsUrlSecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$azureDevOpsUrlSecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$azureDevOpsUrlSecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $azureDevOpsUrlSecretName \
--vault-name $keyVaultName \
--value "$azureDevOpsUrlSecretValue" 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$azureDevOpsUrlSecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$azureDevOpsUrlSecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$azureDevOpsUrlSecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
azureDevOpsPatSecretName="azureDevOpsPat"
azureDevOpsPatSecretValue=$azureDevOpsPat
echo "Checking if [$azureDevOpsPatSecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $azureDevOpsPatSecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$azureDevOpsPatSecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$azureDevOpsPatSecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $azureDevOpsPatSecretName \
--vault-name $keyVaultName \
--value "$azureDevOpsPatSecretValue" 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$azureDevOpsPatSecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$azureDevOpsPatSecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$azureDevOpsPatSecretName] secret already exists in the [$keyVaultName] key vault"
fi
# Check if the secret already exists
azureDevOpsAgentPoolNameSecretName="azureDevOpsAgentPoolName"
azureDevOpsAgentPoolNameSecretValue=$azureDevOpsAgentPoolName
echo "Checking if [$azureDevOpsAgentPoolNameSecretName] secret actually exists in the [$keyVaultName] key vault..."
az keyvault secret show --name $azureDevOpsAgentPoolNameSecretName --vault-name $keyVaultName &> /dev/null
if [[ $? != 0 ]]; then
echo "No [$azureDevOpsAgentPoolNameSecretName] secret actually exists in the [$keyVaultName] key vault"
echo "Creating [$azureDevOpsAgentPoolNameSecretName] secret in the [$keyVaultName] key vault..."
# Create the secret
az keyvault secret set \
--name $azureDevOpsAgentPoolNameSecretName \
--vault-name $keyVaultName \
--value "$azureDevOpsAgentPoolNameSecretValue" 1> /dev/null
if [[ $? == 0 ]]; then
echo "[$azureDevOpsAgentPoolNameSecretName] secret successfully created in the [$keyVaultName] key vault"
else
echo "Failed to create [$azureDevOpsAgentPoolNameSecretName] secret in the [$keyVaultName] key vault"
exit
fi
else
echo "[$azureDevOpsAgentPoolNameSecretName] secret already exists in the [$keyVaultName] key vault"
fi