Skip to content

Commit 6df05a1

Browse files
Add separated TLS transport configs (#252)
1 parent e463ef3 commit 6df05a1

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

pulsar/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ def __init__(self, service_url,
489489
tls_validate_hostname=False,
490490
logger=None,
491491
connection_timeout_ms=10000,
492-
listener_name=None
492+
listener_name=None,
493+
tls_private_key_file_path: Optional[str] = None,
494+
tls_certificate_file_path: Optional[str] = None,
493495
):
494496
"""
495497
Create a new Pulsar client instance.
@@ -555,6 +557,10 @@ def __init__(self, service_url,
555557
Listener name for lookup. Clients can use listenerName to choose one of the listeners as
556558
the service URL to create a connection to the broker as long as the network is accessible.
557559
``advertisedListeners`` must be enabled in broker side.
560+
tls_private_key_file_path: str, optional
561+
The path to the TLS private key file
562+
tls_certificate_file_path: str, optional
563+
The path to the TLS certificate file.
558564
"""
559565
_check_type(str, service_url, 'service_url')
560566
_check_type_or_none(Authentication, authentication, 'authentication')
@@ -570,6 +576,8 @@ def __init__(self, service_url,
570576
_check_type(bool, tls_allow_insecure_connection, 'tls_allow_insecure_connection')
571577
_check_type(bool, tls_validate_hostname, 'tls_validate_hostname')
572578
_check_type_or_none(str, listener_name, 'listener_name')
579+
_check_type_or_none(str, tls_private_key_file_path, 'tls_private_key_file_path')
580+
_check_type_or_none(str, tls_certificate_file_path, 'tls_certificate_file_path')
573581

574582
conf = _pulsar.ClientConfiguration()
575583
if authentication:
@@ -601,6 +609,10 @@ def __init__(self, service_url,
601609
conf.tls_trust_certs_file_path(certifi.where())
602610
conf.tls_allow_insecure_connection(tls_allow_insecure_connection)
603611
conf.tls_validate_hostname(tls_validate_hostname)
612+
if tls_private_key_file_path is not None:
613+
conf.tls_private_key_file_path(tls_private_key_file_path)
614+
if tls_certificate_file_path is not None:
615+
conf.tls_certificate_file_path(tls_certificate_file_path)
604616
self._client = _pulsar.Client(service_url, conf)
605617
self._consumers = []
606618

src/config.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ void export_config(py::module_& m) {
166166
return_value_policy::copy)
167167
.def("tls_trust_certs_file_path", &ClientConfiguration::setTlsTrustCertsFilePath,
168168
return_value_policy::reference)
169+
.def("tls_private_key_file_path", &ClientConfiguration::getTlsPrivateKeyFilePath,
170+
return_value_policy::copy)
171+
.def("tls_private_key_file_path", &ClientConfiguration::setTlsPrivateKeyFilePath,
172+
return_value_policy::reference)
173+
.def("tls_certificate_file_path", &ClientConfiguration::getTlsCertificateFilePath,
174+
return_value_policy::copy)
175+
.def("tls_certificate_file_path", &ClientConfiguration::setTlsCertificateFilePath,
176+
return_value_policy::reference)
169177
.def("tls_allow_insecure_connection", &ClientConfiguration::isTlsAllowInsecureConnection)
170178
.def("tls_allow_insecure_connection", &ClientConfiguration::setTlsAllowInsecureConnection,
171179
return_value_policy::reference)

tests/pulsar_test.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import random
2323
import threading
2424
import logging
25+
from typing import Optional
2526
from unittest import TestCase, main
2627
import time
2728
import os
@@ -1511,10 +1512,22 @@ def _check_type_error(self, fun):
15111512
with self.assertRaises(TypeError):
15121513
fun()
15131514

1514-
def _test_basic_auth(self, id, auth):
1515-
client = Client(self.adminUrl, authentication=auth)
1516-
1517-
topic = "persistent://private/auth/my-python-topic-basic-auth-" + str(id)
1515+
def _test_basic_auth(self, topic_id: int, auth,
1516+
use_tls: bool = False,
1517+
tls_private_key_file_path: Optional[str] = None,
1518+
tls_certificate_file_path: Optional[str] = None) -> None:
1519+
if use_tls:
1520+
service_url = self.serviceUrlTls
1521+
tls_trust_certs_file_path = CERTS_DIR + 'cacert.pem'
1522+
else:
1523+
service_url = self.adminUrl
1524+
tls_trust_certs_file_path = None
1525+
client = Client(service_url, authentication=auth,
1526+
tls_trust_certs_file_path=tls_trust_certs_file_path,
1527+
tls_private_key_file_path=tls_private_key_file_path,
1528+
tls_certificate_file_path=tls_certificate_file_path)
1529+
1530+
topic = "persistent://private/auth/my-python-topic-basic-auth-" + str(topic_id)
15181531
consumer = client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared)
15191532
producer = client.create_producer(topic)
15201533
producer.send(b"hello")
@@ -1546,6 +1559,17 @@ def test_basic_auth_method(self):
15461559
auth_params_string='{{"username": "{}","password": "{}", "method": "unknown"}}'.format(username, password)
15471560
))
15481561

1562+
def test_tls_encryption_with_other_auth(self):
1563+
self._test_basic_auth(6, AuthenticationBasic('admin', '123456'),
1564+
use_tls=True,
1565+
tls_private_key_file_path=CERTS_DIR + 'client-key.pem',
1566+
tls_certificate_file_path=CERTS_DIR + 'client-cert.pem')
1567+
with self.assertRaises(pulsar.ConnectError):
1568+
self._test_basic_auth(7, AuthenticationBasic('admin', '123456'),
1569+
use_tls=True,
1570+
tls_private_key_file_path=CERTS_DIR + 'client-cert.pem',
1571+
tls_certificate_file_path=CERTS_DIR + 'client-key.pem')
1572+
15491573
def test_invalid_basic_auth(self):
15501574
username = "invalid"
15511575
password = "123456"

0 commit comments

Comments
 (0)