Created
June 7, 2022 00:25
-
-
Save lordjabez/2fa9f21c546e9a6b7a4885a400e38218 to your computer and use it in GitHub Desktop.
Use cert from AWS Secrets Manager in requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
secret_id = 'id-for-your-cert-secret-goes-here' | |
secrets_manager = boto3.client('secretsmanager') | |
response = secrets_manager.get_secret_value(SecretId=secret_id) | |
cert_content = json.loads(response['SecretString']) | |
file_type = requests.packages.urllib3.contrib.pyopenssl.OpenSSL.crypto.FILETYPE_PEM | |
cert = requests.packages.urllib3.contrib.pyopenssl.OpenSSL.crypto.load_certificate(file_type, cert_content) | |
pkey = requests.packages.urllib3.contrib.pyopenssl.OpenSSL.crypto.load_privatekey(file_type, cert_content) | |
class Pkcs12Context(requests.packages.urllib3.contrib.pyopenssl.OpenSSL.SSL.Context): | |
def __init__(self, method): | |
super().__init__(method) | |
self.use_certificate(cert) | |
self.use_privatekey(pkey) | |
requests.packages.urllib3.contrib.pyopenssl.OpenSSL.SSL.Context = Pkcs12Context | |
response = requests.get('https://example.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment