generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dxw/add-infrastructure-vpc-with-flow-logs
Add infrastructure VPC with flow logs
- Loading branch information
Showing
17 changed files
with
527 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": ${services} | ||
}, | ||
"Action": "sts:AssumeRole" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents", | ||
"logs:DescribeLogGroups", | ||
"logs:DescribeLogStreams" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} |
19 changes: 19 additions & 0 deletions
19
policies/kms-key-policy-statements/cloudwatch-logs-allow.json.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
%{if log_group_arn != ""}{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "logs.amazonaws.com" | ||
}, | ||
"Action": [ | ||
"kms:Encrypt*", | ||
"kms:Decrypt*", | ||
"kms:ReEncrypt*", | ||
"kms:GenerateDataKey*", | ||
"kms:Describe*" | ||
], | ||
"Resource": "*", | ||
"Condition": { | ||
"ArnEquals": { | ||
"kms:EncryptionContext:aws:logs:arn": "${log_group_arn}" | ||
} | ||
} | ||
}%{endif} |
22 changes: 22 additions & 0 deletions
22
policies/kms-key-policy-statements/log-delivery-allow.json.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
%{if account_id != ""}{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": [ "delivery.logs.amazonaws.com" ] | ||
}, | ||
"Action": [ | ||
"kms:Encrypt", | ||
"kms:Decrypt", | ||
"kms:ReEncrypt*", | ||
"kms:GenerateDataKey*", | ||
"kms:DescribeKey" | ||
], | ||
"Resource": "*", | ||
"Condition": { | ||
"StringEquals": { | ||
"aws:SourceAccount": ["${account_id}"] | ||
}, | ||
"ArnLike": { | ||
"aws:SourceArn": ["arn:aws:logs:${region}:${account_id}:*"] | ||
} | ||
} | ||
}%{endif} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"Principal": "*", | ||
"Action": "s3:*", | ||
"Effect": "Deny", | ||
"Resource": [ | ||
"${bucket_arn}", | ||
"${bucket_arn}/*" | ||
], | ||
"Condition": { | ||
"Bool": { | ||
"aws:SecureTransport": "false" | ||
}, | ||
"NumericLessThan": { | ||
"s3:TlsVersion": "1.2" | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
policies/s3-bucket-policy-statements/log-delivery-access.json.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"Principal": { | ||
"Service": "logging.s3.amazonaws.com" | ||
}, | ||
"Action": [ | ||
"s3:PutObject" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "${log_bucket_arn}/*", | ||
"Condition": { | ||
"ArnLike": { | ||
"aws:SourceArn": ${source_arns} | ||
}, | ||
"StringEquals": { | ||
"aws:SourceAccount": "${account_id}" | ||
} | ||
} | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "delivery.logs.amazonaws.com" | ||
}, | ||
"Action": [ | ||
"s3:GetBucketAcl", | ||
"s3:ListBucket" | ||
], | ||
"Resource": "${log_bucket_arn}", | ||
"Condition": { | ||
"StringEquals": { | ||
"aws:SourceAccount": "${account_id}" | ||
}, | ||
"ArnLike": { | ||
"aws:SourceArn": ${source_arns} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": ${statement} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# https://github.com/aquasecurity/tfsec/issues/2081 | ||
# tfsec:ignore:aws-s3-enable-bucket-logging | ||
resource "aws_s3_bucket" "infrastructure_logs" { | ||
count = local.enable_infrastructure_logs_bucket ? 1 : 0 | ||
|
||
bucket = "${local.resource_prefix_hash}-logs" | ||
} | ||
|
||
resource "aws_s3_bucket_policy" "infrastructure_logs" { | ||
count = local.enable_infrastructure_logs_bucket ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
policy = templatefile( | ||
"${path.module}/policies/s3-bucket-policy.json.tpl", | ||
{ | ||
statement = <<EOT | ||
[ | ||
${templatefile("${path.root}/policies/s3-bucket-policy-statements/enforce-tls.json.tpl", { bucket_arn = aws_s3_bucket.infrastructure_logs[0].arn })}, | ||
${templatefile("${path.root}/policies/s3-bucket-policy-statements/log-delivery-access.json.tpl", { | ||
log_bucket_arn = aws_s3_bucket.infrastructure_logs[0].arn | ||
source_arns = jsonencode(local.logs_bucket_source_arns) | ||
account_id = local.aws_account_id | ||
})} | ||
] | ||
EOT | ||
} | ||
) | ||
} | ||
|
||
resource "aws_s3_bucket_public_access_block" "infrastructure_logs" { | ||
count = local.enable_infrastructure_logs_bucket ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
block_public_acls = true | ||
block_public_policy = true | ||
ignore_public_acls = true | ||
restrict_public_buckets = true | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "infrastructure_logs" { | ||
count = local.enable_infrastructure_logs_bucket ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
|
||
versioning_configuration { | ||
status = "Enabled" | ||
} | ||
} | ||
|
||
# because infrastructure_kms_encryption is only true when multiple other | ||
# vars are true, tfsec can't figure out that this will actually have kms encryption when | ||
# enabled | ||
#tfsec:ignore:aws-s3-encryption-customer-key tfsec:ignore:aws-s3-enable-bucket-encryption | ||
resource "aws_s3_bucket_server_side_encryption_configuration" "infrastructure_logs" { | ||
count = local.enable_infrastructure_logs_bucket ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
|
||
dynamic "rule" { | ||
for_each = local.infrastructure_kms_encryption ? [1] : [] | ||
content { | ||
apply_server_side_encryption_by_default { | ||
kms_master_key_id = aws_kms_key.infrastructure[0].arn | ||
sse_algorithm = "aws:kms" | ||
} | ||
} | ||
} | ||
|
||
dynamic "rule" { | ||
for_each = local.infrastructure_kms_encryption ? [] : [1] | ||
content { | ||
apply_server_side_encryption_by_default { | ||
sse_algorithm = "AES256" | ||
} | ||
} | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_lifecycle_configuration" "logs" { | ||
count = local.enable_infrastructure_logs_bucket && local.infrastructure_logging_bucket_retention != 0 ? 1 : 0 | ||
|
||
bucket = aws_s3_bucket.infrastructure_logs[0].id | ||
|
||
rule { | ||
id = "all_expire" | ||
|
||
filter { | ||
prefix = "" | ||
} | ||
|
||
expiration { | ||
days = local.infrastructure_logging_bucket_retention | ||
} | ||
|
||
status = "Enabled" | ||
} | ||
} |
Oops, something went wrong.