Skip to content

Commit 9c6557f

Browse files
authored
Default AZURE_TOKEN_CREDENTIALS env var when running in Azure (#11832)
* Default AZURE_TOKEN_CREDENTIALS env var when running in Azure This environment variable tells DefaultAzureCredential to only use the ManagedIdentityCredential. * Fix last test
1 parent b4695fa commit 9c6557f

File tree

16 files changed

+76
-6
lines changed

16 files changed

+76
-6
lines changed

src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ protected void AddAzureClientId(IAppIdentityResource? appIdentityResource, Bicep
7575
Name = "AZURE_CLIENT_ID",
7676
Value = AllocateParameter(appIdentityResource.ClientId)
7777
});
78+
79+
// DefaultAzureCredential should only use ManagedIdentityCredential when running in Azure
80+
env.Add(new ContainerAppEnvironmentVariable
81+
{
82+
Name = "AZURE_TOKEN_CREDENTIALS",
83+
Value = "ManagedIdentityCredential"
84+
});
7885
}
7986
}
8087

src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ static FunctionCallExpression Join(BicepExpression args, string delimeter) =>
311311
Name = "AZURE_CLIENT_ID",
312312
Value = appIdentityResource.ClientId.AsProvisioningParameter(infra)
313313
});
314+
315+
// DefaultAzureCredential should only use ManagedIdentityCredential when running in Azure
316+
webSite.SiteConfig.AppSettings.Add(new AppServiceNameValuePair
317+
{
318+
Name = "AZURE_TOKEN_CREDENTIALS",
319+
Value = "ManagedIdentityCredential"
320+
});
314321
}
315322

316323
// Added appsetting to identify the resource in a specific aspire environment

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.KeyvaultReferenceHandling.verified.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ resource webapp 'Microsoft.Web/sites@2024-11-01' = {
9393
name: 'AZURE_CLIENT_ID'
9494
value: api_identity_outputs_clientid
9595
}
96+
{
97+
name: 'AZURE_TOKEN_CREDENTIALS'
98+
value: 'ManagedIdentityCredential'
99+
}
96100
{
97101
name: 'ASPIRE_ENVIRONMENT_NAME'
98102
value: 'env'
@@ -141,4 +145,4 @@ resource api_ra 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
141145
principalType: 'ServicePrincipal'
142146
}
143147
scope: webapp
144-
}
148+
}

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.AzureContainerAppsBicepGenerationIsIdempotent.verified.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ resource api 'Microsoft.App/containerApps@2025-01-01' = {
7979
name: 'AZURE_CLIENT_ID'
8080
value: api_identity_outputs_clientid
8181
}
82+
{
83+
name: 'AZURE_TOKEN_CREDENTIALS'
84+
value: 'ManagedIdentityCredential'
85+
}
8286
]
8387
}
8488
]
@@ -93,4 +97,4 @@ resource api 'Microsoft.App/containerApps@2025-01-01' = {
9397
'${api_identity_outputs_id}': { }
9498
}
9599
}
96-
}
100+
}

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.KeyVaultReferenceHandling.verified.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ resource api 'Microsoft.App/containerApps@2025-01-01' = {
7272
name: 'AZURE_CLIENT_ID'
7373
value: api_identity_outputs_clientid
7474
}
75+
{
76+
name: 'AZURE_TOKEN_CREDENTIALS'
77+
value: 'ManagedIdentityCredential'
78+
}
7579
]
7680
}
7781
]
@@ -86,4 +90,4 @@ resource api 'Microsoft.App/containerApps@2025-01-01' = {
8690
'${api_identity_outputs_id}': { }
8791
}
8892
}
89-
}
93+
}

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.ProjectWithManyReferenceTypes#00.verified.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
187187
name: 'AZURE_CLIENT_ID'
188188
value: api_identity_outputs_clientid
189189
}
190+
{
191+
name: 'AZURE_TOKEN_CREDENTIALS'
192+
value: 'ManagedIdentityCredential'
193+
}
190194
]
191195
}
192196
]
@@ -202,4 +206,4 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
202206
'${env_outputs_azure_container_registry_managed_identity_id}': { }
203207
}
204208
}
205-
}
209+
}

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.ProjectWithManyReferenceTypesAndContainerAppEnvironment#00.verified.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
187187
name: 'AZURE_CLIENT_ID'
188188
value: api_identity_outputs_clientid
189189
}
190+
{
191+
name: 'AZURE_TOKEN_CREDENTIALS'
192+
value: 'ManagedIdentityCredential'
193+
}
190194
]
191195
}
192196
]
@@ -202,4 +206,4 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
202206
'${cae_outputs_azure_container_registry_managed_identity_id}': { }
203207
}
204208
}
205-
}
209+
}

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.RoleAssignmentsWithAsExisting#00.verified.bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
5656
name: 'AZURE_CLIENT_ID'
5757
value: api_identity_outputs_clientid
5858
}
59+
{
60+
name: 'AZURE_TOKEN_CREDENTIALS'
61+
value: 'ManagedIdentityCredential'
62+
}
5963
]
6064
}
6165
]

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.RoleAssignmentsWithAsExistingCosmosDB#00.verified.bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
6262
name: 'AZURE_CLIENT_ID'
6363
value: api_identity_outputs_clientid
6464
}
65+
{
66+
name: 'AZURE_TOKEN_CREDENTIALS'
67+
value: 'ManagedIdentityCredential'
68+
}
6569
]
6670
}
6771
]

tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.RoleAssignmentsWithAsExistingRedis#00.verified.bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ resource api 'Microsoft.App/containerApps@2025-02-02-preview' = {
6262
name: 'AZURE_CLIENT_ID'
6363
value: api_identity_outputs_clientid
6464
}
65+
{
66+
name: 'AZURE_TOKEN_CREDENTIALS'
67+
value: 'ManagedIdentityCredential'
68+
}
6569
]
6670
}
6771
]

0 commit comments

Comments
 (0)