Skip to content

Commit ea8856b

Browse files
emasabdjam-nbs
andauthored
Allow proxy to be specified (#2127) (#2128)
Co-authored-by: David Martin <[email protected]> Co-authored-by: David Martin <[email protected]>
1 parent bfd88b3 commit ea8856b

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/Confluent.SchemaRegistry/CachedSchemaRegistryClient.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Threading.Tasks;
2424
using System.Linq;
2525
using System;
26+
using System.Net;
2627
using System.Threading;
2728
using System.Security.Cryptography.X509Certificates;
2829
using Confluent.Kafka;
@@ -166,8 +167,12 @@ private static SubjectNameStrategyDelegate GetValueSubjectNameStrategy(
166167
/// <param name="authenticationHeaderValueProvider">
167168
/// The authentication header value provider
168169
/// </param>
170+
/// <param name="proxy">
171+
/// The proxy server to use for connections
172+
/// </param>
169173
public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> config,
170-
IAuthenticationHeaderValueProvider authenticationHeaderValueProvider)
174+
IAuthenticationHeaderValueProvider authenticationHeaderValueProvider,
175+
IWebProxy proxy = null)
171176
{
172177
if (config == null)
173178
{
@@ -352,13 +357,8 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
352357
}
353358

354359
var sslCaLocation = config.FirstOrDefault(prop => prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SslCaLocation).Value;
355-
if (string.IsNullOrEmpty(sslCaLocation))
356-
{
357-
this.restService = new RestService(schemaRegistryUris, timeoutMs, authenticationHeaderValueProvider, SetSslConfig(config), sslVerify);
358-
} else
359-
{
360-
this.restService = new RestService(schemaRegistryUris, timeoutMs, authenticationHeaderValueProvider, SetSslConfig(config), sslVerify, new X509Certificate2(sslCaLocation));
361-
}
360+
var sslCaCertificate = string.IsNullOrEmpty(sslCaLocation) ? null : new X509Certificate2(sslCaLocation);
361+
this.restService = new RestService(schemaRegistryUris, timeoutMs, authenticationHeaderValueProvider, SetSslConfig(config), sslVerify, sslCaCertificate, proxy);
362362
}
363363

364364
/// <summary>
@@ -372,6 +372,20 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
372372
{
373373
}
374374

375+
/// <summary>
376+
/// Initialize a new instance of the SchemaRegistryClient class.
377+
/// </summary>
378+
/// <param name="config">
379+
/// Configuration properties.
380+
/// </param>
381+
/// <param name="proxy">
382+
/// The proxy server to use for connections
383+
/// </param>
384+
public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> config, IWebProxy proxy)
385+
: this(config, null, proxy)
386+
{
387+
388+
}
375389

376390
/// <remarks>
377391
/// This is to make sure memory doesn't explode in the case of incorrect usage.

src/Confluent.SchemaRegistry/Rest/RestService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public class RestService : IRestService
5858
/// </summary>
5959
public RestService(string schemaRegistryUrl, int timeoutMs,
6060
IAuthenticationHeaderValueProvider authenticationHeaderValueProvider, List<X509Certificate2> certificates,
61-
bool enableSslCertificateVerification, X509Certificate2 sslCaCertificate = null)
61+
bool enableSslCertificateVerification, X509Certificate2 sslCaCertificate = null, IWebProxy proxy = null)
6262
{
6363
this.authenticationHeaderValueProvider = authenticationHeaderValueProvider;
6464

6565
this.clients = schemaRegistryUrl
6666
.Split(',')
6767
.Select(SanitizeUri) // need http or https - use http if not present.
68-
.Select(uri => new HttpClient(CreateHandler(certificates, enableSslCertificateVerification, sslCaCertificate))
68+
.Select(uri => new HttpClient(CreateHandler(certificates, enableSslCertificateVerification, sslCaCertificate, proxy))
6969
{
7070
BaseAddress = new Uri(uri, UriKind.Absolute), Timeout = TimeSpan.FromMilliseconds(timeoutMs)
7171
})
@@ -79,10 +79,16 @@ private static string SanitizeUri(string uri)
7979
}
8080

8181
private static HttpClientHandler CreateHandler(List<X509Certificate2> certificates,
82-
bool enableSslCertificateVerification, X509Certificate2 sslCaCertificate)
82+
bool enableSslCertificateVerification, X509Certificate2 sslCaCertificate,
83+
IWebProxy proxy)
8384
{
8485
var handler = new HttpClientHandler();
8586

87+
if (proxy != null)
88+
{
89+
handler.Proxy = proxy;
90+
}
91+
8692
if (!enableSslCertificateVerification)
8793
{
8894
handler.ServerCertificateCustomValidationCallback = (_, __, ___, ____) => { return true; };

0 commit comments

Comments
 (0)