Skip to content

Commit

Permalink
Merge pull request #208 from sundaram2021/master
Browse files Browse the repository at this point in the history
Update firewall.go
  • Loading branch information
uzaxirr authored Dec 19, 2024
2 parents 9c0637d + cffe841 commit 5dd589f
Showing 1 changed file with 49 additions and 0 deletions.
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

Check failure on line 230 in firewall.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

comment on exported method Client.IsUsingDefaultRules should be of the form "IsUsingDefaultRules ..."
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
}

0 comments on commit 5dd589f

Please sign in to comment.