Skip to content

Commit 3a5ba7a

Browse files
committed
Integrate Azure Key Vault into UrlShortener API and update Bicep modules for role assignments
1 parent 95c4528 commit 3a5ba7a

File tree

6 files changed

+87
-31
lines changed

6 files changed

+87
-31
lines changed

UrlShortener.Api/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
using Azure.Identity;
2+
13
var builder = WebApplication.CreateBuilder(args);
24

5+
var keyVaultName = builder.Configuration["KeyVaultName"];
6+
if(!string.IsNullOrEmpty(keyVaultName))
7+
{
8+
builder.Configuration.AddAzureKeyVault(
9+
new Uri($"https://{keyVaultName}.vault.azure.net/"),
10+
new DefaultAzureCredential());
11+
}
312

413
builder.Services.AddOpenApi();
514

615
var app = builder.Build();
716

8-
// Configure the HTTP request pipeline.
917
if (app.Environment.IsDevelopment())
1018
{
1119
app.MapOpenApi();

UrlShortener.Api/UrlShortener.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.2" />
11+
<PackageReference Include="Azure.Identity" Version="1.13.1" />
1012
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
1113
</ItemGroup>
1214

infrastructure/main.bicep

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ param location string = resourceGroup().location
22

33
var uniqueId = uniqueString(resourceGroup().id)
44

5-
module keyVault './modules/secrets/keyvault.bicep' = {
5+
module keyVault 'modules/secrets/keyvault.bicep' = {
66
name: 'keyVaultDeployment'
77
params: {
88
vaultName: 'kv-${uniqueId}'
@@ -13,8 +13,27 @@ module keyVault './modules/secrets/keyvault.bicep' = {
1313
module apiService 'modules/compute/appservice.bicep' = {
1414
name: 'apiDeployment'
1515
params: {
16-
location: location
1716
appName: 'api-${uniqueId}'
18-
appServiceplanName: 'plan-api-${uniqueId}'
17+
appServicePlanName: 'plan-api-${uniqueId}'
18+
location: location
19+
keyVaultName: keyVault.outputs.name
20+
}
21+
dependsOn: [
22+
keyVault
23+
]
24+
}
25+
26+
module keyVaultRoleAssignment 'modules/secrets/key-role-assignment.bicep' = {
27+
name: 'keyVaultRoleAssignmentDeployment'
28+
params: {
29+
keyVaultName: keyVault.outputs.name
30+
principalIds: [
31+
apiService.outputs.principalId
32+
// Add more principal IDs as needed
33+
]
1934
}
35+
dependsOn: [
36+
keyVault
37+
apiService
38+
]
2039
}
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
param location string = resourceGroup().location
2-
param appServiceplanName string
2+
param appServicePlanName string
33
param appName string
4+
param keyVaultName string
45

56
resource appServicePlan 'Microsoft.Web/serverfarms@2023-12-01' = {
6-
name: appServiceplanName
7-
location: location
87
kind: 'linux'
9-
sku: {
10-
name: 'B1'
11-
}
8+
location: location
9+
name: appServicePlanName
1210
properties: {
1311
reserved: true
1412
}
13+
sku: {
14+
name: 'B1'
15+
}
1516
}
1617

17-
1818
resource webApp 'Microsoft.Web/sites@2023-12-01' = {
1919
name: appName
2020
location: location
@@ -23,17 +23,26 @@ resource webApp 'Microsoft.Web/sites@2023-12-01' = {
2323
httpsOnly: true
2424
siteConfig: {
2525
linuxFxVersion: 'DOTNETCORE|9.0'
26+
appSettings: [
27+
{
28+
name: 'KeyVaultName'
29+
value: keyVaultName
30+
}
31+
]
2632
}
2733
}
34+
identity: {
35+
type: 'SystemAssigned'
36+
}
2837
}
2938

30-
3139
resource webAppConfig 'Microsoft.Web/sites/config@2023-12-01' = {
3240
parent: webApp
3341
name: 'web'
3442
properties: {
35-
scmType: 'GitHub'
43+
scmType: 'GitHub'
3644
}
3745
}
3846

39-
output webAppId string = webApp.id
47+
output appServiceId string = webApp.id
48+
output principalId string = webApp.identity.principalId
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
param keyVaultName string
2+
param principalIds array
3+
param principalType string = 'ServicePrincipal'
4+
param roleDefinitionId string = '4633458b-17de-408a-b874-0445c86b69e6'
5+
6+
resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
7+
name: keyVaultName
8+
}
9+
10+
resource keyVaultRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [
11+
for principalId in principalIds: {
12+
name: guid(keyVault.id, principalId, roleDefinitionId)
13+
scope: keyVault
14+
properties: {
15+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', roleDefinitionId)
16+
principalId: principalId
17+
principalType: principalType
18+
}
19+
}
20+
]
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
param location string = resourceGroup().location
2-
param vaultName string
3-
4-
resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' = {
5-
name: vaultName
6-
location: location
7-
properties: {
8-
sku: {
9-
family: 'A'
10-
name: 'standard'
11-
}
12-
enableRbacAuthorization: true
13-
tenantId: subscription().tenantId
14-
1+
param location string = resourceGroup().location
2+
param vaultName string
3+
4+
resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' = {
5+
name: vaultName
6+
location: location
7+
properties: {
8+
sku: {
9+
name: 'standard'
10+
family: 'A'
1511
}
12+
enableRbacAuthorization: true
13+
tenantId: subscription().tenantId
1614
}
17-
15+
}
1816

19-
output id string = keyVault.id
20-
output name string = keyVault.name
17+
output id string = keyVault.id
18+
output name string = keyVault.name

0 commit comments

Comments
 (0)