Skip to content

Commit 7c3d7b2

Browse files
Merge pull request #6 from workcontrolgit/develop
Add Azure Key Vault to Bicep infrastructure with managed identity access
2 parents 0a50da6 + 412b1c4 commit 7c3d7b2

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

infra/main.bicep

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ param sqlAdminLogin string
5252
@secure()
5353
param sqlAdminPassword string
5454

55+
@description('Name of the Azure Key Vault')
56+
param keyVaultName string
57+
5558
// ─── App Service Plan ─────────────────────────────────────────────────────────
5659
module appServicePlan 'modules/appServicePlan.bicep' = {
5760
name: 'appServicePlan'
@@ -112,9 +115,24 @@ module sqlServer 'modules/sqlServer.bicep' = {
112115
}
113116
}
114117

118+
// ─── Key Vault ────────────────────────────────────────────────────────────────
119+
module keyVault 'modules/keyVault.bicep' = {
120+
name: 'keyVault'
121+
params: {
122+
keyVaultName: keyVaultName
123+
location: location
124+
readerPrincipalIds: [
125+
apiApp.outputs.principalId
126+
identityApp.outputs.principalId
127+
identityAdminApp.outputs.principalId
128+
]
129+
}
130+
}
131+
115132
// ─── Outputs (used by deployment workflows and post-deployment config) ─────────
116133
output apiAppUrl string = apiApp.outputs.url
117134
output identityAppUrl string = identityApp.outputs.url
118135
output identityAdminAppUrl string = identityAdminApp.outputs.url
119136
output angularAppUrl string = angularSwa.outputs.url
120137
output sqlServerFqdn string = sqlServer.outputs.sqlServerFqdn
138+
output keyVaultUri string = keyVault.outputs.uri

infra/modules/keyVault.bicep

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@description('Name of the Key Vault')
2+
param keyVaultName string
3+
4+
@description('Azure region for the Key Vault')
5+
param location string
6+
7+
@description('Principal IDs of managed identities to grant secret read access')
8+
param readerPrincipalIds array = []
9+
10+
resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' = {
11+
name: keyVaultName
12+
location: location
13+
properties: {
14+
sku: {
15+
family: 'A'
16+
name: 'standard'
17+
}
18+
tenantId: subscription().tenantId
19+
enableRbacAuthorization: true
20+
enableSoftDelete: true
21+
softDeleteRetentionInDays: 7
22+
enabledForDeployment: false
23+
enabledForTemplateDeployment: false
24+
enabledForDiskEncryption: false
25+
}
26+
}
27+
28+
// Grant each managed identity the Key Vault Secrets User role (read secrets)
29+
@batchSize(1)
30+
resource secretsUserRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for (principalId, i) in readerPrincipalIds: {
31+
// Role assignment scope must be the vault resource
32+
scope: keyVault
33+
// Deterministic GUID: vaultId + principalId
34+
name: guid(keyVault.id, principalId, '4633458b-17de-408a-b874-0445c86b69e6')
35+
properties: {
36+
// Key Vault Secrets User built-in role
37+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '4633458b-17de-408a-b874-0445c86b69e6')
38+
principalId: principalId
39+
principalType: 'ServicePrincipal'
40+
}
41+
}]
42+
43+
output id string = keyVault.id
44+
output name string = keyVault.name
45+
output uri string = keyVault.properties.vaultUri

infra/modules/webApp.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@description('Name of the Web App')
22
param webAppName string
33

4-
@description('Azure region for all resources')
4+
@description('Azure region for the Web App')
55
param location string
66

77
@description('Resource ID of the App Service Plan')
@@ -10,6 +10,9 @@ param appServicePlanId string
1010
resource webApp 'Microsoft.Web/sites@2023-01-01' = {
1111
name: webAppName
1212
location: location
13+
identity: {
14+
type: 'SystemAssigned'
15+
}
1316
properties: {
1417
serverFarmId: appServicePlanId
1518
httpsOnly: true
@@ -25,3 +28,4 @@ resource webApp 'Microsoft.Web/sites@2023-01-01' = {
2528
output id string = webApp.id
2629
output defaultHostName string = webApp.properties.defaultHostName
2730
output url string = 'https://${webApp.properties.defaultHostName}'
31+
output principalId string = webApp.identity.principalId

infra/parameters/dev.bicepparam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ param sqlServerName = 'sql-talent-dev'
1616
param apiDbName = 'sqldb-talent-api-dev'
1717
param identityDbName = 'sqldb-talent-ids-dev'
1818

19+
param keyVaultName = 'kv-talent-dev'
20+
1921
// SQL admin credentials
2022
param sqlAdminLogin = 'sqladmin'
2123
param sqlAdminPassword = readEnvironmentVariable('SQL_ADMIN_PASSWORD')

0 commit comments

Comments
 (0)