Feature request: Pass certificates in string / bytes representation instead of file path #2037
Replies: 4 comments 7 replies
-
Hiya @philipptrenz - that's an interesting case you've highlighted there! What would your suggestion be on how we'd change the API to accommodate this? Currently the I'm also interested to know if this issue has been raised in either the |
Beta Was this translation helpful? Give feedback.
-
|
for the wandering googler: currently unaware of an async solution. |
Beta Was this translation helpful? Give feedback.
-
|
I would find some value in this as well. I am getting a certificate passed as an environment variable from a pipeline, and it's impractical to create a file for |
Beta Was this translation helpful? Give feedback.
-
|
If you're happy with a Linux-specific solution to this, you can use import os
import ssl
def create_context_from_data(cert_data: bytes, key_data: bytes, cert_auth_data: bytes) -> ssl.SSLContext:
def _memfd_with_contents(contents: bytes) -> int:
fd = os.memfd_create('')
os.pwrite(fd, contents, 0)
return fd
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
ctx.maximum_version = ssl.TLSVersion.MAXIMUM_SUPPORTED
ctx.load_verify_locations(cadata=cert_auth_data)
tmp_cert_fd: int | None = None
tmp_key_fd: int | None = None
try:
tmp_cert_fd = _memfd_with_contents(cert_data)
tmp_key_fd = _memfd_with_contents(key_data)
ctx.load_cert_chain(
certfile=f"/proc/self/fd/{tmp_cert_fd}",
keyfile=f"/proc/self/fd/{tmp_key_fd}",
)
finally:
if tmp_key_file is not None:
os.close(tmp_key_fd)
if tmp_cert_file is not None:
os.close(tmp_cert_fd)
return ctx |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TL;DR: Requesting a way to pass certificate's content as in-memory data to the
Clientobject of httpx.I'm using the certificate based client authentication with the clients
certparameter. Also, I'm on a distributed cloud environment, where file systems don't make much sense. Therefore the certificates are only available in memory.As far as I could see, there is no way to pass the certificates content as
str,bytesorio.BytesIOobject.Having to write the files to the file system first and then read them via the file path involves a high overhead due to the IO operations ... Passing the data directly does make a lot of sense and is well supported in other libraries, like
cryptography.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions