Harden low-risk infrastructure settings
This commit is contained in:
@@ -76,6 +76,7 @@ module "audit_logging_encryption" {
|
|||||||
|
|
||||||
protect_from_destroy = true
|
protect_from_destroy = true
|
||||||
bucket_names = local.bucket_names
|
bucket_names = local.bucket_names
|
||||||
|
access_log_bucket_name = "aws-s3-access-logs-${var.account_id}-${var.region}-an"
|
||||||
|
|
||||||
trail_name = "management-events"
|
trail_name = "management-events"
|
||||||
kms_alias_name = "alias/vault-kms"
|
kms_alias_name = "alias/vault-kms"
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ run "apply_group1_osaka" {
|
|||||||
error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets."
|
error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert {
|
||||||
|
condition = length(aws_s3_bucket.access_logs_unprotected) == 1 && aws_s3_bucket.access_logs_unprotected[0].force_destroy
|
||||||
|
error_message = "The Osaka test must create one unprotected, force-destroyable S3 access log bucket."
|
||||||
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = !aws_cloudtrail.management_events.is_multi_region_trail
|
condition = !aws_cloudtrail.management_events.is_multi_region_trail
|
||||||
error_message = "The Osaka test CloudTrail trail must be single-Region."
|
error_message = "The Osaka test CloudTrail trail must be single-Region."
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ resource "aws_cloudtrail" "management_events" {
|
|||||||
include_global_service_events = true
|
include_global_service_events = true
|
||||||
is_multi_region_trail = var.cloudtrail_is_multi_region
|
is_multi_region_trail = var.cloudtrail_is_multi_region
|
||||||
is_organization_trail = false
|
is_organization_trail = false
|
||||||
enable_log_file_validation = false
|
enable_log_file_validation = true
|
||||||
enable_logging = true
|
enable_logging = true
|
||||||
|
|
||||||
advanced_event_selector {
|
advanced_event_selector {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ resource "aws_kms_key" "vault_protected" {
|
|||||||
description = "KMS for Vault"
|
description = "KMS for Vault"
|
||||||
key_usage = "ENCRYPT_DECRYPT"
|
key_usage = "ENCRYPT_DECRYPT"
|
||||||
customer_master_key_spec = "SYMMETRIC_DEFAULT"
|
customer_master_key_spec = "SYMMETRIC_DEFAULT"
|
||||||
enable_key_rotation = false
|
enable_key_rotation = true
|
||||||
multi_region = false
|
multi_region = false
|
||||||
deletion_window_in_days = var.kms_deletion_window_in_days
|
deletion_window_in_days = var.kms_deletion_window_in_days
|
||||||
policy = local.kms_policy
|
policy = local.kms_policy
|
||||||
@@ -24,7 +24,7 @@ resource "aws_kms_key" "vault_unprotected" {
|
|||||||
description = "KMS for Vault"
|
description = "KMS for Vault"
|
||||||
key_usage = "ENCRYPT_DECRYPT"
|
key_usage = "ENCRYPT_DECRYPT"
|
||||||
customer_master_key_spec = "SYMMETRIC_DEFAULT"
|
customer_master_key_spec = "SYMMETRIC_DEFAULT"
|
||||||
enable_key_rotation = false
|
enable_key_rotation = true
|
||||||
multi_region = false
|
multi_region = false
|
||||||
deletion_window_in_days = var.kms_deletion_window_in_days
|
deletion_window_in_days = var.kms_deletion_window_in_days
|
||||||
policy = local.kms_policy
|
policy = local.kms_policy
|
||||||
|
|||||||
@@ -4,11 +4,18 @@ locals {
|
|||||||
flowlogs = coalesce(var.bucket_names.flowlogs, "${var.name_prefix}-flowlogs")
|
flowlogs = coalesce(var.bucket_names.flowlogs, "${var.name_prefix}-flowlogs")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
access_log_bucket_name = coalesce(var.access_log_bucket_name, "${var.name_prefix}-s3-access-logs")
|
||||||
|
access_log_prefixes = {
|
||||||
|
for key in keys(local.buckets) : key => "${key}/"
|
||||||
|
}
|
||||||
|
|
||||||
bucket_ids = merge(
|
bucket_ids = merge(
|
||||||
{ for key, bucket in aws_s3_bucket.protected : key => bucket.id },
|
{ for key, bucket in aws_s3_bucket.protected : key => bucket.id },
|
||||||
{ for key, bucket in aws_s3_bucket.unprotected : key => bucket.id },
|
{ for key, bucket in aws_s3_bucket.unprotected : key => bucket.id },
|
||||||
)
|
)
|
||||||
|
|
||||||
|
access_log_bucket_id = var.protect_from_destroy ? aws_s3_bucket.access_logs_protected[0].id : aws_s3_bucket.access_logs_unprotected[0].id
|
||||||
|
|
||||||
vault_kms_key_id = var.protect_from_destroy ? aws_kms_key.vault_protected[0].key_id : aws_kms_key.vault_unprotected[0].key_id
|
vault_kms_key_id = var.protect_from_destroy ? aws_kms_key.vault_protected[0].key_id : aws_kms_key.vault_unprotected[0].key_id
|
||||||
vault_kms_key_arn = var.protect_from_destroy ? aws_kms_key.vault_protected[0].arn : aws_kms_key.vault_unprotected[0].arn
|
vault_kms_key_arn = var.protect_from_destroy ? aws_kms_key.vault_protected[0].arn : aws_kms_key.vault_unprotected[0].arn
|
||||||
|
|
||||||
@@ -97,14 +104,39 @@ locals {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
access_log_bucket_policy = jsonencode({
|
||||||
|
Version = "2012-10-17"
|
||||||
|
Statement = [
|
||||||
|
for key, bucket_name in local.buckets : {
|
||||||
|
Sid = "S3ServerAccessLogsPolicy${key}"
|
||||||
|
Effect = "Allow"
|
||||||
|
Principal = { Service = "logging.s3.amazonaws.com" }
|
||||||
|
Action = "s3:PutObject"
|
||||||
|
Resource = "arn:aws:s3:::${local.access_log_bucket_name}/${local.access_log_prefixes[key]}*"
|
||||||
|
Condition = {
|
||||||
|
ArnLike = {
|
||||||
|
"aws:SourceArn" = "arn:aws:s3:::${bucket_name}"
|
||||||
|
}
|
||||||
|
StringEquals = {
|
||||||
|
"aws:SourceAccount" = var.account_id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
lifecycle_rules = {
|
lifecycle_rules = {
|
||||||
cloudtrail = {
|
cloudtrail = {
|
||||||
id = "DeleteAfter3months"
|
id = "DeleteAfter3months"
|
||||||
expiration_days = 90
|
expiration_days = 90
|
||||||
|
noncurrent_expiration_days = 90
|
||||||
}
|
}
|
||||||
flowlogs = {
|
flowlogs = {
|
||||||
id = "Housekeep"
|
id = "Housekeep"
|
||||||
expiration_days = 7
|
expiration_days = 7
|
||||||
|
noncurrent_expiration_days = 7
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
access_log_retention_days = 90
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ resource "aws_s3_bucket_versioning" "this" {
|
|||||||
bucket = local.bucket_ids[each.key]
|
bucket = local.bucket_ids[each.key]
|
||||||
|
|
||||||
versioning_configuration {
|
versioning_configuration {
|
||||||
status = "Disabled"
|
status = "Enabled"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,5 +82,129 @@ resource "aws_s3_bucket_lifecycle_configuration" "this" {
|
|||||||
expiration {
|
expiration {
|
||||||
days = each.value.expiration_days
|
days = each.value.expiration_days
|
||||||
}
|
}
|
||||||
|
|
||||||
|
noncurrent_version_expiration {
|
||||||
|
noncurrent_days = each.value.noncurrent_expiration_days
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rule {
|
||||||
|
id = "DeleteExpiredObjectDeleteMarkers"
|
||||||
|
status = "Enabled"
|
||||||
|
|
||||||
|
expiration {
|
||||||
|
expired_object_delete_marker = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
depends_on = [aws_s3_bucket_versioning.this]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket" "access_logs_protected" {
|
||||||
|
count = var.protect_from_destroy ? 1 : 0
|
||||||
|
|
||||||
|
bucket = local.access_log_bucket_name
|
||||||
|
force_destroy = false
|
||||||
|
|
||||||
|
lifecycle {
|
||||||
|
prevent_destroy = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket" "access_logs_unprotected" {
|
||||||
|
count = var.protect_from_destroy ? 0 : 1
|
||||||
|
|
||||||
|
bucket = local.access_log_bucket_name
|
||||||
|
force_destroy = var.force_destroy_unprotected_buckets
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_versioning" "access_logs" {
|
||||||
|
bucket = local.access_log_bucket_id
|
||||||
|
|
||||||
|
versioning_configuration {
|
||||||
|
status = "Enabled"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_server_side_encryption_configuration" "access_logs" {
|
||||||
|
bucket = local.access_log_bucket_id
|
||||||
|
|
||||||
|
rule {
|
||||||
|
apply_server_side_encryption_by_default {
|
||||||
|
sse_algorithm = "AES256"
|
||||||
|
}
|
||||||
|
|
||||||
|
bucket_key_enabled = false
|
||||||
|
blocked_encryption_types = ["SSE-C"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_public_access_block" "access_logs" {
|
||||||
|
bucket = local.access_log_bucket_id
|
||||||
|
|
||||||
|
block_public_acls = true
|
||||||
|
block_public_policy = true
|
||||||
|
ignore_public_acls = true
|
||||||
|
restrict_public_buckets = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_ownership_controls" "access_logs" {
|
||||||
|
bucket = local.access_log_bucket_id
|
||||||
|
|
||||||
|
rule {
|
||||||
|
object_ownership = "BucketOwnerEnforced"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_policy" "access_logs" {
|
||||||
|
bucket = local.access_log_bucket_id
|
||||||
|
policy = local.access_log_bucket_policy
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_lifecycle_configuration" "access_logs" {
|
||||||
|
bucket = local.access_log_bucket_id
|
||||||
|
transition_default_minimum_object_size = "all_storage_classes_128K"
|
||||||
|
|
||||||
|
rule {
|
||||||
|
id = "DeleteAfter90Days"
|
||||||
|
status = "Enabled"
|
||||||
|
|
||||||
|
expiration {
|
||||||
|
days = local.access_log_retention_days
|
||||||
|
}
|
||||||
|
|
||||||
|
noncurrent_version_expiration {
|
||||||
|
noncurrent_days = local.access_log_retention_days
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rule {
|
||||||
|
id = "DeleteExpiredObjectDeleteMarkers"
|
||||||
|
status = "Enabled"
|
||||||
|
|
||||||
|
expiration {
|
||||||
|
expired_object_delete_marker = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
depends_on = [aws_s3_bucket_versioning.access_logs]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_s3_bucket_logging" "this" {
|
||||||
|
for_each = local.buckets
|
||||||
|
|
||||||
|
bucket = local.bucket_ids[each.key]
|
||||||
|
target_bucket = local.access_log_bucket_id
|
||||||
|
target_prefix = local.access_log_prefixes[each.key]
|
||||||
|
|
||||||
|
target_object_key_format {
|
||||||
|
partitioned_prefix {
|
||||||
|
partition_date_source = "EventTime"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
depends_on = [
|
||||||
|
aws_s3_bucket_ownership_controls.access_logs,
|
||||||
|
aws_s3_bucket_policy.access_logs,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,6 +45,11 @@ run "plan_group1_osaka" {
|
|||||||
error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets."
|
error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert {
|
||||||
|
condition = length(aws_s3_bucket.access_logs_unprotected) == 1 && aws_s3_bucket.access_logs_unprotected[0].force_destroy
|
||||||
|
error_message = "The Osaka test must create one unprotected, force-destroyable S3 access log bucket."
|
||||||
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = !aws_cloudtrail.management_events.is_multi_region_trail
|
condition = !aws_cloudtrail.management_events.is_multi_region_trail
|
||||||
error_message = "The Osaka test CloudTrail trail must be single-Region."
|
error_message = "The Osaka test CloudTrail trail must be single-Region."
|
||||||
|
|||||||
@@ -34,6 +34,12 @@ variable "bucket_names" {
|
|||||||
default = {}
|
default = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "access_log_bucket_name" {
|
||||||
|
description = "Optional S3 server access log destination bucket name. Omitted values are derived from name_prefix for isolated tests."
|
||||||
|
type = string
|
||||||
|
default = null
|
||||||
|
}
|
||||||
|
|
||||||
variable "trail_name" {
|
variable "trail_name" {
|
||||||
description = "CloudTrail trail name."
|
description = "CloudTrail trail name."
|
||||||
type = string
|
type = string
|
||||||
|
|||||||
@@ -24,29 +24,19 @@ resource "aws_vpc_security_group_egress_rule" "web_all_ipv4" {
|
|||||||
region = var.region
|
region = var.region
|
||||||
|
|
||||||
security_group_id = aws_security_group.web.id
|
security_group_id = aws_security_group.web.id
|
||||||
|
description = "Allow all outbound traffic from the public web instance"
|
||||||
ip_protocol = "-1"
|
ip_protocol = "-1"
|
||||||
cidr_ipv4 = "0.0.0.0/0"
|
cidr_ipv4 = "0.0.0.0/0"
|
||||||
}
|
}
|
||||||
|
|
||||||
# The default SG is adopted. Its two rules are managed as an absolute inline
|
# The default SG is adopted. Its ingress and egress rules are managed as an
|
||||||
# set and must not be mixed with standalone rule resources.
|
# absolute empty set and must not be mixed with standalone rule resources.
|
||||||
resource "aws_default_security_group" "this" {
|
resource "aws_default_security_group" "this" {
|
||||||
region = var.region
|
region = var.region
|
||||||
vpc_id = local.vpc.id
|
vpc_id = local.vpc.id
|
||||||
|
|
||||||
ingress {
|
# ingress/egress use attributes-as-blocks semantics. Explicit empty lists are
|
||||||
description = null
|
# required to revoke the rules already managed in state.
|
||||||
protocol = "tcp"
|
ingress = []
|
||||||
from_port = 22
|
egress = []
|
||||||
to_port = 22
|
|
||||||
cidr_blocks = ["0.0.0.0/0"]
|
|
||||||
}
|
|
||||||
|
|
||||||
egress {
|
|
||||||
description = null
|
|
||||||
protocol = "-1"
|
|
||||||
from_port = 0
|
|
||||||
to_port = 0
|
|
||||||
cidr_blocks = ["0.0.0.0/0"]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user