Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ website/vendor
!command/test-fixtures/**/.terraform/
.terraform.lock.hcl
provider.tf
.worktrees/
10 changes: 8 additions & 2 deletions cloudstack/resource_cloudstack_egress_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func deleteEgressFirewallRules(d *schema.ResourceData, meta interface{}, rules *
return errs.ErrorOrNil()
}

func deleteEgressFirewallRule(d *schema.ResourceData, meta interface{}, rule map[string]interface{}) error {
func deleteEgressFirewallRule(_ *schema.ResourceData, meta interface{}, rule map[string]interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
uuids := rule["uuids"].(map[string]interface{})

Expand Down Expand Up @@ -581,7 +581,13 @@ func verifyEgressFirewallParams(d *schema.ResourceData) error {
return nil
}

func verifyEgressFirewallRuleParams(d *schema.ResourceData, rule map[string]interface{}) error {
func verifyEgressFirewallRuleParams(_ *schema.ResourceData, rule map[string]interface{}) error {
if cidrList, ok := rule["cidr_list"].(*schema.Set); ok {
if cidrList.Contains("0.0.0.0/0") {
return fmt.Errorf("CIDR 0.0.0.0/0 is not allowed in egress firewall rules. cidr_list must be within the network subnet")
}
}

protocol := rule["protocol"].(string)
if strings.ToLower(protocol) != "all" && protocol != "tcp" && protocol != "udp" && protocol != "icmp" {
return fmt.Errorf(
Expand Down
34 changes: 34 additions & 0 deletions cloudstack/resource_cloudstack_egress_firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,44 @@ import (
"testing"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestVerifyEgressFirewallRuleParams_disallowAnyIP(t *testing.T) {
cidrSet := schema.NewSet(schema.HashString, []interface{}{"0.0.0.0/0"})
rule := map[string]interface{}{
"cidr_list": cidrSet,
"protocol": "tcp",
"ports": schema.NewSet(schema.HashString, []interface{}{"80"}),
"uuids": map[string]interface{}{},
}

err := verifyEgressFirewallRuleParams(nil, rule)
if err == nil {
t.Fatal("expected error for cidr 0.0.0.0/0, got nil")
}
if !strings.Contains(err.Error(), "0.0.0.0/0") {
t.Fatalf("expected error message to mention 0.0.0.0/0, got: %s", err.Error())
}
}

func TestVerifyEgressFirewallRuleParams_allowValidCIDR(t *testing.T) {
cidrSet := schema.NewSet(schema.HashString, []interface{}{"10.1.1.0/24"})
rule := map[string]interface{}{
"cidr_list": cidrSet,
"protocol": "tcp",
"ports": schema.NewSet(schema.HashString, []interface{}{"80"}),
"uuids": map[string]interface{}{},
}

err := verifyEgressFirewallRuleParams(nil, rule)
if err != nil {
t.Fatalf("expected no error for valid cidr, got: %s", err.Error())
}
}

func TestAccCloudStackEgressFirewall_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down