Skip to content

Commit 572df9e

Browse files
[DKM] Use ManagedIdentityCredential Instead of DefaultAzureCredential Across Repositories
1 parent dc25290 commit 572df9e

File tree

17 files changed

+102
-13
lines changed

17 files changed

+102
-13
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
public static class AzureCredentialHelper
9+
{
10+
public static TokenCredential GetAzureCredential(string clientId = null)
11+
{
12+
var env = Environment.GetEnvironmentVariable("APP_ENV") ?? "prod";
13+
14+
if (string.Equals(env, "dev", StringComparison.OrdinalIgnoreCase))
15+
{
16+
return new DefaultAzureCredential(); // For local development
17+
}
18+
else
19+
{
20+
return clientId != null
21+
? new ManagedIdentityCredential(clientId)
22+
: new ManagedIdentityCredential();
23+
}
24+
}
25+
26+
public static Task<TokenCredential> GetAzureCredentialAsync(string clientId = null)
27+
{
28+
return Task.FromResult(GetAzureCredential(clientId));
29+
}
30+
}
31+
}

App/kernel-memory/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<PackageVersion Include="AWSSDK.S3" Version="3.7.310.4" />
88
<PackageVersion Include="Azure.AI.DocumentIntelligence" Version="1.0.0" />
99
<PackageVersion Include="Azure.AI.FormRecognizer" Version="4.1.0" />
10-
<PackageVersion Include="Azure.Core" Version="1.42.0" />
10+
<PackageVersion Include="Azure.Core" Version="1.47.1" />
1111
<PackageVersion Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
1212
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
1313
<PackageVersion Include="Azure.Search.Documents" Version="11.5.1" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Azure.Core" />
11+
<PackageReference Include="Azure.Identity" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Azure.Core;
4+
using Azure.Identity;
5+
namespace Helpers;
6+
7+
public static class azure_credential_utils
8+
{
9+
public static TokenCredential GetAzureCredential(string clientId = null)
10+
{
11+
var env = Environment.GetEnvironmentVariable("APP_ENV") ?? "prod";
12+
13+
if (string.Equals(env, "dev", StringComparison.OrdinalIgnoreCase))
14+
{
15+
return new DefaultAzureCredential(); // For local development
16+
}
17+
else
18+
{
19+
return clientId != null
20+
? new ManagedIdentityCredential(clientId)
21+
: new ManagedIdentityCredential();
22+
}
23+
}
24+
25+
public static Task<TokenCredential> GetAzureCredentialAsync(string clientId = null)
26+
{
27+
return Task.FromResult(GetAzureCredential(clientId));
28+
}
29+
}

App/kernel-memory/extensions/AzureAIDocIntel/AzureAIDocIntel.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
<NoWarn>$(NoWarn);KMEXP02;CA1724;CA1308;</NoWarn>
99
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<ProjectReference Include="..\..\service\Abstractions\Abstractions.csproj" />
13-
</ItemGroup>
14-
1511
<ItemGroup>
1612
<PackageReference Include="Azure.Identity" />
1713
<PackageReference Include="Azure.AI.FormRecognizer" />
@@ -30,4 +26,9 @@
3026
<None Include="README.md" Link="README.md" Pack="true" PackagePath="." Visible="false" />
3127
</ItemGroup>
3228

29+
<ItemGroup>
30+
<ProjectReference Include="..\..\Helpers\Helpers.csproj" />
31+
<ProjectReference Include="..\..\service\Abstractions\Abstractions.csproj" />
32+
</ItemGroup>
33+
3334
</Project>

App/kernel-memory/extensions/AzureAIDocIntel/AzureAIDocIntelEngine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
using Azure;
99
using Azure.AI.FormRecognizer.DocumentAnalysis;
1010
using Azure.Identity;
11+
using Helpers;
1112
using Microsoft.Extensions.Logging;
1213
using Microsoft.KernelMemory.Diagnostics;
1314

15+
1416
namespace Microsoft.KernelMemory.DataFormats.AzureAIDocIntel;
1517

1618
/// <summary>
@@ -36,7 +38,7 @@ public AzureAIDocIntelEngine(
3638
switch (config.Auth)
3739
{
3840
case AzureAIDocIntelConfig.AuthTypes.AzureIdentity:
39-
this._recognizerClient = new DocumentAnalysisClient(new Uri(config.Endpoint), new DefaultAzureCredential());
41+
this._recognizerClient = new DocumentAnalysisClient(new Uri(config.Endpoint), azure_credential_utils.GetAzureCredential());
4042
break;
4143

4244
case AzureAIDocIntelConfig.AuthTypes.APIKey:

App/kernel-memory/extensions/AzureAISearch/AzureAISearch/AzureAISearch.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<ProjectReference Include="..\..\..\Helpers\Helpers.csproj" />
1213
<ProjectReference Include="..\..\..\service\Abstractions\Abstractions.csproj" />
1314
</ItemGroup>
1415

App/kernel-memory/extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Azure.Search.Documents.Indexes;
1515
using Azure.Search.Documents.Indexes.Models;
1616
using Azure.Search.Documents.Models;
17+
using Helpers;
1718
using Microsoft.Extensions.Logging;
1819
using Microsoft.KernelMemory.AI;
1920
using Microsoft.KernelMemory.Diagnostics;
@@ -66,7 +67,7 @@ public AzureAISearchMemory(
6667
case AzureAISearchConfig.AuthTypes.AzureIdentity:
6768
this._adminClient = new SearchIndexClient(
6869
new Uri(config.Endpoint),
69-
new DefaultAzureCredential(),
70+
azure_credential_utils.GetAzureCredential(),
7071
GetClientOptions());
7172
break;
7273

App/kernel-memory/extensions/AzureBlobs/AzureBlobs.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<ProjectReference Include="..\..\Helpers\Helpers.csproj" />
1213
<ProjectReference Include="..\..\service\Abstractions\Abstractions.csproj" />
1314
</ItemGroup>
1415

0 commit comments

Comments
 (0)