Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use user assigned identity for all applications #1580

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions .azure/applications/graphql/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ resource containerAppEnvironment 'Microsoft.App/managedEnvironments@2024-03-01'
name: containerAppEnvironmentName
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${namePrefix}-graphql-identity'
location: location
tags: tags
}

var containerAppEnvVars = [
{
name: 'ASPNETCORE_ENVIRONMENT'
Expand All @@ -74,6 +80,10 @@ var containerAppEnvVars = [
name: 'AZURE_APPCONFIG_URI'
value: appConfiguration.properties.endpoint
}
{
name: 'AZURE_CLIENT_ID'
value: managedIdentity.properties.clientId
}
]

var port = 8080
Expand Down Expand Up @@ -157,22 +167,23 @@ module containerApp '../../modules/containerApp/main.bicep' = {
probes: probes
port: port
scale: scale
userAssignedIdentityId: managedIdentity.id
}
}

module keyVaultReaderAccessPolicy '../../modules/keyvault/addReaderRoles.bicep' = {
name: 'keyVaultReaderAccessPolicy-${containerAppName}'
params: {
keyvaultName: environmentKeyVaultResource.name
principalIds: [containerApp.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

module appConfigReaderAccessPolicy '../../modules/appConfiguration/addReaderRoles.bicep' = {
name: 'appConfigReaderAccessPolicy-${containerAppName}'
params: {
appConfigurationName: appConfigurationName
principalIds: [containerApp.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ resource containerAppEnvironment 'Microsoft.App/managedEnvironments@2024-03-01'
name: containerAppEnvironmentName
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${namePrefix}-sync-sr-mappings-identity'
location: location
tags: tags
}

var containerAppEnvVars = [
{
name: 'Infrastructure__DialogDbConnectionString'
Expand All @@ -63,6 +69,10 @@ var containerAppEnvVars = [
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
value: appInsightConnectionString
}
{
name: 'AZURE_CLIENT_ID'
value: managedIdentity.properties.clientId
}
]

// Base URL for accessing secrets in the Key Vault
Expand Down Expand Up @@ -94,16 +104,17 @@ module migrationJob '../../modules/containerAppJob/main.bicep' = {
tags: tags
cronExpression: jobSchedule
args: 'sync-subject-resource-mappings'
userAssignedIdentityId: managedIdentity.id
}
}

module keyVaultReaderAccessPolicy '../../modules/keyvault/addReaderRoles.bicep' = {
name: 'keyVaultReaderAccessPolicy-${name}'
params: {
keyvaultName: environmentKeyVaultName
principalIds: [migrationJob.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

output identityPrincipalId string = migrationJob.outputs.identityPrincipalId
output identityPrincipalId string = managedIdentity.properties.principalId
output name string = migrationJob.outputs.name
15 changes: 13 additions & 2 deletions .azure/applications/web-api-eu/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ resource containerAppEnvironment 'Microsoft.App/managedEnvironments@2024-03-01'
name: containerAppEnvironmentName
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${namePrefix}-webapi-eu-identity'
location: location
tags: tags
}

var containerAppEnvVars = [
{
name: 'ASPNETCORE_ENVIRONMENT'
Expand All @@ -77,6 +83,10 @@ var containerAppEnvVars = [
name: 'ASPNETCORE_URLS'
value: 'http://+:8080'
}
{
name: 'AZURE_CLIENT_ID'
value: managedIdentity.properties.clientId
}
]

@description('The scaling configuration for the container app')
Expand Down Expand Up @@ -159,22 +169,23 @@ module containerApp '../../modules/containerApp/main.bicep' = {
probes: probes
revisionSuffix: revisionSuffix
scale: scale
userAssignedIdentityId: managedIdentity.id
}
}

module keyVaultReaderAccessPolicy '../../modules/keyvault/addReaderRoles.bicep' = {
name: 'keyVaultReaderAccessPolicy-${containerAppName}'
params: {
keyvaultName: environmentKeyVaultResource.name
principalIds: [containerApp.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

module appConfigReaderAccessPolicy '../../modules/appConfiguration/addReaderRoles.bicep' = {
name: 'appConfigReaderAccessPolicy-${containerAppName}'
params: {
appConfigurationName: appConfigurationName
principalIds: [containerApp.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

Expand Down
15 changes: 13 additions & 2 deletions .azure/applications/web-api-migration-job/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ resource containerAppEnvironment 'Microsoft.App/managedEnvironments@2024-03-01'
name: containerAppEnvironmentName
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${namePrefix}-migration-job-identity'
location: location
tags: tags
}

var containerAppEnvVars = [
{
name: 'Infrastructure__DialogDbConnectionString'
secretRef: 'dbconnectionstring'
}
{
name: 'AZURE_CLIENT_ID'
value: managedIdentity.properties.clientId
}
]

// https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-deployment#example-1
Expand All @@ -62,16 +72,17 @@ module migrationJob '../../modules/containerAppJob/main.bicep' = {
environmentVariables: containerAppEnvVars
secrets: secrets
tags: tags
userAssignedIdentityId: managedIdentity.id
}
}

module keyVaultReaderAccessPolicy '../../modules/keyvault/addReaderRoles.bicep' = {
name: 'keyVaultReaderAccessPolicy-${name}'
params: {
keyvaultName: environmentKeyVaultName
principalIds: [migrationJob.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

output identityPrincipalId string = migrationJob.outputs.identityPrincipalId
output identityPrincipalId string = managedIdentity.properties.principalId
output name string = migrationJob.outputs.name
15 changes: 13 additions & 2 deletions .azure/applications/web-api-so/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ resource containerAppEnvironment 'Microsoft.App/managedEnvironments@2024-03-01'
name: containerAppEnvironmentName
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: '${namePrefix}-webapi-so-identity'
location: location
tags: tags
}

var containerAppEnvVars = [
{
name: 'ASPNETCORE_ENVIRONMENT'
Expand All @@ -105,6 +111,10 @@ var containerAppEnvVars = [
name: 'ASPNETCORE_URLS'
value: 'http://+:8080'
}
{
name: 'AZURE_CLIENT_ID'
value: managedIdentity.properties.clientId
}
]

resource environmentKeyVaultResource 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
Expand Down Expand Up @@ -160,22 +170,23 @@ module containerApp '../../modules/containerApp/main.bicep' = {
port: port
revisionSuffix: revisionSuffix
scale: scale
userAssignedIdentityId: managedIdentity.id
}
}

module keyVaultReaderAccessPolicy '../../modules/keyvault/addReaderRoles.bicep' = {
name: 'keyVaultReaderAccessPolicy-${containerAppName}'
params: {
keyvaultName: environmentKeyVaultResource.name
principalIds: [containerApp.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

module appConfigReaderAccessPolicy '../../modules/appConfiguration/addReaderRoles.bicep' = {
name: 'appConfigReaderAccessPolicy-${containerAppName}'
params: {
appConfigurationName: appConfigurationName
principalIds: [containerApp.outputs.identityPrincipalId]
principalIds: [managedIdentity.properties.principalId]
}
}

Expand Down
28 changes: 12 additions & 16 deletions .azure/modules/containerApp/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ param scale Scale = {
rules: []
}

// TODO: Refactor to make userAssignedIdentityId a required parameter once all container apps use user-assigned identities
@description('The ID of the user-assigned managed identity (optional)')
param userAssignedIdentityId string = ''
@description('The ID of the user-assigned managed identity')
@minLength(1)
param userAssignedIdentityId string

// Container app revision name does not allow '.' character
var cleanedRevisionSuffix = replace(revisionSuffix, '.', '-')
Expand All @@ -81,19 +81,19 @@ var ingress = {
ipSecurityRestrictions: ipSecurityRestrictions
}

var identityConfig = empty(userAssignedIdentityId) ? {
type: 'SystemAssigned'
} : {
type: 'UserAssigned'
userAssignedIdentities: {
'${userAssignedIdentityId}': {}
}
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
name: last(split(userAssignedIdentityId, '/'))
}

resource containerApp 'Microsoft.App/containerApps@2024-03-01' = {
name: name
location: location
identity: identityConfig
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${userAssignedIdentityId}': {}
}
}
properties: {
configuration: {
ingress: ingress
Expand All @@ -116,10 +116,6 @@ resource containerApp 'Microsoft.App/containerApps@2024-03-01' = {
tags: tags
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = if (!empty(userAssignedIdentityId)) {
name: last(split(userAssignedIdentityId, '/'))
}

output identityPrincipalId string = empty(userAssignedIdentityId) ? containerApp.identity.principalId : managedIdentity.properties.principalId
output identityPrincipalId string = managedIdentity.properties.principalId
output name string = containerApp.name
output revisionName string = containerApp.properties.latestRevisionName
15 changes: 13 additions & 2 deletions .azure/modules/containerAppJob/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ param cronExpression string = ''
@description('The container args for the job (optional)')
param args string = ''

@description('The ID of the user-assigned managed identity')
@minLength(1)
param userAssignedIdentityId string

var isScheduled = !empty(cronExpression)

var scheduledJobProperties = {
Expand All @@ -42,11 +46,18 @@ var manualJobProperties = {
}
}

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
name: last(split(userAssignedIdentityId, '/'))
}

resource job 'Microsoft.App/jobs@2024-03-01' = {
name: name
location: location
identity: {
type: 'SystemAssigned'
type: 'UserAssigned'
userAssignedIdentities: {
'${userAssignedIdentityId}': {}
}
}
properties: {
configuration: union(
Expand All @@ -72,5 +83,5 @@ resource job 'Microsoft.App/jobs@2024-03-01' = {
tags: tags
}

output identityPrincipalId string = job.identity.principalId
output identityPrincipalId string = managedIdentity.properties.principalId
output name string = job.name
Loading