eclipse-paho / eclipse-paho/paho.mqtt.python
Help with SNI
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
Hi Team ,
i have a Multiple MQTT Broker hosted in K8's , these MQTT Brokers are behind the ingress controller
ingress controller routes the traffic to appropriate broker based on SNI
for non TLS i am able to verify the connection using below command
openssl s_client -showcerts -connect istio-test.westus2.cloudapp.azure.com:8883 -servername example1.test.com
openssl s_client -showcerts -connect istio-test.westus2.cloudapp.azure.com:8883 -servername example2.test.com
with TLS traffic i am not able to set specific SNI (servername).
By default SNI going as istio-test.westus2.cloudapp.azure.com
please fine the sample i am trying
```
import paho.mqtt.client as paho
from paho.mqtt import client as mqtt
import ssl
import time
import socket
#path_to_root_cert = "/home/challal/Downloads/cacert.pem"
#path_to_root_cert = "/home/challal/Downloads/certs/azure-iot-test-only.root.ca.cert.pem"
path_to_root_cert = "/home/challal/Downloads/test.pem"
device_id = "pub_cert"
cert_file = "/Users/l0c0gvk/Workspace/Testing/mqtttest/example_certs/device_cert_filename.pem"
key_file = "/Users/l0c0gvk/Workspace/Testing/mqtttest/example_certs/device_cert_key_filename.key"
ca_cert='/Users/l0c0gvk/Workspace/Testing/mqtttest/example_certs/root_CA_cert_filename.pem'
def on_connect(client, userdata, flags, rc):
print("Device connected with result code: " + str(rc))
def on_disconnect(client, userdata, rc):
print("Device disconnected with result code: " + str(rc))
def on_publish(client, userdata, mid):
print("Device sent message")
def sni_callback(sock, req_hostname, cb_context, as_callback=True):
print('sni_callback')
# context1 = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
# context1.load_cert_chain(certfile=cert_file,keyfile=key_file)
# context1.wrap_socket(socket.socket(socket.AF_INET),server_hostname="example1.test.com")
print('Loading certs for {}'.format(req_hostname))
# print(type(cb_context))
client = paho.Client(client_id=device_id,clean_session=True,userdata=None,protocol=mqtt.MQTTv311)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
# Set the certificate and key paths on your client
#client.tls_set(ca_certs=path_to_root_cert, certfile=cert_file, keyfile=key_file,
# cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
ssl_ctx = ssl.create_default_context(cafile=ca_cert)
ssl_ctx.check_hostname = False
# ssl_ctx.load_cert_chain(certfile=cert_file, keyfile=key_file)
# ssl_ctx.verify_mode = ssl.CERT_NONE
# client.tls_set_context(ssl_ctx)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_verify_locations(ca_cert)
context.load_cert_chain(cert_file, key_file)
#context.wrap_socket(server_hostname="example1.test.com")
context.sni_callback=sni_callback
client.tls_set_context(context)
client.tls_insecure_set(True)
client.connect("istio-test.westus2.cloudapp.azure.com", 8883,60)
print(type(client.socket))
client.loop_start()
while True:
client.publish("test_topic", "{id=123}", qos=1)
time.sleep(0.1)
```
Can some one help me here
Contributor guide
Assessment
This issue has not been assessed yet.