-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDMS-181 attempts to proxy connections to login.microsoftonline.com v… (
#7) * CDMS-181 attempts to proxy connections to login.microsoftonline.com via squid * CDMS-181 provides the TokenCredential by using ConfidentialClientApplicationBuilder
- Loading branch information
1 parent
7d74471
commit 3e72066
Showing
8 changed files
with
121 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Btms.Azure/ConfidentialClientApplicationTokenCredential.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Azure.Core; | ||
using Btms.Azure.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Identity.Client; | ||
|
||
namespace Btms.Azure; | ||
|
||
/// <summary> | ||
/// Takes care of retriving a token via ConfidentialClientApplicationBuilder | ||
/// which allows us to inject our CDP_HTTPS_PROXY based http client. | ||
/// | ||
/// It's unclear why this isn't available out of the box! | ||
/// - IMsalHttpClientFactory isn't used by ClientSecretCredential | ||
/// - The ClientSecretCredential has an internal constructor accepting MsalConfidentialClient but nothing seems to use it | ||
/// - MsalConfidentialClient is itself internal | ||
/// </summary> | ||
/// <param name="token"></param> | ||
/// <param name="expiresOn"></param> | ||
public class ConfidentialClientApplicationTokenCredential : TokenCredential | ||
{ | ||
private readonly string[] _scopes = { "https://storage.azure.com/.default" }; | ||
|
||
private readonly IConfidentialClientApplication _app; | ||
public ConfidentialClientApplicationTokenCredential(IServiceProvider serviceProvider, IAzureConfig config) | ||
{ | ||
var httpClientFactory = serviceProvider.GetRequiredService<MsalHttpClientFactoryAdapter>(); | ||
|
||
_app = ConfidentialClientApplicationBuilder.Create(config.AzureClientId) | ||
.WithHttpClientFactory(httpClientFactory) | ||
.WithTenantId(config.AzureTenantId) | ||
.WithClientSecret(config.AzureClientSecret) | ||
.Build(); | ||
} | ||
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
var authResult = _app.AcquireTokenForClient(_scopes).ExecuteAsync(cancellationToken).Result; | ||
return ValueTask.FromResult(new AccessToken(authResult.AccessToken, authResult.ExpiresOn)); | ||
} | ||
|
||
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
var authResult = _app.AcquireTokenForClient(_scopes).ExecuteAsync(cancellationToken).Result; | ||
return new AccessToken(authResult.AccessToken, authResult.ExpiresOn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
using System.Net.Http.Headers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Identity.Client; | ||
|
||
namespace Btms.Azure.Extensions; | ||
|
||
/// <summary> | ||
/// The Azure SDK doesn't use the CDP proxied Http Client by default, so previously we used the HTTPS_CLIENT env var to | ||
/// send the requests via CDPs squid proxy. This code is intended to use the http client we already setup that uses the proxy | ||
/// when the CDP_HTTPS_PROXY env var is set. | ||
/// Code borrowed from https://anthonysimmon.com/overriding-msal-httpclient-with-ihttpclientfactory/ | ||
/// </summary> | ||
/// <param name="httpClientFactory"></param> | ||
public class MsalHttpClientFactoryAdapter(IHttpClientFactory httpClientFactory) : IMsalHttpClientFactory | ||
{ | ||
public HttpClient GetHttpClient() | ||
{ | ||
return httpClientFactory.CreateClient("Msal"); | ||
} | ||
} | ||
|
||
public static class IServiceExtensions | ||
{ | ||
public static void AddMsalHttpProxyClient(this IServiceCollection services, Func<IServiceProvider, HttpClientHandler> configurePrimaryHttpMessageHandler) | ||
{ | ||
// Dependency injection registration | ||
services.AddHttpClient("Msal") | ||
.ConfigurePrimaryHttpMessageHandler(configurePrimaryHttpMessageHandler) | ||
.ConfigureHttpClient(httpClient => | ||
{ | ||
// Default MSAL settings: | ||
// https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.61.3/src/client/Microsoft.Identity.Client/Http/HttpClientConfig.cs#L18-L20 | ||
httpClient.MaxResponseContentBufferSize = 1024 * 1024; | ||
httpClient.DefaultRequestHeaders.Accept.Clear(); | ||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
}); | ||
|
||
services.AddSingleton<MsalHttpClientFactoryAdapter>(); | ||
|
||
// services.AddSingleton(sp => new MsalCl | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters