这是indexloc提供的服务,不要输入任何密码
Skip to content

[firewall config resource] Add AI bots support #326

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

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/resources/firewall_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ resource "vercel_firewall_config" "managed" {
action = "log"
active = true
}

ai_bots {
action = "log"
active = true
}
}
}

Expand Down Expand Up @@ -235,9 +240,19 @@ Read-Only:

Optional:

- `ai_bots` (Block, Optional) Enable the ai_bots managed ruleset and select action (see [below for nested schema](#nestedblock--managed_rulesets--ai_bots))
- `bot_filter` (Block, Optional) Enable the bot_filter managed ruleset and select action (see [below for nested schema](#nestedblock--managed_rulesets--bot_filter))
- `owasp` (Block, Optional) Enable the owasp managed rulesets and select ruleset behaviors (see [below for nested schema](#nestedblock--managed_rulesets--owasp))

<a id="nestedblock--managed_rulesets--ai_bots"></a>
### Nested Schema for `managed_rulesets.ai_bots`

Optional:

- `action` (String)
- `active` (Boolean)


<a id="nestedblock--managed_rulesets--bot_filter"></a>
### Nested Schema for `managed_rulesets.bot_filter`

Expand Down
5 changes: 5 additions & 0 deletions examples/resources/vercel_firewall_config/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ resource "vercel_firewall_config" "managed" {
action = "log"
active = true
}

ai_bots {
action = "log"
active = true
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions vercel/resource_firewall_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ Define Custom Rules to shape the way your traffic is handled by the Vercel Edge
},
},
},
"ai_bots": schema.SingleNestedBlock{
Description: "Enable the ai_bots managed ruleset and select action",
Attributes: map[string]schema.Attribute{
"active": schema.BoolAttribute{
Optional: true,
},
"action": schema.StringAttribute{
Optional: true,
},
},
},
},
},
"rules": schema.SingleNestedBlock{
Expand Down Expand Up @@ -456,6 +467,7 @@ type FirewallConfig struct {
type FirewallManagedRulesets struct {
OWASP *CRSRule `tfsdk:"owasp"`
BotFilter *BotFilterConfig `tfsdk:"bot_filter"`
AiBots *AiBotsConfig `tfsdk:"ai_bots"`
}

type CRSRule struct {
Expand Down Expand Up @@ -496,6 +508,11 @@ type BotFilterConfig struct {
Action types.String `tfsdk:"action"`
}

type AiBotsConfig struct {
Active types.Bool `tfsdk:"active"`
Action types.String `tfsdk:"action"`
}

type FirewallRules struct {
Rules []FirewallRule `tfsdk:"rule"`
}
Expand Down Expand Up @@ -861,6 +878,7 @@ func fromClient(conf client.FirewallConfig, state FirewallConfig) (FirewallConfi
if conf.CRS != nil && state.ManagedRulesets != nil {
cfg.ManagedRulesets.OWASP = fromCRS(conf.CRS, state.ManagedRulesets)
}

botFilter, botFilterExist := conf.ManagedRulesets["bot_filter"]
if botFilterExist {
botFilterConf := &BotFilterConfig{
Expand All @@ -869,6 +887,15 @@ func fromClient(conf client.FirewallConfig, state FirewallConfig) (FirewallConfi
}
cfg.ManagedRulesets.BotFilter = botFilterConf
}

aiBots, aiBotsExist := conf.ManagedRulesets["ai_bots"]
if aiBotsExist {
aiBotsConf := &AiBotsConfig{
Active: types.BoolValue(aiBots.Active),
Action: types.StringValue(aiBots.Action),
}
cfg.ManagedRulesets.AiBots = aiBotsConf
}
}

return cfg, nil
Expand Down Expand Up @@ -897,13 +924,22 @@ func (f *FirewallConfig) toClient() (client.FirewallConfig, error) {
}
}
}

botFilter := f.ManagedRulesets.BotFilter
if botFilter != nil {
conf.ManagedRulesets["bot_filter"] = client.ManagedRule{
Active: botFilter.Active.ValueBool(),
Action: botFilter.Action.ValueString(),
}
}

aiBots := f.ManagedRulesets.AiBots
if aiBots != nil {
conf.ManagedRulesets["ai_bots"] = client.ManagedRule{
Active: aiBots.Active.ValueBool(),
Action: aiBots.Action.ValueString(),
}
}
}
if f.Rules != nil && len(f.Rules.Rules) > 0 {
for _, rule := range f.Rules.Rules {
Expand Down
53 changes: 51 additions & 2 deletions vercel/resource_firewall_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func TestAcc_FirewallConfigResource(t *testing.T) {
"vercel_firewall_config.ips",
"ip_rules.rule.2.hostname",
"*"),

resource.TestCheckResourceAttr(
"vercel_firewall_config.botfilter",
"managed_rulesets.bot_filter.action",
Expand All @@ -178,6 +177,14 @@ func TestAcc_FirewallConfigResource(t *testing.T) {
"vercel_firewall_config.botfilter",
"managed_rulesets.bot_filter.active",
"true"),
resource.TestCheckResourceAttr(
"vercel_firewall_config.aibots",
"managed_rulesets.ai_bots.action",
"challenge"),
resource.TestCheckResourceAttr(
"vercel_firewall_config.aibots",
"managed_rulesets.ai_bots.active",
"true"),
),
},
{
Expand All @@ -200,6 +207,11 @@ func TestAcc_FirewallConfigResource(t *testing.T) {
ResourceName: "vercel_firewall_config.botfilter",
ImportStateIdFunc: getFirewallImportID("vercel_firewall_config.botfilter"),
},
{
ImportState: true,
ResourceName: "vercel_firewall_config.aibots",
ImportStateIdFunc: getFirewallImportID("vercel_firewall_config.aibots"),
},
{
Config: cfg(testAccFirewallConfigResourceUpdated(name)),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -343,7 +355,6 @@ func TestAcc_FirewallConfigResource(t *testing.T) {
"vercel_firewall_config.ips",
"ip_rules.rule.2.hostname",
"*"),

resource.TestCheckResourceAttr(
"vercel_firewall_config.botfilter",
"managed_rulesets.bot_filter.action",
Expand All @@ -352,6 +363,14 @@ func TestAcc_FirewallConfigResource(t *testing.T) {
"vercel_firewall_config.botfilter",
"managed_rulesets.bot_filter.active",
"false"),
resource.TestCheckResourceAttr(
"vercel_firewall_config.aibots",
"managed_rulesets.ai_bots.action",
"deny"),
resource.TestCheckResourceAttr(
"vercel_firewall_config.aibots",
"managed_rulesets.ai_bots.active",
"false"),
),
},
},
Expand Down Expand Up @@ -517,6 +536,21 @@ resource "vercel_firewall_config" "botfilter" {
}
}

resource "vercel_project" "aibots" {
name = "test-acc-%[1]s-aibots"
}

resource "vercel_firewall_config" "aibots" {
project_id = vercel_project.aibots.id

managed_rulesets {
ai_bots {
action = "challenge"
active = true
}
}
}

`, name)
}

Expand Down Expand Up @@ -694,5 +728,20 @@ resource "vercel_firewall_config" "botfilter" {
}
}
}

resource "vercel_project" "aibots" {
name = "test-acc-%[1]s-aibots"
}

resource "vercel_firewall_config" "aibots" {
project_id = vercel_project.aibots.id

managed_rulesets {
ai_bots {
action = "deny"
active = false
}
}
}
`, name)
}