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

fix vpc acl inconsistent when port is ALL #2135

Merged
merged 2 commits into from
Sep 20, 2023
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
3 changes: 3 additions & 0 deletions .changelog/2135.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/tencentcloud_vpc_acl: Fix vpc acl entry inconsistent problem while port is `ALL`.
```
87 changes: 77 additions & 10 deletions tencentcloud/resource_tc_vpc_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"log"
"strings"

vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
Expand Down Expand Up @@ -190,28 +192,77 @@ func resourceTencentCloudVpcACLRead(d *schema.ResourceData, meta interface{}) er
_ = d.Set("name", info.NetworkAclName)
egressList := make([]string, 0, len(info.EgressEntries))
for i := range info.EgressEntries {
if info.EgressEntries[i].Port == nil || *info.EgressEntries[i].Port == "" {
// remove default rule
if CheckIfDefaultRule(info.EgressEntries[i]) {
continue
}

var (
action string
cidrBlock string
port string
protocol string
)

if info.EgressEntries[i].Action != nil {
action = *info.EgressEntries[i].Action
}
if info.EgressEntries[i].CidrBlock != nil {
cidrBlock = *info.EgressEntries[i].CidrBlock
}
if info.EgressEntries[i].Port == nil || *info.EgressEntries[i].Port == "" {
port = "ALL"
} else {
port = *info.EgressEntries[i].Port
}
if info.EgressEntries[i].Protocol != nil {
protocol = *info.EgressEntries[i].Protocol
}

result := strings.Join([]string{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了Port为空时,需给默认值"ALL"。
其余3个参数也需要额外判空?原逻辑不满足要求吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了Port为空时,需给默认值"ALL"。 其余3个参数也需要额外判空?原逻辑不满足要求吗?

后面要取值的,如果没返回值,直接*,会crash的。譬如之后如果支持ipv6规则,如果直接取cidr值,便会直接crash。

*info.EgressEntries[i].Action,
*info.EgressEntries[i].CidrBlock,
*info.EgressEntries[i].Port,
*info.EgressEntries[i].Protocol,
action,
cidrBlock,
port,
protocol,
}, FILED_SP)

egressList = append(egressList, strings.ToUpper(result))
}

ingressList := make([]string, 0, len(info.IngressEntries))
for i := range info.IngressEntries {
if info.IngressEntries[i].Port == nil || *info.IngressEntries[i].Port == "" {
// remove default rule
if CheckIfDefaultRule(info.IngressEntries[i]) {
continue
}

var (
action string
cidrBlock string
port string
protocol string
)

if info.IngressEntries[i].Action != nil {
action = *info.IngressEntries[i].Action
}
if info.IngressEntries[i].CidrBlock != nil {
cidrBlock = *info.IngressEntries[i].CidrBlock
}
if info.IngressEntries[i].Port == nil || *info.IngressEntries[i].Port == "" {
port = "ALL"
} else {
port = *info.IngressEntries[i].Port
}
if info.IngressEntries[i].Protocol != nil {
protocol = *info.IngressEntries[i].Protocol
}

result := strings.Join([]string{
*info.IngressEntries[i].Action,
*info.IngressEntries[i].CidrBlock,
*info.IngressEntries[i].Port,
*info.IngressEntries[i].Protocol,
action,
cidrBlock,
port,
protocol,
}, FILED_SP)
ingressList = append(ingressList, strings.ToUpper(result))
}
Expand Down Expand Up @@ -350,3 +401,19 @@ func resourceTencentCloudVpcACLDelete(d *schema.ResourceData, meta interface{})
}
return nil
}

func CheckIfDefaultRule(aclEntry *vpc.NetworkAclEntry) bool {
// remove default ipv6 rule
if aclEntry.Protocol != nil && *aclEntry.Protocol == "all" &&
aclEntry.Ipv6CidrBlock != nil && *aclEntry.Ipv6CidrBlock == "::/0" &&
aclEntry.Action != nil && *aclEntry.Action == "Accept" {
return true
}
// remove default cidr rule
if aclEntry.Protocol != nil && *aclEntry.Protocol == "all" &&
aclEntry.CidrBlock != nil && *aclEntry.CidrBlock == "0.0.0.0/0" &&
aclEntry.Action != nil && *aclEntry.Action == "Drop" {
return true
}
return false
}
29 changes: 27 additions & 2 deletions tencentcloud/resource_tc_vpc_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccTencentCloudVpcAcl_basic(t *testing.T) {
func TestAccTencentCloudVpcAclResource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -33,7 +33,7 @@ func TestAccTencentCloudVpcAcl_basic(t *testing.T) {
},
})
}
func TestAccTencentCloudVpcAclRulesUpdate(t *testing.T) {
func TestAccTencentCloudVpcAclRulesResource_Update(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -89,6 +89,15 @@ func TestAccTencentCloudVpcAclRulesUpdate(t *testing.T) {
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "egress.1", "ACCEPT#192.168.1.0/24#800-900#TCP"),
),
},
{
Config: testAccVpcACLConfigAllRules,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcACLExists("tencentcloud_vpc_acl.foo"),
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "name", "test_acl_update"),
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "ingress.0", "ACCEPT#0.0.0.0/0#ALL#ALL"),
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "egress.0", "ACCEPT#0.0.0.0/0#ALL#ALL"),
),
},
},
})
}
Expand Down Expand Up @@ -222,3 +231,19 @@ resource "tencentcloud_vpc_acl" "foo" {
]
}
`
const testAccVpcACLConfigAllRules = `
data "tencentcloud_vpc_instances" "default" {
is_default = true
}

resource "tencentcloud_vpc_acl" "foo" {
vpc_id = data.tencentcloud_vpc_instances.default.instance_list.0.vpc_id
name = "test_acl_update"
ingress = [
"ACCEPT#0.0.0.0/0#ALL#ALL"
]
egress = [
"ACCEPT#0.0.0.0/0#ALL#ALL"
]
}
`
Loading