helpers/subnetmap: fix bug when using IPv6-mapped IPv4 addresses

Go will display them as IPv4. We want everything to be IPv6.
This commit is contained in:
Vincent Bernat
2022-07-31 22:45:19 +02:00
parent c0d9d301b3
commit f1585f3e6c
2 changed files with 12 additions and 0 deletions

View File

@@ -70,6 +70,8 @@ func SubnetMapUnmarshallerHook[V any]() mapstructure.DecodeHookFunc {
} }
if bits == 32 { if bits == 32 {
key = fmt.Sprintf("::ffff:%s/%d", ipNet.IP.String(), ones+96) key = fmt.Sprintf("::ffff:%s/%d", ipNet.IP.String(), ones+96)
} else if ipNet.IP.To4() != nil {
key = fmt.Sprintf("::ffff:%s/%d", ipNet.IP.String(), ones)
} else { } else {
key = ipNet.String() key = ipNet.String()
} }

View File

@@ -62,6 +62,16 @@ func TestSubnetMapUnmarshalHook(t *testing.T) {
"2001:db8:1::2": "customer2", "2001:db8:1::2": "customer2",
"2001:db8:2::2": "", "2001:db8:2::2": "",
}, },
}, {
Description: "IPv6-mapped-IPv4 subnet",
Input: gin.H{"::ffff:203.0.113.0/120": "customer2"},
Tests: map[string]string{
"::ffff:203.0.113.10": "customer2",
"203.0.113.10": "customer2",
"::ffff:203.0.112.10": "",
"203.0.112.10": "",
},
YAML: gin.H{"203.0.113.0/24": "customer2"},
}, { }, {
Description: "IPv6 IP", Description: "IPv6 IP",
Input: gin.H{"2001:db8:1::1": "customer2"}, Input: gin.H{"2001:db8:1::1": "customer2"},