I am attempting to use client side encryption to encrypt sensitive data before moving it to cloud storage on S3 and moving it over to redshift. I tried using the sample code provided by AWS, and after paling around with it I got it to run without returning an error, however, it isn't doing anything that I can tell because nothing prints as it should.
def cycle_string(key_arn, source_plaintext, botocore_session=None):
"""Encrypts and then decrypts a string using a KMS customer master key (CMK)
:param str key_arn: [encryption key]
(http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html)
:param bytes source_plaintext:
:param botocore_session: Existing botocore session
:type botocore_session: botocore.session.Session
"""
# Create a KMS master key provider
kms_kwargs = dict(key_ids=[key_arn])
if botocore_session is not None:
kms_kwargs['botocore_session'] = botocore_session
master_key_provider =
aws_encryption_sdk.KMSMasterKeyProvider(**kms_kwargs)
# Encrypt the plaintext source data
ciphertext, encryptor_header = aws_encryption_sdk.encrypt(
source=source_plaintext,
key_provider=master_key_provider
)
print('Ciphertext: ', ciphertext)
# Decrypt the ciphertext
cycled_plaintext, decrypted_header = aws_encryption_sdk.decrypt(
source=ciphertext,
key_provider=master_key_provider
)
# Verify that the "cycled" (encrypted, then decrypted) plaintext is identical to the source
# plaintext
assert cycled_plaintext == source_plaintext
# Verify that the encryption context used in the decrypt operation includes all key pairs from
# the encrypt operation. (The SDK can add pairs, so don't require an exact match.)
#
# In production, always use a meaningful encryption context. In this sample, we omit the
# encryption context (no key pairs).
assert all(
pair in decrypted_header.encryption_context.items()
for pair in encryptor_header.encryption_context.items()
)
print('Decrypted: ', cycled_plaintext)
I am new to Python and encryption so I may be missing some of the syntax or just lacking the knowledge of how it works. Is this the best way of using client side encryption with AWS in python? And if so, why does this code not return anything?
UPDATE: I got it to work using a slightly different method
session = botocore.session.get_session()
client = session.create_client('kms',
region_name = 'us-east-1',
aws_access_key_id = '[YOUR ACCESS KEY]',
aws_secret_access_key = '[YOUR SECRET ACCESSKEY]')
key_id = '[KEY ID]'
plaintext='[FILEPATH\FILENAME.CSV]'
ciphertext = kms.encrypt(KeyId=key_id, Plaintext=plaintext)
#decrypt_ciphertext = kms.decrypt(CiphertextBlob = ciphertext['CiphertextBlob'])
print('Ciphertext: ', ciphertext)
#print('Decrypted Ciphertext: ', decrypt_ciphertext)
now it prints but I am not sure how to tell if the data is actually encrypted