Last active
May 4, 2020 20:31
-
-
Save douglascodes/0ac23cc67cc8bcffd96ec83338ad2874 to your computer and use it in GitHub Desktop.
Encrypts all environment variables using the KMS key specified in env['aws_key_arn']. Much simpler to use as a utility in something like PyCharm with run configurations.
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
#! /usr/bin/python | |
""" | |
Script to encrypt environment variables to their encrypted version. | |
aws_key_arn - Resource ID # for aws encryption key. | |
format: 'arn:aws:kms:us-east-1:123456789:key/711d0e6d-620c-47da-a2f6-7141eb8cbde4' | |
""" | |
import os | |
import boto3 | |
from base64 import b64encode | |
keyarn = os.getenv('aws_key_arn') | |
kms = boto3.client('kms', region_name='us-east-1') | |
def encrypt_with_arn(value): | |
return b64encode(kms.encrypt(KeyId=keyarn, Plaintext=value)['CiphertextBlob']) | |
# Encrypted versions for webform or code | |
print('\n'.join(sorted([f"{str(e).upper()}: {encrypt_with_arn(os.getenv(e))}" for e in os.environ.keys() if os.getenv(e)]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment