| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"log/slog" |
| 7 |
"math" |
| 8 |
"net/netip" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/gaissmai/bart" |
| 13 |
"github.com/slackhq/nebula/cert" |
| 14 |
"github.com/slackhq/nebula/config" |
| 15 |
"github.com/slackhq/nebula/firewall" |
| 16 |
"github.com/slackhq/nebula/test" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
) |
| 20 |
|
| 21 |
func TestNewFirewall(t *testing.T) { |
| 22 |
l := test.NewLogger() |
| 23 |
c := &dummyCert{} |
| 24 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 25 |
conntrack := fw.Conntrack |
| 26 |
assert.NotNil(t, conntrack) |
| 27 |
assert.NotNil(t, conntrack.Conns) |
| 28 |
assert.NotNil(t, conntrack.TimerWheel) |
| 29 |
assert.NotNil(t, fw.InRules) |
| 30 |
assert.NotNil(t, fw.OutRules) |
| 31 |
assert.Equal(t, time.Second, fw.TCPTimeout) |
| 32 |
assert.Equal(t, time.Minute, fw.UDPTimeout) |
| 33 |
assert.Equal(t, time.Hour, fw.DefaultTimeout) |
| 34 |
|
| 35 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 36 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 37 |
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen) |
| 38 |
|
| 39 |
fw = NewFirewall(l, time.Second, time.Hour, time.Minute, c) |
| 40 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 41 |
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen) |
| 42 |
|
| 43 |
fw = NewFirewall(l, time.Hour, time.Second, time.Minute, c) |
| 44 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 45 |
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen) |
| 46 |
|
| 47 |
fw = NewFirewall(l, time.Hour, time.Minute, time.Second, c) |
| 48 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 49 |
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen) |
| 50 |
|
| 51 |
fw = NewFirewall(l, time.Minute, time.Hour, time.Second, c) |
| 52 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 53 |
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen) |
| 54 |
|
| 55 |
fw = NewFirewall(l, time.Minute, time.Second, time.Hour, c) |
| 56 |
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration) |
| 57 |
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen) |
| 58 |
} |
| 59 |
|
| 60 |
func TestFirewall_AddRule(t *testing.T) { |
| 61 |
ob := &bytes.Buffer{} |
| 62 |
l := test.NewLoggerWithOutput(ob) |
| 63 |
|
| 64 |
c := &dummyCert{} |
| 65 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 66 |
assert.NotNil(t, fw.InRules) |
| 67 |
assert.NotNil(t, fw.OutRules) |
| 68 |
|
| 69 |
ti, err := netip.ParsePrefix("1.2.3.4/32") |
| 70 |
require.NoError(t, err) |
| 71 |
|
| 72 |
ti6, err := netip.ParsePrefix("fd12::34/128") |
| 73 |
require.NoError(t, err) |
| 74 |
|
| 75 |
require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", "", "", "", "")) |
| 76 |
// An empty rule is any |
| 77 |
assert.True(t, fw.InRules.TCP[1].Any.Any.Any) |
| 78 |
assert.Empty(t, fw.InRules.TCP[1].Any.Groups) |
| 79 |
assert.Empty(t, fw.InRules.TCP[1].Any.Hosts) |
| 80 |
|
| 81 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 82 |
require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", "")) |
| 83 |
assert.Nil(t, fw.InRules.UDP[1].Any.Any) |
| 84 |
assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0].Groups, "g1") |
| 85 |
assert.Empty(t, fw.InRules.UDP[1].Any.Hosts) |
| 86 |
|
| 87 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 88 |
require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", "", "", "", "")) |
| 89 |
//no matter what port is given for icmp, it should end up as "any" |
| 90 |
assert.Nil(t, fw.InRules.ICMP[firewall.PortAny].Any.Any) |
| 91 |
assert.Empty(t, fw.InRules.ICMP[firewall.PortAny].Any.Groups) |
| 92 |
assert.Contains(t, fw.InRules.ICMP[firewall.PortAny].Any.Hosts, "h1") |
| 93 |
|
| 94 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 95 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti.String(), "", "", "")) |
| 96 |
assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any) |
| 97 |
_, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti) |
| 98 |
assert.True(t, ok) |
| 99 |
|
| 100 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 101 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti6.String(), "", "", "")) |
| 102 |
assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any) |
| 103 |
_, ok = fw.OutRules.AnyProto[1].Any.CIDR.Get(ti6) |
| 104 |
assert.True(t, ok) |
| 105 |
|
| 106 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 107 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti.String(), "", "")) |
| 108 |
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any) |
| 109 |
ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti) |
| 110 |
assert.True(t, ok) |
| 111 |
|
| 112 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 113 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti6.String(), "", "")) |
| 114 |
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any) |
| 115 |
ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti6) |
| 116 |
assert.True(t, ok) |
| 117 |
|
| 118 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 119 |
require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", "")) |
| 120 |
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name") |
| 121 |
|
| 122 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 123 |
require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha")) |
| 124 |
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha") |
| 125 |
|
| 126 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 127 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", "", "", "", "")) |
| 128 |
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any) |
| 129 |
|
| 130 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 131 |
anyIp, err := netip.ParsePrefix("0.0.0.0/0") |
| 132 |
require.NoError(t, err) |
| 133 |
|
| 134 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp.String(), "", "", "")) |
| 135 |
assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any) |
| 136 |
table, ok := fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1")) |
| 137 |
assert.True(t, table.Any) |
| 138 |
table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9")) |
| 139 |
assert.False(t, ok) |
| 140 |
|
| 141 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 142 |
anyIp6, err := netip.ParsePrefix("::/0") |
| 143 |
require.NoError(t, err) |
| 144 |
|
| 145 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp6.String(), "", "", "")) |
| 146 |
assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any) |
| 147 |
table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9")) |
| 148 |
assert.True(t, table.Any) |
| 149 |
table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1")) |
| 150 |
assert.False(t, ok) |
| 151 |
|
| 152 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 153 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "any", "", "", "")) |
| 154 |
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any) |
| 155 |
|
| 156 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 157 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp.String(), "", "")) |
| 158 |
assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any) |
| 159 |
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1"))) |
| 160 |
assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9"))) |
| 161 |
|
| 162 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 163 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp6.String(), "", "")) |
| 164 |
assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any) |
| 165 |
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9"))) |
| 166 |
assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1"))) |
| 167 |
|
| 168 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 169 |
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", "any", "", "")) |
| 170 |
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any) |
| 171 |
|
| 172 |
// Test error conditions |
| 173 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c) |
| 174 |
require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", "", "", "", "")) |
| 175 |
require.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", "", "", "", "")) |
| 176 |
} |
| 177 |
|
| 178 |
func TestFirewall_Drop(t *testing.T) { |
| 179 |
ob := &bytes.Buffer{} |
| 180 |
l := test.NewLoggerWithOutput(ob) |
| 181 |
myVpnNetworksTable := new(bart.Lite) |
| 182 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8")) |
| 183 |
p := firewall.Packet{ |
| 184 |
LocalAddr: netip.MustParseAddr("1.2.3.4"), |
| 185 |
RemoteAddr: netip.MustParseAddr("1.2.3.4"), |
| 186 |
LocalPort: 10, |
| 187 |
RemotePort: 90, |
| 188 |
Protocol: firewall.ProtoUDP, |
| 189 |
Fragment: false, |
| 190 |
} |
| 191 |
|
| 192 |
c := dummyCert{ |
| 193 |
name: "host1", |
| 194 |
networks: []netip.Prefix{netip.MustParsePrefix("1.2.3.4/24")}, |
| 195 |
groups: []string{"default-group"}, |
| 196 |
issuer: "signer-shasum", |
| 197 |
} |
| 198 |
h := HostInfo{ |
| 199 |
ConnectionState: &ConnectionState{ |
| 200 |
peerCert: &cert.CachedCertificate{ |
| 201 |
Certificate: &c, |
| 202 |
InvertedGroups: map[string]struct{}{"default-group": {}}, |
| 203 |
}, |
| 204 |
}, |
| 205 |
vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")}, |
| 206 |
} |
| 207 |
h.buildNetworks(myVpnNetworksTable, &c) |
| 208 |
|
| 209 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 210 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 211 |
cp := cert.NewCAPool() |
| 212 |
|
| 213 |
// Drop outbound |
| 214 |
assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil)) |
| 215 |
// Allow inbound |
| 216 |
resetConntrack(fw) |
| 217 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 218 |
// Allow outbound because conntrack |
| 219 |
require.NoError(t, fw.Drop(p, false, &h, cp, nil)) |
| 220 |
|
| 221 |
// test remote mismatch |
| 222 |
oldRemote := p.RemoteAddr |
| 223 |
p.RemoteAddr = netip.MustParseAddr("1.2.3.10") |
| 224 |
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP) |
| 225 |
p.RemoteAddr = oldRemote |
| 226 |
|
| 227 |
// ensure signer doesn't get in the way of group checks |
| 228 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 229 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum")) |
| 230 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad")) |
| 231 |
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule) |
| 232 |
|
| 233 |
// test caSha doesn't drop on match |
| 234 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 235 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad")) |
| 236 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum")) |
| 237 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 238 |
|
| 239 |
// ensure ca name doesn't get in the way of group checks |
| 240 |
cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}} |
| 241 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 242 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", "")) |
| 243 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", "")) |
| 244 |
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule) |
| 245 |
|
| 246 |
// test caName doesn't drop on match |
| 247 |
cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}} |
| 248 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 249 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", "")) |
| 250 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", "")) |
| 251 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 252 |
} |
| 253 |
|
| 254 |
func TestFirewall_DropV6(t *testing.T) { |
| 255 |
ob := &bytes.Buffer{} |
| 256 |
l := test.NewLoggerWithOutput(ob) |
| 257 |
|
| 258 |
myVpnNetworksTable := new(bart.Lite) |
| 259 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7")) |
| 260 |
|
| 261 |
p := firewall.Packet{ |
| 262 |
LocalAddr: netip.MustParseAddr("fd12::34"), |
| 263 |
RemoteAddr: netip.MustParseAddr("fd12::34"), |
| 264 |
LocalPort: 10, |
| 265 |
RemotePort: 90, |
| 266 |
Protocol: firewall.ProtoUDP, |
| 267 |
Fragment: false, |
| 268 |
} |
| 269 |
|
| 270 |
c := dummyCert{ |
| 271 |
name: "host1", |
| 272 |
networks: []netip.Prefix{netip.MustParsePrefix("fd12::34/120")}, |
| 273 |
groups: []string{"default-group"}, |
| 274 |
issuer: "signer-shasum", |
| 275 |
} |
| 276 |
h := HostInfo{ |
| 277 |
ConnectionState: &ConnectionState{ |
| 278 |
peerCert: &cert.CachedCertificate{ |
| 279 |
Certificate: &c, |
| 280 |
InvertedGroups: map[string]struct{}{"default-group": {}}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
vpnAddrs: []netip.Addr{netip.MustParseAddr("fd12::34")}, |
| 284 |
} |
| 285 |
h.buildNetworks(myVpnNetworksTable, &c) |
| 286 |
|
| 287 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 288 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 289 |
cp := cert.NewCAPool() |
| 290 |
|
| 291 |
// Drop outbound |
| 292 |
assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil)) |
| 293 |
// Allow inbound |
| 294 |
resetConntrack(fw) |
| 295 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 296 |
// Allow outbound because conntrack |
| 297 |
require.NoError(t, fw.Drop(p, false, &h, cp, nil)) |
| 298 |
|
| 299 |
// test remote mismatch |
| 300 |
oldRemote := p.RemoteAddr |
| 301 |
p.RemoteAddr = netip.MustParseAddr("fd12::56") |
| 302 |
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP) |
| 303 |
p.RemoteAddr = oldRemote |
| 304 |
|
| 305 |
// ensure signer doesn't get in the way of group checks |
| 306 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 307 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum")) |
| 308 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad")) |
| 309 |
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule) |
| 310 |
|
| 311 |
// test caSha doesn't drop on match |
| 312 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 313 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad")) |
| 314 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum")) |
| 315 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 316 |
|
| 317 |
// ensure ca name doesn't get in the way of group checks |
| 318 |
cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}} |
| 319 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 320 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", "")) |
| 321 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", "")) |
| 322 |
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule) |
| 323 |
|
| 324 |
// test caName doesn't drop on match |
| 325 |
cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}} |
| 326 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 327 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", "")) |
| 328 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", "")) |
| 329 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 330 |
} |
| 331 |
|
| 332 |
func BenchmarkFirewallTable_match(b *testing.B) { |
| 333 |
f := &Firewall{} |
| 334 |
ft := FirewallTable{ |
| 335 |
TCP: firewallPort{}, |
| 336 |
} |
| 337 |
|
| 338 |
pfix := netip.MustParsePrefix("172.1.1.1/32") |
| 339 |
_ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix.String(), "", "", "") |
| 340 |
_ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix.String(), "", "") |
| 341 |
|
| 342 |
pfix6 := netip.MustParsePrefix("fd11::11/128") |
| 343 |
_ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix6.String(), "", "", "") |
| 344 |
_ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix6.String(), "", "") |
| 345 |
cp := cert.NewCAPool() |
| 346 |
|
| 347 |
b.Run("fail on proto", func(b *testing.B) { |
| 348 |
// This benchmark is showing us the cost of failing to match the protocol |
| 349 |
c := &cert.CachedCertificate{ |
| 350 |
Certificate: &dummyCert{}, |
| 351 |
} |
| 352 |
for n := 0; n < b.N; n++ { |
| 353 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoUDP}, true, c, cp)) |
| 354 |
} |
| 355 |
}) |
| 356 |
|
| 357 |
b.Run("pass proto, fail on port", func(b *testing.B) { |
| 358 |
// This benchmark is showing us the cost of matching a specific protocol but failing to match the port |
| 359 |
c := &cert.CachedCertificate{ |
| 360 |
Certificate: &dummyCert{}, |
| 361 |
} |
| 362 |
for n := 0; n < b.N; n++ { |
| 363 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 1}, true, c, cp)) |
| 364 |
} |
| 365 |
}) |
| 366 |
|
| 367 |
b.Run("pass proto, port, fail on local CIDR", func(b *testing.B) { |
| 368 |
c := &cert.CachedCertificate{ |
| 369 |
Certificate: &dummyCert{}, |
| 370 |
} |
| 371 |
ip := netip.MustParsePrefix("9.254.254.254/32") |
| 372 |
for n := 0; n < b.N; n++ { |
| 373 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp)) |
| 374 |
} |
| 375 |
}) |
| 376 |
b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) { |
| 377 |
c := &cert.CachedCertificate{ |
| 378 |
Certificate: &dummyCert{}, |
| 379 |
} |
| 380 |
ip := netip.MustParsePrefix("fd99::99/128") |
| 381 |
for n := 0; n < b.N; n++ { |
| 382 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp)) |
| 383 |
} |
| 384 |
}) |
| 385 |
|
| 386 |
b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) { |
| 387 |
c := &cert.CachedCertificate{ |
| 388 |
Certificate: &dummyCert{ |
| 389 |
name: "nope", |
| 390 |
networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")}, |
| 391 |
}, |
| 392 |
InvertedGroups: map[string]struct{}{"nope": {}}, |
| 393 |
} |
| 394 |
for n := 0; n < b.N; n++ { |
| 395 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)) |
| 396 |
} |
| 397 |
}) |
| 398 |
b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) { |
| 399 |
c := &cert.CachedCertificate{ |
| 400 |
Certificate: &dummyCert{ |
| 401 |
name: "nope", |
| 402 |
networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")}, |
| 403 |
}, |
| 404 |
InvertedGroups: map[string]struct{}{"nope": {}}, |
| 405 |
} |
| 406 |
for n := 0; n < b.N; n++ { |
| 407 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)) |
| 408 |
} |
| 409 |
}) |
| 410 |
|
| 411 |
b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) { |
| 412 |
c := &cert.CachedCertificate{ |
| 413 |
Certificate: &dummyCert{ |
| 414 |
name: "nope", |
| 415 |
networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")}, |
| 416 |
}, |
| 417 |
InvertedGroups: map[string]struct{}{"nope": {}}, |
| 418 |
} |
| 419 |
for n := 0; n < b.N; n++ { |
| 420 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp)) |
| 421 |
} |
| 422 |
}) |
| 423 |
b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) { |
| 424 |
c := &cert.CachedCertificate{ |
| 425 |
Certificate: &dummyCert{ |
| 426 |
name: "nope", |
| 427 |
networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")}, |
| 428 |
}, |
| 429 |
InvertedGroups: map[string]struct{}{"nope": {}}, |
| 430 |
} |
| 431 |
for n := 0; n < b.N; n++ { |
| 432 |
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp)) |
| 433 |
} |
| 434 |
}) |
| 435 |
|
| 436 |
b.Run("pass on group on any local cidr", func(b *testing.B) { |
| 437 |
c := &cert.CachedCertificate{ |
| 438 |
Certificate: &dummyCert{ |
| 439 |
name: "nope", |
| 440 |
}, |
| 441 |
InvertedGroups: map[string]struct{}{"good-group": {}}, |
| 442 |
} |
| 443 |
for n := 0; n < b.N; n++ { |
| 444 |
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)) |
| 445 |
} |
| 446 |
}) |
| 447 |
|
| 448 |
b.Run("pass on group on specific local cidr", func(b *testing.B) { |
| 449 |
c := &cert.CachedCertificate{ |
| 450 |
Certificate: &dummyCert{ |
| 451 |
name: "nope", |
| 452 |
}, |
| 453 |
InvertedGroups: map[string]struct{}{"good-group": {}}, |
| 454 |
} |
| 455 |
for n := 0; n < b.N; n++ { |
| 456 |
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp)) |
| 457 |
} |
| 458 |
}) |
| 459 |
b.Run("pass on group on specific local cidr6", func(b *testing.B) { |
| 460 |
c := &cert.CachedCertificate{ |
| 461 |
Certificate: &dummyCert{ |
| 462 |
name: "nope", |
| 463 |
}, |
| 464 |
InvertedGroups: map[string]struct{}{"good-group": {}}, |
| 465 |
} |
| 466 |
for n := 0; n < b.N; n++ { |
| 467 |
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp)) |
| 468 |
} |
| 469 |
}) |
| 470 |
|
| 471 |
b.Run("pass on name", func(b *testing.B) { |
| 472 |
c := &cert.CachedCertificate{ |
| 473 |
Certificate: &dummyCert{ |
| 474 |
name: "good-host", |
| 475 |
}, |
| 476 |
InvertedGroups: map[string]struct{}{"nope": {}}, |
| 477 |
} |
| 478 |
for n := 0; n < b.N; n++ { |
| 479 |
ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp) |
| 480 |
} |
| 481 |
}) |
| 482 |
} |
| 483 |
|
| 484 |
func TestFirewall_Drop2(t *testing.T) { |
| 485 |
ob := &bytes.Buffer{} |
| 486 |
l := test.NewLoggerWithOutput(ob) |
| 487 |
myVpnNetworksTable := new(bart.Lite) |
| 488 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8")) |
| 489 |
|
| 490 |
p := firewall.Packet{ |
| 491 |
LocalAddr: netip.MustParseAddr("1.2.3.4"), |
| 492 |
RemoteAddr: netip.MustParseAddr("1.2.3.4"), |
| 493 |
LocalPort: 10, |
| 494 |
RemotePort: 90, |
| 495 |
Protocol: firewall.ProtoUDP, |
| 496 |
Fragment: false, |
| 497 |
} |
| 498 |
|
| 499 |
network := netip.MustParsePrefix("1.2.3.4/24") |
| 500 |
|
| 501 |
c := cert.CachedCertificate{ |
| 502 |
Certificate: &dummyCert{ |
| 503 |
name: "host1", |
| 504 |
networks: []netip.Prefix{network}, |
| 505 |
}, |
| 506 |
InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}}, |
| 507 |
} |
| 508 |
h := HostInfo{ |
| 509 |
ConnectionState: &ConnectionState{ |
| 510 |
peerCert: &c, |
| 511 |
}, |
| 512 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 513 |
} |
| 514 |
h.buildNetworks(myVpnNetworksTable, c.Certificate) |
| 515 |
|
| 516 |
c1 := cert.CachedCertificate{ |
| 517 |
Certificate: &dummyCert{ |
| 518 |
name: "host1", |
| 519 |
networks: []netip.Prefix{network}, |
| 520 |
}, |
| 521 |
InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}}, |
| 522 |
} |
| 523 |
h1 := HostInfo{ |
| 524 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 525 |
ConnectionState: &ConnectionState{ |
| 526 |
peerCert: &c1, |
| 527 |
}, |
| 528 |
} |
| 529 |
h1.buildNetworks(myVpnNetworksTable, c1.Certificate) |
| 530 |
|
| 531 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 532 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", "", "", "", "")) |
| 533 |
cp := cert.NewCAPool() |
| 534 |
|
| 535 |
// h1/c1 lacks the proper groups |
| 536 |
require.ErrorIs(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule) |
| 537 |
// c has the proper groups |
| 538 |
resetConntrack(fw) |
| 539 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 540 |
} |
| 541 |
|
| 542 |
func TestFirewall_Drop3(t *testing.T) { |
| 543 |
ob := &bytes.Buffer{} |
| 544 |
l := test.NewLoggerWithOutput(ob) |
| 545 |
myVpnNetworksTable := new(bart.Lite) |
| 546 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8")) |
| 547 |
|
| 548 |
p := firewall.Packet{ |
| 549 |
LocalAddr: netip.MustParseAddr("1.2.3.4"), |
| 550 |
RemoteAddr: netip.MustParseAddr("1.2.3.4"), |
| 551 |
LocalPort: 1, |
| 552 |
RemotePort: 1, |
| 553 |
Protocol: firewall.ProtoUDP, |
| 554 |
Fragment: false, |
| 555 |
} |
| 556 |
|
| 557 |
network := netip.MustParsePrefix("1.2.3.4/24") |
| 558 |
c := cert.CachedCertificate{ |
| 559 |
Certificate: &dummyCert{ |
| 560 |
name: "host-owner", |
| 561 |
networks: []netip.Prefix{network}, |
| 562 |
}, |
| 563 |
} |
| 564 |
|
| 565 |
c1 := cert.CachedCertificate{ |
| 566 |
Certificate: &dummyCert{ |
| 567 |
name: "host1", |
| 568 |
networks: []netip.Prefix{network}, |
| 569 |
issuer: "signer-sha-bad", |
| 570 |
}, |
| 571 |
} |
| 572 |
h1 := HostInfo{ |
| 573 |
ConnectionState: &ConnectionState{ |
| 574 |
peerCert: &c1, |
| 575 |
}, |
| 576 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 577 |
} |
| 578 |
h1.buildNetworks(myVpnNetworksTable, c1.Certificate) |
| 579 |
|
| 580 |
c2 := cert.CachedCertificate{ |
| 581 |
Certificate: &dummyCert{ |
| 582 |
name: "host2", |
| 583 |
networks: []netip.Prefix{network}, |
| 584 |
issuer: "signer-sha", |
| 585 |
}, |
| 586 |
} |
| 587 |
h2 := HostInfo{ |
| 588 |
ConnectionState: &ConnectionState{ |
| 589 |
peerCert: &c2, |
| 590 |
}, |
| 591 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 592 |
} |
| 593 |
h2.buildNetworks(myVpnNetworksTable, c2.Certificate) |
| 594 |
|
| 595 |
c3 := cert.CachedCertificate{ |
| 596 |
Certificate: &dummyCert{ |
| 597 |
name: "host3", |
| 598 |
networks: []netip.Prefix{network}, |
| 599 |
issuer: "signer-sha-bad", |
| 600 |
}, |
| 601 |
} |
| 602 |
h3 := HostInfo{ |
| 603 |
ConnectionState: &ConnectionState{ |
| 604 |
peerCert: &c3, |
| 605 |
}, |
| 606 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 607 |
} |
| 608 |
h3.buildNetworks(myVpnNetworksTable, c3.Certificate) |
| 609 |
|
| 610 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 611 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", "", "", "", "")) |
| 612 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", "signer-sha")) |
| 613 |
cp := cert.NewCAPool() |
| 614 |
|
| 615 |
// c1 should pass because host match |
| 616 |
require.NoError(t, fw.Drop(p, true, &h1, cp, nil)) |
| 617 |
// c2 should pass because ca sha match |
| 618 |
resetConntrack(fw) |
| 619 |
require.NoError(t, fw.Drop(p, true, &h2, cp, nil)) |
| 620 |
// c3 should fail because no match |
| 621 |
resetConntrack(fw) |
| 622 |
assert.Equal(t, fw.Drop(p, true, &h3, cp, nil), ErrNoMatchingRule) |
| 623 |
|
| 624 |
// Test a remote address match |
| 625 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 626 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "1.2.3.4/24", "", "", "")) |
| 627 |
require.NoError(t, fw.Drop(p, true, &h1, cp, nil)) |
| 628 |
} |
| 629 |
|
| 630 |
func TestFirewall_Drop3V6(t *testing.T) { |
| 631 |
ob := &bytes.Buffer{} |
| 632 |
l := test.NewLoggerWithOutput(ob) |
| 633 |
myVpnNetworksTable := new(bart.Lite) |
| 634 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7")) |
| 635 |
|
| 636 |
p := firewall.Packet{ |
| 637 |
LocalAddr: netip.MustParseAddr("fd12::34"), |
| 638 |
RemoteAddr: netip.MustParseAddr("fd12::34"), |
| 639 |
LocalPort: 1, |
| 640 |
RemotePort: 1, |
| 641 |
Protocol: firewall.ProtoUDP, |
| 642 |
Fragment: false, |
| 643 |
} |
| 644 |
|
| 645 |
network := netip.MustParsePrefix("fd12::34/120") |
| 646 |
c := cert.CachedCertificate{ |
| 647 |
Certificate: &dummyCert{ |
| 648 |
name: "host-owner", |
| 649 |
networks: []netip.Prefix{network}, |
| 650 |
}, |
| 651 |
} |
| 652 |
h := HostInfo{ |
| 653 |
ConnectionState: &ConnectionState{ |
| 654 |
peerCert: &c, |
| 655 |
}, |
| 656 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 657 |
} |
| 658 |
h.buildNetworks(myVpnNetworksTable, c.Certificate) |
| 659 |
|
| 660 |
// Test a remote address match |
| 661 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 662 |
cp := cert.NewCAPool() |
| 663 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "fd12::34/120", "", "", "")) |
| 664 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 665 |
} |
| 666 |
|
| 667 |
func TestFirewall_DropConntrackReload(t *testing.T) { |
| 668 |
ob := &bytes.Buffer{} |
| 669 |
l := test.NewLoggerWithOutput(ob) |
| 670 |
myVpnNetworksTable := new(bart.Lite) |
| 671 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8")) |
| 672 |
|
| 673 |
p := firewall.Packet{ |
| 674 |
LocalAddr: netip.MustParseAddr("1.2.3.4"), |
| 675 |
RemoteAddr: netip.MustParseAddr("1.2.3.4"), |
| 676 |
LocalPort: 10, |
| 677 |
RemotePort: 90, |
| 678 |
Protocol: firewall.ProtoUDP, |
| 679 |
Fragment: false, |
| 680 |
} |
| 681 |
network := netip.MustParsePrefix("1.2.3.4/24") |
| 682 |
|
| 683 |
c := cert.CachedCertificate{ |
| 684 |
Certificate: &dummyCert{ |
| 685 |
name: "host1", |
| 686 |
networks: []netip.Prefix{network}, |
| 687 |
groups: []string{"default-group"}, |
| 688 |
issuer: "signer-shasum", |
| 689 |
}, |
| 690 |
InvertedGroups: map[string]struct{}{"default-group": {}}, |
| 691 |
} |
| 692 |
h := HostInfo{ |
| 693 |
ConnectionState: &ConnectionState{ |
| 694 |
peerCert: &c, |
| 695 |
}, |
| 696 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 697 |
} |
| 698 |
h.buildNetworks(myVpnNetworksTable, c.Certificate) |
| 699 |
|
| 700 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 701 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 702 |
cp := cert.NewCAPool() |
| 703 |
|
| 704 |
// Drop outbound |
| 705 |
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule) |
| 706 |
// Allow inbound |
| 707 |
resetConntrack(fw) |
| 708 |
require.NoError(t, fw.Drop(p, true, &h, cp, nil)) |
| 709 |
// Allow outbound because conntrack |
| 710 |
require.NoError(t, fw.Drop(p, false, &h, cp, nil)) |
| 711 |
|
| 712 |
oldFw := fw |
| 713 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 714 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", "", "", "", "")) |
| 715 |
fw.Conntrack = oldFw.Conntrack |
| 716 |
fw.rulesVersion = oldFw.rulesVersion + 1 |
| 717 |
|
| 718 |
// Allow outbound because conntrack and new rules allow port 10 |
| 719 |
require.NoError(t, fw.Drop(p, false, &h, cp, nil)) |
| 720 |
|
| 721 |
oldFw = fw |
| 722 |
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 723 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", "", "", "", "")) |
| 724 |
fw.Conntrack = oldFw.Conntrack |
| 725 |
fw.rulesVersion = oldFw.rulesVersion + 1 |
| 726 |
|
| 727 |
// Drop outbound because conntrack doesn't match new ruleset |
| 728 |
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule) |
| 729 |
} |
| 730 |
|
| 731 |
func TestFirewall_ICMPPortBehavior(t *testing.T) { |
| 732 |
ob := &bytes.Buffer{} |
| 733 |
l := test.NewLoggerWithOutput(ob) |
| 734 |
myVpnNetworksTable := new(bart.Lite) |
| 735 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8")) |
| 736 |
|
| 737 |
network := netip.MustParsePrefix("1.2.3.4/24") |
| 738 |
|
| 739 |
c := cert.CachedCertificate{ |
| 740 |
Certificate: &dummyCert{ |
| 741 |
name: "host1", |
| 742 |
networks: []netip.Prefix{network}, |
| 743 |
groups: []string{"default-group"}, |
| 744 |
issuer: "signer-shasum", |
| 745 |
}, |
| 746 |
InvertedGroups: map[string]struct{}{"default-group": {}}, |
| 747 |
} |
| 748 |
h := HostInfo{ |
| 749 |
ConnectionState: &ConnectionState{ |
| 750 |
peerCert: &c, |
| 751 |
}, |
| 752 |
vpnAddrs: []netip.Addr{network.Addr()}, |
| 753 |
} |
| 754 |
h.buildNetworks(myVpnNetworksTable, c.Certificate) |
| 755 |
|
| 756 |
cp := cert.NewCAPool() |
| 757 |
|
| 758 |
templ := firewall.Packet{ |
| 759 |
LocalAddr: netip.MustParseAddr("1.2.3.4"), |
| 760 |
RemoteAddr: netip.MustParseAddr("1.2.3.4"), |
| 761 |
Protocol: firewall.ProtoICMP, |
| 762 |
Fragment: false, |
| 763 |
} |
| 764 |
|
| 765 |
t.Run("ICMP allowed", func(t *testing.T) { |
| 766 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 767 |
require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 768 |
t.Run("zero ports", func(t *testing.T) { |
| 769 |
p := templ.Copy() |
| 770 |
p.LocalPort = 0 |
| 771 |
p.RemotePort = 0 |
| 772 |
// Drop outbound |
| 773 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 774 |
// Allow inbound |
| 775 |
resetConntrack(fw) |
| 776 |
require.NoError(t, fw.Drop(*p, true, &h, cp, nil)) |
| 777 |
//now also allow outbound |
| 778 |
require.NoError(t, fw.Drop(*p, false, &h, cp, nil)) |
| 779 |
}) |
| 780 |
|
| 781 |
t.Run("nonzero ports", func(t *testing.T) { |
| 782 |
p := templ.Copy() |
| 783 |
p.LocalPort = 0xabcd |
| 784 |
p.RemotePort = 0x1234 |
| 785 |
// Drop outbound |
| 786 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 787 |
// Allow inbound |
| 788 |
resetConntrack(fw) |
| 789 |
require.NoError(t, fw.Drop(*p, true, &h, cp, nil)) |
| 790 |
//now also allow outbound |
| 791 |
require.NoError(t, fw.Drop(*p, false, &h, cp, nil)) |
| 792 |
}) |
| 793 |
}) |
| 794 |
|
| 795 |
t.Run("Any proto, some ports allowed", func(t *testing.T) { |
| 796 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 797 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 80, 444, []string{"any"}, "", "", "", "", "")) |
| 798 |
t.Run("zero ports, still blocked", func(t *testing.T) { |
| 799 |
p := templ.Copy() |
| 800 |
p.LocalPort = 0 |
| 801 |
p.RemotePort = 0 |
| 802 |
// Drop outbound |
| 803 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 804 |
// Allow inbound |
| 805 |
resetConntrack(fw) |
| 806 |
assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule) |
| 807 |
//now also allow outbound |
| 808 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 809 |
}) |
| 810 |
|
| 811 |
t.Run("nonzero ports, still blocked", func(t *testing.T) { |
| 812 |
p := templ.Copy() |
| 813 |
p.LocalPort = 0xabcd |
| 814 |
p.RemotePort = 0x1234 |
| 815 |
// Drop outbound |
| 816 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 817 |
// Allow inbound |
| 818 |
resetConntrack(fw) |
| 819 |
assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule) |
| 820 |
//now also allow outbound |
| 821 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 822 |
}) |
| 823 |
|
| 824 |
t.Run("nonzero, matching ports, still blocked", func(t *testing.T) { |
| 825 |
p := templ.Copy() |
| 826 |
p.LocalPort = 80 |
| 827 |
p.RemotePort = 80 |
| 828 |
// Drop outbound |
| 829 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 830 |
// Allow inbound |
| 831 |
resetConntrack(fw) |
| 832 |
assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule) |
| 833 |
//now also allow outbound |
| 834 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 835 |
}) |
| 836 |
}) |
| 837 |
t.Run("Any proto, any port", func(t *testing.T) { |
| 838 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 839 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 840 |
t.Run("zero ports, allowed", func(t *testing.T) { |
| 841 |
resetConntrack(fw) |
| 842 |
p := templ.Copy() |
| 843 |
p.LocalPort = 0 |
| 844 |
p.RemotePort = 0 |
| 845 |
// Drop outbound |
| 846 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 847 |
// Allow inbound |
| 848 |
resetConntrack(fw) |
| 849 |
require.NoError(t, fw.Drop(*p, true, &h, cp, nil)) |
| 850 |
//now also allow outbound |
| 851 |
require.NoError(t, fw.Drop(*p, false, &h, cp, nil)) |
| 852 |
}) |
| 853 |
|
| 854 |
t.Run("nonzero ports, allowed", func(t *testing.T) { |
| 855 |
resetConntrack(fw) |
| 856 |
p := templ.Copy() |
| 857 |
p.LocalPort = 0xabcd |
| 858 |
p.RemotePort = 0x1234 |
| 859 |
// Drop outbound |
| 860 |
assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 861 |
// Allow inbound |
| 862 |
resetConntrack(fw) |
| 863 |
require.NoError(t, fw.Drop(*p, true, &h, cp, nil)) |
| 864 |
//now also allow outbound |
| 865 |
require.NoError(t, fw.Drop(*p, false, &h, cp, nil)) |
| 866 |
//different ID is blocked |
| 867 |
p.RemotePort++ |
| 868 |
require.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule) |
| 869 |
}) |
| 870 |
}) |
| 871 |
|
| 872 |
} |
| 873 |
|
| 874 |
func TestFirewall_DropIPSpoofing(t *testing.T) { |
| 875 |
ob := &bytes.Buffer{} |
| 876 |
l := test.NewLoggerWithOutput(ob) |
| 877 |
myVpnNetworksTable := new(bart.Lite) |
| 878 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24")) |
| 879 |
|
| 880 |
c := cert.CachedCertificate{ |
| 881 |
Certificate: &dummyCert{ |
| 882 |
name: "host-owner", |
| 883 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")}, |
| 884 |
}, |
| 885 |
} |
| 886 |
|
| 887 |
c1 := cert.CachedCertificate{ |
| 888 |
Certificate: &dummyCert{ |
| 889 |
name: "host", |
| 890 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")}, |
| 891 |
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")}, |
| 892 |
}, |
| 893 |
} |
| 894 |
h1 := HostInfo{ |
| 895 |
ConnectionState: &ConnectionState{ |
| 896 |
peerCert: &c1, |
| 897 |
}, |
| 898 |
vpnAddrs: []netip.Addr{c1.Certificate.Networks()[0].Addr()}, |
| 899 |
} |
| 900 |
h1.buildNetworks(myVpnNetworksTable, c1.Certificate) |
| 901 |
|
| 902 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate) |
| 903 |
|
| 904 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", "")) |
| 905 |
cp := cert.NewCAPool() |
| 906 |
|
| 907 |
// Packet spoofed by `c1`. Note that the remote addr is not a valid one. |
| 908 |
p := firewall.Packet{ |
| 909 |
LocalAddr: netip.MustParseAddr("192.0.2.1"), |
| 910 |
RemoteAddr: netip.MustParseAddr("192.0.2.3"), |
| 911 |
LocalPort: 1, |
| 912 |
RemotePort: 1, |
| 913 |
Protocol: firewall.ProtoUDP, |
| 914 |
Fragment: false, |
| 915 |
} |
| 916 |
assert.Equal(t, fw.Drop(p, true, &h1, cp, nil), ErrInvalidRemoteIP) |
| 917 |
} |
| 918 |
|
| 919 |
func TestFirewall_ConntrackSourceSpoofingAcrossPeers(t *testing.T) { |
| 920 |
l := test.NewLoggerWithOutput(&bytes.Buffer{}) |
| 921 |
|
| 922 |
myVpnNetworksTable := new(bart.Lite) |
| 923 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24")) |
| 924 |
|
| 925 |
owner := &dummyCert{ |
| 926 |
name: "owner", |
| 927 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")}, |
| 928 |
} |
| 929 |
|
| 930 |
victim := &cert.CachedCertificate{ |
| 931 |
Certificate: &dummyCert{ |
| 932 |
name: "victim", |
| 933 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")}, |
| 934 |
}, |
| 935 |
} |
| 936 |
victimHI := HostInfo{ |
| 937 |
ConnectionState: &ConnectionState{peerCert: victim}, |
| 938 |
vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")}, |
| 939 |
} |
| 940 |
victimHI.buildNetworks(myVpnNetworksTable, victim.Certificate) |
| 941 |
|
| 942 |
attacker := &cert.CachedCertificate{ |
| 943 |
Certificate: &dummyCert{ |
| 944 |
name: "attacker", |
| 945 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.3/24")}, |
| 946 |
}, |
| 947 |
} |
| 948 |
attackerHI := HostInfo{ |
| 949 |
ConnectionState: &ConnectionState{peerCert: attacker}, |
| 950 |
vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.3")}, |
| 951 |
} |
| 952 |
attackerHI.buildNetworks(myVpnNetworksTable, attacker.Certificate) |
| 953 |
|
| 954 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner) |
| 955 |
// Allow any inbound traffic that passes the cert / source-IP checks. |
| 956 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 957 |
cp := cert.NewCAPool() |
| 958 |
|
| 959 |
flow := firewall.Packet{ |
| 960 |
LocalAddr: netip.MustParseAddr("192.0.2.1"), |
| 961 |
RemoteAddr: netip.MustParseAddr("192.0.2.2"), |
| 962 |
LocalPort: 443, |
| 963 |
RemotePort: 55000, |
| 964 |
Protocol: firewall.ProtoUDP, |
| 965 |
} |
| 966 |
|
| 967 |
require.NoError(t, fw.Drop(flow, true, &victimHI, cp, nil), |
| 968 |
"victim's own traffic from its own overlay IP must be allowed") |
| 969 |
|
| 970 |
unseen := flow |
| 971 |
unseen.RemotePort = 55001 |
| 972 |
assert.Equal(t, ErrInvalidRemoteIP, fw.Drop(unseen, true, &attackerHI, cp, nil), |
| 973 |
"sanity: attacker forging victim's source IP must be rejected when no conntrack entry exists") |
| 974 |
|
| 975 |
got := fw.Drop(flow, true, &attackerHI, cp, nil) |
| 976 |
t.Logf("attacker replaying victim's 4-tuple: Drop returned %v (nil == packet ALLOWED == spoof succeeded)", got) |
| 977 |
assert.Equal(t, ErrInvalidRemoteIP, got, |
| 978 |
"SECURITY: attacker spoofed victim's overlay source IP (192.0.2.2) by reusing an existing conntrack 4-tuple; Drop returned %v instead of rejecting", got) |
| 979 |
} |
| 980 |
|
| 981 |
// BenchmarkFirewallDropConntrackHit measures Drop on an already-established flow |
| 982 |
// (a conntrack hit). This is the fast path that the source-IP<->cert binding |
| 983 |
// reordering adds work to, so it quantifies the cost of moving the address checks |
| 984 |
// ahead of the conntrack lookup. Cases: |
| 985 |
// - simple: peer cert has one address, no unsafe networks (h.networks == nil), |
| 986 |
// so the remote-address check is a single netip.Addr compare. |
| 987 |
// - complex: peer cert has unsafe networks (h.networks populated), so the |
| 988 |
// remote-address check is a BART lookup. |
| 989 |
// - noCache/localCache: whether a per-batch ConntrackCache is supplied, which in |
| 990 |
// the original code let the fast path skip straight past the address checks. |
| 991 |
func BenchmarkFirewallDropConntrackHit(b *testing.B) { |
| 992 |
l := test.NewLoggerWithOutput(&bytes.Buffer{}) |
| 993 |
|
| 994 |
myVpnNetworksTable := new(bart.Lite) |
| 995 |
myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24")) |
| 996 |
|
| 997 |
owner := &dummyCert{ |
| 998 |
name: "owner", |
| 999 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")}, |
| 1000 |
} |
| 1001 |
|
| 1002 |
simpleCert := &cert.CachedCertificate{ |
| 1003 |
Certificate: &dummyCert{ |
| 1004 |
name: "simple", |
| 1005 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")}, |
| 1006 |
}, |
| 1007 |
} |
| 1008 |
simpleHost := &HostInfo{ |
| 1009 |
ConnectionState: &ConnectionState{peerCert: simpleCert}, |
| 1010 |
vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")}, |
| 1011 |
} |
| 1012 |
simpleHost.buildNetworks(myVpnNetworksTable, simpleCert.Certificate) |
| 1013 |
|
| 1014 |
complexCert := &cert.CachedCertificate{ |
| 1015 |
Certificate: &dummyCert{ |
| 1016 |
name: "complex", |
| 1017 |
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")}, |
| 1018 |
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")}, |
| 1019 |
}, |
| 1020 |
} |
| 1021 |
complexHost := &HostInfo{ |
| 1022 |
ConnectionState: &ConnectionState{peerCert: complexCert}, |
| 1023 |
vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")}, |
| 1024 |
} |
| 1025 |
complexHost.buildNetworks(myVpnNetworksTable, complexCert.Certificate) |
| 1026 |
|
| 1027 |
cp := cert.NewCAPool() |
| 1028 |
|
| 1029 |
flow := firewall.Packet{ |
| 1030 |
LocalAddr: netip.MustParseAddr("192.0.2.1"), |
| 1031 |
RemoteAddr: netip.MustParseAddr("192.0.2.2"), |
| 1032 |
LocalPort: 443, |
| 1033 |
RemotePort: 55000, |
| 1034 |
Protocol: firewall.ProtoUDP, |
| 1035 |
} |
| 1036 |
|
| 1037 |
cases := []struct { |
| 1038 |
name string |
| 1039 |
host *HostInfo |
| 1040 |
useCache bool |
| 1041 |
}{ |
| 1042 |
{"simple/noCache", simpleHost, false}, |
| 1043 |
{"simple/localCache", simpleHost, true}, |
| 1044 |
{"complex/noCache", complexHost, false}, |
| 1045 |
{"complex/localCache", complexHost, true}, |
| 1046 |
} |
| 1047 |
|
| 1048 |
for _, tc := range cases { |
| 1049 |
b.Run(tc.name, func(b *testing.B) { |
| 1050 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner) |
| 1051 |
require.NoError(b, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 1052 |
|
| 1053 |
// Establish the conntrack entry so every benchmarked Drop is a hit. |
| 1054 |
require.NoError(b, fw.Drop(flow, true, tc.host, cp, nil)) |
| 1055 |
|
| 1056 |
var cache firewall.ConntrackCache |
| 1057 |
if tc.useCache { |
| 1058 |
cache = firewall.ConntrackCache{} |
| 1059 |
} |
| 1060 |
|
| 1061 |
b.ReportAllocs() |
| 1062 |
b.ResetTimer() |
| 1063 |
for i := 0; i < b.N; i++ { |
| 1064 |
if err := fw.Drop(flow, true, tc.host, cp, cache); err != nil { |
| 1065 |
b.Fatal(err) |
| 1066 |
} |
| 1067 |
} |
| 1068 |
}) |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
func BenchmarkLookup(b *testing.B) { |
| 1073 |
ml := func(m map[string]struct{}, a [][]string) { |
| 1074 |
for n := 0; n < b.N; n++ { |
| 1075 |
for _, sg := range a { |
| 1076 |
found := false |
| 1077 |
|
| 1078 |
for _, g := range sg { |
| 1079 |
if _, ok := m[g]; !ok { |
| 1080 |
found = false |
| 1081 |
break |
| 1082 |
} |
| 1083 |
|
| 1084 |
found = true |
| 1085 |
} |
| 1086 |
|
| 1087 |
if found { |
| 1088 |
return |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
b.Run("array to map best", func(b *testing.B) { |
| 1095 |
m := map[string]struct{}{ |
| 1096 |
"1ne": {}, |
| 1097 |
"2wo": {}, |
| 1098 |
"3hr": {}, |
| 1099 |
"4ou": {}, |
| 1100 |
"5iv": {}, |
| 1101 |
"6ix": {}, |
| 1102 |
} |
| 1103 |
|
| 1104 |
a := [][]string{ |
| 1105 |
{"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"}, |
| 1106 |
{"one", "2wo", "3hr", "4ou", "5iv", "6ix"}, |
| 1107 |
{"one", "two", "3hr", "4ou", "5iv", "6ix"}, |
| 1108 |
{"one", "two", "thr", "4ou", "5iv", "6ix"}, |
| 1109 |
{"one", "two", "thr", "fou", "5iv", "6ix"}, |
| 1110 |
{"one", "two", "thr", "fou", "fiv", "6ix"}, |
| 1111 |
{"one", "two", "thr", "fou", "fiv", "six"}, |
| 1112 |
} |
| 1113 |
|
| 1114 |
for n := 0; n < b.N; n++ { |
| 1115 |
ml(m, a) |
| 1116 |
} |
| 1117 |
}) |
| 1118 |
|
| 1119 |
b.Run("array to map worst", func(b *testing.B) { |
| 1120 |
m := map[string]struct{}{ |
| 1121 |
"one": {}, |
| 1122 |
"two": {}, |
| 1123 |
"thr": {}, |
| 1124 |
"fou": {}, |
| 1125 |
"fiv": {}, |
| 1126 |
"six": {}, |
| 1127 |
} |
| 1128 |
|
| 1129 |
a := [][]string{ |
| 1130 |
{"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"}, |
| 1131 |
{"one", "2wo", "3hr", "4ou", "5iv", "6ix"}, |
| 1132 |
{"one", "two", "3hr", "4ou", "5iv", "6ix"}, |
| 1133 |
{"one", "two", "thr", "4ou", "5iv", "6ix"}, |
| 1134 |
{"one", "two", "thr", "fou", "5iv", "6ix"}, |
| 1135 |
{"one", "two", "thr", "fou", "fiv", "6ix"}, |
| 1136 |
{"one", "two", "thr", "fou", "fiv", "six"}, |
| 1137 |
} |
| 1138 |
|
| 1139 |
for n := 0; n < b.N; n++ { |
| 1140 |
ml(m, a) |
| 1141 |
} |
| 1142 |
}) |
| 1143 |
} |
| 1144 |
|
| 1145 |
func Test_parsePort(t *testing.T) { |
| 1146 |
_, _, err := parsePort("") |
| 1147 |
require.EqualError(t, err, "was not a number; ``") |
| 1148 |
|
| 1149 |
_, _, err = parsePort(" ") |
| 1150 |
require.EqualError(t, err, "was not a number; ` `") |
| 1151 |
|
| 1152 |
_, _, err = parsePort("-") |
| 1153 |
require.EqualError(t, err, "appears to be a range but could not be parsed; `-`") |
| 1154 |
|
| 1155 |
_, _, err = parsePort(" - ") |
| 1156 |
require.EqualError(t, err, "appears to be a range but could not be parsed; ` - `") |
| 1157 |
|
| 1158 |
_, _, err = parsePort("a-b") |
| 1159 |
require.EqualError(t, err, "beginning range was not a number; `a`") |
| 1160 |
|
| 1161 |
_, _, err = parsePort("1-b") |
| 1162 |
require.EqualError(t, err, "ending range was not a number; `b`") |
| 1163 |
|
| 1164 |
s, e, err := parsePort(" 1 - 2 ") |
| 1165 |
assert.Equal(t, int32(1), s) |
| 1166 |
assert.Equal(t, int32(2), e) |
| 1167 |
require.NoError(t, err) |
| 1168 |
|
| 1169 |
s, e, err = parsePort("0-1") |
| 1170 |
assert.Equal(t, int32(0), s) |
| 1171 |
assert.Equal(t, int32(0), e) |
| 1172 |
require.NoError(t, err) |
| 1173 |
|
| 1174 |
s, e, err = parsePort("9919") |
| 1175 |
assert.Equal(t, int32(9919), s) |
| 1176 |
assert.Equal(t, int32(9919), e) |
| 1177 |
require.NoError(t, err) |
| 1178 |
|
| 1179 |
s, e, err = parsePort("any") |
| 1180 |
assert.Equal(t, int32(0), s) |
| 1181 |
assert.Equal(t, int32(0), e) |
| 1182 |
require.NoError(t, err) |
| 1183 |
} |
| 1184 |
|
| 1185 |
// Test_parsePort_invalid covers inputs that must error. The named bug is |
| 1186 |
// that int32(strconv.Atoi("4294967296")) truncates to 0 == firewall.PortAny, |
| 1187 |
// silently turning a typo into a match-all-ports rule; the rest are |
| 1188 |
// representative syntax/range probes. |
| 1189 |
func Test_parsePort_invalid(t *testing.T) { |
| 1190 |
tests := []struct { |
| 1191 |
name string |
| 1192 |
input string |
| 1193 |
wantErrContains string |
| 1194 |
}{ |
| 1195 |
// Numeric overflow (the named bug + boundary). |
| 1196 |
{"named bug: 2^32 truncates to PortAny", "4294967296", "out of range"}, |
| 1197 |
{"just above max real port", "65536", "out of range"}, |
| 1198 |
|
| 1199 |
// Negatives route through the range branch and hit the empty-half |
| 1200 |
// guard; included as defense in depth so a future refactor cannot |
| 1201 |
// accidentally reach the int32 cast. |
| 1202 |
{"negative", "-1", "could not be parsed"}, |
| 1203 |
|
| 1204 |
// Syntax probes. |
| 1205 |
{"NUL between digits", "4\x002", "was not a number"}, |
| 1206 |
{"hex notation", "0x10", "was not a number"}, |
| 1207 |
{"scientific notation", "1e3", "was not a number"}, |
| 1208 |
{"leading whitespace", " 42", "was not a number"}, |
| 1209 |
{"fullwidth digits", "42", "was not a number"}, |
| 1210 |
|
| 1211 |
// Range branch. |
| 1212 |
{"range upper out of range", "1-65536", "ending range out of range"}, |
| 1213 |
{"range lower out of range", "65536-65537", "beginning range out of range"}, |
| 1214 |
{"range with negative upper", "1--1", "ending range was not a number"}, |
| 1215 |
} |
| 1216 |
|
| 1217 |
for _, tc := range tests { |
| 1218 |
t.Run(tc.name, func(t *testing.T) { |
| 1219 |
_, _, err := parsePort(tc.input) |
| 1220 |
require.Error(t, err, "input %q must error", tc.input) |
| 1221 |
require.ErrorContains(t, err, tc.wantErrContains) |
| 1222 |
}) |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
// Test_parsePort_valid_boundaries locks in success cases at 0, 1, and 65535 |
| 1227 |
// so a future refactor cannot regress the boundaries. |
| 1228 |
func Test_parsePort_valid_boundaries(t *testing.T) { |
| 1229 |
tests := []struct { |
| 1230 |
name string |
| 1231 |
input string |
| 1232 |
wantStart int32 |
| 1233 |
wantEnd int32 |
| 1234 |
}{ |
| 1235 |
{"zero is PortAny", "0", 0, 0}, |
| 1236 |
{"min real port", "1", 1, 1}, |
| 1237 |
{"max real port", "65535", 65535, 65535}, |
| 1238 |
{"range zero to max forces end to zero", "0-65535", 0, 0}, |
| 1239 |
{"range max to max", "65535-65535", 65535, 65535}, |
| 1240 |
{"range one to max", "1-65535", 1, 65535}, |
| 1241 |
{"range with whitespace inside", " 1 - 2 ", 1, 2}, |
| 1242 |
} |
| 1243 |
|
| 1244 |
for _, tc := range tests { |
| 1245 |
t.Run(tc.name, func(t *testing.T) { |
| 1246 |
s, e, err := parsePort(tc.input) |
| 1247 |
require.NoError(t, err) |
| 1248 |
assert.Equal(t, tc.wantStart, s, "start port") |
| 1249 |
assert.Equal(t, tc.wantEnd, e, "end port") |
| 1250 |
}) |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
func TestNewFirewallFromConfig(t *testing.T) { |
| 1255 |
l := test.NewLogger() |
| 1256 |
// Test a bad rule definition |
| 1257 |
c := &dummyCert{} |
| 1258 |
cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil, "aes") |
| 1259 |
require.NoError(t, err) |
| 1260 |
|
| 1261 |
conf := config.NewC(test.NewLogger()) |
| 1262 |
conf.Settings["firewall"] = map[string]any{"outbound": "asdf"} |
| 1263 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1264 |
require.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules") |
| 1265 |
|
| 1266 |
// Test both port and code |
| 1267 |
conf = config.NewC(test.NewLogger()) |
| 1268 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "code": "2"}}} |
| 1269 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1270 |
require.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided") |
| 1271 |
|
| 1272 |
// Test missing host, group, cidr, ca_name and ca_sha |
| 1273 |
conf = config.NewC(test.NewLogger()) |
| 1274 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{}}} |
| 1275 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1276 |
require.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, local_cidr, ca_name, or ca_sha must be provided") |
| 1277 |
|
| 1278 |
// Test code/port error |
| 1279 |
conf = config.NewC(test.NewLogger()) |
| 1280 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "a", "host": "testh", "proto": "any"}}} |
| 1281 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1282 |
require.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`") |
| 1283 |
|
| 1284 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "a", "host": "testh", "proto": "any"}}} |
| 1285 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1286 |
require.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`") |
| 1287 |
|
| 1288 |
// Test proto error |
| 1289 |
conf = config.NewC(test.NewLogger()) |
| 1290 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "host": "testh"}}} |
| 1291 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1292 |
require.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``") |
| 1293 |
|
| 1294 |
// Test cidr parse error |
| 1295 |
conf = config.NewC(test.NewLogger()) |
| 1296 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "cidr": "testh", "proto": "any"}}} |
| 1297 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1298 |
require.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'") |
| 1299 |
|
| 1300 |
// Test local_cidr parse error |
| 1301 |
conf = config.NewC(test.NewLogger()) |
| 1302 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "local_cidr": "testh", "proto": "any"}}} |
| 1303 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1304 |
require.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'") |
| 1305 |
|
| 1306 |
// Test both group and groups |
| 1307 |
conf = config.NewC(test.NewLogger()) |
| 1308 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}} |
| 1309 |
_, err = NewFirewallFromConfig(l, cs, conf) |
| 1310 |
require.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided") |
| 1311 |
} |
| 1312 |
|
| 1313 |
func TestAddFirewallRulesFromConfig(t *testing.T) { |
| 1314 |
l := test.NewLogger() |
| 1315 |
// Test adding tcp rule |
| 1316 |
conf := config.NewC(test.NewLogger()) |
| 1317 |
mf := &mockFirewall{} |
| 1318 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "tcp", "host": "a"}}} |
| 1319 |
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf)) |
| 1320 |
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall) |
| 1321 |
|
| 1322 |
// Test adding udp rule |
| 1323 |
conf = config.NewC(test.NewLogger()) |
| 1324 |
mf = &mockFirewall{} |
| 1325 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "udp", "host": "a"}}} |
| 1326 |
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf)) |
| 1327 |
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall) |
| 1328 |
|
| 1329 |
// Test adding icmp rule |
| 1330 |
conf = config.NewC(test.NewLogger()) |
| 1331 |
mf = &mockFirewall{} |
| 1332 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "icmp", "host": "a"}}} |
| 1333 |
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf)) |
| 1334 |
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall) |
| 1335 |
|
| 1336 |
// Test adding icmp rule no port |
| 1337 |
conf = config.NewC(test.NewLogger()) |
| 1338 |
mf = &mockFirewall{} |
| 1339 |
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"proto": "icmp", "host": "a"}}} |
| 1340 |
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf)) |
| 1341 |
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall) |
| 1342 |
|
| 1343 |
// Test adding any rule |
| 1344 |
conf = config.NewC(test.NewLogger()) |
| 1345 |
mf = &mockFirewall{} |
| 1346 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}} |
| 1347 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1348 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall) |
| 1349 |
|
| 1350 |
// Test adding rule with cidr |
| 1351 |
cidr := netip.MustParsePrefix("10.0.0.0/8") |
| 1352 |
conf = config.NewC(test.NewLogger()) |
| 1353 |
mf = &mockFirewall{} |
| 1354 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr.String()}}} |
| 1355 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1356 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr.String(), localIp: ""}, mf.lastCall) |
| 1357 |
|
| 1358 |
// Test adding rule with local_cidr |
| 1359 |
conf = config.NewC(test.NewLogger()) |
| 1360 |
mf = &mockFirewall{} |
| 1361 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr.String()}}} |
| 1362 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1363 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr.String()}, mf.lastCall) |
| 1364 |
|
| 1365 |
// Test adding rule with cidr ipv6 |
| 1366 |
cidr6 := netip.MustParsePrefix("fd00::/8") |
| 1367 |
conf = config.NewC(test.NewLogger()) |
| 1368 |
mf = &mockFirewall{} |
| 1369 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr6.String()}}} |
| 1370 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1371 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr6.String(), localIp: ""}, mf.lastCall) |
| 1372 |
|
| 1373 |
// Test adding rule with any cidr |
| 1374 |
conf = config.NewC(test.NewLogger()) |
| 1375 |
mf = &mockFirewall{} |
| 1376 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "any"}}} |
| 1377 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1378 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "any", localIp: ""}, mf.lastCall) |
| 1379 |
|
| 1380 |
// Test adding rule with junk cidr |
| 1381 |
conf = config.NewC(test.NewLogger()) |
| 1382 |
mf = &mockFirewall{} |
| 1383 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "junk/junk"}}} |
| 1384 |
require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP") |
| 1385 |
|
| 1386 |
// Test adding rule with local_cidr ipv6 |
| 1387 |
conf = config.NewC(test.NewLogger()) |
| 1388 |
mf = &mockFirewall{} |
| 1389 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr6.String()}}} |
| 1390 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1391 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr6.String()}, mf.lastCall) |
| 1392 |
|
| 1393 |
// Test adding rule with any local_cidr |
| 1394 |
conf = config.NewC(test.NewLogger()) |
| 1395 |
mf = &mockFirewall{} |
| 1396 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "any"}}} |
| 1397 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1398 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, localIp: "any"}, mf.lastCall) |
| 1399 |
|
| 1400 |
// Test adding rule with junk local_cidr |
| 1401 |
conf = config.NewC(test.NewLogger()) |
| 1402 |
mf = &mockFirewall{} |
| 1403 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "junk/junk"}}} |
| 1404 |
require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP") |
| 1405 |
|
| 1406 |
// Test adding rule with ca_sha |
| 1407 |
conf = config.NewC(test.NewLogger()) |
| 1408 |
mf = &mockFirewall{} |
| 1409 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_sha": "12312313123"}}} |
| 1410 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1411 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caSha: "12312313123"}, mf.lastCall) |
| 1412 |
|
| 1413 |
// Test adding rule with ca_name |
| 1414 |
conf = config.NewC(test.NewLogger()) |
| 1415 |
mf = &mockFirewall{} |
| 1416 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_name": "root01"}}} |
| 1417 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1418 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caName: "root01"}, mf.lastCall) |
| 1419 |
|
| 1420 |
// Test single group |
| 1421 |
conf = config.NewC(test.NewLogger()) |
| 1422 |
mf = &mockFirewall{} |
| 1423 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a"}}} |
| 1424 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1425 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall) |
| 1426 |
|
| 1427 |
// Test single groups |
| 1428 |
conf = config.NewC(test.NewLogger()) |
| 1429 |
mf = &mockFirewall{} |
| 1430 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": "a"}}} |
| 1431 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1432 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall) |
| 1433 |
|
| 1434 |
// Test multiple AND groups |
| 1435 |
conf = config.NewC(test.NewLogger()) |
| 1436 |
mf = &mockFirewall{} |
| 1437 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}} |
| 1438 |
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf)) |
| 1439 |
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall) |
| 1440 |
|
| 1441 |
// Test Add error |
| 1442 |
conf = config.NewC(test.NewLogger()) |
| 1443 |
mf = &mockFirewall{} |
| 1444 |
mf.nextCallReturn = errors.New("test error") |
| 1445 |
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}} |
| 1446 |
require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`") |
| 1447 |
} |
| 1448 |
|
| 1449 |
func TestFirewall_convertRule(t *testing.T) { |
| 1450 |
ob := &bytes.Buffer{} |
| 1451 |
l := test.NewLoggerWithOutput(ob) |
| 1452 |
|
| 1453 |
// Ensure group array of 1 is converted and a warning is printed |
| 1454 |
c := map[string]any{ |
| 1455 |
"group": []any{"group1"}, |
| 1456 |
} |
| 1457 |
|
| 1458 |
r, err := convertRule(l, c, "test", 1) |
| 1459 |
assert.Contains(t, ob.String(), "group was an array with a single value, converting to simple value") |
| 1460 |
assert.Contains(t, ob.String(), "table=test") |
| 1461 |
assert.Contains(t, ob.String(), "rule=1") |
| 1462 |
require.NoError(t, err) |
| 1463 |
assert.Equal(t, []string{"group1"}, r.Groups) |
| 1464 |
|
| 1465 |
// Ensure group array of > 1 is errord |
| 1466 |
ob.Reset() |
| 1467 |
c = map[string]any{ |
| 1468 |
"group": []any{"group1", "group2"}, |
| 1469 |
} |
| 1470 |
|
| 1471 |
r, err = convertRule(l, c, "test", 1) |
| 1472 |
assert.Empty(t, ob.String()) |
| 1473 |
require.Error(t, err, "group should contain a single value, an array with more than one entry was provided") |
| 1474 |
|
| 1475 |
// Make sure a well formed group is alright |
| 1476 |
ob.Reset() |
| 1477 |
c = map[string]any{ |
| 1478 |
"group": "group1", |
| 1479 |
} |
| 1480 |
|
| 1481 |
r, err = convertRule(l, c, "test", 1) |
| 1482 |
require.NoError(t, err) |
| 1483 |
assert.Equal(t, []string{"group1"}, r.Groups) |
| 1484 |
} |
| 1485 |
|
| 1486 |
func TestFirewall_convertRuleSanity(t *testing.T) { |
| 1487 |
ob := &bytes.Buffer{} |
| 1488 |
l := test.NewLoggerWithOutput(ob) |
| 1489 |
|
| 1490 |
noWarningPlease := []map[string]any{ |
| 1491 |
{"group": "group1"}, |
| 1492 |
{"groups": []any{"group2"}}, |
| 1493 |
{"host": "bob"}, |
| 1494 |
{"cidr": "1.1.1.1/1"}, |
| 1495 |
{"groups": []any{"group2"}, "host": "bob"}, |
| 1496 |
{"cidr": "1.1.1.1/1", "host": "bob"}, |
| 1497 |
{"groups": []any{"group2"}, "cidr": "1.1.1.1/1"}, |
| 1498 |
{"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"}, |
| 1499 |
} |
| 1500 |
for _, c := range noWarningPlease { |
| 1501 |
r, err := convertRule(l, c, "test", 1) |
| 1502 |
require.NoError(t, err) |
| 1503 |
require.NoError(t, r.sanity(), "should not generate a sanity warning, %+v", c) |
| 1504 |
} |
| 1505 |
|
| 1506 |
yesWarningPlease := []map[string]any{ |
| 1507 |
{"group": "group1"}, |
| 1508 |
{"groups": []any{"group2"}}, |
| 1509 |
{"cidr": "1.1.1.1/1"}, |
| 1510 |
{"groups": []any{"group2"}, "host": "bob"}, |
| 1511 |
{"cidr": "1.1.1.1/1", "host": "bob"}, |
| 1512 |
{"groups": []any{"group2"}, "cidr": "1.1.1.1/1"}, |
| 1513 |
{"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"}, |
| 1514 |
} |
| 1515 |
for _, c := range yesWarningPlease { |
| 1516 |
c["host"] = "any" |
| 1517 |
r, err := convertRule(l, c, "test", 1) |
| 1518 |
require.NoError(t, err) |
| 1519 |
err = r.sanity() |
| 1520 |
require.Error(t, err, "I wanted a warning: %+v", c) |
| 1521 |
} |
| 1522 |
//reset the list |
| 1523 |
yesWarningPlease = []map[string]any{ |
| 1524 |
{"group": "group1"}, |
| 1525 |
{"groups": []any{"group2"}}, |
| 1526 |
{"cidr": "1.1.1.1/1"}, |
| 1527 |
{"groups": []any{"group2"}, "host": "bob"}, |
| 1528 |
{"cidr": "1.1.1.1/1", "host": "bob"}, |
| 1529 |
{"groups": []any{"group2"}, "cidr": "1.1.1.1/1"}, |
| 1530 |
{"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"}, |
| 1531 |
} |
| 1532 |
for _, c := range yesWarningPlease { |
| 1533 |
r, err := convertRule(l, c, "test", 1) |
| 1534 |
require.NoError(t, err) |
| 1535 |
r.Groups = append(r.Groups, "any") |
| 1536 |
err = r.sanity() |
| 1537 |
require.Error(t, err, "I wanted a warning: %+v", c) |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
type testcase struct { |
| 1542 |
h *HostInfo |
| 1543 |
p firewall.Packet |
| 1544 |
c cert.Certificate |
| 1545 |
err error |
| 1546 |
} |
| 1547 |
|
| 1548 |
func (c *testcase) Test(t *testing.T, fw *Firewall) { |
| 1549 |
t.Helper() |
| 1550 |
cp := cert.NewCAPool() |
| 1551 |
resetConntrack(fw) |
| 1552 |
err := fw.Drop(c.p, true, c.h, cp, nil) |
| 1553 |
if c.err == nil { |
| 1554 |
require.NoError(t, err, "failed to not drop remote address %s", c.p.RemoteAddr) |
| 1555 |
} else { |
| 1556 |
require.ErrorIs(t, c.err, err, "failed to drop remote address %s", c.p.RemoteAddr) |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
func buildTestCase(setup testsetup, err error, theirPrefixes ...netip.Prefix) testcase { |
| 1561 |
c1 := dummyCert{ |
| 1562 |
name: "host1", |
| 1563 |
networks: theirPrefixes, |
| 1564 |
groups: []string{"default-group"}, |
| 1565 |
issuer: "signer-shasum", |
| 1566 |
} |
| 1567 |
h := HostInfo{ |
| 1568 |
ConnectionState: &ConnectionState{ |
| 1569 |
peerCert: &cert.CachedCertificate{ |
| 1570 |
Certificate: &c1, |
| 1571 |
InvertedGroups: map[string]struct{}{"default-group": {}}, |
| 1572 |
}, |
| 1573 |
}, |
| 1574 |
vpnAddrs: make([]netip.Addr, len(theirPrefixes)), |
| 1575 |
} |
| 1576 |
for i := range theirPrefixes { |
| 1577 |
h.vpnAddrs[i] = theirPrefixes[i].Addr() |
| 1578 |
} |
| 1579 |
h.buildNetworks(setup.myVpnNetworksTable, &c1) |
| 1580 |
p := firewall.Packet{ |
| 1581 |
LocalAddr: setup.c.Networks()[0].Addr(), //todo? |
| 1582 |
RemoteAddr: theirPrefixes[0].Addr(), |
| 1583 |
LocalPort: 10, |
| 1584 |
RemotePort: 90, |
| 1585 |
Protocol: firewall.ProtoUDP, |
| 1586 |
Fragment: false, |
| 1587 |
} |
| 1588 |
return testcase{ |
| 1589 |
h: &h, |
| 1590 |
p: p, |
| 1591 |
c: &c1, |
| 1592 |
err: err, |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
type testsetup struct { |
| 1597 |
c dummyCert |
| 1598 |
myVpnNetworksTable *bart.Lite |
| 1599 |
fw *Firewall |
| 1600 |
} |
| 1601 |
|
| 1602 |
func newSetup(t *testing.T, l *slog.Logger, myPrefixes ...netip.Prefix) testsetup { |
| 1603 |
c := dummyCert{ |
| 1604 |
name: "me", |
| 1605 |
networks: myPrefixes, |
| 1606 |
groups: []string{"default-group"}, |
| 1607 |
issuer: "signer-shasum", |
| 1608 |
} |
| 1609 |
|
| 1610 |
return newSetupFromCert(t, l, c) |
| 1611 |
} |
| 1612 |
|
| 1613 |
func newSetupFromCert(t *testing.T, l *slog.Logger, c dummyCert) testsetup { |
| 1614 |
myVpnNetworksTable := new(bart.Lite) |
| 1615 |
for _, prefix := range c.Networks() { |
| 1616 |
myVpnNetworksTable.Insert(prefix) |
| 1617 |
} |
| 1618 |
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c) |
| 1619 |
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", "")) |
| 1620 |
|
| 1621 |
return testsetup{ |
| 1622 |
c: c, |
| 1623 |
fw: fw, |
| 1624 |
myVpnNetworksTable: myVpnNetworksTable, |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
func TestFirewall_Drop_EnforceIPMatch(t *testing.T) { |
| 1629 |
t.Parallel() |
| 1630 |
ob := &bytes.Buffer{} |
| 1631 |
l := test.NewLoggerWithOutput(ob) |
| 1632 |
|
| 1633 |
myPrefix := netip.MustParsePrefix("1.1.1.1/8") |
| 1634 |
// for now, it's okay that these are all "incoming", the logic this test tries to check doesn't care about in/out |
| 1635 |
t.Run("allow inbound all matching", func(t *testing.T) { |
| 1636 |
t.Parallel() |
| 1637 |
setup := newSetup(t, l, myPrefix) |
| 1638 |
tc := buildTestCase(setup, nil, netip.MustParsePrefix("1.2.3.4/24")) |
| 1639 |
tc.Test(t, setup.fw) |
| 1640 |
}) |
| 1641 |
t.Run("allow inbound local matching", func(t *testing.T) { |
| 1642 |
t.Parallel() |
| 1643 |
setup := newSetup(t, l, myPrefix) |
| 1644 |
tc := buildTestCase(setup, ErrInvalidLocalIP, netip.MustParsePrefix("1.2.3.4/24")) |
| 1645 |
tc.p.LocalAddr = netip.MustParseAddr("1.2.3.8") |
| 1646 |
tc.Test(t, setup.fw) |
| 1647 |
}) |
| 1648 |
t.Run("block inbound remote mismatched", func(t *testing.T) { |
| 1649 |
t.Parallel() |
| 1650 |
setup := newSetup(t, l, myPrefix) |
| 1651 |
tc := buildTestCase(setup, ErrInvalidRemoteIP, netip.MustParsePrefix("1.2.3.4/24")) |
| 1652 |
tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9") |
| 1653 |
tc.Test(t, setup.fw) |
| 1654 |
}) |
| 1655 |
t.Run("Block a vpn peer packet", func(t *testing.T) { |
| 1656 |
t.Parallel() |
| 1657 |
setup := newSetup(t, l, myPrefix) |
| 1658 |
tc := buildTestCase(setup, ErrPeerRejected, netip.MustParsePrefix("2.2.2.2/24")) |
| 1659 |
tc.Test(t, setup.fw) |
| 1660 |
}) |
| 1661 |
twoPrefixes := []netip.Prefix{ |
| 1662 |
netip.MustParsePrefix("1.2.3.4/24"), netip.MustParsePrefix("2.2.2.2/24"), |
| 1663 |
} |
| 1664 |
t.Run("allow inbound one matching", func(t *testing.T) { |
| 1665 |
t.Parallel() |
| 1666 |
setup := newSetup(t, l, myPrefix) |
| 1667 |
tc := buildTestCase(setup, nil, twoPrefixes...) |
| 1668 |
tc.Test(t, setup.fw) |
| 1669 |
}) |
| 1670 |
t.Run("block inbound multimismatch", func(t *testing.T) { |
| 1671 |
t.Parallel() |
| 1672 |
setup := newSetup(t, l, myPrefix) |
| 1673 |
tc := buildTestCase(setup, ErrInvalidRemoteIP, twoPrefixes...) |
| 1674 |
tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9") |
| 1675 |
tc.Test(t, setup.fw) |
| 1676 |
}) |
| 1677 |
t.Run("allow inbound 2nd one matching", func(t *testing.T) { |
| 1678 |
t.Parallel() |
| 1679 |
setup2 := newSetup(t, l, netip.MustParsePrefix("2.2.2.1/24")) |
| 1680 |
tc := buildTestCase(setup2, nil, twoPrefixes...) |
| 1681 |
tc.p.RemoteAddr = twoPrefixes[1].Addr() |
| 1682 |
tc.Test(t, setup2.fw) |
| 1683 |
}) |
| 1684 |
t.Run("allow inbound unsafe route", func(t *testing.T) { |
| 1685 |
t.Parallel() |
| 1686 |
unsafePrefix := netip.MustParsePrefix("192.168.0.0/24") |
| 1687 |
c := dummyCert{ |
| 1688 |
name: "me", |
| 1689 |
networks: []netip.Prefix{myPrefix}, |
| 1690 |
unsafeNetworks: []netip.Prefix{unsafePrefix}, |
| 1691 |
groups: []string{"default-group"}, |
| 1692 |
issuer: "signer-shasum", |
| 1693 |
} |
| 1694 |
unsafeSetup := newSetupFromCert(t, l, c) |
| 1695 |
tc := buildTestCase(unsafeSetup, nil, twoPrefixes...) |
| 1696 |
tc.p.LocalAddr = netip.MustParseAddr("192.168.0.3") |
| 1697 |
tc.err = ErrNoMatchingRule |
| 1698 |
tc.Test(t, unsafeSetup.fw) //should hit firewall and bounce off |
| 1699 |
require.NoError(t, unsafeSetup.fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", unsafePrefix.String(), "", "")) |
| 1700 |
tc.err = nil |
| 1701 |
tc.Test(t, unsafeSetup.fw) //should pass |
| 1702 |
}) |
| 1703 |
} |
| 1704 |
|
| 1705 |
type addRuleCall struct { |
| 1706 |
incoming bool |
| 1707 |
proto uint8 |
| 1708 |
startPort int32 |
| 1709 |
endPort int32 |
| 1710 |
groups []string |
| 1711 |
host string |
| 1712 |
ip string |
| 1713 |
localIp string |
| 1714 |
caName string |
| 1715 |
caSha string |
| 1716 |
} |
| 1717 |
|
| 1718 |
type mockFirewall struct { |
| 1719 |
lastCall addRuleCall |
| 1720 |
nextCallReturn error |
| 1721 |
} |
| 1722 |
|
| 1723 |
func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip, localIp, caName string, caSha string) error { |
| 1724 |
mf.lastCall = addRuleCall{ |
| 1725 |
incoming: incoming, |
| 1726 |
proto: proto, |
| 1727 |
startPort: startPort, |
| 1728 |
endPort: endPort, |
| 1729 |
groups: groups, |
| 1730 |
host: host, |
| 1731 |
ip: ip, |
| 1732 |
localIp: localIp, |
| 1733 |
caName: caName, |
| 1734 |
caSha: caSha, |
| 1735 |
} |
| 1736 |
|
| 1737 |
err := mf.nextCallReturn |
| 1738 |
mf.nextCallReturn = nil |
| 1739 |
return err |
| 1740 |
} |
| 1741 |
|
| 1742 |
func resetConntrack(fw *Firewall) { |
| 1743 |
fw.Conntrack.Lock() |
| 1744 |
fw.Conntrack.Conns = map[firewall.Packet]*conn{} |
| 1745 |
fw.Conntrack.Unlock() |
| 1746 |
} |