-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmockSpacestation.bicep
executable file
·172 lines (153 loc) · 5.57 KB
/
mockSpacestation.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
//////////
// CONSTS
//////////
// Administrator Values
var adminUsername = 'azureuser'
// User Assigned Identity Values
var userAssignedIdentityName = 'mockSpacestationIdentity'
// SSH Key Generation Script Values
var generateSshKeyScriptContent = loadTextContent('./scripts/generateSshKey.sh')
var generateSshKeyScriptName = 'generateSshKey'
var removeSshKeyGenResultScriptName = 'removeSshKeyGenResultScript'
var removeSshKeyGenResultScriptContent = loadTextContent('./scripts/removeSshKeyResult.sh')
var removeSshKeyGenScriptWithGroupName = replace(removeSshKeyGenResultScriptContent, 'resourceGroupNameDefaultValue', resourceGroup().name)
var removeSshKeyGenScriptWithGroupNameAndScriptName = replace(removeSshKeyGenScriptWithGroupName, 'generateSshKeyScriptName', generateSshKeyScriptName)
// KeyVault Values
var keyvaultName = toLower('mockisskv${uniqueString(resourceGroup().id)}')
var keyvaultTenantId = subscription().tenantId
var privateKeySecretName = 'sshPrivateKey'
var publicKeySecretName = 'sshPublicKey'
//////////
// PARAMS
//////////
// Groundstation Parameters
@description('The name of the Mock Groundstation Virtual Machine')
param groundstationVirtualMachineName string = 'mockGroundstation'
@description('The region to deploy Mock Groundstation resources into')
param groundstationLocation string = 'eastus'
// Spacestation Parameters
@description('The name of the Mock Spacestation Virtual Machine')
param spacestationVirtualMachineName string = 'mockSpacestation'
@description('The region to deploy Mock Spacestation resources into')
param spacestationLocation string = 'australiaeast'
//////////
// MAIN
//////////
resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: userAssignedIdentityName
location: resourceGroup().location
}
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: '${guid(resourceGroup().id, userAssignedIdentity.id)}'
scope: resourceGroup()
properties: {
// The 'Contributor' RBAC role definition ID is a hardcoded value:
// https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#contributor
roleDefinitionId: '${subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')}'
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
dependsOn: [
userAssignedIdentity
]
}
resource keyvault 'Microsoft.KeyVault/vaults@2019-09-01' = {
name: keyvaultName
location: resourceGroup().location
properties: {
accessPolicies: []
enabledForDeployment: true
enabledForTemplateDeployment: true
networkAcls: {
defaultAction: 'Allow'
bypass: 'AzureServices'
}
sku: {
name: 'standard'
family: 'A'
}
tenantId: keyvaultTenantId
}
}
resource generateSshKeyScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: generateSshKeyScriptName
location: resourceGroup().location
kind: 'AzureCLI'
properties: {
azCliVersion: '2.25.0'
cleanupPreference: 'OnSuccess'
retentionInterval: 'P1D' // retain script for 1 day
scriptContent: generateSshKeyScriptContent
timeout: 'PT30M' // timeout after 30 minutes
}
}
resource publicKeySecret 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = {
name: '${keyvault.name}/${publicKeySecretName}'
properties: {
value: generateSshKeyScript.properties.outputs.keyinfo.publicKey
}
}
resource privateKeySecret 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = {
name: '${keyvault.name}/${privateKeySecretName}'
properties: {
value: generateSshKeyScript.properties.outputs.keyinfo.privateKey
}
}
module groundstation 'modules/linuxVirtualMachine.bicep' = {
name: 'mockGroundstationVm'
params: {
adminUsername: adminUsername
location: groundstationLocation
sshPrivateKey: generateSshKeyScript.properties.outputs.keyinfo.privateKey
sshPublicKey: generateSshKeyScript.properties.outputs.keyinfo.publicKey
virtualMachineName: groundstationVirtualMachineName
}
}
module spacestation 'modules/linuxVirtualMachine.bicep' = {
name: 'mockSpacestationVm'
params: {
adminUsername: adminUsername
location: spacestationLocation
hostToSync: groundstation.outputs.hostName
sshPrivateKey: generateSshKeyScript.properties.outputs.keyinfo.privateKey
sshPublicKey: generateSshKeyScript.properties.outputs.keyinfo.publicKey
virtualMachineName: spacestationVirtualMachineName
}
}
resource removeSshKeyGenResultScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: removeSshKeyGenResultScriptName
location: resourceGroup().location
kind: 'AzureCLI'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${userAssignedIdentity.id}': {}
}
}
properties: {
azCliVersion: '2.25.0'
cleanupPreference: 'OnSuccess'
retentionInterval: 'P1D' // retain script for 1 day
scriptContent: removeSshKeyGenScriptWithGroupNameAndScriptName
timeout: 'PT30M' // timeout after 30 minutes
}
dependsOn: [ // make sure to run this last
userAssignedIdentity
roleAssignment
keyvault
generateSshKeyScript
publicKeySecret
privateKeySecret
groundstation
spacestation
]
}
//////////
// OUTPUT
//////////
output groundstationAdminUsername string = adminUsername
output groundstationHostName string = groundstation.outputs.hostName
output keyvaultName string = keyvault.name
output privateKeySecretName string = privateKeySecretName
output spacestationAdminUsername string = adminUsername
output spacestationHostName string = spacestation.outputs.hostName