.. note that this README gets 'include'ed into the main documentation
.. image:: https://vignette2.wikia.nocookie.net/jadensadventures/images/1/1e/Kaa%27s_hypnotic_eyes.jpg/revision/latest?cb=20140310173415 :width: 200px :align: right
You wrote a cool network client or server. It encrypts connections
using TLS <https://en.wikipedia.org/wiki/Transport_Layer_Security>
__. Your test
suite needs to make TLS connections to itself.
Uh oh. Your test suite probably doesn't have a valid TLS certificate. Now what?
trustme
is a tiny Python package that does one thing: it gives you
a fake <https://martinfowler.com/bliki/TestDouble.html>
__
certificate authority (CA) that you can use to generate fake TLS certs
to use in your tests. Well, technically they're real certs, they're
just signed by your CA, which nobody trusts. But you can trust
it. Trust me.
Install: pip install -U trustme
Documentation: https://trustme.readthedocs.io
Bug tracker and source code: https://github.com/python-trio/trustme
Tested on: Python 3.8+, CPython and PyPy
License: MIT or Apache 2, your choice.
Code of conduct: Contributors are requested to follow our code of conduct <https://github.com/python-trio/trustme/blob/master/CODE_OF_CONDUCT.md>
__
in all project spaces.
Programmatic usage:
.. code-block:: python
import trustme
ca = trustme.CA()
server_cert = ca.issue_cert("test-host.example.org")
ca.configure_trust(ssl_context)
server_cert.configure_cert(ssl_context)
ca.cert_pem.write_to_path("ca.pem") server_cert.private_key_and_cert_chain_pem.write_to_path("server.pem")
with ca.cert_pem.tempfile() as ca_temp_path: requests.get("https://...", verify=ca_temp_path)
Command line usage:
.. code-block:: console
$ # Certs may be generated from anywhere. Here's where we are: $ pwd /tmp $ # ----- Creating certs ----- $ python -m trustme Generated a certificate for 'localhost', '127.0.0.1', '::1' Configure your server to use the following files: cert=/tmp/server.pem key=/tmp/server.key Configure your client to use the following files: cert=/tmp/client.pem $ # ----- Using certs ----- $ gunicorn --keyfile server.key --certfile server.pem app:app $ curl --cacert client.pem https://localhost:8000/ Hello, world!
Should I use these certs for anything real? Certainly not.
Why not just use self-signed certificates? These are more realistic. You don't have to disable your certificate validation code in your test suite, which is good because you want to test what you run in production, and you would never disable your certificate validation code in production, right? Plus, they're just as easy to work with. Actually easier, in many cases.
What if I want to test how my code handles some bizarre TLS
configuration? We think trustme hits a sweet spot of ease-of-use
and generality as it is. The defaults are carefully chosen to work
on all major operating systems and be as fast as possible. We don't
want to turn trustme into a second-rate re-export of everything in
cryptography <https://cryptography.io>
__. If you have more complex
needs, consider using them directly, possibly starting from the
trustme code.
Will you automate installing CA cert into system trust store? No.
mkcert <https://github.com/FiloSottile/mkcert>
__ already does this
well, and we would not have anything to add.