Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion infrastructure/modules/s3/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module "s3" {
}

resource "aws_s3_bucket_policy" "enforce_kms_truststore" {
count = var.enable_kms_encryption ? 1 : 0
count = var.enable_kms_encryption && var.enable_kms_bucket_policy ? 1 : 0
bucket = module.s3.s3_bucket_id
policy = jsonencode({ Version = "2012-10-17"
Statement = [
Expand Down
20 changes: 18 additions & 2 deletions infrastructure/modules/s3/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ variable "bucket_name" {
# description = "The path to the zip file"
# type = string
# }
variable "attach_policy" { default = false }
variable "policy" { default = null }
variable "attach_policy" {
description = "Whether to attach a policy to the s3 bucket"
type = bool
default = false
}

variable "policy" {
description = "s3 bucket policy as a JSON string"
type = string
default = null
}

variable "lifecycle_rule_inputs" { default = [] }

variable "target_access_logging_bucket" {
Expand Down Expand Up @@ -82,3 +92,9 @@ variable "enable_kms_encryption" {
type = bool
default = false
}

variable "enable_kms_bucket_policy" {
description = "Whether to attach the KMS enforcement bucket policy. Disable if managing policy externally"
type = bool
default = true
}
48 changes: 38 additions & 10 deletions infrastructure/stacks/artefact_management/s3.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,18 @@ locals {
all_bucket_principals = flatten(values(local.principals_by_environment))
}


module "artefacts_bucket" {
source = "../../modules/s3"
bucket_name = local.artefacts_bucket
enable_kms_encryption = true
s3_encryption_key_arn = module.s3_artefacts_encryption_key.arn
}


resource "aws_s3_bucket_policy" "artefacts_bucket_policy" {
depends_on = [module.artefacts_bucket]
bucket = local.artefacts_bucket
policy = data.aws_iam_policy_document.artefacts_bucket_policy.json
enable_kms_encryption = true
s3_encryption_key_arn = module.s3_artefacts_encryption_key.arn
attach_policy = true
policy = data.aws_iam_policy_document.artefacts_bucket_policy.json
enable_kms_bucket_policy = false
}

data "aws_iam_policy_document" "artefacts_bucket_policy" {

statement {
principals {
type = "AWS"
Expand Down Expand Up @@ -79,4 +75,36 @@ data "aws_iam_policy_document" "artefacts_bucket_policy" {
"${module.artefacts_bucket.s3_bucket_arn}/*",
]
}

statement {
sid = "DenyUnencryptedUploads"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["s3:PutObject"]
resources = ["${module.artefacts_bucket.s3_bucket_arn}/*"]
condition {
test = "StringNotEquals"
variable = "s3:x-amz-server-side-encryption"
values = ["aws:kms"]
}
}

statement {
sid = "DenyUnencryptedKMSUploads"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["s3:PutObject"]
resources = ["${module.artefacts_bucket.s3_bucket_arn}/*"]
condition {
test = "ArnNotEquals"
variable = "s3:x-amz-server-side-encryption-aws-kms-key-id"
values = ["module.s3_artefacts_encryption_key.arn"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"Sid": "TerraformStateLockingDynamoTable",
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:Delete*",
"dynamodb:Create*",
"dynamodb:Describe*",
"dynamodb:Get*",
"dynamodb:List*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"Sid": "TerraformStateLockingDynamoTable",
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:Delete*",
"dynamodb:Create*",
"dynamodb:Describe*",
"dynamodb:Get*",
"dynamodb:List*",
Expand Down
Loading