Improvements to KMS encryption stance on ALB logs (#185)
This commit is contained in:
1 parent
7c957c4656
commit
d5da95f5a9
9 files changed
+1104
-25
No files matched your search
@@ -0,0 +1,34 @@
|
||||
"""Lambda that re-encrypts newly created S3 objects in-place from SSE-S3 to SSE-KMS."""
|
||||
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
import boto3
|
||||
|
||||
s3 = boto3.client("s3")
|
||||
|
||||
KMS_KEY_ARN = os.environ["KMS_KEY_ARN"]
|
||||
|
||||
|
||||
def handler(event, context):
|
||||
for record in event["Records"]:
|
||||
bucket = record["s3"]["bucket"]["name"]
|
||||
key = urllib.parse.unquote_plus(record["s3"]["object"]["key"])
|
||||
|
||||
# Safety belt: skip if already encrypted with KMS
|
||||
head = s3.head_object(Bucket=bucket, Key=key)
|
||||
if head.get("ServerSideEncryption") == "aws:kms":
|
||||
print(f"Skipping {key}: already SSE-KMS")
|
||||
continue
|
||||
|
||||
s3.copy_object(
|
||||
Bucket=bucket,
|
||||
Key=key,
|
||||
CopySource={"Bucket": bucket, "Key": key},
|
||||
ServerSideEncryption="aws:kms",
|
||||
SSEKMSKeyId=KMS_KEY_ARN,
|
||||
BucketKeyEnabled=True,
|
||||
MetadataDirective="REPLACE",
|
||||
Metadata=head.get("Metadata", {}),
|
||||
)
|
||||
print(f"Re-encrypted {key} to SSE-KMS")
|
||||
Reference in new issue
Block a user