Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure.Identity;
using Microsoft.Extensions.Azure;
using Microsoft.GS.DPSHost.AppConfiguration;
using Microsoft.GS.DPSHost.Helpers;

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

//Read ServiceConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;

namespace Microsoft.GS.DPSHost.Helpers
{
/// <summary>
/// The Azure Credential Helper class
/// </summary>
public static class AzureCredentialHelper
{
/// <summary>
/// Get the Azure Credentials based on the environment type
/// </summary>
/// <param name="clientId">The client Id in case of User assigned Managed identity</param>
/// <returns>The Credential Object</returns>
public static TokenCredential GetAzureCredential(string? clientId = null)
{
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

if (string.Equals(env, "Development", StringComparison.OrdinalIgnoreCase))
{
return new DefaultAzureCredential(); // CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
}
else
{
return clientId != null
? new ManagedIdentityCredential(clientId)
: new ManagedIdentityCredential();
}
}
}
}