-
Notifications
You must be signed in to change notification settings - Fork 79
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
Add the possibility to use tls_set() in addition to tls_context #14
Comments
Hi svinz, thanks for raising this issue. Let me have a look. :) Indeed, the We do already have the Let me know your thoughts. |
Yeah, I saw the I made a small test by copy&paste from Alex |
Glad to hear that. :) As far as I can see, Paho's Let me offer some design suggestions: Add this functionality via keyword arguments to the from asyncio_mqtt import TlsTuple, Client
tls_tuple = TlsTuple(ca_certs=None, certfile=None, keyfile=None, cert_reqs=None, tls_version=None, ciphers=None)
client = Client("localhost", tls_tuple=tls_tuple) Where In turn, the we can them simply forward the given tuple as follows (inside the if tls_tuple is not None:
self._client.tls_set(**tls_tuple._asdict()) # Assuming that tls_tuple is a namedtuple How does that sound to you? Were you thinking of taking this in another direction? Let me know. :) |
I was more in to the not so elegant, but simple, solution, by using a class Tls_set:
@staticmethod
def tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=None, tls_version=None, ciphers=None) -> ssl.SSLContext:
if ssl is None:
raise ValueError('This platform has no SSL/TLS.')
if not hasattr(ssl, 'SSLContext'):
# Require Python version that has SSL context support in standard library
raise ValueError(
'Python 2.7.9 and 3.2 are the minimum supported versions for TLS.')
if ca_certs is None and not hasattr(ssl.SSLContext, 'load_default_certs'):
raise ValueError('ca_certs must not be None.')
# Create SSLContext object
if tls_version is None:
tls_version = ssl.PROTOCOL_TLSv1
# If the python version supports it, use highest TLS version automatically
if hasattr(ssl, "PROTOCOL_TLS"):
tls_version = ssl.PROTOCOL_TLS
context = ssl.SSLContext(tls_version)
# Configure context
if certfile is not None:
context.load_cert_chain(certfile, keyfile)
if cert_reqs == ssl.CERT_NONE and hasattr(context, 'check_hostname'):
context.check_hostname = False
context.verify_mode = ssl.CERT_REQUIRED if cert_reqs is None else cert_reqs
if ca_certs is not None:
context.load_verify_locations(ca_certs)
else:
context.load_default_certs()
if ciphers is not None:
context.set_ciphers(ciphers)
return context and then use:
when setting up the client. |
Good thinking. :) I like the idea of having this functionality as a free function (your I'm concerned, however, about directly copying code from paho. After all, asyncio-mqtt is meant to be a wrapper around paho and not a reimplementation of paho. Copying code goes against that. Furthermore, it increases the maintenance burden: We now have to support the copied code. Ideally, we simply call the code from paho whenever we can. This way, if something gets fixed in All that being said, I think you're on to something with your For asyncio-mqtt I think that we should step in paho's footsteps (for better and worse) and simply call the existing What do you think? Does it make sense? |
Hard to disagree with your thoughts... I'll see if I can follow your design suggestions and come up with something. Alex |
This would be super useful for me. So I'm giving it a bump 😄 |
If this issue is still open, I would like to work on this issue and it via a PR request |
@Sohaib90, that's great to hear. Go for it. :) I'm ready to review the PR. 👍 |
Okay great. I will add it and open a pull request soon then. |
To ease the use of certificates for authentication, it would be really nice if the tls_set function was implemented in asyncio-mqtt client.py.
This would be a nice feature for newbies (like me)
The text was updated successfully, but these errors were encountered: