-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The cli normalized the given CIDRs by default, so when a user entered…
… 10.0.0.1/8 (as a sample) the cli normalized it to 10.0.0.0/8 silent. After this MR we now validate that the given IP is the start of the CIDR block (e.g. 10.0.0.0/8). (#304) Signed-off-by: Lukas Kämmerling <[email protected]>
- Loading branch information
1 parent
6c04c99
commit 5442833
Showing
4 changed files
with
94 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package firewall | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func validateFirewallIP(ip string) (*net.IPNet, error) { | ||
i, n, err := net.ParseCIDR(ip) | ||
if err != nil { | ||
return nil, fmt.Errorf("%s", err) | ||
} | ||
if i.String() != n.IP.String() { | ||
return nil, fmt.Errorf("%s is not the start of the cidr block %s", ip, n) | ||
} | ||
|
||
return n, nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package firewall | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateFirewallIP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ip string | ||
err error | ||
}{ | ||
{ | ||
name: "Valid CIDR (IPv4)", | ||
ip: "10.0.0.0/8", | ||
}, | ||
{ | ||
name: "Valid CIDR (IPv6)", | ||
ip: "fe80::/128", | ||
}, | ||
{ | ||
name: "Invalid IP", | ||
ip: "test", | ||
err: fmt.Errorf("invalid CIDR address: test"), | ||
}, | ||
{ | ||
name: "Missing CIDR notation (IPv4)", | ||
ip: "10.0.0.0", | ||
err: fmt.Errorf("invalid CIDR address: 10.0.0.0"), | ||
}, | ||
{ | ||
name: "Missing CIDR notation (IPv6)", | ||
ip: "fe80::", | ||
err: fmt.Errorf("invalid CIDR address: fe80::"), | ||
}, | ||
{ | ||
name: "Host bit set (IPv4)", | ||
ip: "10.0.0.5/8", | ||
err: fmt.Errorf("10.0.0.5/8 is not the start of the cidr block 10.0.0.0/8"), | ||
}, | ||
{ | ||
name: "Host bit set (IPv6)", | ||
ip: "fe80::1337/64", | ||
err: fmt.Errorf("fe80::1337/64 is not the start of the cidr block fe80::/64"), | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
net, err := validateFirewallIP(test.ip) | ||
|
||
if test.err != nil { | ||
assert.Equal(t, err, test.err) | ||
assert.Nil(t, net) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, net) | ||
}) | ||
} | ||
} |