-
Notifications
You must be signed in to change notification settings - Fork 55
Fix egress firewall rules to support all-ports without explicit ports #218
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
Open
sidshas03
wants to merge
13
commits into
apache:main
Choose a base branch
from
sidshas03:fix-egress-firewall-all-ports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e16d5a2
egress: robust TCP/UDP all-ports read (0/0, -1/-1, 1/65535) + CIDR-se…
sidshas03 987dc7b
ci: retrigger acceptance tests
sidshas03 ec57a77
egress: improve error handling and add comprehensive unit tests
sidshas03 94bedc5
Fix code review comments: improve function documentation and grammar
sidshas03 56b0cc3
Apply code review fixes: improve documentation and comments
sidshas03 b93d1a5
Fix all-ports rule state handling
sidshas03 cc6b123
Fix all-ports rule state: remove ports field instead of setting empty…
sidshas03 a9371b8
Complete fix for all test issues
sidshas03 294f668
Fix linting issues: lowercase error messages
sidshas03 f14f64a
CRITICAL FIX: Restore delete(rule, 'ports') for all-ports rules
sidshas03 46e078c
Trigger CI: ensure proper formatting
sidshas03 ff7ae5e
Fix gofmt formatting issues
sidshas03 980edee
Merge pr-218 fixes into fix-egress-firewall-all-ports
sidshas03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ package cloudstack | |
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
|
|
@@ -31,6 +32,81 @@ import ( | |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| // treats 'all ports' for tcp/udp across CS versions returning 0/0, -1/-1, or 1/65535 | ||
| func isAllPortsTCPUDP(protocol string, start, end int) bool { | ||
| p := strings.ToLower(protocol) | ||
| if p != "tcp" && p != "udp" { | ||
| return false | ||
| } | ||
| // handle various representations of all ports across CloudStack versions | ||
| if (start == 0 && end == 0) || | ||
| (start == -1 && end == -1) || | ||
| (start == 1 && end == 65535) { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // normalizeRemoteCIDRs normalizes a comma-separated CIDR string from CloudStack API | ||
| func normalizeRemoteCIDRs(cidrList string) []string { | ||
| if cidrList == "" { | ||
| return []string{} | ||
| } | ||
|
|
||
| cidrs := strings.Split(cidrList, ",") | ||
| normalized := make([]string, 0, len(cidrs)) | ||
|
|
||
| for _, cidr := range cidrs { | ||
| trimmed := strings.TrimSpace(cidr) | ||
| if trimmed != "" { | ||
| normalized = append(normalized, trimmed) | ||
| } | ||
| } | ||
|
|
||
| sort.Strings(normalized) | ||
| return normalized | ||
| } | ||
|
|
||
| // normalizeLocalCIDRs normalizes a Terraform schema.Set of CIDRs | ||
| func normalizeLocalCIDRs(cidrSet *schema.Set) []string { | ||
| if cidrSet == nil { | ||
| return []string{} | ||
| } | ||
|
|
||
| normalized := make([]string, 0, cidrSet.Len()) | ||
| for _, cidr := range cidrSet.List() { | ||
| if cidrStr, ok := cidr.(string); ok { | ||
| trimmed := strings.TrimSpace(cidrStr) | ||
| if trimmed != "" { | ||
| normalized = append(normalized, trimmed) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sort.Strings(normalized) | ||
| return normalized | ||
| } | ||
|
|
||
| // cidrSetsEqual compares normalized CIDR sets for equality (order/whitespace agnostic) | ||
| func cidrSetsEqual(remoteCidrList string, localCidrSet *schema.Set) bool { | ||
| remoteCidrs := normalizeRemoteCIDRs(remoteCidrList) | ||
| localCidrs := normalizeLocalCIDRs(localCidrSet) | ||
|
|
||
| // Compare lengths first | ||
| if len(remoteCidrs) != len(localCidrs) { | ||
| return false | ||
| } | ||
|
|
||
| // Compare each element (both are already sorted) | ||
| for i, remoteCidr := range remoteCidrs { | ||
| if remoteCidr != localCidrs[i] { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func resourceCloudStackEgressFirewall() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Create: resourceCloudStackEgressFirewallCreate, | ||
|
|
@@ -250,6 +326,15 @@ func createEgressFirewallRule(d *schema.ResourceData, meta interface{}, rule map | |
| uuids[port.(string)] = r.Id | ||
| rule["uuids"] = uuids | ||
| } | ||
| } else { | ||
| // No ports specified - create a rule that encompasses all ports | ||
| // by not setting startport and endport parameters | ||
| r, err := cs.Firewall.CreateEgressFirewallRule(p) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| uuids["all"] = r.Id | ||
| rule["uuids"] = uuids | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -368,8 +453,74 @@ func resourceCloudStackEgressFirewallRead(d *schema.ResourceData, meta interface | |
| rule["ports"] = ports | ||
| rules.Add(rule) | ||
| } | ||
| } else { | ||
| // No ports specified - check if we have an "all" rule | ||
| id, ok := uuids["all"] | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| // Get the rule | ||
| r, ok := ruleMap[id.(string)] | ||
| if !ok { | ||
| delete(uuids, "all") | ||
| continue | ||
| } | ||
|
|
||
| // Verify this is actually an all-ports rule using our helper | ||
| if !isAllPortsTCPUDP(r.Protocol, r.Startport, r.Endport) { | ||
| // This rule has specific ports, but we expected all-ports | ||
| // This might happen if CloudStack behavior changed | ||
| continue | ||
| } | ||
|
|
||
| // Delete the known rule so only unknown rules remain in the ruleMap | ||
| delete(ruleMap, id.(string)) | ||
|
|
||
| // Create a set with all CIDR's | ||
| cidrs := &schema.Set{F: schema.HashString} | ||
| for _, cidr := range strings.Split(r.Cidrlist, ",") { | ||
| cidrs.Add(cidr) | ||
| } | ||
|
|
||
| // Update the values | ||
| rule["protocol"] = r.Protocol | ||
| rule["cidr_list"] = cidrs | ||
| rules.Add(rule) | ||
| } | ||
| } | ||
|
|
||
| // Fallback: Check if any remaining rules in ruleMap match our expected all-ports pattern | ||
| // This handles cases where CloudStack might return all-ports rules in unexpected formats | ||
| if rule["protocol"].(string) != "icmp" && strings.ToLower(rule["protocol"].(string)) != "all" { | ||
| // Look for any remaining rules that might be our all-ports rule | ||
| for ruleID, r := range ruleMap { | ||
| // Get local CIDR set for comparison | ||
| localCidrSet, ok := rule["cidr_list"].(*schema.Set) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| if isAllPortsTCPUDP(r.Protocol, r.Startport, r.Endport) && | ||
| strings.EqualFold(r.Protocol, rule["protocol"].(string)) && | ||
| cidrSetsEqual(r.Cidrlist, localCidrSet) { | ||
| // This looks like our all-ports rule, add it to state | ||
| cidrs := &schema.Set{F: schema.HashString} | ||
| for _, cidr := range strings.Split(r.Cidrlist, ",") { | ||
| cidrs.Add(cidr) | ||
| } | ||
|
|
||
| rule["protocol"] = r.Protocol | ||
| rule["cidr_list"] = cidrs | ||
| rules.Add(rule) | ||
|
|
||
| // Remove from ruleMap so it's not processed again | ||
| delete(ruleMap, ruleID) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if strings.ToLower(rule["protocol"].(string)) == "all" { | ||
| id, ok := uuids["all"] | ||
| if !ok { | ||
|
|
@@ -606,10 +757,9 @@ func verifyEgressFirewallRuleParams(d *schema.ResourceData, rule map[string]inte | |
| "%q is not a valid port value. Valid options are '80' or '80-90'", port.(string)) | ||
| } | ||
| } | ||
| } else { | ||
| return fmt.Errorf( | ||
| "Parameter ports is a required parameter when *not* using protocol 'icmp'") | ||
| } | ||
| // Note: ports parameter is optional for TCP/UDP protocols | ||
| // When omitted, the rule will encompass all ports | ||
|
||
| } else if strings.ToLower(protocol) == "all" { | ||
| if ports, _ := rule["ports"].(*schema.Set); ports.Len() > 0 { | ||
| return fmt.Errorf( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment has grammatical issues. Consider revising to: '// isAllPortsTCPUDP determines if a rule represents all ports for TCP/UDP protocols across CloudStack versions that may return 0/0, -1/-1, or 1/65535'