Created
April 18, 2020 10:10
-
-
Save vishnuhd/670aabdcb60a8c22044241161d3f971f to your computer and use it in GitHub Desktop.
Python script to create encrypted secrets in AWS secret manager using KMS Key
This file contains 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 boto3 | |
import os | |
import json | |
filename = 'data.json' | |
kms_id = '' | |
secretdata= {"username":"chris","password":"BnQw!XDWgaEeT9XGTT29"} | |
secret_data={} | |
for k,v in secretdata.items(): | |
encrypt_string = os.popen(("aws kms encrypt --key-id %s --plaintext '%s' --query CiphertextBlob --output text" % (kms_id, v))).read() | |
secret_data[k] = encrypt_string | |
json_data = json.dumps(secret_data) | |
with open(filename,'w') as f: | |
f.write(json_data) | |
with open('data.json', 'r') as content_file: | |
secret_content = content_file.read() | |
print(secret_content) | |
client = boto3.client('secretsmanager') | |
response = client.create_secret( | |
Description='My test secret', | |
Name='MyTestt', | |
SecretString=secret_content, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment