Skip to content

Commit c72ca85

Browse files
authored
Enhances proxy configuration and removes custom logging (#225)
Improves HTTP proxy configuration to support authenticated proxies. Parses username and password from the proxy URI to set credentials, while redacting them from the URI to prevent sensitive data logging. Sets `BypassProxyOnLocal` to `true` by default, ensuring local requests bypass the proxy. Removes the `ProxyLoggingHandler` as it is no longer necessary, simplifying the HTTP client pipeline. Relates to CDMS-1245
1 parent c6c499c commit c72ca85

File tree

4 files changed

+31
-51
lines changed

4 files changed

+31
-51
lines changed

BtmsGateway/Config/ConfigureServices.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public static void AddServices(this WebApplicationBuilder builder)
3535
Proxy.DefaultCdsHttpClientRetries
3636
);
3737

38-
builder.Services.AddTransient<ProxyLoggingHandler>();
39-
4038
HttpRoutedClientWithRetryBuilder = builder.Services.AddHttpProxyRoutedClientWithRetry(httpClientTimeoutSeconds);
4139
HttpClientWithRetryBuilder = builder.Services.AddHttpProxyClientWithRetry(
4240
httpClientTimeoutSeconds,

BtmsGateway/Utils/Http/Proxy.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Polly.Extensions.Http;
66
using Polly.Retry;
77
using Polly.Timeout;
8-
using Serilog;
8+
using Environment = System.Environment;
99

1010
namespace BtmsGateway.Utils.Http;
1111

@@ -25,7 +25,6 @@ public static IHttpClientBuilder AddHttpProxyClientWithoutRetry(this IServiceCol
2525
return services
2626
.AddHttpClient(ProxyClientWithoutRetry)
2727
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler)
28-
.AddHttpMessageHandler<ProxyLoggingHandler>()
2928
.ConfigureHttpClient(client => client.Timeout = ConfigureHealthChecks.Timeout);
3029
}
3130

@@ -48,7 +47,6 @@ int httpClientTimeoutInSeconds
4847
return services
4948
.AddHttpClient(RoutedClientWithRetry)
5049
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler)
51-
.AddHttpMessageHandler<ProxyLoggingHandler>()
5250
.AddPolicyHandler(strategy);
5351
}
5452

@@ -71,7 +69,6 @@ int httpClientRetryCount
7169

7270
return services
7371
.AddHttpClient(CdsProxyClientWithRetry)
74-
.AddHttpMessageHandler<ProxyLoggingHandler>()
7572
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler)
7673
.AddPolicyHandler(strategy);
7774
}
@@ -91,13 +88,36 @@ public static HttpClientHandler CreateHttpClientHandler(string? proxyUri)
9188

9289
public static WebProxy CreateProxy(string? proxyUri)
9390
{
94-
Log.Logger.Information("Proxy Uri from ENV: {ProxyUri}", proxyUri);
95-
var proxy = new WebProxy { BypassProxyOnLocal = false };
91+
var proxy = new WebProxy { BypassProxyOnLocal = true };
9692
if (proxyUri != null)
9793
{
98-
proxy.Address = new Uri(proxyUri, UriKind.RelativeOrAbsolute);
94+
ConfigureProxy(proxy, proxyUri);
9995
}
100-
Log.Logger.Information("WebProxy.Address: {ProxyUri}", proxy.Address);
10196
return proxy;
10297
}
98+
99+
public static void ConfigureProxy(WebProxy proxy, string proxyUri)
100+
{
101+
var uri = new UriBuilder(proxyUri);
102+
103+
var credentials = GetCredentialsFromUri(uri);
104+
if (credentials != null)
105+
{
106+
proxy.Credentials = credentials;
107+
}
108+
109+
// Remove credentials from URI to so they don't get logged.
110+
uri.UserName = "";
111+
uri.Password = "";
112+
proxy.Address = uri.Uri;
113+
}
114+
115+
private static NetworkCredential? GetCredentialsFromUri(UriBuilder uri)
116+
{
117+
var username = uri.UserName;
118+
var password = uri.Password;
119+
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
120+
return null;
121+
return new NetworkCredential(username, password);
122+
}
103123
}

BtmsGateway/Utils/Http/ProxyLoggingHandler.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/BtmsGateway.Test/Http/ProxyTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using BtmsGateway.Utils.Http;
22
using FluentAssertions;
33

4-
namespace BtmsGateway.Test.Http;
5-
64
public class ProxyTest
75
{
86
private const string ProxyUri = "http://user:password@localhost:8080";
@@ -31,8 +29,8 @@ public void ProxyShouldBypassLocal()
3129
{
3230
var proxy = Proxy.CreateProxy(ProxyUri);
3331

34-
proxy.BypassProxyOnLocal.Should().BeFalse();
35-
proxy.IsBypassed(new Uri(Localhost)).Should().BeFalse();
32+
proxy.BypassProxyOnLocal.Should().BeTrue();
33+
proxy.IsBypassed(new Uri(Localhost)).Should().BeTrue();
3634
proxy.IsBypassed(new Uri("https://defra.gov.uk")).Should().BeFalse();
3735
}
3836

@@ -46,7 +44,7 @@ public void HandlerShouldHaveProxy()
4644
handler.Proxy?.Credentials.Should().BeNull();
4745
handler.Proxy?.GetProxy(new Uri(Localhost)).Should().NotBeNull();
4846
handler.Proxy?.GetProxy(new Uri("http://google.com")).Should().NotBeNull();
49-
handler.Proxy?.GetProxy(new Uri(Localhost))?.AbsoluteUri.Should().Be(LocalProxy);
47+
handler.Proxy?.GetProxy(new Uri(Localhost))?.AbsoluteUri.Should().Be(Localhost);
5048
handler.Proxy?.GetProxy(new Uri("http://google.com"))?.AbsoluteUri.Should().Be(LocalProxy);
5149
}
5250
}

0 commit comments

Comments
 (0)