Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Passphrase programmatically in Python

I am using request library for automating APIs/microservices. I tried passing URL, certificates(path of the certificate file and key file) in get request.

After running the program, It asks for PEM pass phrase. Please refer below lines of command prompt.

>>> r = requests.get("https://foo.example.com/api/user/bill", cert=("client.crt", "client.key"))
Enter PEM pass phrase:
>>>

How to pass the passphrase programmatically in the program in order to avoid manual intervention of entering PEM passphrase in the program?

like image 897
Anup Patil Avatar asked Aug 16 '18 07:08

Anup Patil


1 Answers

The requests library doesn't support password-protected PEM files yet.

One option is to convert it to a pkcs12 file and use the requests-pkcs12 libary from https://pypi.org/project/requests-pkcs12/

Another option is to convert it to a pkcs12 file and then to a PEM file without password.

You can convert it as follows:

# Password protected PEM to pkcs12
openssl pkcs12 -export -out cert.p12 -in cert.pem -inkey key.pem -passin pass:supersecret -passout pass:supersecret
# pkcs12 to PEM without password
openssl pkcs12 -in cert.p12 -out cert_without_pwd.pem -nodes -password supersecret
like image 120
Marcel Avatar answered Sep 23 '22 13:09

Marcel