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

allow to use any port as TLS port #145

Merged
merged 7 commits into from
Feb 22, 2023
Merged

Conversation

vladak
Copy link
Contributor

@vladak vladak commented Jan 13, 2023

Instead of using the MQTT_TLS_PORT value as indication whether TLS should be used, this change introduces private field that is set based on the MQTT() is_ssl argument.

The downside of this approach is that if someone specifies port=1883 explicitly without setting is_ssl to False, the port will be treated as TLS port since the is_ssl is True by default and OSError("Failed SSL handshake") will be thrown, unless the broker is configured to accept TLS on that port. I have not made my mind yet whether to special case it and force to insecure so leaving it open for the reviewers.

@tekktrik tekktrik requested a review from a team January 13, 2023 21:36
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this @vladak!

I do think we want to have port 1883 be a special case that will use is_ssl=False even when the user didn't pass in is_ssl. Maybe we could have that default to None instead of True, and then if user didn't set it have it get set based on the port number (which does still default to ssl port).

The learn guide for mini mqtt that uses the example code from here directs people to test with adafruit.io and it's using 1883. So the examples don't work by default with this change, we could theoretically add is_ssl=False to them as a fix, but I think it's more likely to be expected by users that 1883 will be non-ssl by default.

@@ -194,11 +194,12 @@ def __init__(

self.port = MQTT_TCP_PORT
if is_ssl:
self._is_ssl = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written this argument doesn't actually work if user attempts to set it to False In that case if is_ssl: evaluates to False and therefore self._is_ssl never gets created and there is a exception raised when the code first tries to access it.

I tested one possible fix by changing this line and moving it to above the if statement like this:

        self._is_ssl = is_ssl
        self.port = MQTT_TCP_PORT
        if is_ssl:
            self.port = MQTT_TLS_PORT
        if port:
            self.port = port

This way self._is_ssl will always get created and set to the value that the user passed in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I used that as a basis, hopefully it looks better now.

Copy link
Contributor Author

@vladak vladak Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I added buch of tests (wink #64) that will hopefully make it more clear.

Copy link
Contributor Author

@vladak vladak Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that with the changed code, there is a change of behavior - the is_ssl has to be specified explicitly even for the default TLS port.

@vladak vladak force-pushed the tls_port_detection branch from cba3cde to f304d7f Compare February 15, 2023 19:52
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for the improvement @vladak! Also for adding a test to validate the port behavior.

I tested the latest version successfully with the native networking aio example in the repo.

@FoamyGuy FoamyGuy merged commit bfcaea6 into adafruit:main Feb 22, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 22, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_HT16K33 to 4.6.3 from 4.6.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_HT16K33#113 from adafruit/matrix_fill_color_swap

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.3.0 from 7.2.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#155 from vladak/recv_into_cleanup
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#145 from vladak/tls_port_detection

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants