Skip to content

Commit

Permalink
Merge pull request #48 from squat/master
Browse files Browse the repository at this point in the history
lib: fix unmarshalling ipv6 ::/0
  • Loading branch information
JamesClonk authored Oct 27, 2017
2 parents 2fd0705 + c08bdc1 commit e5dad9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ func (r *FirewallRule) UnmarshalJSON(data []byte) (err error) {
r.Protocol = fmt.Sprintf("%v", fields["protocol"])
r.Port = fmt.Sprintf("%v", fields["port"])
subnet := fmt.Sprintf("%v", fields["subnet"])
if subnet == "<nil>" {
subnet = ""
}

if subnetSize > 0 && len(subnet) > 0 {
if len(subnet) > 0 {
_, r.Network, err = net.ParseCIDR(fmt.Sprintf("%s/%d", subnet, subnetSize))
if err != nil {
return fmt.Errorf("Failed to parse subnet from Vultr API")
}
} else {
// This case is used to create a valid default CIDR when the Vultr API does not return a subnet/subnet size at all, e.g. the response after creating a new rule.
_, r.Network, _ = net.ParseCIDR("0.0.0.0/0")
}

Expand Down
13 changes: 12 additions & 1 deletion lib/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func Test_Firewall_GetRules_Ok(t *testing.T) {
"2":{
"rulenumber":2,"action":"accept","protocol": "tcp","port": "80",
"subnet": "10.234.22.0","subnet_size": 24
},
"3":{
"rulenumber":3,"action":"accept","protocol": "tcp","port": "80",
"subnet": "::","subnet_size": 0
}}`)
defer server.Close()

Expand All @@ -144,7 +148,7 @@ func Test_Firewall_GetRules_Ok(t *testing.T) {
t.Error(err)
}
if assert.NotNil(t, rules) {
assert.Equal(t, 2, len(rules))
assert.Equal(t, 3, len(rules))

assert.Equal(t, rules[0].RuleNumber, 1)
assert.Equal(t, rules[0].Action, "accept")
Expand All @@ -159,6 +163,13 @@ func Test_Firewall_GetRules_Ok(t *testing.T) {
assert.Equal(t, rules[1].Port, "80")
_, netw, _ = net.ParseCIDR("10.234.22.0/24")
assert.Equal(t, rules[1].Network, netw)

assert.Equal(t, rules[2].RuleNumber, 3)
assert.Equal(t, rules[2].Action, "accept")
assert.Equal(t, rules[2].Protocol, "tcp")
assert.Equal(t, rules[2].Port, "80")
_, netw, _ = net.ParseCIDR("::/0")
assert.Equal(t, rules[2].Network, netw)
}
}

Expand Down

0 comments on commit e5dad9a

Please sign in to comment.