Skip to content

Commit

Permalink
Add flag to allow TLS hostname validation to be disabled in python (a…
Browse files Browse the repository at this point in the history
…pache#4217)

* Add flag to allow TLS hostname validation to be disabled in python

* Update default to false
  • Loading branch information
ivankelly authored and merlimat committed May 9, 2019
1 parent 4ddd4b7 commit 345e9ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pulsar-client-cpp/lib/HTTPLookupService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ HTTPLookupService::HTTPLookupService(const std::string &lookupUrl,
lookupTimeoutInSeconds_(clientConfiguration.getOperationTimeoutSeconds()),
isUseTls_(clientConfiguration.isUseTls()),
tlsAllowInsecure_(clientConfiguration.isTlsAllowInsecureConnection()),
tlsTrustCertsFilePath_(clientConfiguration.getTlsTrustCertsFilePath()) {
tlsTrustCertsFilePath_(clientConfiguration.getTlsTrustCertsFilePath()),
tlsValidateHostname_(clientConfiguration.isValidateHostName()) {
if (lookupUrl[lookupUrl.length() - 1] == '/') {
// Remove trailing '/'
adminUrl_ = lookupUrl.substr(0, lookupUrl.length() - 1);
Expand Down Expand Up @@ -225,6 +226,8 @@ Result HTTPLookupService::sendHTTPRequest(const std::string completeUrl, std::st
curl_easy_setopt(handle, CURLOPT_CAINFO, tlsTrustCertsFilePath_.c_str());
}

curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, tlsValidateHostname_ ? 1L : 0L);

if (authDataContent->hasDataForTls()) {
curl_easy_setopt(handle, CURLOPT_SSLCERT, authDataContent->getTlsCertificates().c_str());
curl_easy_setopt(handle, CURLOPT_SSLKEY, authDataContent->getTlsPrivateKey().c_str());
Expand Down
1 change: 1 addition & 0 deletions pulsar-client-cpp/lib/HTTPLookupService.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class HTTPLookupService : public LookupService, public std::enable_shared_from_t
bool tlsAllowInsecure_;
bool isUseTls_;
std::string tlsTrustCertsFilePath_;
bool tlsValidateHostname_;

static LookupDataResultPtr parsePartitionData(const std::string&);
static LookupDataResultPtr parseLookupData(const std::string&);
Expand Down
9 changes: 8 additions & 1 deletion pulsar-client-cpp/python/pulsar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ def __init__(self, service_url,
log_conf_file_path=None,
use_tls=False,
tls_trust_certs_file_path=None,
tls_allow_insecure_connection=False
tls_allow_insecure_connection=False,
tls_validate_hostname=False,
):
"""
Create a new Pulsar client instance.
Expand Down Expand Up @@ -329,6 +330,10 @@ def __init__(self, service_url,
* `tls_allow_insecure_connection`:
Configure whether the Pulsar client accepts untrusted TLS certificates
from the broker.
* `tls_validate_hostname`:
Configure whether the Pulsar client validates that the hostname of the
endpoint, matches the common name on the TLS certificate presented by
the endpoint.
"""
_check_type(str, service_url, 'service_url')
_check_type_or_none(Authentication, authentication, 'authentication')
Expand All @@ -340,6 +345,7 @@ def __init__(self, service_url,
_check_type(bool, use_tls, 'use_tls')
_check_type_or_none(str, tls_trust_certs_file_path, 'tls_trust_certs_file_path')
_check_type(bool, tls_allow_insecure_connection, 'tls_allow_insecure_connection')
_check_type(bool, tls_validate_hostname, 'tls_validate_hostname')

conf = _pulsar.ClientConfiguration()
if authentication:
Expand All @@ -357,6 +363,7 @@ def __init__(self, service_url,
else:
conf.tls_trust_certs_file_path(certifi.where())
conf.tls_allow_insecure_connection(tls_allow_insecure_connection)
conf.tls_validate_hostname(tls_validate_hostname)
self._client = _pulsar.Client(service_url, conf)
self._consumers = []

Expand Down

0 comments on commit 345e9ab

Please sign in to comment.