Skip to content

Commit b42d5ad

Browse files
authored
Merge pull request #99 from microsoft/users/prnikumb/defaultCredential
Removed usermanaged identity from managed identity credential as Syst…
2 parents 834b9d1 + 780e8ee commit b42d5ad

19 files changed

Lines changed: 67 additions & 57 deletions

src/service/API/Microsoft.FeatureFlighting.API.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<PackageReference Include="AppInsights.EnterpriseTelemetry.AspNetCore.Extension" Version="6.0.0" />
2222
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
2323
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
24-
<PackageReference Include="Azure.Identity" Version="1.13.1" />
24+
<PackageReference Include="Azure.Identity" Version="1.14.0" />
2525
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.12" />
2626
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
2727
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
@@ -45,6 +45,7 @@
4545
</ItemGroup>
4646

4747
<ItemGroup>
48+
<ProjectReference Include="..\Common\Microsoft.FeatureFlighting.Common.csproj" />
4849
<ProjectReference Include="..\Domain\Microsoft.FeatureFlighting.Core.csproj" />
4950
<ProjectReference Include="..\Infrastructure\Microsoft.FeatureFlighting.Infrastructure.csproj" />
5051
</ItemGroup>

src/service/API/Program.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Azure.Extensions.AspNetCore.Configuration.Secrets;
1111
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
1212
using Azure.Core;
13-
13+
using Microsoft.FeatureFlighting.Common;
1414
namespace Microsoft.PS.Services.FlightingService.Api
1515
{
1616
[ExcludeFromCodeCoverage]
@@ -38,14 +38,9 @@ public static IHostBuilder CreateHostBuilder(string[] args)
3838

3939
private static void AddKeyVault(IConfigurationBuilder config)
4040
{
41-
var builtConfig = config.Build();
41+
var builtConfig = config.Build();
4242
TokenCredential credential;
43-
#if DEBUG
44-
credential = new VisualStudioCredential();
45-
#else
46-
credential = new ManagedIdentityCredential(
47-
ManagedIdentityId.FromUserAssignedClientId(builtConfig["UserAssignedClientId"]));
48-
#endif
43+
credential = ManagedIdentityHelper.GetTokenCredential();
4944

5045
config.AddAzureKeyVault(
5146
new SecretClient(
@@ -66,15 +61,9 @@ private static void AddAzureAppConfiguration(IConfigurationBuilder config)
6661
string appConfigurationUri = builtConfig["AzureAppConfigurationUri"];
6762
string flightingAppConfigLabel = builtConfig["AppConfiguration:FeatureFlightsLabel"];
6863
string configurationCommonLabel = builtConfig["AppConfiguration:ConfigurationCommonLabel"];
69-
string configurationEnvLabel = builtConfig["AppConfiguration:ConfigurationEnvLabel"];
64+
string configurationEnvLabel = builtConfig["AppConfiguration:ConfigurationEnvLabel"];
7065
TokenCredential credential;
71-
#if DEBUG
72-
credential = new VisualStudioCredential();
73-
#else
74-
credential = new ManagedIdentityCredential(
75-
ManagedIdentityId.FromUserAssignedClientId(builtConfig["UserAssignedClientId"]));
76-
#endif
77-
66+
credential = ManagedIdentityHelper.GetTokenCredential();
7867
config.AddAzureAppConfiguration(options =>
7968
{
8069
options

src/service/Common/Authentication/ITokenGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ public interface ITokenGenerator
1212
/// </summary>
1313
/// <param name="authority">Authority to generate the token</param>
1414
/// <param name="clientId">ID of the application for generating the token</param>
15-
/// <param name="resourceId">Resource ID for which the token is generated</param>
16-
/// <param name="userAssignedClientId">user Assigned Client Id</param>
15+
/// <param name="resourceId">Resource ID for which the token is generated</param>
1716
/// <returns>Bearer token</returns>
18-
Task<string> GenerateToken(string authority, string clientId, string resourceId, string userAssignedClientId);
17+
Task<string> GenerateToken(string authority, string clientId, string resourceId);
1918
}
2019
}

src/service/Common/Authorization/IAuthorizationService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ public interface IAuthorizationService
2929
/// </summary>
3030
/// <param name="authority">IDP authority</param>
3131
/// <param name="clientId">AAD Client ID</param>
32-
/// <param name="resourceId">AAD Client ID against which the token is acquired</param>
33-
/// <param name="userAssignedClientId">user Assigned Client Id</param>
32+
/// <param name="resourceId">AAD Client ID against which the token is acquired</param>
3433
/// <returns>Bearer token</returns>
35-
Task<string> GetAuthenticationToken(string authority, string clientId, string resourceId,string userAssignedClientId);
34+
Task<string> GetAuthenticationToken(string authority, string clientId, string resourceId);
3635

3736
/// <summary>
3837
/// Augments the user identity with the required claims
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Azure.Core;
2+
using Azure.Identity;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.FeatureFlighting.Common
10+
{
11+
public class ManagedIdentityHelper
12+
{
13+
/// <summary>
14+
/// Get the token credential based on the environment (Debug/Release).
15+
/// </summary>
16+
/// <returns>Token Credential</returns>
17+
public static TokenCredential GetTokenCredential()
18+
{
19+
TokenCredential credential = null;
20+
21+
#if DEBUG
22+
credential = new VisualStudioCredential();
23+
#else
24+
credential = new ManagedIdentityCredential();
25+
#endif
26+
27+
return credential;
28+
}
29+
}
30+
}

src/service/Common/Microsoft.FeatureFlighting.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="AppInsights.EnterpriseTelemetry" Version="2.0.0" />
1010
<PackageReference Include="Autofac" Version="6.3.0" />
11+
<PackageReference Include="Azure.Identity" Version="1.14.0" />
1112
<PackageReference Include="CQRS.Mediatr.Lite" Version="1.2.0" />
1213
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1314
</ItemGroup>

src/service/Domain/Microsoft.FeatureFlighting.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Azure.Data.AppConfiguration" Version="1.2.0" />
16+
<PackageReference Include="Azure.Identity" Version="1.14.0" />
1617
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
1718
<PackageReference Include="Microsoft.FeatureManagement" Version="2.6.1" />
1819
<PackageReference Include="Microsoft.Identity.Client" Version="4.72.1" />

src/service/Infrastructure/AppConfig/AzureConfigurationClientProvider.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Azure.Data.AppConfiguration;
44
using Azure.Identity;
55
using Microsoft.Extensions.Configuration;
6+
using Microsoft.FeatureFlighting.Common;
67
using static Microsoft.AspNetCore.Hosting.Internal.HostingApplication;
78

89
namespace Microsoft.FeatureFlighting.Infrastructure.AppConfig
@@ -33,12 +34,7 @@ public ConfigurationClient GetConfigurationClient()
3334
options.Retry.MaxRetries = 10;
3435
options.Retry.Delay = TimeSpan.FromSeconds(1);
3536
TokenCredential credential;
36-
#if DEBUG
37-
credential = new VisualStudioCredential();
38-
#else
39-
credential = new ManagedIdentityCredential(
40-
ManagedIdentityId.FromUserAssignedClientId(_configuration["UserAssignedClientId"]));
41-
#endif
37+
credential = ManagedIdentityHelper.GetTokenCredential();
4238
string appConfigUri = _configuration["AzureAppConfigurationUri"];
4339
_configurationClient = new ConfigurationClient(new Uri(appConfigUri), credential, options);
4440
return _configurationClient;

src/service/Infrastructure/Authentication/AadTokenGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Azure.Core;
1111
using Azure.Identity;
1212
using System.Threading;
13+
using Microsoft.FeatureFlighting.Common;
1314

1415
namespace Microsoft.FeatureFlighting.Infrastructure.Authentication
1516
{
@@ -26,17 +27,17 @@ public AadTokenGenerator()
2627
}
2728

2829
// <inheritdoc/>
29-
public async Task<string> GenerateToken(string authority, string clientId, string resourceId, string userAssignedClientId)
30+
public async Task<string> GenerateToken(string authority, string clientId, string resourceId)
3031
{
31-
IConfidentialClientApplication client = GetOrCreateConfidentialApp(authority, clientId, userAssignedClientId);
32+
IConfidentialClientApplication client = GetOrCreateConfidentialApp(authority, clientId);
3233
var scopes = new string[] { resourceId };
3334
AuthenticationResult authenticationResult = await client
3435
.AcquireTokenForClient(scopes)
3536
.ExecuteAsync();
3637
return authenticationResult.AccessToken;
3738
}
3839

39-
private IConfidentialClientApplication GetOrCreateConfidentialApp(string authority, string clientId, string userAssignedClientId)
40+
private IConfidentialClientApplication GetOrCreateConfidentialApp(string authority, string clientId)
4041
{
4142
string confidentialAppCacheKey = CreateConfidentialAppCacheKey(authority, clientId);
4243
if (_cache.ContainsKey(confidentialAppCacheKey))
@@ -58,8 +59,7 @@ private IConfidentialClientApplication GetOrCreateConfidentialApp(string authori
5859
return client;
5960

6061
#else
61-
var credential = new ManagedIdentityCredential(userAssignedClientId);
62-
62+
var credential = ManagedIdentityHelper.GetTokenCredential();
6363
IConfidentialClientApplication client =
6464
ConfidentialClientApplicationBuilder
6565
.Create(clientId)

src/service/Infrastructure/Authorization/AuthorizationService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Azure.Identity;
1616
using Azure.Core;
1717
using System.Threading;
18+
using Microsoft.FeatureFlighting.Common;
1819

1920
[assembly: InternalsVisibleTo("Microsoft.FeatureFlighting.Infrastructure.Tests")]
2021

@@ -75,14 +76,14 @@ public bool IsAuthorized(string appName)
7576
return false;
7677
}
7778

78-
public async Task<string> GetAuthenticationToken(string authority, string clientId, string resourceId,string userAssignedClientId)
79+
public async Task<string> GetAuthenticationToken(string authority, string clientId, string resourceId)
7980
{
8081
AuthenticationResult authenticationResult;
8182
const string MsalScopeSuffix = "/.default";
8283
string bearerToken = null;
8384
try
8485
{
85-
IConfidentialClientApplication app = GetOrCreateConfidentialApp(authority, clientId, userAssignedClientId);
86+
IConfidentialClientApplication app = GetOrCreateConfidentialApp(authority, clientId);
8687
if (app != null)
8788
{
8889
var scopes = new[] { resourceId + MsalScopeSuffix };
@@ -97,7 +98,7 @@ public async Task<string> GetAuthenticationToken(string authority, string client
9798
return bearerToken;
9899
}
99100

100-
private IConfidentialClientApplication GetOrCreateConfidentialApp(string authority, string clientId,string userAssignedClientId)
101+
private IConfidentialClientApplication GetOrCreateConfidentialApp(string authority, string clientId)
101102
{
102103
string confidentialAppCacheKey = $"{authority}-{clientId}";
103104
if (_confidentialApps.ContainsKey(confidentialAppCacheKey))
@@ -115,7 +116,7 @@ private IConfidentialClientApplication GetOrCreateConfidentialApp(string authori
115116
_confidentialApps.TryAdd(confidentialAppCacheKey, app);
116117
return app;
117118
#else
118-
var credential = new ManagedIdentityCredential(userAssignedClientId);
119+
var credential = ManagedIdentityHelper.GetTokenCredential();
119120
IConfidentialClientApplication app =
120121
ConfidentialClientApplicationBuilder
121122
.Create(clientId)

0 commit comments

Comments
 (0)