Use managed identity for authentication#
Security · Container App · Rule · 2023_03 · Important
Ensure managed identity is used for authentication.
Description#
Using managed identities have the following benefits:
- Your app connects to resources with the managed identity. You don't need to manage credentials in your container app.
- You can use role-based access control to grant specific permissions to a managed identity.
- System-assigned identities are automatically created and managed. They're deleted when your container app is deleted.
- You can add and delete user-assigned identities and assign them to multiple resources. They're independent of your container app's life cycle.
- You can use managed identity to authenticate with a private Azure Container Registry without a username and password to pull containers for your Container App.
- You can use managed identity to create connections for Dapr-enabled applications via Dapr components.
Recommendation#
Consider configure a managed identity for each container app.
Examples#
Configure with Azure template#
To deploy Container Apps that pass this rule:
- Set
identity.type
toSystemAssigned
orUserAssigned
orSystemAssigned,UserAssigned
. - If
identity.type
isUserAssigned
orSystemAssigned,UserAssigned
, reference the identity withidentity.userAssignedIdentities
.
For example:
Azure Template snippet
{
"type": "Microsoft.App/containerApps",
"apiVersion": "2023-05-01",
"name": "[parameters('appName')]",
"location": "[parameters('location')]",
"identity": {
"type": "SystemAssigned"
},
"properties": {
"environmentId": "[resourceId('Microsoft.App/managedEnvironments', parameters('envName'))]",
"template": {
"revisionSuffix": "[parameters('revision')]",
"containers": "[variables('containers')]",
"scale": {
"minReplicas": 2
}
},
"configuration": {
"ingress": {
"allowInsecure": false,
"stickySessions": {
"affinity": "none"
}
}
}
},
"dependsOn": [
"[resourceId('Microsoft.App/managedEnvironments', parameters('envName'))]"
]
}
Configure with Bicep#
To deploy Container Apps that pass this rule:
- Set
identity.type
toSystemAssigned
orUserAssigned
orSystemAssigned,UserAssigned
. - If
identity.type
isUserAssigned
orSystemAssigned,UserAssigned
, reference the identity withidentity.userAssignedIdentities
.
For example:
Azure Bicep snippet
resource containerApp 'Microsoft.App/containerApps@2023-05-01' = {
name: appName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
environmentId: containerEnv.id
template: {
revisionSuffix: revision
containers: containers
scale: {
minReplicas: 2
}
}
configuration: {
ingress: {
allowInsecure: false
stickySessions: {
affinity: 'none'
}
}
}
}
}
Configure with Azure Verified Modules
A pre-validated module supported by Microsoft is available from the Azure Bicep public registry. To reference the module, please use the following syntax:
For example:
To use the latest version:
Configure with Azure Policy#
To address this issue at runtime use the following policies:
- Managed Identity should be enabled for Container Apps
/providers/Microsoft.Authorization/policyDefinitions/b874ab2d-72dd-47f1-8cb5-4a306478a4e7
Notes#
Using managed identities in scale rules isn't supported. Init containers can't access managed identities.
Links#
- SE:05 Identity and access management
- Managed identities in Azure Container Apps
- Azure deployment reference