Skip to content

Commit b1cdea2

Browse files
author
cblkwell
authored
Merge pull request #69 from trussworks/cblkwell-173722122-encrypt-cloudwatch
Mandatory encryption for S3 and Cloudwatch Logs
2 parents 2e02d21 + 217da30 commit b1cdea2

File tree

7 files changed

+35
-51
lines changed

7 files changed

+35
-51
lines changed

.markdownlintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"first-header-h1": false,
44
"first-line-h1": false,
55
"line_length": false,
6-
"no-multiple-blanks": false
6+
"no-multiple-blanks": false,
7+
"commands-show-output": false
78
}

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This module creates AWS CloudTrail and configures it so that logs go to cloudwat
55

66
## Terraform Versions
77

8-
Terraform 0.12. Pin module version to `~> 2.X`. Submit pull-requests to `master` branch.
8+
Terraform 0.12. Pin module version to `~> 3.X`. Submit pull-requests to `master` branch.
99

1010
Terraform 0.11. Pin module version to `~> 1.X`. Submit pull-requests to `terraform011` branch.
1111

@@ -19,6 +19,15 @@ module "aws_cloudtrail" {
1919
}
2020
```
2121

22+
## Upgrade Instructions for v2 -> v3
23+
24+
Starting in v3, encryption is not optional and will be on for both logs
25+
delivered to S3 and Cloudwatch Logs. The KMS key resource created this
26+
module will be used to encrypt both S3 and Cloudwatch-based logs.
27+
28+
Because of this change, remove the `encrypt_cloudtrail` parameter from
29+
previous invocations of the module prior to upgrading the version.
30+
2231
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2332
## Requirements
2433

@@ -38,7 +47,6 @@ module "aws_cloudtrail" {
3847
|------|-------------|------|---------|:--------:|
3948
| cloudwatch\_log\_group\_name | The name of the CloudWatch Log Group that receives CloudTrail events. | `string` | `"cloudtrail-events"` | no |
4049
| enabled | Enables logging for the trail. Defaults to true. Setting this to false will pause logging. | `bool` | `true` | no |
41-
| encrypt\_cloudtrail | Whether or not to use a custom KMS key to encrypt CloudTrail logs. | `string` | `"false"` | no |
4250
| key\_deletion\_window\_in\_days | Duration in days after which the key is deleted after destruction of the resource, must be 7-30 days. Default 30 days. | `string` | `30` | no |
4351
| log\_retention\_days | Number of days to keep AWS logs around in specific log group. | `string` | `90` | no |
4452
| org\_trail | Whether or not this is an organization trail. Only valid in master account. | `string` | `"false"` | no |

examples/simple/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module "aws_cloudtrail" {
77

88
s3_bucket_name = module.logs.aws_logs_bucket
99
s3_key_prefix = var.s3_key_prefix
10-
11-
encrypt_cloudtrail = var.encrypt_cloudtrail
1210
}
1311

1412
module "logs" {

examples/simple/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ variable "trail_name" {
1717
variable "cloudwatch_log_group_name" {
1818
type = string
1919
}
20-
21-
variable "encrypt_cloudtrail" {
22-
type = bool
23-
}

main.tf

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ resource "aws_iam_role" "cloudtrail_cloudwatch_role" {
3838
resource "aws_cloudwatch_log_group" "cloudtrail" {
3939
name = var.cloudwatch_log_group_name
4040
retention_in_days = var.log_retention_days
41+
kms_key_id = aws_kms_key.cloudtrail.arn
4142

4243
tags = {
4344
Automation = "Terraform"
@@ -206,11 +207,29 @@ data "aws_iam_policy_document" "cloudtrail_kms_policy_doc" {
206207

207208
resources = ["*"]
208209
}
210+
211+
statement {
212+
sid = "Allow logs KMS access"
213+
effect = "Allow"
214+
215+
principals {
216+
type = "Service"
217+
identifiers = ["logs.${data.aws_region.current.name}.amazonaws.com"]
218+
}
219+
220+
actions = [
221+
"kms:Encrypt*",
222+
"kms:Decrypt*",
223+
"kms:ReEncrypt*",
224+
"kms:GenerateDataKey*",
225+
"kms:Describe*"
226+
]
227+
resources = ["*"]
228+
}
229+
209230
}
210231

211232
resource "aws_kms_key" "cloudtrail" {
212-
count = var.encrypt_cloudtrail ? 1 : 0
213-
214233
description = "A KMS key used to encrypt CloudTrail log files stored in S3."
215234
deletion_window_in_days = var.key_deletion_window_in_days
216235
enable_key_rotation = "true"
@@ -222,10 +241,8 @@ resource "aws_kms_key" "cloudtrail" {
222241
}
223242

224243
resource "aws_kms_alias" "cloudtrail" {
225-
count = var.encrypt_cloudtrail ? 1 : 0
226-
227244
name = "alias/${var.trail_name}"
228-
target_key_id = aws_kms_key.cloudtrail[0].key_id
245+
target_key_id = aws_kms_key.cloudtrail.key_id
229246
}
230247

231248
#
@@ -253,7 +270,7 @@ resource "aws_cloudtrail" "main" {
253270
# enable log file validation to detect tampering
254271
enable_log_file_validation = true
255272

256-
kms_key_id = var.encrypt_cloudtrail ? aws_kms_key.cloudtrail[0].arn : null
273+
kms_key_id = aws_kms_key.cloudtrail.arn
257274

258275
# Enables logging for the trail. Defaults to true. Setting this to false will pause logging.
259276
enable_logging = var.enabled

test/terraform_aws_cloudtrail_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,6 @@ func IsLogging(t *testing.T, region string, trailName string) (bool, error) {
4040
return *trailStatus.IsLogging, nil
4141
}
4242

43-
func TestTerraformAwsCloudtrail(t *testing.T) {
44-
testName := fmt.Sprintf("terratest-aws-cloudtrail-%s", strings.ToLower(random.UniqueId()))
45-
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/simple")
46-
47-
terraformOptions := &terraform.Options{
48-
TerraformDir: tempTestFolder,
49-
Vars: map[string]interface{}{
50-
"trail_name": testName,
51-
"cloudwatch_log_group_name": testName,
52-
"logs_bucket": testName,
53-
"region": awsRegion,
54-
"s3_key_prefix": "testName",
55-
"encrypt_cloudtrail": false,
56-
},
57-
EnvVars: map[string]string{
58-
"AWS_DEFAULT_REGION": awsRegion,
59-
},
60-
}
61-
62-
defer terraform.Destroy(t, terraformOptions)
63-
terraform.InitAndApply(t, terraformOptions)
64-
65-
cloudtrailArn := terraform.Output(t, terraformOptions, "cloudtrail_arn")
66-
isLogging, err := IsLogging(t, awsRegion, cloudtrailArn)
67-
assert.NoError(t, err)
68-
assert.True(t, isLogging)
69-
70-
}
71-
7243
func TestTerraformAwsCloudtrailEncryption(t *testing.T) {
7344
testName := fmt.Sprintf("terratest-aws-cloudtrail-%s", strings.ToLower(random.UniqueId()))
7445
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/simple")
@@ -81,7 +52,6 @@ func TestTerraformAwsCloudtrailEncryption(t *testing.T) {
8152
"logs_bucket": testName,
8253
"region": awsRegion,
8354
"s3_key_prefix": "testName",
84-
"encrypt_cloudtrail": true,
8555
},
8656
EnvVars: map[string]string{
8757
"AWS_DEFAULT_REGION": awsRegion,

variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ variable "org_trail" {
2727
type = string
2828
}
2929

30-
variable "encrypt_cloudtrail" {
31-
description = "Whether or not to use a custom KMS key to encrypt CloudTrail logs."
32-
default = "false"
33-
type = string
34-
}
35-
3630
variable "key_deletion_window_in_days" {
3731
description = "Duration in days after which the key is deleted after destruction of the resource, must be 7-30 days. Default 30 days."
3832
default = 30

0 commit comments

Comments
 (0)