-
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.
OPS-5899 Adopt old module for cloudflare firewall rules (#1)
- Loading branch information
Showing
10 changed files
with
171 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ override.tf.json | |
|
||
# Vars file | ||
/*.tfvars | ||
|
||
#IDEA | ||
.idea |
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,5 @@ | ||
data "cloudflare_zones" "domain" { | ||
filter { | ||
name = var.domain | ||
} | ||
} |
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 @@ | ||
locals { | ||
rules = [for rule in var.rules : | ||
{ | ||
action = rule.action | ||
action_parameters = rule.action == "skip" ? { | ||
ruleset = length(rule.products) == 0 ? "current" : null | ||
products = length(rule.products) > 0 ? rule.products : null | ||
} : null | ||
description = rule.description | ||
enabled = rule.enabled | ||
expression = rule.expression | ||
logging = rule.action == "skip" ? { | ||
enabled = true | ||
} : null | ||
} | ||
] | ||
} |
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,30 @@ | ||
resource "cloudflare_ruleset" "http_request_firewall_custom" { | ||
zone_id = lookup(data.cloudflare_zones.domain.zones[0], "id") | ||
name = "default" | ||
kind = "zone" | ||
phase = "http_request_firewall_custom" | ||
|
||
dynamic "rules" { | ||
for_each = local.rules | ||
|
||
content { | ||
action = rules.value.action | ||
dynamic "action_parameters" { | ||
for_each = rules.value.action_parameters[*] | ||
content { | ||
ruleset = action_parameters.value.ruleset | ||
products = action_parameters.value.products | ||
} | ||
} | ||
description = rules.value.description | ||
enabled = rules.value.enabled | ||
expression = rules.value.expression | ||
dynamic "logging" { | ||
for_each = rules.value.logging[*] | ||
content { | ||
enabled = logging.value.enabled | ||
} | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
output "domain" { | ||
description = "Current zone information." | ||
value = data.cloudflare_zones.domain.zones | ||
} | ||
|
||
output "rules" { | ||
description = "Created Cloudflare rules for the current zone." | ||
value = cloudflare_ruleset.http_request_firewall_custom.rules | ||
} |
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,3 @@ | ||
provider "cloudflare" { | ||
api_token = var.api_token | ||
} |
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,36 @@ | ||
variable "api_token" { | ||
description = "The Cloudflare API token." | ||
type = string | ||
sensitive = true | ||
} | ||
|
||
variable "domain" { | ||
description = "Cloudflare domain to apply rules for." | ||
type = string | ||
} | ||
|
||
variable "rules" { | ||
description = "List of Cloudflare firewall rule objects." | ||
type = list(object({ | ||
description = string | ||
enabled = bool | ||
action = string | ||
expression = string | ||
products = list(string) | ||
})) | ||
default = [] | ||
|
||
# Ensure we specify only allows action values | ||
# https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/firewall_rule#action | ||
validation { | ||
condition = can([for rule in var.rules : contains(["block", "challenge", "js_challenge", "log", "managed_challenge", "skip"], rule.action)]) | ||
error_message = "Only the following action elements are allowed: block, challenge, js_challenge, log, managed_challenge, skip." | ||
} | ||
|
||
# Ensure we specify only allowed products values | ||
# https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/firewall_rule#products | ||
validation { | ||
condition = can([for rule in var.rules : [for product in rule.products : contains(["bic", "hot", "ratelimit", "securityLevel", "uablock", "waf", "zonelockdown"], product)]]) | ||
error_message = "Only the following product elements are allowed: bic, hot, ratelimit, securityLevel, uablock, waf, zonelockdown." | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
terraform { | ||
required_providers { | ||
cloudflare = { | ||
source = "cloudflare/cloudflare" | ||
version = "~> 4.20" | ||
} | ||
} | ||
required_version = "~> 1.3" | ||
} |