Skip to main content

Tools for letting the macOS Keychain verify certs for python requests

Project description

MacSesh

This package allows requests to verify certs with the macOS keychain, rather than using certifi. It also includes some tools for easily hooking up a SecureTransport adapter (a la Pip) and then later undoing all of the sneaky infiltrations required to set this up.

Which certs?

It uses any of the trusted certs from keychains included in the current user's keychain search list, as well as the system roots. Typically, this is the user's default at ~/Library/Keychains/login.keychain, the system keychain at /Library/Keychains/System.keychain, and the System Roots keychain at /System/Library/Keychains/SystemRootCertificates.keychain.

To achieve this, one of three different strategies can be employed:

  1. KeychainSession uses a custom SSLContext, requests Adapter, and requests Session, and injects the SSLContext into urllib3. This approach is the recommendation.
  2. SecureTransportSession uses the urllib3 contrib module for injecting SecureTransport equivalents into stock urllib3. While this approach uses more of the native networking framework, it also seems to be written primarily with the goal of solving the issues with macOS and aging OpenSSL versions to ensure that Macs could still use pip. Therefore, it's not entirely feature-complete in providing a full requests Adapter. It's definitely worth experimenting with.
  3. SimpleKeychainSession circumvents the normal flow of session startup, and tells the SSLContext to load its trust information early; in this case from certs dumped from the keychain.

Example Usage:

>>> import macsesh
>>> sesh = macsesh.KeychainSession()
>>> response = sesh.get('https://nethack.org')

If you want to use the "basic" requests API without creating a session:

>>> macsesh.inject_into_requests()
>>> requests.get('https://en.wikipedia.org/wiki/Taco')  # Uses keychain

Advanced

If for some reason you want to revert to "normal" requests (probably using certifi), in the same python process, you'll need to remove this module's injected stuff from urllib3 or requests.

Remove the SSLContext if you used any of the Sessions: macsesh.extract_from_urllib3() Clean up after using the "basic" API: macsesh.extract_from_requests()

Any certs added to the keychains after starting a session will not be available. Digging down in and updating the SSLContext is rough; just make a new session if you have this need!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

MacSesh-0.2.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

MacSesh-0.2.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file MacSesh-0.2.1.tar.gz.

File metadata

  • Download URL: MacSesh-0.2.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for MacSesh-0.2.1.tar.gz
Algorithm Hash digest
SHA256 261b7957427cd4d456c466c1ba168f342a2ed168b5e002cdaedc1e678f4517b4
MD5 63a8830482a93c2f6303ff10489beb4c
BLAKE2b-256 351b6fb784640499b9e7c301831a5a14d5cf843ca33cea6d5ae0705048d1c99f

See more details on using hashes here.

File details

Details for the file MacSesh-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: MacSesh-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for MacSesh-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 370b7b282d04e628460bb44646d0493389a634e8b9834a81183615f7d4da73cd
MD5 59b1cbdec9e209f5b8412d2a7ce00870
BLAKE2b-256 76407940fc0f59d32ffab06c324de4991d695fe4d220fdaf6a4b3487dca9848c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page