Skip to content

Commit 66f96f2

Browse files
authored
feat/waf (#2298)
* feat/waf * feat/waf
1 parent 30e167b commit 66f96f2

25 files changed

+2006
-136
lines changed

.changelog/2298.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```release-note:new-resource
2+
tencentcloud_waf_auto_deny_rules
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_waf_module_status
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_waf_protection_mode
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_waf_web_shell
15+
```
16+
17+
```release-note:enhancement
18+
tencentcloud_waf_saas_domain: Support set waf status
19+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ require (
9797
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf v1.0.674
9898
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
9999
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.779
100-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.771
100+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.788
101101
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.782
102102
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
103103
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.779 h1:4NpjQiF
993993
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.779/go.mod h1:kYBG2jgpjL7CuhYM+K1fkEtbWvNXrtt7NSLwXVCqmKA=
994994
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.771 h1:y047JWTfU9KUy2H5SDqQfizq1+n7rJlYCBEMCZJEy5M=
995995
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.771/go.mod h1:ahzakUD9//SLiEPseHAS9hZhth6lqSYHfc2w2rmQ/sM=
996+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.788 h1:xHqalD5i8WG9NoIrURhH/1elbeVzR0ODQGVuxJLuepY=
997+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.788/go.mod h1:cQ1AQPJ+XpJi3v4LkAQ4axonhwpxWCpCIXaQl/XJFWU=
996998
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.782 h1:pVTxKpthJC8bw+nKPnLVHrprBOXdY7T0KVQ892yg81o=
997999
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.782/go.mod h1:Oy6D8ARyX6BCeMEbayqubSqNqXAOkenLexnzdwlbcs8=
9981000
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJPnZ9GvA677cTB6ELBR6B68wCR2QY1sNoGQc4=

tencentcloud/extension_waf.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ var API_SAFE_STATUS = []int{
120120
API_SAFE_STATUS_1,
121121
}
122122

123+
const (
124+
PROTECTION_STATUS_0 = 0
125+
PROTECTION_STATUS_1 = 1
126+
)
127+
128+
var PROTECTION_STATUS = []int{
129+
PROTECTION_STATUS_0,
130+
PROTECTION_STATUS_1,
131+
}
132+
123133
const (
124134
IPV6_ON = 1
125135
IPV6_OFF = 2

tencentcloud/provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,10 @@ Web Application Firewall(WAF)
19651965
tencentcloud_waf_saas_instance
19661966
tencentcloud_waf_anti_fake
19671967
tencentcloud_waf_anti_info_leak
1968+
tencentcloud_waf_auto_deny_rules
1969+
tencentcloud_waf_module_status
1970+
tencentcloud_waf_protection_mode
1971+
tencentcloud_waf_web_shell
19681972
19691973
Wedata
19701974
Data Source
@@ -3735,6 +3739,10 @@ func Provider() *schema.Provider {
37353739
"tencentcloud_waf_saas_instance": resourceTencentCloudWafSaasInstance(),
37363740
"tencentcloud_waf_anti_fake": resourceTencentCloudWafAntiFake(),
37373741
"tencentcloud_waf_anti_info_leak": resourceTencentCloudWafAntiInfoLeak(),
3742+
"tencentcloud_waf_auto_deny_rules": resourceTencentCloudWafAutoDenyRules(),
3743+
"tencentcloud_waf_module_status": resourceTencentCloudWafModuleStatus(),
3744+
"tencentcloud_waf_protection_mode": resourceTencentCloudWafProtectionMode(),
3745+
"tencentcloud_waf_web_shell": resourceTencentCloudWafWebShell(),
37383746
"tencentcloud_wedata_function": resourceTencentCloudWedataFunction(),
37393747
"tencentcloud_wedata_resource": resourceTencentCloudWedataResource(),
37403748
"tencentcloud_wedata_script": resourceTencentCloudWedataScript(),
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
Provides a resource to create a waf auto_deny_rules
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_waf_auto_deny_rules" "example" {
8+
domain = "demo.waf.com"
9+
attack_threshold = 20
10+
time_threshold = 12
11+
deny_time_threshold = 5
12+
}
13+
```
14+
15+
Import
16+
17+
waf auto_deny_rules can be imported using the id, e.g.
18+
19+
```
20+
terraform import tencentcloud_waf_auto_deny_rules.example demo.waf.com
21+
```
22+
*/
23+
package tencentcloud
24+
25+
import (
26+
"context"
27+
"fmt"
28+
"log"
29+
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
32+
waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125"
33+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
34+
)
35+
36+
func resourceTencentCloudWafAutoDenyRules() *schema.Resource {
37+
return &schema.Resource{
38+
Create: resourceTencentCloudWafAutoDenyRulesCreate,
39+
Read: resourceTencentCloudWafAutoDenyRulesRead,
40+
Delete: resourceTencentCloudWafAutoDenyRulesDelete,
41+
Importer: &schema.ResourceImporter{
42+
State: schema.ImportStatePassthrough,
43+
},
44+
Schema: map[string]*schema.Schema{
45+
"domain": {
46+
Required: true,
47+
ForceNew: true,
48+
Type: schema.TypeString,
49+
Description: "Domain.",
50+
},
51+
"attack_threshold": {
52+
Required: true,
53+
ForceNew: true,
54+
Type: schema.TypeInt,
55+
ValidateFunc: validateIntegerInRange(2, 100),
56+
Description: "The threshold number of attacks that triggers IP autodeny, ranging from 2 to 100 times.",
57+
},
58+
"time_threshold": {
59+
Required: true,
60+
ForceNew: true,
61+
Type: schema.TypeInt,
62+
ValidateFunc: validateIntegerInRange(1, 60),
63+
Description: "IP autodeny statistical time, ranging from 1-60 minutes.",
64+
},
65+
"deny_time_threshold": {
66+
Required: true,
67+
ForceNew: true,
68+
Type: schema.TypeInt,
69+
ValidateFunc: validateIntegerInRange(5, 360),
70+
Description: "The IP autodeny time after triggering the IP autodeny, ranging from 5 to 360 minutes.",
71+
},
72+
},
73+
}
74+
}
75+
76+
func resourceTencentCloudWafAutoDenyRulesCreate(d *schema.ResourceData, meta interface{}) error {
77+
defer logElapsed("resource.tencentcloud_waf_auto_deny_rules.create")()
78+
defer inconsistentCheck(d, meta)()
79+
80+
var (
81+
logId = getLogId(contextNil)
82+
request = waf.NewModifyWafAutoDenyRulesRequest()
83+
domain string
84+
)
85+
86+
if v, ok := d.GetOk("domain"); ok {
87+
request.Domain = helper.String(v.(string))
88+
domain = v.(string)
89+
}
90+
91+
if v, ok := d.GetOkExists("attack_threshold"); ok {
92+
request.AttackThreshold = helper.IntInt64(v.(int))
93+
}
94+
95+
if v, ok := d.GetOkExists("time_threshold"); ok {
96+
request.TimeThreshold = helper.IntInt64(v.(int))
97+
}
98+
99+
if v, ok := d.GetOkExists("deny_time_threshold"); ok {
100+
request.DenyTimeThreshold = helper.IntInt64(v.(int))
101+
}
102+
103+
request.DefenseStatus = helper.IntInt64(1)
104+
105+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
106+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseWafClient().ModifyWafAutoDenyRules(request)
107+
if e != nil {
108+
return retryError(e)
109+
} else {
110+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
111+
}
112+
113+
if result == nil || *result.Response.Success.Code != "Success" {
114+
e = fmt.Errorf("create waf autoDenyRules not exists")
115+
return resource.NonRetryableError(e)
116+
}
117+
118+
return nil
119+
})
120+
121+
if err != nil {
122+
log.Printf("[CRITAL]%s create waf autoDenyRules failed, reason:%+v", logId, err)
123+
return err
124+
}
125+
126+
d.SetId(domain)
127+
128+
return resourceTencentCloudWafAutoDenyRulesRead(d, meta)
129+
}
130+
131+
func resourceTencentCloudWafAutoDenyRulesRead(d *schema.ResourceData, meta interface{}) error {
132+
defer logElapsed("resource.tencentcloud_waf_auto_deny_rules.read")()
133+
defer inconsistentCheck(d, meta)()
134+
135+
var (
136+
logId = getLogId(contextNil)
137+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
138+
service = WafService{client: meta.(*TencentCloudClient).apiV3Conn}
139+
domain = d.Id()
140+
)
141+
142+
autoDenyRules, err := service.DescribeWafAutoDenyRulesById(ctx, domain)
143+
if err != nil {
144+
return err
145+
}
146+
147+
if autoDenyRules == nil {
148+
d.SetId("")
149+
log.Printf("[WARN]%s resource `WafAutoDenyRules` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
150+
return nil
151+
}
152+
153+
_ = d.Set("domain", domain)
154+
155+
if autoDenyRules.AttackThreshold != nil {
156+
_ = d.Set("attack_threshold", autoDenyRules.AttackThreshold)
157+
}
158+
159+
if autoDenyRules.TimeThreshold != nil {
160+
_ = d.Set("time_threshold", autoDenyRules.TimeThreshold)
161+
}
162+
163+
if autoDenyRules.DenyTimeThreshold != nil {
164+
_ = d.Set("deny_time_threshold", autoDenyRules.DenyTimeThreshold)
165+
}
166+
167+
return nil
168+
}
169+
170+
func resourceTencentCloudWafAutoDenyRulesDelete(d *schema.ResourceData, meta interface{}) error {
171+
defer logElapsed("resource.tencentcloud_waf_auto_deny_rules.delete")()
172+
defer inconsistentCheck(d, meta)()
173+
174+
var (
175+
logId = getLogId(contextNil)
176+
request = waf.NewModifyWafAutoDenyRulesRequest()
177+
domain = d.Id()
178+
)
179+
180+
if v, ok := d.GetOkExists("attack_threshold"); ok {
181+
request.AttackThreshold = helper.IntInt64(v.(int))
182+
}
183+
184+
if v, ok := d.GetOkExists("time_threshold"); ok {
185+
request.TimeThreshold = helper.IntInt64(v.(int))
186+
}
187+
188+
if v, ok := d.GetOkExists("deny_time_threshold"); ok {
189+
request.DenyTimeThreshold = helper.IntInt64(v.(int))
190+
}
191+
192+
request.Domain = &domain
193+
request.DefenseStatus = helper.IntInt64(0)
194+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
195+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseWafClient().ModifyWafAutoDenyRules(request)
196+
if e != nil {
197+
return retryError(e)
198+
} else {
199+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
200+
}
201+
202+
if result == nil || *result.Response.Success.Code != "Success" {
203+
e = fmt.Errorf("delete waf autoDenyRules not exists")
204+
return resource.NonRetryableError(e)
205+
}
206+
207+
return nil
208+
})
209+
210+
if err != nil {
211+
log.Printf("[CRITAL]%s delete waf autoDenyRules failed, reason:%+v", logId, err)
212+
return err
213+
}
214+
215+
return nil
216+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudWafAutoDenyRulesResource_basic -v
10+
func TestAccTencentCloudWafAutoDenyRulesResource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccWafAutoDenyRules,
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttrSet("tencentcloud_waf_auto_deny_rules.example", "id"),
22+
resource.TestCheckResourceAttrSet("tencentcloud_waf_auto_deny_rules.example", "attack_threshold"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_waf_auto_deny_rules.example", "time_threshold"),
24+
resource.TestCheckResourceAttrSet("tencentcloud_waf_auto_deny_rules.example", "deny_time_threshold"),
25+
),
26+
},
27+
{
28+
ResourceName: "tencentcloud_waf_auto_deny_rules.example",
29+
ImportState: true,
30+
ImportStateVerify: true,
31+
},
32+
},
33+
})
34+
}
35+
36+
const testAccWafAutoDenyRules = `
37+
resource "tencentcloud_waf_auto_deny_rules" "example" {
38+
domain = "keep.qcloudwaf.com"
39+
attack_threshold = 20
40+
time_threshold = 12
41+
deny_time_threshold = 5
42+
}
43+
`

0 commit comments

Comments
 (0)