Skip to content

Commit 7a6cd3b

Browse files
author
hoguni
committed
fix hostname verification
1 parent de94bc5 commit 7a6cd3b

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/ClientConnection.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
201201
} else {
202202
ctx.set_verify_mode(boost::asio::ssl::context::verify_peer);
203203

204-
if (clientConfiguration.isValidateHostName()) {
205-
LOG_DEBUG("Validating hostname for " << serviceUrl.host() << ":" << serviceUrl.port());
206-
ctx.set_verify_callback(boost::asio::ssl::rfc2818_verification(physicalAddress));
207-
}
208-
209204
std::string trustCertFilePath = clientConfiguration.getTlsTrustCertsFilePath();
210205
if (!trustCertFilePath.empty()) {
211206
if (file_exists(trustCertFilePath)) {
@@ -254,6 +249,11 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
254249

255250
tlsSocket_ = ExecutorService::createTlsSocket(socket_, ctx);
256251

252+
if (!clientConfiguration.isTlsAllowInsecureConnection() && clientConfiguration.isValidateHostName()) {
253+
LOG_DEBUG("Validating hostname for " << serviceUrl.host() << ":" << serviceUrl.port());
254+
tlsSocket_->set_verify_callback(boost::asio::ssl::rfc2818_verification(serviceUrl.host()));
255+
}
256+
257257
LOG_DEBUG("TLS SNI Host: " << serviceUrl.host());
258258
if (!SSL_set_tlsext_host_name(tlsSocket_->native_handle(), serviceUrl.host().c_str())) {
259259
boost::system::error_code ec{static_cast<int>(::ERR_get_error()),

tests/AuthPluginTest.cc

+32
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ TEST(AuthPluginTest, testTlsDetectPulsarSslWithHostNameValidation) {
147147
Client client(serviceUrlTls, config);
148148
std::string topicName = "persistent://private/auth/testTlsDetectPulsarSslWithHostNameValidation";
149149

150+
Producer producer;
151+
Result res = client.createProducer(topicName, producer);
152+
ASSERT_EQ(ResultOk, res);
153+
}
154+
155+
TEST(AuthPluginTest, testTlsDetectPulsarSslWithHostNameValidationMissingCertsFile) {
156+
ClientConfiguration config = ClientConfiguration();
157+
config.setTlsAllowInsecureConnection(false);
158+
config.setValidateHostName(true);
159+
config.setAuth(pulsar::AuthTls::create(clientPublicKeyPath, clientPrivateKeyPath));
160+
161+
Client client(serviceUrlTls, config);
162+
std::string topicName =
163+
"persistent://private/auth/testTlsDetectPulsarSslWithHostNameValidationMissingCertsFile";
164+
150165
Producer producer;
151166
Result res = client.createProducer(topicName, producer);
152167
ASSERT_EQ(ResultConnectError, res);
@@ -183,6 +198,23 @@ TEST(AuthPluginTest, testTlsDetectHttpsWithHostNameValidation) {
183198

184199
std::string topicName = "persistent://private/auth/test-tls-detect-https-with-hostname-validation";
185200

201+
Producer producer;
202+
Result res = client.createProducer(topicName, producer);
203+
ASSERT_EQ(ResultOk, res);
204+
}
205+
206+
TEST(AuthPluginTest, testTlsDetectHttpsWithHostNameValidationMissingCertsFile) {
207+
ClientConfiguration config = ClientConfiguration();
208+
config.setUseTls(true); // shouldn't be needed soon
209+
config.setTlsAllowInsecureConnection(false);
210+
config.setAuth(pulsar::AuthTls::create(clientPublicKeyPath, clientPrivateKeyPath));
211+
config.setValidateHostName(true);
212+
213+
Client client(serviceUrlHttps, config);
214+
215+
std::string topicName =
216+
"persistent://private/auth/test-tls-detect-https-with-hostname-validation-missing-certs-file";
217+
186218
Producer producer;
187219
Result res = client.createProducer(topicName, producer);
188220
ASSERT_NE(ResultOk, res);

0 commit comments

Comments
 (0)