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

Update firewall.go #208

Merged
merged 3 commits into from
Dec 19, 2024
Merged
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
49 changes: 49 additions & 0 deletions firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,52 @@ func (c *Client) DeleteFirewallRule(id string, ruleID string) (*SimpleResponse,

return c.DecodeSimpleResponse(resp)
}



// Check if the firewall is using the default rules
func (c *Client) IsUsingDefaultRules(firewallID string) (bool, error) {
// Define default firewall rules
var defaultRules = []FirewallRule{
{Protocol: "tcp", Ports: "22", Cidr: []string{"0.0.0.0/0"}, Direction: "ingress", Action: "allow"},
{Protocol: "tcp", Ports: "80", Cidr: []string{"0.0.0.0/0"}, Direction: "ingress", Action: "allow"},
{Protocol: "tcp", Ports: "443", Cidr: []string{"0.0.0.0/0"}, Direction: "ingress", Action: "allow"},
}

// Retrieve actual firewall rules
rules, err := c.ListFirewallRules(firewallID)
if err != nil {
return false, fmt.Errorf("error retrieving firewall rules: %s", err)
}

// Compare the actual rules with the default rules
return areDefaultRules(rules, defaultRules), nil
}

// Helper function to check if the firewall rules match the default rules
func areDefaultRules(rules []FirewallRule, defaultRules []FirewallRule) bool {
if len(rules) != len(defaultRules) {
return false
}

for _, defaultRule := range defaultRules {
match := false
for _, rule := range rules {
if rule.Protocol == defaultRule.Protocol &&
rule.Ports == defaultRule.Ports &&
rule.Direction == defaultRule.Direction &&
rule.Action == defaultRule.Action &&
len(rule.Cidr) == len(defaultRule.Cidr) &&
rule.Cidr[0] == defaultRule.Cidr[0] {
match = true
break
}
}
if !match {
return false
}
}

return true
}