← Poskytovatel Cloudflare Terraform / terraform / additional-configurations
Konfigurace spravovaných sad pravidel DDoS pomocí Terraformu
Tato stránka obsahuje příklady konfigurace Spravované sady pravidel DDoS ve vaší zóně nebo účtu pomocí Terraformu. Pokrývá tyto konfigurace:
- Příklad: Konfigurace ochrany proti HTTP DDoS útokům
- Příklad: Konfigurace ochrany proti DDoS útokům na síťové vrstvě
- Případ užití: zmírnění rozsáhlých HTTP DDoS útoků a sledování označeného provozu
Spravované sady pravidel DDoS jsou vždy povoleny. V závislosti na vašich službách Cloudflare můžete mít možnost upravit jejich chování.
Pokud používáte Cloudflare API, podívejte se na následující zdroje:
- Nastavení HTTP DDoS Attack Protection přes API
- Nastavení Network-layer DDoS Attack Protection přes API
Další informace o nasazení a konfiguraci sad pravidel pomocí Rulesets API najdete v Práce se spravovanými sadami pravidel v dokumentaci k Ruleset Engine.
Než začnete
Získejte potřebná ID účtu, zóny a spravované sady pravidel
Konfigurace Terraformu uvedené na této stránce vyžadují ID zóny (nebo ID účtu) zóny či účtu, do kterého budete nasazovat spravované rulesety.
- Chcete-li získat seznam účtů, ke kterým máte přístup, včetně jejich ID, použijte Výpis účtů operace.
- Chcete-li získat seznam zón, ke kterým máte přístup, včetně jejich ID, použijte Výpis zón operace.
Nasazení spravovaných rulesetů přes Terraform vyžaduje použití ID rulesetů. Chcete-li zjistit ID spravovaných rulesetů, použijte Seznam sad pravidel na úrovni účtu operaci. Odpověď bude obsahovat popis a ID existujících spravovaných sad pravidel.
(Volitelné) Odstraňte existující rulesety a začněte od začátku
Terraform předpokládá, že má nad rulesety účtu a zóny úplnou kontrolu. Pokud již máte v účtu nebo zóně rulesety nakonfigurované, postupujte jedním z následujících způsobů:
- Import existujících sad pravidel do Terraformu pomocí
cf-terraformingnástroje. Novější verze tohoto nástroje umí vygenerovat definice prostředků pro existující rulesety a importovat jejich konfiguraci do stavu Terraformu. - Začněte od začátku tím, že odstraňování stávajících rulesetů (sady pravidel účtu a zóny s
"kind": "root"a"kind": "zone", v tomto pořadí) a poté nadefinujete konfiguraci svých rulesetů v Terraformu.
Příklad: Konfigurace ochrany proti HTTP DDoS útokům
Tento příklad konfiguruje HTTP DDoS Attack Protection spravovanou sadu pravidel pro zónu pomocí Terraformu.
Požadovaná oprávnění API tokenu
Alespoň jeden z následujících oprávnění tokenu je povinné:
HTTP DDoS Managed Ruleset Write
Nakonfigurujte cloudflare_ruleset ↗ prostředek:
resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
zone_id = var.cloudflare_zone_id
name = "HTTP DDoS Attack Protection entry point ruleset"
description = ""
kind = "zone"
phase = "ddos_l7"
rules = [{
action = "execute"
action_parameters = {
# Cloudflare L7 DDoS Attack Protection Ruleset
id = "4d21379b4f9f4bb088e0729962c8b3cf"
overrides = {
action = "block"
sensitivity_level = "default"
rules = [
{
# Adaptive DDoS Protection based on Locations (Available only to Enterprise zones with Advanced DDoS service)
id = "a8c6333711ff4b0a81371d1c444be2c3"
sensitivity_level = "default"
action = "managed_challenge"
},
{
# Adaptive DDoS Protection based on User-Agents (Available only to Enterprise zones with Advanced DDoS service)
id = "7709d496081e458899c1e3a6e4fe8e55"
sensitivity_level = "default"
action = "managed_challenge"
},
{
# HTTP requests causing a high number of origin errors.
id = "dd42da7baabe4e518eaf11c393596a9d"
sensitivity_level = "default"
action = "managed_challenge"
},
]
}
}
expression = "true"
description = "Zone-wide HTTP DDoS Override"
enabled = true
}]
}resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
zone_id = "<ZONE_ID>"
name = "HTTP DDoS Attack Protection entry point ruleset"
description = ""
kind = "zone"
phase = "ddos_l7"
rules {
action = "execute"
action_parameters {
# Cloudflare L7 DDoS Attack Protection Ruleset
id = "4d21379b4f9f4bb088e0729962c8b3cf"
overrides {
action = "block"
sensitivity_level = "default"
rules {
# Adaptive DDoS Protection based on Locations (Available only to Enterprise zones with Advanced DDoS service)
id = "a8c6333711ff4b0a81371d1c444be2c3"
sensitivity_level = "default"
action = "managed_challenge"
}
rules {
# Adaptive DDoS Protection based on User-Agents (Available only to Enterprise zones with Advanced DDoS service)
id = "7709d496081e458899c1e3a6e4fe8e55"
sensitivity_level = "default"
action = "managed_challenge"
}
rules {
# HTTP requests causing a high number of origin errors.
id = "dd42da7baabe4e518eaf11c393596a9d"
sensitivity_level = "default"
action = "managed_challenge"
}
}
}
expression = "true"
description = "Zone-wide HTTP DDoS Override"
enabled = true
}
}Další informace o ochraně proti HTTP DDoS útokům najdete v Spravovaná sada pravidel HTTP DDoS Attack Protection.
Příklad: Konfigurace ochrany proti DDoS útokům na síťové vrstvě
Tento příklad konfiguruje Network-layer DDoS Attack Protection spravovanou sadu pravidel pro účet pomocí Terraformu a mění úroveň citlivosti pravidla s ID low pomocí přepsání.
Požadovaná oprávnění API tokenu
Alespoň jeden z následujících oprávnění tokenu je povinné:
L4 DDoS Managed Ruleset Write
Nakonfigurujte cloudflare_ruleset ↗ prostředek:
resource "cloudflare_ruleset" "account_level_network_ddos_config" {
account_id = var.cloudflare_account_id
name = "Network-layer DDoS Attack Protection entry point ruleset"
description = ""
kind = "root"
phase = "ddos_l4"
rules = [{
ref = "override_l7_ddos_ruleset_dst_ip"
description = "Override the HTTP DDoS Attack Protection managed ruleset"
expression = "ip.dst in { 192.0.2.0/24 }"
action = "execute"
action_parameters = {
# Cloudflare L3/4 DDoS Attack Protection Ruleset
id = "3b64149bfa6e4220bbbc2bd6db589552"
overrides = {
rules = [{
# Rule: Generic high-volume UDP traffic flows.
id = "599dab0942ff4898ac1b7797e954e98b"
sensitivity_level = "low"
}]
}
}
}]
}resource "cloudflare_ruleset" "account_level_network_ddos_config" {
account_id = "<ACCOUNT_ID>"
name = "Network-layer DDoS Attack Protection entry point ruleset"
description = ""
kind = "root"
phase = "ddos_l4"
rules {
ref = "override_l7_ddos_ruleset_dst_ip"
description = "Override the HTTP DDoS Attack Protection managed ruleset"
expression = "ip.dst in { 192.0.2.0/24 }"
action = "execute"
action_parameters {
# Cloudflare L3/4 DDoS Attack Protection Ruleset
id = "3b64149bfa6e4220bbbc2bd6db589552"
overrides {
rules {
# Rule: Generic high-volume UDP traffic flows.
id = "599dab0942ff4898ac1b7797e954e98b"
sensitivity_level = "low"
}
}
}
}
}Další informace o ochraně proti DDoS útokům na síťové vrstvě najdete v Spravovaná sada pravidel Network-layer DDoS Attack Protection.
Případ užití: zmírnění rozsáhlých HTTP DDoS útoků a sledování označeného provozu
V následujícím příkladu se zákazník obává falešně pozitivních výsledků, ale zároveň chce mít ochranu proti rozsáhlým HTTP DDoS útokům. Dvě pravidla, z nichž každé obsahuje dva přepisy, v jejich Ochrana před útoky HTTP DDoS konfigurace bude mít následující chování:
- Zmírněte velké HTTP DDoS útoky nakonfigurováním pravidla s Low úroveň citlivosti a Block jako akci.
- Sledujte provoz označený systémem ochrany proti DDoS útokům nakonfigurováním pravidla s výchozí úrovní citlivosti (Vysoká) a Log jako akci.
Pořadí pravidel je důležité: pravidlo s nejvyšší úrovní citlivosti musí být uvedeno až po pravidle s nejnižší úrovní citlivosti, jinak nikdy nebude vyhodnoceno.
Požadovaná oprávnění API tokenu
Alespoň jeden z následujících oprávnění tokenu je povinné:
HTTP DDoS Managed Ruleset Write
Nakonfigurujte cloudflare_ruleset ↗ prostředek:
resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
zone_id = var.cloudflare_zone_id
name = "HTTP DDoS - Terraform managed"
description = ""
kind = "zone"
phase = "ddos_l7"
# The resource configuration contains two rules:
# 1. The first rule has the lowest sensitivity level (highest threshold)
# and it will block attacks.
# 2. The second rule has a higher sensitivity level (lower threshold) and
# will only apply a Log action.
#
# In practice, evaluation stops whenever a rule matches both the expression
# and the threshold, so the rule order is important:
# - When the traffic rate is below the (low) threshold of the default
# sensitivity level ('High'), no rules match (no action is applied).
# - When the traffic rate is between the thresholds of the 'Low' and
# default ('High') sensitivity levels, the first rule does not match,
# but the second rule does (traffic gets logged).
# - When the traffic rate goes above the (high) threshold of the 'Low'
# sensitivity level, the first rule matches (traffic gets blocked).
#
# The DDoS protection systems will still apply mitigation actions to incoming
# traffic when rates exceed the threshold of the _Essentially Off_ sensitivity
# level.
rules = [
{
ref = "l7_ddos_block_traffic_low_threshold"
description = "At the low sensitivity threshold, block the traffic"
expression = "true"
action = "execute"
action_parameters = {
# Cloudflare L7 DDoS Attack Protection Ruleset
id = "4d21379b4f9f4bb088e0729962c8b3cf"
overrides = {
rules = [
{
# Rule: HTTP requests from known botnet (signature #4).
id = "29d170ba2f004cc787b1ac272c9e04e7"
sensitivity_level = "low"
action = "block"
},
{
# Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
id = "60a48054bbcf4014ac63c44f1712a123"
sensitivity_level = "low"
action = "block"
},
]
}
}
},
{
ref = "l7_ddos_log_default_threshold"
description = "At the default sensitivity threshold, log to see if any legitimate traffic gets caught"
expression = "true"
action = "execute"
action_parameters = {
# Cloudflare L7 DDoS Attack Protection Ruleset
id = "4d21379b4f9f4bb088e0729962c8b3cf"
overrides = {
rules = [
{
# Rule: HTTP requests from known botnet (signature #4).
id = "29d170ba2f004cc787b1ac272c9e04e7"
sensitivity_level = "default"
action = "log"
},
{
# Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
id = "60a48054bbcf4014ac63c44f1712a123"
sensitivity_level = "default"
action = "log"
},
]
}
}
},
]
}variable "zone_id" {
default = "<ZONE_ID>"
}
resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
zone_id = var.zone_id
name = "HTTP DDoS - Terraform managed"
description = ""
kind = "zone"
phase = "ddos_l7"
# The resource configuration contains two rules:
# 1. The first rule has the lowest sensitivity level (highest threshold)
# and it will block attacks.
# 2. The second rule has a higher sensitivity level (lower threshold) and
# will only apply a Log action.
#
# In practice, evaluation stops whenever a rule matches both the expression
# and the threshold, so the rule order is important:
# - When the traffic rate is below the (low) threshold of the default
# sensitivity level ('High'), no rules match (no action is applied).
# - When the traffic rate is between the thresholds of the 'Low' and
# default ('High') sensitivity levels, the first rule does not match,
# but the second rule does (traffic gets logged).
# - When the traffic rate goes above the (high) threshold of the 'Low'
# sensitivity level, the first rule matches (traffic gets blocked).
#
# The DDoS protection systems will still apply mitigation actions to incoming
# traffic when rates exceed the threshold of the _Essentially Off_ sensitivity
# level.
rules {
ref = "l7_ddos_block_traffic_low_threshold"
description = "At the low sensitivity threshold, block the traffic"
expression = "true"
action = "execute"
action_parameters {
# Cloudflare L7 DDoS Attack Protection Ruleset
id = "4d21379b4f9f4bb088e0729962c8b3cf"
overrides {
rules {
# Rule: HTTP requests from known botnet (signature #4).
id = "29d170ba2f004cc787b1ac272c9e04e7"
sensitivity_level = "low"
action = "block"
}
rules {
# Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
id = "60a48054bbcf4014ac63c44f1712a123"
sensitivity_level = "low"
action = "block"
}
}
}
}
rules {
ref = "l7_ddos_log_default_threshold"
description = "At the default sensitivity threshold, log to see if any legitimate traffic gets caught"
expression = "true"
action = "execute"
action_parameters {
# Cloudflare L7 DDoS Attack Protection Ruleset
id = "4d21379b4f9f4bb088e0729962c8b3cf"
overrides {
rules {
# Rule: HTTP requests from known botnet (signature #4).
id = "29d170ba2f004cc787b1ac272c9e04e7"
sensitivity_level = "default"
action = "log"
}
rules {
# Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
id = "60a48054bbcf4014ac63c44f1712a123"
sensitivity_level = "default"
action = "log"
}
}
}
}
}