Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ip_set support #49

Closed
wants to merge 13 commits into from
3 changes: 3 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ references:
- name: aws_wafv2_web_acl_logging_configuration
description: Creates a WAFv2 Web ACL Logging Configuration
url: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_logging_configuration.html
- name: aws_wafv2_ip_set
description: Creates a WAFv2 Web ACL resource
url: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_ip_set

description: |-
Terraform module to create and manage AWS WAFv2 rules.
Expand Down
15 changes: 15 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,20 @@ module "waf" {
}
]

ip_set_reference_statement_rules = [
{
name = "rule-100"
priority = 100
action = "block"

statement = {
ip_set = {
ip_address_version = "IPV4"
addresses = ["17.0.0.0/8"]
}
}
}
]

context = module.this.context
}
34 changes: 34 additions & 0 deletions ipset.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
locals {
ip_sets = local.enabled && var.ip_set_reference_statement_rules != null ? {
for indx, rule in flatten(var.ip_set_reference_statement_rules) :
lookup(rule, "name", null) != null ? format("%s-ip-set", rule.name) : format("ip-set-%d", rule.priority)
=> rule.statement.ipset if try(rule.statement.ipset, null) != null && try(rule.statement.arn, null) == null
} : {}

ip_rule_to_ip_set = local.enabled && local.ip_set_reference_statement_rules != null ? {
for name, rule in local.ip_set_reference_statement_rules :
name => lookup(rule, "name", null) != null ? format("%s-ip-set", rule.name) : format("ip-set-%d", rule.priority)
} : {}
}

module "ip_set_label" {
for_each = local.ip_sets

source = "cloudposse/label/null"
version = "0.25.0"

attributes = [each.key]
context = module.this.context
}

resource "aws_wafv2_ip_set" "default" {
for_each = local.ip_sets

name = module.ip_set_label[each.key].id
description = each.value.description
scope = var.scope
ip_address_version = each.value.ip_address_version
addresses = each.value.addresses

tags = module.this.tags
}
2 changes: 1 addition & 1 deletion rules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ resource "aws_wafv2_web_acl" "default" {
for_each = lookup(rule.value, "statement", null) != null ? [rule.value.statement] : []

content {
arn = ip_set_reference_statement.value.arn
arn = aws_wafv2_ip_set.default[local.ip_rule_to_ip_set[rule.key]]
Sytten marked this conversation as resolved.
Show resolved Hide resolved

dynamic "ip_set_forwarded_ip_config" {
for_each = lookup(ip_set_reference_statement.value, "ip_set_forwarded_ip_config", null) != null ? [ip_set_reference_statement.value.ip_set_forwarded_ip_config] : []
Expand Down
10 changes: 10 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ variable "ip_set_reference_statement_rules" {
statement:
arn:
The ARN of the IP Set that this statement references.
ip_set:
Defines a new IP Set

description:
A friendly description of the IP Set
addresses:
Contains an array of strings that specifies zero or more IP addresses or blocks of IP addresses.
All addresses must be specified using Classless Inter-Domain Routing (CIDR) notation.
ip_address_version:
Specify `IPV4` or `IPV6`
ip_set_forwarded_ip_config:
fallback_behavior:
The match status to assign to the web request if the request doesn't have a valid IP address in the specified position.
Expand Down