Skip to content

Commit 7610c67

Browse files
authored
Add CloudTrail Advanced Event Selector (#70)
1 parent e704eed commit 7610c67

File tree

16 files changed

+1140
-410
lines changed

16 files changed

+1140
-410
lines changed

.editorconfig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Unix-style newlines with a newline ending every file
2+
[*]
3+
charset = utf-8
4+
end_of_line = lf
5+
indent_size = 2
6+
indent_style = space
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
10+
# Override for Makefile
11+
[{Makefile, makefile, GNUmakefile, Makefile.*}]
12+
tab_width = 4
13+
indent_style = tab
14+
indent_size = tab
15+
16+
[*.sh]
17+
indent_size = unset
18+
indent_style = tab
19+
20+
# Enforce `go` formatting rules
21+
[*.go]
22+
indent_size = unset
23+
indent_style = tab
24+
25+
[*.json]
26+
insert_final_newline = false
27+
28+
[COMMIT_EDITMSG]
29+
max_line_length = 0

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ Available targets:
132132

133133
| Name | Version |
134134
|------|---------|
135-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
136-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.0 |
135+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
136+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.0 |
137137

138138
## Providers
139139

140140
| Name | Version |
141141
|------|---------|
142-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.0 |
142+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.0 |
143143

144144
## Modules
145145

@@ -158,6 +158,7 @@ Available targets:
158158
| Name | Description | Type | Default | Required |
159159
|------|-------------|------|---------|:--------:|
160160
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
161+
| <a name="input_advanced_event_selector"></a> [advanced\_event\_selector](#input\_advanced\_event\_selector) | Specifies an advanced event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable | <pre>list(object({<br> name = optional(string)<br> field_selector = list(object({<br> field = string<br> ends_with = optional(list(string))<br> not_ends_with = optional(list(string))<br> equals = optional(list(string))<br> not_equals = optional(list(string))<br> starts_with = optional(list(string))<br> not_starts_with = optional(list(string))<br> }))<br> }))</pre> | `[]` | no |
161162
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
162163
| <a name="input_cloud_watch_logs_group_arn"></a> [cloud\_watch\_logs\_group\_arn](#input\_cloud\_watch\_logs\_group\_arn) | Specifies a log group name using an Amazon Resource Name (ARN), that represents the log group to which CloudTrail logs will be delivered | `string` | `""` | no |
163164
| <a name="input_cloud_watch_logs_role_arn"></a> [cloud\_watch\_logs\_role\_arn](#input\_cloud\_watch\_logs\_role\_arn) | Specifies the role for the CloudWatch Logs endpoint to assume to write to a user’s log group | `string` | `""` | no |
@@ -168,7 +169,7 @@ Available targets:
168169
| <a name="input_enable_logging"></a> [enable\_logging](#input\_enable\_logging) | Enable logging for the trail | `bool` | `true` | no |
169170
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
170171
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
171-
| <a name="input_event_selector"></a> [event\_selector](#input\_event\_selector) | Specifies an event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable | <pre>list(object({<br> include_management_events = bool<br> read_write_type = string<br><br> data_resource = list(object({<br> type = string<br> values = list(string)<br> }))<br> }))</pre> | `[]` | no |
172+
| <a name="input_event_selector"></a> [event\_selector](#input\_event\_selector) | Specifies an event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable | <pre>list(object({<br> include_management_events = bool<br> read_write_type = string<br> exclude_management_event_sources = optional(set(string))<br><br> data_resource = list(object({<br> type = string<br> values = list(string)<br> }))<br> }))</pre> | `[]` | no |
172173
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
173174
| <a name="input_include_global_service_events"></a> [include\_global\_service\_events](#input\_include\_global\_service\_events) | Specifies whether the trail is publishing events from global services such as IAM to the log files | `bool` | `false` | no |
174175
| <a name="input_insight_selector"></a> [insight\_selector](#input\_insight\_selector) | Specifies an insight selector for type of insights to log on a trail | <pre>list(object({<br> insight_type = string<br> }))</pre> | `[]` | no |
@@ -195,7 +196,7 @@ Available targets:
195196
|------|-------------|
196197
| <a name="output_cloudtrail_arn"></a> [cloudtrail\_arn](#output\_cloudtrail\_arn) | The Amazon Resource Name of the trail |
197198
| <a name="output_cloudtrail_home_region"></a> [cloudtrail\_home\_region](#output\_cloudtrail\_home\_region) | The region in which the trail was created |
198-
| <a name="output_cloudtrail_id"></a> [cloudtrail\_id](#output\_cloudtrail\_id) | The name of the trail |
199+
| <a name="output_cloudtrail_id"></a> [cloudtrail\_id](#output\_cloudtrail\_id) | The ID of the trail. (Name for provider < v5, ARN for provider >= v5). |
199200
<!-- markdownlint-restore -->
200201

201202

docs/terraform.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
| Name | Version |
55
|------|---------|
6-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
7-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.0 |
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.0 |
88

99
## Providers
1010

1111
| Name | Version |
1212
|------|---------|
13-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.0 |
13+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.0 |
1414

1515
## Modules
1616

@@ -29,6 +29,7 @@
2929
| Name | Description | Type | Default | Required |
3030
|------|-------------|------|---------|:--------:|
3131
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
32+
| <a name="input_advanced_event_selector"></a> [advanced\_event\_selector](#input\_advanced\_event\_selector) | Specifies an advanced event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable | <pre>list(object({<br> name = optional(string)<br> field_selector = list(object({<br> field = string<br> ends_with = optional(list(string))<br> not_ends_with = optional(list(string))<br> equals = optional(list(string))<br> not_equals = optional(list(string))<br> starts_with = optional(list(string))<br> not_starts_with = optional(list(string))<br> }))<br> }))</pre> | `[]` | no |
3233
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
3334
| <a name="input_cloud_watch_logs_group_arn"></a> [cloud\_watch\_logs\_group\_arn](#input\_cloud\_watch\_logs\_group\_arn) | Specifies a log group name using an Amazon Resource Name (ARN), that represents the log group to which CloudTrail logs will be delivered | `string` | `""` | no |
3435
| <a name="input_cloud_watch_logs_role_arn"></a> [cloud\_watch\_logs\_role\_arn](#input\_cloud\_watch\_logs\_role\_arn) | Specifies the role for the CloudWatch Logs endpoint to assume to write to a user’s log group | `string` | `""` | no |
@@ -39,7 +40,7 @@
3940
| <a name="input_enable_logging"></a> [enable\_logging](#input\_enable\_logging) | Enable logging for the trail | `bool` | `true` | no |
4041
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
4142
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
42-
| <a name="input_event_selector"></a> [event\_selector](#input\_event\_selector) | Specifies an event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable | <pre>list(object({<br> include_management_events = bool<br> read_write_type = string<br><br> data_resource = list(object({<br> type = string<br> values = list(string)<br> }))<br> }))</pre> | `[]` | no |
43+
| <a name="input_event_selector"></a> [event\_selector](#input\_event\_selector) | Specifies an event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable | <pre>list(object({<br> include_management_events = bool<br> read_write_type = string<br> exclude_management_event_sources = optional(set(string))<br><br> data_resource = list(object({<br> type = string<br> values = list(string)<br> }))<br> }))</pre> | `[]` | no |
4344
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
4445
| <a name="input_include_global_service_events"></a> [include\_global\_service\_events](#input\_include\_global\_service\_events) | Specifies whether the trail is publishing events from global services such as IAM to the log files | `bool` | `false` | no |
4546
| <a name="input_insight_selector"></a> [insight\_selector](#input\_insight\_selector) | Specifies an insight selector for type of insights to log on a trail | <pre>list(object({<br> insight_type = string<br> }))</pre> | `[]` | no |
@@ -66,5 +67,5 @@
6667
|------|-------------|
6768
| <a name="output_cloudtrail_arn"></a> [cloudtrail\_arn](#output\_cloudtrail\_arn) | The Amazon Resource Name of the trail |
6869
| <a name="output_cloudtrail_home_region"></a> [cloudtrail\_home\_region](#output\_cloudtrail\_home\_region) | The region in which the trail was created |
69-
| <a name="output_cloudtrail_id"></a> [cloudtrail\_id](#output\_cloudtrail\_id) | The name of the trail |
70+
| <a name="output_cloudtrail_id"></a> [cloudtrail\_id](#output\_cloudtrail\_id) | The ID of the trail. (Name for provider < v5, ARN for provider >= v5). |
7071
<!-- markdownlint-restore -->

examples/complete/fixtures.us-east-2.tfvars

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
enabled = true
2-
31
region = "us-east-2"
42

53
namespace = "eg"
@@ -17,3 +15,22 @@ include_global_service_events = false
1715
enable_logging = true
1816

1917
is_organization_trail = false
18+
19+
advanced_event_selector = [
20+
{
21+
name = "WriteOnly"
22+
field_selector = [
23+
{
24+
field = "eventCategory"
25+
equals = ["Data"]
26+
},
27+
{
28+
field = "resources.type"
29+
equals = ["AWS::Lambda::Function"]
30+
},
31+
{
32+
field = "readOnly"
33+
equals = ["false"]
34+
}]
35+
}
36+
]

examples/complete/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ module "cloudtrail" {
1111
is_multi_region_trail = var.is_multi_region_trail
1212
is_organization_trail = var.is_organization_trail
1313
s3_bucket_name = module.cloudtrail_s3_bucket.bucket_id
14+
advanced_event_selector = var.advanced_event_selector
1415

1516
context = module.this.context
1617
}
1718

1819
module "cloudtrail_s3_bucket" {
1920
source = "cloudposse/cloudtrail-s3-bucket/aws"
20-
version = "0.14.0"
21+
version = "0.26.4"
2122

2223
force_destroy = true
2324

examples/complete/variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,20 @@ variable "is_organization_trail" {
2727
type = bool
2828
description = "The trail is an AWS Organizations trail"
2929
}
30+
31+
variable "advanced_event_selector" {
32+
type = list(object({
33+
name = optional(string)
34+
field_selector = list(object({
35+
field = string
36+
ends_with = optional(list(string))
37+
not_ends_with = optional(list(string))
38+
equals = optional(list(string))
39+
not_equals = optional(list(string))
40+
starts_with = optional(list(string))
41+
not_starts_with = optional(list(string))
42+
}))
43+
}))
44+
description = "Specifies an advanced event selector for enabling data event logging. See: https://www.terraform.io/docs/providers/aws/r/cloudtrail.html for details on this variable"
45+
default = []
46+
}

examples/complete/versions.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 0.13.0"
2+
required_version = ">= 1.3.0"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 2.0"
7+
version = ">= 3.0"
88
}
99
local = {
1010
source = "hashicorp/local"

main.tf

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ resource "aws_cloudtrail" "default" {
2424
dynamic "event_selector" {
2525
for_each = var.event_selector
2626
content {
27-
include_management_events = lookup(event_selector.value, "include_management_events", null)
28-
read_write_type = lookup(event_selector.value, "read_write_type", null)
27+
include_management_events = lookup(event_selector.value, "include_management_events", null)
28+
read_write_type = lookup(event_selector.value, "read_write_type", null)
29+
exclude_management_event_sources = event_selector.value.exclude_management_event_sources
2930

3031
dynamic "data_resource" {
3132
for_each = lookup(event_selector.value, "data_resource", [])
@@ -36,4 +37,24 @@ resource "aws_cloudtrail" "default" {
3637
}
3738
}
3839
}
39-
}
40+
41+
dynamic "advanced_event_selector" {
42+
for_each = var.advanced_event_selector
43+
content {
44+
name = lookup(advanced_event_selector.value, "name", null)
45+
46+
dynamic "field_selector" {
47+
for_each = advanced_event_selector.value.field_selector
48+
content {
49+
field = field_selector.value.field
50+
equals = lookup(field_selector.value, "equals", null)
51+
not_equals = lookup(field_selector.value, "not_equals", null)
52+
starts_with = lookup(field_selector.value, "starts_with", null)
53+
not_starts_with = lookup(field_selector.value, "not_starts_with", null)
54+
ends_with = lookup(field_selector.value, "ends_with", null)
55+
not_ends_with = lookup(field_selector.value, "not_ends_with", null)
56+
}
57+
}
58+
}
59+
}
60+
}

outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
output "cloudtrail_id" {
2-
value = join("", aws_cloudtrail.default.*.id)
3-
description = "The name of the trail"
2+
value = join("", aws_cloudtrail.default[*].id)
3+
description = "The ID of the trail. (Name for provider < v5, ARN for provider >= v5)."
44
}
55

66
output "cloudtrail_home_region" {
7-
value = join("", aws_cloudtrail.default.*.home_region)
7+
value = join("", aws_cloudtrail.default[*].home_region)
88
description = "The region in which the trail was created"
99
}
1010

1111
output "cloudtrail_arn" {
12-
value = join("", aws_cloudtrail.default.*.arn)
12+
value = join("", aws_cloudtrail.default[*].arn)
1313
description = "The Amazon Resource Name of the trail"
1414
}

test/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ clean:
3333
all: module examples/complete
3434

3535
## Run basic sanity checks against the module itself
36-
module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
36+
module: export TESTS ?= installed lint module-pinning provider-pinning validate terraform-docs input-descriptions output-descriptions
3737
module: deps
3838
$(call RUN_TESTS, ../)
3939

4040
## Run tests against example
41-
examples/complete: export TESTS ?= installed lint get-modules get-plugins validate
41+
examples/complete: export TESTS ?= installed lint validate
4242
examples/complete: deps
4343
$(call RUN_TESTS, ../$@)

0 commit comments

Comments
 (0)