hotfix(firewall): fix ip prefix parsing for ipv6 (again)
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"regexp"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -153,11 +152,15 @@ func parseInstructionFlag(key, value string, instruction *iptablesInstruction) (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var regexCidrSuffix = regexp.MustCompile(`/[0-9][0-9]{0,2}$`)
|
|
||||||
|
|
||||||
func parseIPPrefix(value string) (prefix netip.Prefix, err error) {
|
func parseIPPrefix(value string) (prefix netip.Prefix, err error) {
|
||||||
if !regexCidrSuffix.MatchString(value) {
|
slashIndex := strings.Index(value, "/")
|
||||||
value += "/32"
|
if slashIndex >= 0 {
|
||||||
|
return netip.ParsePrefix(value)
|
||||||
}
|
}
|
||||||
return netip.ParsePrefix(value)
|
|
||||||
|
ip, err := netip.ParseAddr(value)
|
||||||
|
if err != nil {
|
||||||
|
return netip.Prefix{}, fmt.Errorf("parsing IP address: %w", err)
|
||||||
|
}
|
||||||
|
return netip.PrefixFrom(ip, ip.BitLen()), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,3 +82,57 @@ func Test_parseIptablesInstruction(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_parseIPPrefix(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
testCases := map[string]struct {
|
||||||
|
value string
|
||||||
|
prefix netip.Prefix
|
||||||
|
errMessage string
|
||||||
|
}{
|
||||||
|
"empty": {
|
||||||
|
errMessage: `parsing IP address: ParseAddr(""): unable to parse IP`,
|
||||||
|
},
|
||||||
|
"invalid": {
|
||||||
|
value: "invalid",
|
||||||
|
errMessage: `parsing IP address: ParseAddr("invalid"): unable to parse IP`,
|
||||||
|
},
|
||||||
|
"valid_ipv4_with_bits": {
|
||||||
|
value: "10.0.0.0/16",
|
||||||
|
prefix: netip.PrefixFrom(netip.AddrFrom4([4]byte{10, 0, 0, 0}), 16),
|
||||||
|
},
|
||||||
|
"valid_ipv4_without_bits": {
|
||||||
|
value: "10.0.0.4",
|
||||||
|
prefix: netip.PrefixFrom(netip.AddrFrom4([4]byte{10, 0, 0, 4}), 32),
|
||||||
|
},
|
||||||
|
"valid_ipv6_with_bits": {
|
||||||
|
value: "2001:db8::/32",
|
||||||
|
prefix: netip.PrefixFrom(
|
||||||
|
netip.AddrFrom16([16]byte{0x20, 0x01, 0x0d, 0xb8}),
|
||||||
|
32),
|
||||||
|
},
|
||||||
|
"valid_ipv6_without_bits": {
|
||||||
|
value: "2001:db8::",
|
||||||
|
prefix: netip.PrefixFrom(
|
||||||
|
netip.AddrFrom16([16]byte{0x20, 0x01, 0x0d, 0xb8}),
|
||||||
|
128),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, testCase := range testCases {
|
||||||
|
testCase := testCase
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
prefix, err := parseIPPrefix(testCase.value)
|
||||||
|
|
||||||
|
assert.Equal(t, testCase.prefix, prefix)
|
||||||
|
if testCase.errMessage != "" {
|
||||||
|
assert.EqualError(t, err, testCase.errMessage)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user