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

CPython example not working with secure broker #62

Closed
brentru opened this issue Feb 3, 2021 · 5 comments
Closed

CPython example not working with secure broker #62

brentru opened this issue Feb 3, 2021 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@brentru
Copy link
Member

brentru commented Feb 3, 2021

This library currently works with CPython sockets connecting to a insecure MQTT broker port (1883) but falls down when attempting to connect to a secure MQTT broker port (8883):

  File "/Users/brentrubell/Desktop/github_brentru/Adafruit_CircuitPython_MiniMQTT/test_cpython.py", line 63, in <module>
    mqtt_client.connect()
  File "/Users/brentrubell/Desktop/github_brentru/Adafruit_CircuitPython_MiniMQTT/adafruit_minimqtt/adafruit_minimqtt.py", line 467, in connect
    self._sock = self._get_socket(self.broker, self.port)
  File "/Users/brentrubell/Desktop/github_brentru/Adafruit_CircuitPython_MiniMQTT/adafruit_minimqtt/adafruit_minimqtt.py", line 284, in _get_socket
    sock = self._ssl_context.wrap_socket(sock, server_hostname=host)
  File "/Users/brentrubell/Desktop/github_brentru/Adafruit_CircuitPython_MiniMQTT/adafruit_minimqtt/adafruit_minimqtt.py", line 114, in wrap_socket
    return _FakeSSLSocket(socket, self._iface.TLS_MODE)
AttributeError: 'NoneType' object has no attribute 'TLS_MODE'```
@brentru brentru added the bug Something isn't working label Feb 3, 2021
@brentru brentru self-assigned this Feb 3, 2021
@brentru
Copy link
Member Author

brentru commented Feb 3, 2021

Digging deeper, this is more an issue with the example itself and the macOS SSL installation under Python3 than the library, I'll be making a pull request shortly to fix it.

In the future, if a user on macOS gets [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123), they'll need to upgrade the certifi package by running the following command:

/Applications/Python\ 3.9/Install\ Certificates.command

@tannewt
Copy link
Member

tannewt commented Feb 3, 2021

Why are you using FakeSSLSocket when using CPython? Your self._ssl_context should be from the real ssl library.

@tannewt
Copy link
Member

tannewt commented Feb 3, 2021

FakeSSLSocket should only be used with ESP32SPI.

@brentru
Copy link
Member Author

brentru commented Feb 3, 2021

@tannewt The code which threw this error incorrectly did not pass the ssl_context from the ssl library into the MiniMQTT constructor and looked like:

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=socket,
)

Instead of:

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=socket,
    ssl_context=ssl.create_default_context(),
)

@brentru brentru changed the title Incompatible with CPython SSL Socket CPython example not working with secure broker Feb 3, 2021
@brentru
Copy link
Member Author

brentru commented Feb 3, 2021

Looking at how requests does it, the code should raise a RuntimeError that the "ssl_context must be set before using adafruit_mqtt for secure MQTT.", but it appears that the SSLContext is incorrectly set to _FakeSSLContext(_the_interface) if not set in the __init__ for this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants