Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix #1897) : skip server SSL certs verification when configured #2174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 12 additions & 20 deletions src/Confluent.SchemaRegistry/Rest/RestService.cs
Expand Up @@ -57,26 +57,14 @@ internal class RestService : IRestService
/// <summary>
/// Initializes a new instance of the RestService class.
/// </summary>
public RestService(string schemaRegistryUrl, int timeoutMs, IAuthenticationHeaderValueProvider authenticationHeaderValueProvider, List<X509Certificate2> certificates, bool enableSslCertificateVerification)
public RestService(string schemaRegistryUrl, int timeoutMs, IAuthenticationHeaderValueProvider authenticationHeaderValueProvider, List<X509Certificate2> certificates, bool enableSslCertificateVerification)
{
this.authenticationHeaderValueProvider = authenticationHeaderValueProvider;

this.clients = schemaRegistryUrl
.Split(',')
.Select(SanitizeUri)// need http or https - use http if not present.
.Select(uri =>
{
HttpClient client;
                    if (certificates.Count > 0)
                    {
                        client = new HttpClient(CreateHandler(certificates, enableSslCertificateVerification)) { BaseAddress = new Uri(uri, UriKind.Absolute), Timeout = TimeSpan.FromMilliseconds(timeoutMs) };
                    }
                    else
                    {
                        client = new HttpClient() { BaseAddress = new Uri(uri, UriKind.Absolute), Timeout = TimeSpan.FromMilliseconds(timeoutMs) };
                    }
return client;
})
.Select(SanitizeUri) // need http or https - use http if not present.
.Select(uri => new HttpClient(CreateHandler(certificates, enableSslCertificateVerification)) { BaseAddress = new Uri(uri, UriKind.Absolute), Timeout = TimeSpan.FromMilliseconds(timeoutMs) })
.ToList();
}

Expand All @@ -86,18 +74,22 @@ private static string SanitizeUri(string uri)
return $"{sanitized.TrimEnd('/')}/";
}

private static HttpClientHandler CreateHandler(List<X509Certificate2> certificates, bool enableSslCertificateVerification)
private static HttpClientHandler CreateHandler(List<X509Certificate2> certificates, bool enableSslCertificateVerification)
{
    var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
var handler = new HttpClientHandler();

if (!enableSslCertificateVerification)
{
handler.ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, certChain, policyErrors) => { return true; };
}

    certificates.ForEach(c => handler.ClientCertificates.Add(c));
    return handler;
if (certificates.Count > 0)
{
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
certificates.ForEach(c => handler.ClientCertificates.Add(c));
}

return handler;
}

private RegisteredSchema SanitizeRegisteredSchema(RegisteredSchema schema)
Expand Down