-
-
Save garystafford/9802ac71157555169c5fa92724aedde0 to your computer and use it in GitHub Desktop.
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/env ruby | |
require 'aws-sdk' | |
# reference: https://www.promptworks.com/blog/handling-environment-secrets-in-docker-on-the-aws-container-service | |
########## CHANGE THESE VARIABLES ########## | |
file_to_encrypt = '.env' | |
file_path = "../../../fav-color-private-stuff/#{file_to_encrypt}" | |
key_alias = 'demo-key' | |
bucket_name = 'fav-color-secrets' | |
your_region = 'us-east-1' | |
############################################ | |
# initialize S3 client | |
s3_client = Aws::S3::Client.new(region: your_region) | |
# initialize KMS client | |
kms_client = Aws::KMS::Client.new(region: your_region) | |
# retrieve an 'aliase list' (array) of your AWS account's KMS encryption keys | |
aliases = kms_client.list_aliases.aliases | |
# select your key | |
key = aliases.find { |alias_struct| alias_struct.alias_name == "alias/#{key_alias}" } | |
# grab the key's id | |
key_id = key.target_key_id | |
# initialize the S3 encryption client | |
s3_encryption_client = Aws::S3::Encryption::Client.new( | |
client: s3_client, kms_key_id: key_id, kms_client: kms_client) | |
# specify the path to the file that will be encrypted | |
path = File.expand_path(file_path, __FILE__) | |
# open the file. 'put' it to S3. close the file. | |
File.open(path) do |file| | |
s3_encryption_client.put_object(bucket: bucket_name, key: file_to_encrypt, body: file) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment