Skip to content

Commit 4e01380

Browse files
Merge pull request #14 from devopsabcs-engineering/feature/13-acr-managed-identity-auth
Fix ACR authentication for Azure Web App using managed identity
2 parents 1a577bc + aee811b commit 4e01380

3 files changed

Lines changed: 71 additions & 12 deletions

File tree

end_to_end_test.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
Modify this file to trigger workflows!
22
trigger again 2
33
fixed all references to devopsabcs-engineering
4-
trigger cicd to deploy app
4+
trigger cicd to deploy app
5+
6+
--- fixed
7+
Enabled System-Assigned Managed Identity on the Web App
8+
Granted AcrPull Role to the managed identity on your container registry
9+
Configured acrUseManagedIdentityCreds=true on the Web App
10+
Restarted the Web App to apply changes

infra/deploy.ps1

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,54 @@ else {
108108
# Show deployment outputs
109109
Write-Host ""
110110
Write-Host "Deployment outputs:" -ForegroundColor Cyan
111-
az deployment sub show `
111+
$outputs = az deployment sub show `
112112
--name $DeploymentName `
113113
--query "properties.outputs" `
114-
--output table
114+
--output json | ConvertFrom-Json
115+
116+
$outputs | ConvertTo-Json | Write-Host
117+
118+
# Configure ACR managed identity authentication
119+
if ($outputs.webAppName) {
120+
$webAppName = $outputs.webAppName.value
121+
$resourceGroupName = (az webapp show --name $webAppName --query resourceGroup -o tsv)
122+
123+
Write-Host ""
124+
Write-Host "Configuring ACR managed identity authentication..." -ForegroundColor Yellow
125+
126+
# Ensure acrUseManagedIdentityCreds is set (should be set by Bicep, but double-check)
127+
Write-Host "Verifying ACR managed identity configuration..." -ForegroundColor Cyan
128+
$config = az webapp config show --name $webAppName --resource-group $resourceGroupName --query "acrUseManagedIdentityCreds" -o tsv
129+
130+
if ($config -ne "true") {
131+
Write-Host "Setting acrUseManagedIdentityCreds=true..." -ForegroundColor Cyan
132+
az resource update `
133+
--ids "/subscriptions/$($account.id)/resourceGroups/$resourceGroupName/providers/Microsoft.Web/sites/$webAppName/config/web" `
134+
--set properties.acrUseManagedIdentityCreds=true
135+
} else {
136+
Write-Host "ACR managed identity already configured" -ForegroundColor Green
137+
}
138+
139+
# Restart the web app to apply all changes
140+
Write-Host "Restarting web app to apply configuration..." -ForegroundColor Cyan
141+
az webapp restart --name $webAppName --resource-group $resourceGroupName
142+
143+
if ($LASTEXITCODE -eq 0) {
144+
Write-Host "Web app restarted successfully!" -ForegroundColor Green
145+
Write-Host ""
146+
Write-Host "=== Configuration Summary ===" -ForegroundColor Cyan
147+
Write-Host "✓ System-assigned managed identity enabled" -ForegroundColor Green
148+
Write-Host "✓ AcrPull role assigned to managed identity" -ForegroundColor Green
149+
Write-Host "✓ ACR authentication configured to use managed identity" -ForegroundColor Green
150+
Write-Host "✓ Web app restarted" -ForegroundColor Green
151+
Write-Host ""
152+
if ($outputs.webAppUrl) {
153+
Write-Host "Web App URL: $($outputs.webAppUrl.value)" -ForegroundColor Green
154+
}
155+
} else {
156+
Write-Warning "Failed to restart web app. You may need to restart it manually."
157+
}
158+
}
115159
}
116160
else {
117161
Write-Error "Deployment failed with exit code: $LASTEXITCODE"

infra/resources.bicep

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ resource acr 'Microsoft.ContainerRegistry/registries@2023-01-01-preview' = {
2424
name: acrSku
2525
}
2626
properties: {
27-
adminUserEnabled: true
27+
adminUserEnabled: false // Use managed identity instead
2828
}
2929
}
3030

@@ -54,19 +54,12 @@ resource webApp 'Microsoft.Web/sites@2024-04-01' = {
5454
properties: {
5555
serverFarmId: appServicePlan.id
5656
siteConfig: {
57+
acrUseManagedIdentityCreds: true // Use managed identity for ACR authentication
5758
appSettings: [
5859
{
5960
name: 'DOCKER_REGISTRY_SERVER_URL'
6061
value: 'https://${acr.name}.azurecr.io'
6162
}
62-
{
63-
name: 'DOCKER_REGISTRY_SERVER_USERNAME'
64-
value: acr.properties.loginServer
65-
}
66-
{
67-
name: 'DOCKER_REGISTRY_SERVER_PASSWORD'
68-
value: acr.listCredentials().passwords[0].value
69-
}
7063
{
7164
name: 'WEBSITES_ENABLE_APP_SERVICE_STORAGE'
7265
value: 'false'
@@ -80,3 +73,19 @@ resource webApp 'Microsoft.Web/sites@2024-04-01' = {
8073
}
8174
}
8275
}
76+
77+
// Assign AcrPull role to the Web App's managed identity
78+
resource acrPullRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
79+
name: guid(acr.id, webApp.id, 'AcrPull')
80+
scope: acr
81+
properties: {
82+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '7f951dda-4ed3-4680-a7ca-43fe172d538d') // AcrPull role ID
83+
principalId: webApp.identity.principalId
84+
principalType: 'ServicePrincipal'
85+
}
86+
}
87+
88+
output webAppName string = webApp.name
89+
output webAppUrl string = 'https://${webApp.properties.defaultHostName}'
90+
output acrLoginServer string = acr.properties.loginServer
91+
output webAppPrincipalId string = webApp.identity.principalId

0 commit comments

Comments
 (0)