Skip to content

Commit e9f6568

Browse files
Matt Calhounmilldrgoruha
authored
add additional waf features (#791)
Co-authored-by: Dan Miller <[email protected]> Co-authored-by: Igor Rodionov <[email protected]>
1 parent 207fba8 commit e9f6568

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

modules/waf/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,17 @@ components:
7676
| Name | Type |
7777
|------|------|
7878
| [aws_ssm_parameter.acl_arn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
79+
| [aws_alb.alb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/alb) | data source |
80+
| [aws_lbs.alb_by_tags](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/lbs) | data source |
7981
8082
## Inputs
8183
8284
| Name | Description | Type | Default | Required |
8385
|------|-------------|------|---------|:--------:|
8486
| <a name="input_acl_name"></a> [acl\_name](#input\_acl\_name) | Friendly name of the ACL. The ACL ARN will be stored in SSM under {ssm\_path\_prefix}/{acl\_name}/arn | `string` | n/a | yes |
8587
| <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 |
88+
| <a name="input_alb_names"></a> [alb\_names](#input\_alb\_names) | list of ALB names to associate with the web ACL. | `list(string)` | `[]` | no |
89+
| <a name="input_alb_tags"></a> [alb\_tags](#input\_alb\_tags) | list of tags to match one or more ALBs to associate with the web ACL. | `list(map(string))` | `[]` | no |
8690
| <a name="input_association_resource_arns"></a> [association\_resource\_arns](#input\_association\_resource\_arns) | A list of ARNs of the resources to associate with the web ACL.<br>This must be an ARN of an Application Load Balancer, Amazon API Gateway stage, or AWS AppSync.<br><br>Do not use this variable to associate a Cloudfront Distribution.<br>Instead, you should use the `web_acl_id` property on the `cloudfront_distribution` resource.<br>For more details, refer to https://docs.aws.amazon.com/waf/latest/APIReference/API_AssociateWebACL.html | `list(string)` | `[]` | no |
8791
| <a name="input_association_resource_component_selectors"></a> [association\_resource\_component\_selectors](#input\_association\_resource\_component\_selectors) | A list of Atmos component selectors to get from the remote state and associate their ARNs with the web ACL.<br>The components must be Application Load Balancers, Amazon API Gateway stages, or AWS AppSync.<br><br>component:<br> Atmos component name<br>component\_arn\_output:<br> The component output that defines the component ARN<br><br>Set `tenant`, `environment` and `stage` if the components are in different OUs, regions or accounts.<br><br>Do not use this variable to select a Cloudfront Distribution component.<br>Instead, you should use the `web_acl_id` property on the `cloudfront_distribution` resource.<br>For more details, refer to https://docs.aws.amazon.com/waf/latest/APIReference/API_AssociateWebACL.html | <pre>list(object({<br> component = string<br> namespace = optional(string, null)<br> tenant = optional(string, null)<br> environment = optional(string, null)<br> stage = optional(string, null)<br> component_arn_output = string<br> }))</pre> | `[]` | no |
8892
| <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 |

modules/waf/alb.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
locals {
2+
alb_arns = concat(local.alb_name_arns, local.alb_tag_arns)
3+
alb_name_arns = [for alb_instance in data.aws_alb.alb : alb_instance.arn]
4+
alb_tag_arns = flatten([for alb_instance in data.aws_lbs.alb_by_tags : alb_instance.arns])
5+
}
6+
7+
data "aws_alb" "alb" {
8+
for_each = toset(var.alb_names)
9+
name = each.key
10+
}
11+
12+
data "aws_lbs" "alb_by_tags" {
13+
for_each = { for i, v in var.alb_tags : i => v }
14+
tags = each.value
15+
}

modules/waf/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ locals {
66
if local.enabled
77
]
88

9-
association_resource_arns = concat(var.association_resource_arns, local.association_resource_component_selectors_arns)
9+
association_resource_arns = toset(concat(var.association_resource_arns, local.association_resource_component_selectors_arns, local.alb_arns))
1010

1111
log_destination_component_selectors = [
1212
for i, v in var.log_destination_component_selectors : module.log_destination_components[i].outputs[v.component_output]

modules/waf/remote-state.tf

100644100755
File mode changed.

modules/waf/variables.tf

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,119 @@ variable "token_domains" {
107107
default = null
108108
}
109109

110+
# Logging configuration
111+
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_logging_configuration.html
112+
variable "log_destination_configs" {
113+
type = list(string)
114+
default = []
115+
description = "The Amazon Kinesis Data Firehose, CloudWatch Log log group, or S3 bucket Amazon Resource Names (ARNs) that you want to associate with the web ACL"
116+
}
117+
118+
variable "redacted_fields" {
119+
type = map(object({
120+
method = optional(bool, false)
121+
uri_path = optional(bool, false)
122+
query_string = optional(bool, false)
123+
single_header = optional(list(string), null)
124+
}))
125+
default = {}
126+
description = <<-DOC
127+
The parts of the request that you want to keep out of the logs.
128+
You can only specify one of the following: `method`, `query_string`, `single_header`, or `uri_path`
129+
130+
method:
131+
Whether to enable redaction of the HTTP method.
132+
The method indicates the type of operation that the request is asking the origin to perform.
133+
uri_path:
134+
Whether to enable redaction of the URI path.
135+
This is the part of a web request that identifies a resource.
136+
query_string:
137+
Whether to enable redaction of the query string.
138+
This is the part of a URL that appears after a `?` character, if any.
139+
single_header:
140+
The list of names of the query headers to redact.
141+
DOC
142+
nullable = false
143+
}
144+
145+
variable "logging_filter" {
146+
type = object({
147+
default_behavior = string
148+
filter = list(object({
149+
behavior = string
150+
requirement = string
151+
condition = list(object({
152+
action_condition = optional(object({
153+
action = string
154+
}), null)
155+
label_name_condition = optional(object({
156+
label_name = string
157+
}), null)
158+
}))
159+
}))
160+
})
161+
default = null
162+
description = <<-DOC
163+
A configuration block that specifies which web requests are kept in the logs and which are dropped.
164+
You can filter on the rule action and on the web request labels that were applied by matching rules during web ACL evaluation.
165+
DOC
166+
}
167+
168+
# Association resources
169+
variable "association_resource_arns" {
170+
type = list(string)
171+
default = []
172+
description = <<-DOC
173+
A list of ARNs of the resources to associate with the web ACL.
174+
This must be an ARN of an Application Load Balancer, Amazon API Gateway stage, or AWS AppSync.
175+
176+
Do not use this variable to associate a Cloudfront Distribution.
177+
Instead, you should use the `web_acl_id` property on the `cloudfront_distribution` resource.
178+
For more details, refer to https://docs.aws.amazon.com/waf/latest/APIReference/API_AssociateWebACL.html
179+
DOC
180+
nullable = false
181+
}
182+
183+
variable "alb_names" {
184+
description = "list of ALB names to associate with the web ACL."
185+
type = list(string)
186+
default = []
187+
nullable = false
188+
}
189+
190+
variable "alb_tags" {
191+
description = "list of tags to match one or more ALBs to associate with the web ACL."
192+
type = list(map(string))
193+
default = []
194+
nullable = false
195+
}
196+
197+
variable "association_resource_component_selectors" {
198+
type = list(object({
199+
component = string
200+
namespace = optional(string, null)
201+
tenant = optional(string, null)
202+
environment = optional(string, null)
203+
stage = optional(string, null)
204+
component_arn_output = string
205+
}))
206+
default = []
207+
description = <<-DOC
208+
A list of Atmos component selectors to get from the remote state and associate their ARNs with the web ACL.
209+
The components must be Application Load Balancers, Amazon API Gateway stages, or AWS AppSync.
210+
211+
component:
212+
Atmos component name
213+
component_arn_output:
214+
The component output that defines the component ARN
215+
216+
Do not use this variable to select a Cloudfront Distribution component.
217+
Instead, you should use the `web_acl_id` property on the `cloudfront_distribution` resource.
218+
For more details, refer to https://docs.aws.amazon.com/waf/latest/APIReference/API_AssociateWebACL.html
219+
DOC
220+
nullable = false
221+
}
222+
110223
# Rules
111224
variable "byte_match_statement_rules" {
112225
type = list(object({

0 commit comments

Comments
 (0)