99
1010data "aws_region" "current" {}
1111
12+ data "aws_caller_identity" "current" {}
13+
1214module "certificate" {
1315 providers = {
1416 aws = aws.us_east_1
@@ -40,7 +42,11 @@ resource "aws_cloudfront_origin_access_identity" "this" {
4042 comment = " Deprecated: Access from CF to S3 - ${ local . main_domain } - Superseeded by OAC"
4143}
4244
43- data "aws_iam_policy_document" "bucket_policy" {
45+ data "aws_iam_policy_document" "s3_bucket_policy" {
46+ override_policy_documents = [
47+ var . s3_bucket_policy ,
48+ ]
49+
4450 statement {
4551 sid = " AllowCloudFrontServicePrincipalReadOnly"
4652 actions = [
@@ -64,7 +70,86 @@ data "aws_iam_policy_document" "bucket_policy" {
6470 variable = " AWS:SourceArn"
6571 values = [aws_cloudfront_distribution . this . arn ]
6672 }
73+ }
74+ }
75+
76+ resource "aws_kms_key" "this" {
77+ description = " This key is used to encrypt the S3 bucket ${ var . s3_bucket_name } "
78+ enable_key_rotation = true
79+ deletion_window_in_days = var. kms_deletion_window_in_days
80+ tags = local. tags
81+ }
82+
83+ resource "aws_kms_alias" "this" {
84+ name = " alias/s3/${ var . s3_bucket_name } "
85+ target_key_id = aws_kms_key. this . key_id
86+ }
87+
88+ resource "aws_kms_key_policy" "this" {
89+ key_id = aws_kms_key. this . arn
90+ policy = data. aws_iam_policy_document . kms_key_policy . json
91+ }
6792
93+ data "aws_iam_policy_document" "kms_key_policy" {
94+ override_policy_documents = [
95+ var . kms_key_policy ,
96+ ]
97+
98+ statement {
99+ sid = " Allow root privs"
100+ effect = " Allow"
101+ actions = [
102+ " kms:*"
103+ ]
104+ resources = [" *" ]
105+ principals {
106+ type = " AWS"
107+ identifiers = [" arn:aws:iam::${ data . aws_caller_identity . current . id } :root" ]
108+ }
109+ }
110+
111+ dynamic "statement" {
112+ for_each = var. enable_deploy_user == true ? [1 ] : []
113+ content {
114+ sid = " Allow deploy user to use the CMK"
115+ actions = [
116+ " kms:GenerateDataKey*" ,
117+ " kms:Encrypt" ,
118+ " kms:Decrypt"
119+ ]
120+ resources = [" *" ]
121+
122+ principals {
123+ type = " AWS"
124+ identifiers = [aws_iam_user . deploy [0 ]. arn ]
125+ }
126+ effect = " Allow"
127+ }
128+ }
129+
130+ statement {
131+ sid = " Allow CloudFront usage of the key"
132+ effect = " Allow"
133+ actions = [
134+ " kms:GenerateDataKey*" ,
135+ " kms:Encrypt" ,
136+ " kms:Decrypt" ,
137+ ]
138+ resources = [" *" ]
139+
140+ principals {
141+ type = " Service"
142+ identifiers = [" cloudfront.amazonaws.com" ]
143+ }
144+
145+ condition {
146+ test = " StringEquals"
147+ variable = " AWS:SourceArn"
148+
149+ values = [
150+ aws_cloudfront_distribution . this . arn
151+ ]
152+ }
68153 }
69154}
70155
@@ -75,7 +160,7 @@ module "s3_bucket" {
75160 bucket = var. s3_bucket_name
76161
77162 attach_policy = true
78- policy = data. aws_iam_policy_document . bucket_policy . json
163+ policy = data. aws_iam_policy_document . s3_bucket_policy . json
79164
80165 attach_deny_insecure_transport_policy = true
81166 attach_require_latest_tls_policy = true
@@ -85,9 +170,14 @@ module "s3_bucket" {
85170 target_prefix = " s3/access_log/${ var . s3_bucket_name } "
86171 }
87172
173+ expected_bucket_owner = data. aws_caller_identity . current . account_id
174+
88175 server_side_encryption_configuration = {
89176 rule = {
90- apply_server_side_encryption_by_default = {
177+ apply_server_side_encryption_by_default = var.encrypt_with_kms ? {
178+ kms_master_key_id = aws_kms_key.this.arn
179+ sse_algorithm = " aws:kms"
180+ } : {
91181 sse_algorithm = " AES256"
92182 }
93183 }
0 commit comments