Identity
Managed identity is the Azure trust anchor for a sandbox group. Attach a system-assigned or user-assigned identity, then grant it Azure roles for the resources your sandboxes call.
Managed identities are assigned to a sandbox group. All sandboxes within the group inherit the identity and can obtain Entra tokens for that identity to access other Azure resources.
Create a group with a system-assigned identity
A system-assigned identity belongs to one sandbox group. Azure deletes the identity when the group is deleted.
- Open sandboxes.azure.com/sandbox-groups and select Create.
- Choose a subscription, resource group, name, and region.
- Open Identity.
- Turn System assigned on.
- Select Create. After provisioning, copy the Principal ID from the identity card.
Setting identity at group creation time is currently supported in the portal flow and SDK only. CLI support for inline identity assignment at creation is coming soon. For now, create the group first with the CLI, then use the next section to attach an identity after creation.
from azure.identity import DefaultAzureCredential
from azure.containerapps.sandbox import SandboxGroupManagementClient
credential = DefaultAzureCredential()
mgmt = SandboxGroupManagementClient(
credential,
subscription_id=AZURE_SUBSCRIPTION_ID,
resource_group=ACA_RESOURCE_GROUP,
)
group = mgmt.begin_create_group(
"mi-demo",
ACA_SANDBOXGROUP_REGION,
identity={"type": "SystemAssigned"},
).result()
print(group.identity.get("principalId"))
Add identity to an existing group
Use this flow when the group already exists and you want to add Azure access without recreating sandboxes.
- Open the sandbox group.
- Select Identity.
- Turn System assigned on, or select Add under User assigned and choose an existing managed identity.
- Select Save.
- Wait for the identity details to appear before assigning roles.
- Bash
- PowerShell
- SDK
aca sandboxgroup identity assign \
--group mi-demo \
--system-assigned
aca sandboxgroup identity assign \
--group mi-demo \
--user-assigned "$USER_ASSIGNED_IDENTITY_RESOURCE_ID"
aca sandboxgroup identity assign `
--group mi-demo `
--system-assigned
aca sandboxgroup identity assign `
--group mi-demo `
--user-assigned $env:USER_ASSIGNED_IDENTITY_RESOURCE_ID
mgmt.patch_group_identity(
"mi-demo",
{"type": "SystemAssigned"},
)
mgmt.patch_group_identity(
"mi-demo",
{
"type": "UserAssigned",
"userAssignedIdentities": {
USER_ASSIGNED_IDENTITY_RESOURCE_ID: {},
},
},
)
Read identity details
Before you create role assignments, read the identity details. The principal ID can take a few seconds to appear after assignment.
- Open the sandbox group and select Identity.
- Review the system-assigned identity card for Status, Object (principal) ID, and Permissions. Review user-assigned identity rows for Name, Client ID, and Resource ID.
- Copy the displayed identity IDs you need for Azure role assignment.
- Bash
- PowerShell
- SDK
aca sandboxgroup identity show --group mi-demo
aca sandboxgroup identity show --group mi-demo
group = mgmt.get_group("mi-demo")
identity = group.identity or {}
print(identity.get("type"))
print(identity.get("principalId"))
print(identity.get("tenantId"))
Remove identity
Removing a system-assigned identity deletes its service principal and role assignments. Removing a user-assigned identity detaches it from the group but does not delete the identity resource.
- Open the sandbox group and select Identity.
- Turn System assigned off, or remove the user-assigned identity row.
- Select Save and confirm.
- Bash
- PowerShell
- SDK
aca sandboxgroup identity remove --group mi-demo
aca sandboxgroup identity remove --group mi-demo
mgmt.patch_group_identity("mi-demo", {"type": "None"})
mgmt.delete_group("mi-demo")
mgmt.close()
credential.close()
Connect to Azure services in app code
With managed identities, a sandbox can obtain tokens to access Azure resources that use Microsoft Entra ID, such as Azure SQL Database, Azure Key Vault, and Azure Storage. These tokens represent the application accessing the resource, and not any specific user of the application.
Sandboxes are provided with an internally accessible REST endpoint to retrieve tokens. The REST endpoint is available from within the sandbox with a standard HTTP GET request, which you can send with a generic HTTP client in your preferred language. For .NET, JavaScript, Java, and Python, the Azure Identity client library provides an abstraction over this REST endpoint. You can connect to other Azure services by adding a credential object to the service-specific client.
When using Azure Identity client library, you need to explicitly specify the user-assigned managed identity client ID.
A raw HTTP GET request looks like the following example.
Obtain the token endpoint URL from the IDENTITY_ENDPOINT environment variable. x-identity-header contains the GUID that is stored in the IDENTITY_HEADER environment variable.
GET ${IDENTITY_ENDPOINT}?resource=https://vault.azure.net&api-version=2019-08-01 HTTP/1.1
x-identity-header: ${IDENTITY_HEADER}
A response might look like this example:
HTTP/1.1 200 OK
Content-Type: application/json
{
"access_token": "eyJ0eXAi…",
"expires_on": "1586984735",
"resource": "https://vault.azure.net",
"token_type": "Bearer",
"client_id": "aaaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
}
REST endpoint reference
A sandbox with a managed identity exposes the identity endpoint by defining two environment variables:
IDENTITY_ENDPOINT: Local URL from which your container app can request tokens.IDENTITY_HEADER: A header used to help mitigate server-side request forgery (SSRF) attacks. The value is rotated by the platform.
To get a token for a resource, make an HTTP GET request to the endpoint, including the following parameters:
| Parameter name | In | Description |
|---|---|---|
| resource | Query | The Microsoft Entra resource URI of the resource for which a token should be obtained. The resource could be one of the Azure services that support Microsoft Entra authentication or any other resource URI. |
| api-version | Query | The version of the token API to be used. Use "2019-08-01" or later. |
| X-IDENTITY-HEADER | Header | The value of the IDENTITY_HEADER environment variable. This header mitigates server-side request forgery (SSRF) attacks. |
| client_id | Query | (Optional) The client ID of the user-assigned identity to be used. |
| mi_res_id | Query | (Optional) The Azure resource ID of the user-assigned identity to be used. |
If neither supported identity selector is supplied, the system-assigned identity is preferred. If none is available, the first available identity is used.
To request a specific user-assigned identity, include client_id or mi_res_id rather than relying on the default identity selection.
Control managed identity availability
By default, managed identity access tokens are available in each sandbox for every managed identity configured on the sandbox group. However, in situations where your sandboxes are running untrusted code, you may not want a managed identity token to be available inside the sandbox, but you still want the platform to use your managed identity to access other resources, such as Storage for volumes or Azure Container Registry for disk images.
You can control which managed identities are available inside a sandbox when the sandbox is created. The following example shows a sandbox group with a system-assigned managed identity and a user-assigned identity, and a sandbox that can get access tokens for only the system-assigned identity:
Sandbox group
{
"location": "eastus",
"identity": {
"type": "SystemAssigned,UserAssigned",
"userAssignedIdentities": {
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-uami": {}
}
},
"properties": {}
}
Sandbox
{
"sandboxGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.App/sandboxGroups/my-group",
"identitySettings": [
{
"identity": "system",
"lifecycle": "main"
},
{
"identity": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-uami",
"lifecycle": "none"
}
]
}