Skip to content

Commit 5516f95

Browse files
Merge pull request microsoft#431 from microsoft/fix/managed-identity-upgrade
fix: replace DefaultAzureCredential with ManagedIdentityCredential
2 parents 89f004d + f098a92 commit 5516f95

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

App/backend-api/Microsoft.GS.DPS.Host/AppConfiguration/AppConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Azure.Identity;
22
using Microsoft.Extensions.Azure;
33
using Microsoft.GS.DPSHost.AppConfiguration;
4+
using Microsoft.GS.DPSHost.Helpers;
45

56
namespace Microsoft.GS.DPSHost.AppConfiguration
67
{
@@ -16,7 +17,7 @@ public static void Config(IHostApplicationBuilder builder)
1617
//Read AppConfiguration with managed Identity
1718
builder.Configuration.AddAzureAppConfiguration(options =>
1819
{
19-
options.Connect(new Uri(builder.Configuration["ConnectionStrings:AppConfig"]), new DefaultAzureCredential());
20+
options.Connect(new Uri(builder.Configuration["ConnectionStrings:AppConfig"]), AzureCredentialHelper.GetAzureCredential());
2021
});
2122

2223
//Read ServiceConfiguration
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Azure.Core;
4+
using Azure.Identity;
5+
6+
namespace Microsoft.GS.DPSHost.Helpers
7+
{
8+
/// <summary>
9+
/// The Azure Credential Helper class
10+
/// </summary>
11+
public static class AzureCredentialHelper
12+
{
13+
/// <summary>
14+
/// Get the Azure Credentials based on the environment type
15+
/// </summary>
16+
/// <param name="clientId">The client Id in case of User assigned Managed identity</param>
17+
/// <returns>The Credential Object</returns>
18+
public static TokenCredential GetAzureCredential(string? clientId = null)
19+
{
20+
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
21+
22+
if (string.Equals(env, "Development", StringComparison.OrdinalIgnoreCase))
23+
{
24+
return new DefaultAzureCredential(); // CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
25+
}
26+
else
27+
{
28+
return clientId != null
29+
? new ManagedIdentityCredential(clientId)
30+
: new ManagedIdentityCredential();
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)