Skip to content

Commit cc5a8e0

Browse files
authored
Implement MSAL's IMsalSFHttpClientFactory for service fabric scenarios (Azure#49544)
1 parent 1ba8f5a commit cc5a8e0

6 files changed

+26
-16
lines changed

eng/Packages.Data.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@
168168
<!-- Other approved packages -->
169169
<PackageReference Update="Microsoft.Azure.Amqp" Version="2.6.9" />
170170
<PackageReference Update="Microsoft.Azure.WebPubSub.Common" Version="1.4.0" />
171-
<PackageReference Update="Microsoft.Identity.Client" Version="4.70.2" />
172-
<PackageReference Update="Microsoft.Identity.Client.Extensions.Msal" Version="4.70.2" />
173-
<PackageReference Update="Microsoft.Identity.Client.Broker" Version="4.70.2" />
171+
<PackageReference Update="Microsoft.Identity.Client" Version="4.71.0" />
172+
<PackageReference Update="Microsoft.Identity.Client.Extensions.Msal" Version="4.71.0" />
173+
<PackageReference Update="Microsoft.Identity.Client.Broker" Version="4.71.0" />
174174
<PackageReference Update="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.35.0" />
175175
<PackageReference Update="Microsoft.IdentityModel.Tokens" Version="6.35.0" />
176176
<PackageReference Update="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />

sdk/identity/Azure.Identity/src/Azure.Identity.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<ItemGroup>
2222
<PackageReference Include="Azure.Core" />
2323
<PackageReference Include="System.Memory" />
24-
<PackageReference Include="Microsoft.Identity.Client" />
24+
<PackageReference Include="Microsoft.Identity.Client" VersionOverride="4.71.1-preview" />
2525
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" />
2626
</ItemGroup>
2727
<ItemGroup>

sdk/identity/Azure.Identity/src/CredentialPipeline.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private CredentialPipeline(TokenCredentialOptions options)
1717
{
1818
HttpPipeline = HttpPipelineBuilder.Build(new HttpPipelineOptions(options) { RequestFailedDetailsParser = new ManagedIdentityRequestFailedDetailsParser() });
1919
Diagnostics = new ClientDiagnostics(options);
20+
ClientOptions = options;
2021
}
2122

2223
public CredentialPipeline(HttpPipeline httpPipeline, ClientDiagnostics diagnostics)
@@ -52,6 +53,8 @@ private static CredentialPipeline configureOptionsForManagedIdentity(TokenCreden
5253

5354
public HttpPipeline HttpPipeline { get; }
5455

56+
public ClientOptions ClientOptions { get; }
57+
5558
public ClientDiagnostics Diagnostics { get; }
5659

5760
public CredentialDiagnosticScope StartGetTokenScope(string fullyQualifiedMethod, TokenRequestContext context)

sdk/identity/Azure.Identity/src/HttpPipelineClientFactory.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,41 @@
33

44
using Azure.Core.Pipeline;
55
using Microsoft.Identity.Client;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
96
using System.Net.Http;
10-
using System.Text;
11-
using System.Threading;
12-
using System.Threading.Tasks;
137
using Azure.Core;
8+
using System;
9+
using System.Security.Cryptography.X509Certificates;
10+
using System.Net.Security;
1411

1512
namespace Azure.Identity
1613
{
1714
/// <summary>
1815
/// This class is an HttpClient factory which creates an HttpClient which delegates it's transport to an HttpPipeline, to enable MSAL to send requests through an Azure.Core HttpPipeline.
1916
/// </summary>
20-
internal class HttpPipelineClientFactory : IMsalHttpClientFactory
17+
internal class HttpPipelineClientFactory : IMsalSFHttpClientFactory
2118
{
2219
private readonly HttpPipeline _pipeline;
20+
private readonly ClientOptions _options;
2321

24-
public HttpPipelineClientFactory(HttpPipeline pipeline)
22+
public HttpPipelineClientFactory(HttpPipeline pipeline, ClientOptions options = null)
2523
{
2624
_pipeline = pipeline;
25+
_options = options ?? new TokenCredentialOptions();
2726
}
2827

2928
public HttpClient GetHttpClient()
3029
{
3130
return new HttpClient(new HttpPipelineMessageHandler(_pipeline));
3231
}
32+
33+
public HttpClient GetHttpClient(Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> validateServerCert)
34+
{
35+
var pipeline = HttpPipelineBuilder.Build(new HttpPipelineOptions(_options) { RequestFailedDetailsParser = new ManagedIdentityRequestFailedDetailsParser() },
36+
new HttpPipelineTransportOptions()
37+
{
38+
ServerCertificateCustomValidationCallback = (args) => validateServerCert(null, args.Certificate, args.CertificateAuthorityChain, args.SslPolicyErrors)
39+
});
40+
return new HttpClient(new HttpPipelineMessageHandler(pipeline));
41+
}
3342
}
3443
}

sdk/identity/Azure.Identity/src/MsalManagedIdentityClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected virtual ValueTask<IManagedIdentityApplication> CreateClientCoreAsync(b
6767

6868
ManagedIdentityApplicationBuilder miAppBuilder = ManagedIdentityApplicationBuilder
6969
.Create(ManagedIdentityId)
70-
.WithHttpClientFactory(new HttpPipelineClientFactory(Pipeline.HttpPipeline), false)
70+
.WithHttpClientFactory(new HttpPipelineClientFactory(Pipeline.HttpPipeline, Pipeline.ClientOptions), false)
7171
.WithLogging(AzureIdentityEventSource.Singleton, enablePiiLogging: IsSupportLoggingEnabled);
7272

7373
if (clientCapabilities.Length > 0)

sdk/identity/Azure.Identity/tests/ManagedIdentityCredentialTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ public async Task VerifyServiceFabricRequestWithResourceIdMockAsync(string clien
273273

274274
var mockTransport = new MockTransport(req =>
275275
{
276-
Assert.Fail("transport");
277276
return CreateMockResponse(200, ExpectedToken);
278277
});
279278
var options = new TokenCredentialOptions { Transport = mockTransport };
@@ -283,8 +282,7 @@ public async Task VerifyServiceFabricRequestWithResourceIdMockAsync(string clien
283282
{
284283
(Item1: null, Item2: true) => new ManagedIdentityClientOptions() { ManagedIdentityId = ManagedIdentityId.FromUserAssignedResourceId(new ResourceIdentifier(_expectedResourceId)), Pipeline = pipeline, IsForceRefreshEnabled = true },
285284
(Item1: not null, Item2: false) => new ManagedIdentityClientOptions() { ManagedIdentityId = ManagedIdentityId.FromUserAssignedClientId(clientId), Pipeline = pipeline, IsForceRefreshEnabled = true },
286-
_ => null // TODO: remove null logic and uncomment the following line once MSAL is able to take a custom transport for Service Fabric MI source
287-
//_ => new ManagedIdentityClientOptions() { ClientId = null, ResourceIdentifier = null, Pipeline = pipeline, Options = options, PreserveTransport = true, IsForceRefreshEnabled = true }
285+
_ => new ManagedIdentityClientOptions() { Pipeline = pipeline, Options = options, PreserveTransport = true, IsForceRefreshEnabled = true }
288286
};
289287
if (clientOptions == null)
290288
{

0 commit comments

Comments
 (0)