Skip to content

Commit e039a87

Browse files
authored
Merge pull request #11 from cookielab/jj/add-kms-optional-deploy-user
Add KMS key for bucket encryption
2 parents 18b3f52 + 7a72046 commit e039a87

File tree

4 files changed

+154
-11
lines changed

4 files changed

+154
-11
lines changed

deploy.tf

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
resource "aws_iam_user" "deploy" {
2-
name = "zvirt-${local.main_domain_sanitized}-deploy"
2+
count = var.enable_deploy_user == true ? 1 : 0
3+
name = "zvirt-${local.main_domain_sanitized}-deploy"
34
}
45

56
resource "aws_iam_access_key" "deploy" {
6-
user = aws_iam_user.deploy.name
7+
count = var.enable_deploy_user == true ? 1 : 0
8+
user = aws_iam_user.deploy[0].name
79
}
810

911
data "aws_iam_policy_document" "deploy" {
12+
count = var.enable_deploy_user == true ? 1 : 0
1013
statement {
1114
effect = "Allow"
1215
actions = [
@@ -31,9 +34,11 @@ data "aws_iam_policy_document" "deploy" {
3134
}
3235

3336
resource "aws_iam_user_policy" "deploy" {
34-
user = aws_iam_user.deploy.name
37+
count = var.enable_deploy_user == true ? 1 : 0
3538

36-
policy = data.aws_iam_policy_document.deploy.json
39+
user = aws_iam_user.deploy[0].name
40+
41+
policy = data.aws_iam_policy_document.deploy[0].json
3742
}
3843

3944
module "gitlab" {
@@ -46,8 +51,8 @@ module "gitlab" {
4651

4752
aws_s3_bucket_name = module.s3_bucket.s3_bucket_id
4853
aws_cloudfront_distribution_id = aws_cloudfront_distribution.this.id
49-
aws_access_key_id = aws_iam_access_key.deploy.id
50-
aws_secret_access_key = aws_iam_access_key.deploy.secret
54+
aws_access_key_id = aws_iam_access_key.deploy[0].id
55+
aws_secret_access_key = aws_iam_access_key.deploy[0].secret
5156
aws_default_region = data.aws_region.current.name
5257
}
5358

@@ -70,3 +75,18 @@ moved {
7075
from = gitlab_project_variable.site_aws_secret_access_key[0]
7176
to = module.gitlab[0].gitlab_project_variable.site_aws_secret_access_key
7277
}
78+
79+
moved {
80+
from = aws_iam_access_key.deploy
81+
to = aws_iam_access_key.deploy[0]
82+
}
83+
84+
moved {
85+
from = aws_iam_access_key.deploy
86+
to = aws_iam_access_key.deploy[0]
87+
}
88+
89+
moved {
90+
from = aws_iam_user.deploy
91+
to = aws_iam_user.deploy[0]
92+
}

main.tf

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ locals {
99

1010
data "aws_region" "current" {}
1111

12+
data "aws_caller_identity" "current" {}
13+
1214
module "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
}

outputs.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ output "aws_cloudfront_distribution_id" {
77
}
88

99
output "aws_access_key_id" {
10-
value = aws_iam_access_key.deploy.id
10+
value = var.enable_deploy_user ? aws_iam_access_key.deploy[0].id : null
1111
}
1212

1313
output "aws_secret_access_key" {
14-
value = aws_iam_access_key.deploy.secret
14+
value = var.enable_deploy_user ? aws_iam_access_key.deploy[0].secret : null
1515
sensitive = true
1616
}
1717

@@ -22,3 +22,6 @@ output "aws_s3_bucket_arn" {
2222
output "aws_s3_bucket_regional_domain_name" {
2323
value = module.s3_bucket.s3_bucket_bucket_regional_domain_name
2424
}
25+
output "s3_kms_key_arn" {
26+
value = aws_kms_key.this.arn
27+
}

variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ variable "s3_bucket_name" {
1616
type = string
1717
}
1818

19+
variable "s3_bucket_policy" {
20+
type = string
21+
default = null
22+
description = "Additional S3 bucket policy"
23+
}
24+
1925
variable "gitlab_project_id" {
2026
type = string
2127
default = null
@@ -71,3 +77,27 @@ variable "functions" {
7177
})
7278
default = {}
7379
}
80+
81+
variable "enable_deploy_user" {
82+
type = bool
83+
default = true
84+
description = "Toggle s3 deploy user creation"
85+
}
86+
87+
variable "encrypt_with_kms" {
88+
type = bool
89+
default = false
90+
description = "Enable server side s3 bucket encryption with KMS key"
91+
}
92+
93+
variable "kms_deletion_window_in_days" {
94+
type = number
95+
default = 30
96+
description = "The waiting period, specified in number of days. After the waiting period ends, AWS KMS deletes the KMS key"
97+
}
98+
99+
variable "kms_key_policy" {
100+
type = string
101+
default = null
102+
description = "Additional KSM key policy"
103+
}

0 commit comments

Comments
 (0)