mirror of
https://github.com/juanfont/headscale.git
synced 2025-05-22 18:11:52 -04:00
policy/matcher: fix bug using contains instead of overlap (#2556)
* policy/matcher: slices.ContainsFunc Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * policy/matcher: slices.ContainsFunc, correct contains vs overlap Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * policy: add tests to validate fix for 2181 Fixes #2181 Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> --------- Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
93afb03f67
commit
d810597414
@ -3,6 +3,8 @@ package matcher
|
|||||||
import (
|
import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/juanfont/headscale/hscontrol/util"
|
"github.com/juanfont/headscale/hscontrol/util"
|
||||||
"go4.org/netipx"
|
"go4.org/netipx"
|
||||||
"tailscale.com/tailcfg"
|
"tailscale.com/tailcfg"
|
||||||
@ -58,41 +60,17 @@ func MatchFromStrings(sources, destinations []string) Match {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Match) SrcsContainsIPs(ips ...netip.Addr) bool {
|
func (m *Match) SrcsContainsIPs(ips ...netip.Addr) bool {
|
||||||
for _, ip := range ips {
|
return slices.ContainsFunc(ips, m.srcs.Contains)
|
||||||
if m.srcs.Contains(ip) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Match) DestsContainsIP(ips ...netip.Addr) bool {
|
func (m *Match) DestsContainsIP(ips ...netip.Addr) bool {
|
||||||
for _, ip := range ips {
|
return slices.ContainsFunc(ips, m.dests.Contains)
|
||||||
if m.dests.Contains(ip) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Match) SrcsOverlapsPrefixes(prefixes ...netip.Prefix) bool {
|
func (m *Match) SrcsOverlapsPrefixes(prefixes ...netip.Prefix) bool {
|
||||||
for _, prefix := range prefixes {
|
return slices.ContainsFunc(prefixes, m.srcs.OverlapsPrefix)
|
||||||
if m.srcs.ContainsPrefix(prefix) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Match) DestsOverlapsPrefixes(prefixes ...netip.Prefix) bool {
|
func (m *Match) DestsOverlapsPrefixes(prefixes ...netip.Prefix) bool {
|
||||||
for _, prefix := range prefixes {
|
return slices.ContainsFunc(prefixes, m.dests.OverlapsPrefix)
|
||||||
if m.dests.ContainsPrefix(prefix) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,11 @@ package policy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/juanfont/headscale/hscontrol/policy/matcher"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/juanfont/headscale/hscontrol/policy/matcher"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/juanfont/headscale/hscontrol/types"
|
"github.com/juanfont/headscale/hscontrol/types"
|
||||||
"github.com/juanfont/headscale/hscontrol/util"
|
"github.com/juanfont/headscale/hscontrol/util"
|
||||||
@ -1370,7 +1371,6 @@ func TestFilterNodesByACL(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "subnet-router-with-only-route",
|
name: "subnet-router-with-only-route",
|
||||||
args: args{
|
args: args{
|
||||||
@ -1422,6 +1422,108 @@ func TestFilterNodesByACL(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "subnet-router-with-only-route-smaller-mask-2181",
|
||||||
|
args: args{
|
||||||
|
nodes: []*types.Node{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
IPv4: ap("100.64.0.1"),
|
||||||
|
Hostname: "router",
|
||||||
|
User: types.User{Name: "router"},
|
||||||
|
Hostinfo: &tailcfg.Hostinfo{
|
||||||
|
RoutableIPs: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
ApprovedRoutes: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 2,
|
||||||
|
IPv4: ap("100.64.0.2"),
|
||||||
|
Hostname: "node",
|
||||||
|
User: types.User{Name: "node"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
rules: []tailcfg.FilterRule{
|
||||||
|
{
|
||||||
|
SrcIPs: []string{
|
||||||
|
"100.64.0.2/32",
|
||||||
|
},
|
||||||
|
DstPorts: []tailcfg.NetPortRange{
|
||||||
|
{IP: "10.99.0.2/32", Ports: tailcfg.PortRangeAny},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
node: &types.Node{
|
||||||
|
ID: 1,
|
||||||
|
IPv4: ap("100.64.0.1"),
|
||||||
|
Hostname: "router",
|
||||||
|
User: types.User{Name: "router"},
|
||||||
|
Hostinfo: &tailcfg.Hostinfo{
|
||||||
|
RoutableIPs: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
ApprovedRoutes: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []*types.Node{
|
||||||
|
{
|
||||||
|
ID: 2,
|
||||||
|
IPv4: ap("100.64.0.2"),
|
||||||
|
Hostname: "node",
|
||||||
|
User: types.User{Name: "node"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "node-to-subnet-router-with-only-route-smaller-mask-2181",
|
||||||
|
args: args{
|
||||||
|
nodes: []*types.Node{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
IPv4: ap("100.64.0.1"),
|
||||||
|
Hostname: "router",
|
||||||
|
User: types.User{Name: "router"},
|
||||||
|
Hostinfo: &tailcfg.Hostinfo{
|
||||||
|
RoutableIPs: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
ApprovedRoutes: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 2,
|
||||||
|
IPv4: ap("100.64.0.2"),
|
||||||
|
Hostname: "node",
|
||||||
|
User: types.User{Name: "node"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
rules: []tailcfg.FilterRule{
|
||||||
|
{
|
||||||
|
SrcIPs: []string{
|
||||||
|
"100.64.0.2/32",
|
||||||
|
},
|
||||||
|
DstPorts: []tailcfg.NetPortRange{
|
||||||
|
{IP: "10.99.0.2/32", Ports: tailcfg.PortRangeAny},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
node: &types.Node{
|
||||||
|
ID: 2,
|
||||||
|
IPv4: ap("100.64.0.2"),
|
||||||
|
Hostname: "node",
|
||||||
|
User: types.User{Name: "node"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []*types.Node{
|
||||||
|
{
|
||||||
|
ID: 1,
|
||||||
|
IPv4: ap("100.64.0.1"),
|
||||||
|
Hostname: "router",
|
||||||
|
User: types.User{Name: "router"},
|
||||||
|
Hostinfo: &tailcfg.Hostinfo{
|
||||||
|
RoutableIPs: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
ApprovedRoutes: []netip.Prefix{netip.MustParsePrefix("10.99.0.0/16")},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user