| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"fmt" |
| 6 |
"net/netip" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/gaissmai/bart" |
| 10 |
"github.com/slackhq/nebula/cert" |
| 11 |
"github.com/slackhq/nebula/config" |
| 12 |
"github.com/slackhq/nebula/header" |
| 13 |
"github.com/slackhq/nebula/test" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
"go.yaml.in/yaml/v3" |
| 17 |
) |
| 18 |
|
| 19 |
func TestOldIPv4Only(t *testing.T) { |
| 20 |
// This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility |
| 21 |
b := []byte{8, 129, 130, 132, 80, 16, 10} |
| 22 |
var m V4AddrPort |
| 23 |
err := m.Unmarshal(b) |
| 24 |
require.NoError(t, err) |
| 25 |
ip := netip.MustParseAddr("10.1.1.1") |
| 26 |
bp := ip.As4() |
| 27 |
assert.Equal(t, binary.BigEndian.Uint32(bp[:]), m.GetAddr()) |
| 28 |
} |
| 29 |
|
| 30 |
func Test_lhStaticMapping(t *testing.T) { |
| 31 |
l := test.NewLogger() |
| 32 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/16") |
| 33 |
nt := new(bart.Lite) |
| 34 |
nt.Insert(myVpnNet) |
| 35 |
cs := &CertState{ |
| 36 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 37 |
myVpnNetworksTable: nt, |
| 38 |
} |
| 39 |
lh1 := "10.128.0.2" |
| 40 |
|
| 41 |
c := config.NewC(l) |
| 42 |
c.Settings["lighthouse"] = map[string]any{"hosts": []any{lh1}} |
| 43 |
c.Settings["static_host_map"] = map[string]any{lh1: []any{"1.1.1.1:4242"}} |
| 44 |
_, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 45 |
require.NoError(t, err) |
| 46 |
|
| 47 |
lh2 := "10.128.0.3" |
| 48 |
c = config.NewC(l) |
| 49 |
c.Settings["lighthouse"] = map[string]any{"hosts": []any{lh1, lh2}} |
| 50 |
c.Settings["static_host_map"] = map[string]any{lh1: []any{"100.1.1.1:4242"}} |
| 51 |
_, err = NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 52 |
require.EqualError(t, err, "lighthouse 10.128.0.3 does not have a static_host_map entry") |
| 53 |
} |
| 54 |
|
| 55 |
func TestReloadLighthouseInterval(t *testing.T) { |
| 56 |
l := test.NewLogger() |
| 57 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/16") |
| 58 |
nt := new(bart.Lite) |
| 59 |
nt.Insert(myVpnNet) |
| 60 |
cs := &CertState{ |
| 61 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 62 |
myVpnNetworksTable: nt, |
| 63 |
} |
| 64 |
lh1 := "10.128.0.2" |
| 65 |
|
| 66 |
c := config.NewC(l) |
| 67 |
c.Settings["lighthouse"] = map[string]any{ |
| 68 |
"hosts": []any{lh1}, |
| 69 |
"interval": "1s", |
| 70 |
} |
| 71 |
|
| 72 |
c.Settings["static_host_map"] = map[string]any{lh1: []any{"1.1.1.1:4242"}} |
| 73 |
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 74 |
require.NoError(t, err) |
| 75 |
lh.ifce = &mockEncWriter{} |
| 76 |
|
| 77 |
// The first one routine is kicked off by main.go currently, lets make sure that one dies |
| 78 |
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 5")) |
| 79 |
assert.Equal(t, int64(5), lh.interval.Load()) |
| 80 |
|
| 81 |
// Subsequent calls are killed off by the LightHouse.Reload function |
| 82 |
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 10")) |
| 83 |
assert.Equal(t, int64(10), lh.interval.Load()) |
| 84 |
|
| 85 |
// If this completes then nothing is stealing our reload routine |
| 86 |
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 11")) |
| 87 |
assert.Equal(t, int64(11), lh.interval.Load()) |
| 88 |
} |
| 89 |
|
| 90 |
func BenchmarkLighthouseHandleRequest(b *testing.B) { |
| 91 |
l := test.NewLogger() |
| 92 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/0") |
| 93 |
nt := new(bart.Lite) |
| 94 |
nt.Insert(myVpnNet) |
| 95 |
cs := &CertState{ |
| 96 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 97 |
myVpnNetworksTable: nt, |
| 98 |
} |
| 99 |
|
| 100 |
c := config.NewC(l) |
| 101 |
lh, err := NewLightHouseFromConfig(b.Context(), l, c, cs, nil, nil) |
| 102 |
require.NoError(b, err) |
| 103 |
|
| 104 |
hAddr := netip.MustParseAddrPort("4.5.6.7:12345") |
| 105 |
hAddr2 := netip.MustParseAddrPort("4.5.6.7:12346") |
| 106 |
|
| 107 |
vpnIp3 := netip.MustParseAddr("0.0.0.3") |
| 108 |
lh.addrMap[vpnIp3] = NewRemoteList([]netip.Addr{vpnIp3}, nil) |
| 109 |
lh.addrMap[vpnIp3].unlockedSetV4( |
| 110 |
vpnIp3, |
| 111 |
vpnIp3, |
| 112 |
[]*V4AddrPort{ |
| 113 |
netAddrToProtoV4AddrPort(hAddr.Addr(), hAddr.Port()), |
| 114 |
netAddrToProtoV4AddrPort(hAddr2.Addr(), hAddr2.Port()), |
| 115 |
}, |
| 116 |
func(netip.Addr, *V4AddrPort) bool { return true }, |
| 117 |
) |
| 118 |
|
| 119 |
rAddr := netip.MustParseAddrPort("1.2.2.3:12345") |
| 120 |
rAddr2 := netip.MustParseAddrPort("1.2.2.3:12346") |
| 121 |
vpnIp2 := netip.MustParseAddr("0.0.0.3") |
| 122 |
lh.addrMap[vpnIp2] = NewRemoteList([]netip.Addr{vpnIp2}, nil) |
| 123 |
lh.addrMap[vpnIp2].unlockedSetV4( |
| 124 |
vpnIp3, |
| 125 |
vpnIp3, |
| 126 |
[]*V4AddrPort{ |
| 127 |
netAddrToProtoV4AddrPort(rAddr.Addr(), rAddr.Port()), |
| 128 |
netAddrToProtoV4AddrPort(rAddr2.Addr(), rAddr2.Port()), |
| 129 |
}, |
| 130 |
func(netip.Addr, *V4AddrPort) bool { return true }, |
| 131 |
) |
| 132 |
|
| 133 |
mw := &mockEncWriter{} |
| 134 |
|
| 135 |
hi := []netip.Addr{vpnIp2} |
| 136 |
b.Run("notfound", func(b *testing.B) { |
| 137 |
lhh := lh.NewRequestHandler() |
| 138 |
req := &NebulaMeta{ |
| 139 |
Type: NebulaMeta_HostQuery, |
| 140 |
Details: &NebulaMetaDetails{ |
| 141 |
OldVpnAddr: 4, |
| 142 |
V4AddrPorts: nil, |
| 143 |
}, |
| 144 |
} |
| 145 |
p, err := req.Marshal() |
| 146 |
require.NoError(b, err) |
| 147 |
for n := 0; n < b.N; n++ { |
| 148 |
lhh.HandleRequest(rAddr, hi, p, mw) |
| 149 |
} |
| 150 |
}) |
| 151 |
b.Run("found", func(b *testing.B) { |
| 152 |
lhh := lh.NewRequestHandler() |
| 153 |
req := &NebulaMeta{ |
| 154 |
Type: NebulaMeta_HostQuery, |
| 155 |
Details: &NebulaMetaDetails{ |
| 156 |
OldVpnAddr: 3, |
| 157 |
V4AddrPorts: nil, |
| 158 |
}, |
| 159 |
} |
| 160 |
p, err := req.Marshal() |
| 161 |
require.NoError(b, err) |
| 162 |
|
| 163 |
for n := 0; n < b.N; n++ { |
| 164 |
lhh.HandleRequest(rAddr, hi, p, mw) |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
func TestLighthouse_Memory(t *testing.T) { |
| 170 |
l := test.NewLogger() |
| 171 |
|
| 172 |
myUdpAddr0 := netip.MustParseAddrPort("10.0.0.2:4242") |
| 173 |
myUdpAddr1 := netip.MustParseAddrPort("192.168.0.2:4242") |
| 174 |
myUdpAddr2 := netip.MustParseAddrPort("172.16.0.2:4242") |
| 175 |
myUdpAddr3 := netip.MustParseAddrPort("100.152.0.2:4242") |
| 176 |
myUdpAddr4 := netip.MustParseAddrPort("24.15.0.2:4242") |
| 177 |
myUdpAddr5 := netip.MustParseAddrPort("192.168.0.2:4243") |
| 178 |
myUdpAddr6 := netip.MustParseAddrPort("192.168.0.2:4244") |
| 179 |
myUdpAddr7 := netip.MustParseAddrPort("192.168.0.2:4245") |
| 180 |
myUdpAddr8 := netip.MustParseAddrPort("192.168.0.2:4246") |
| 181 |
myUdpAddr9 := netip.MustParseAddrPort("192.168.0.2:4247") |
| 182 |
myUdpAddr10 := netip.MustParseAddrPort("192.168.0.2:4248") |
| 183 |
myUdpAddr11 := netip.MustParseAddrPort("192.168.0.2:4249") |
| 184 |
myVpnIp := netip.MustParseAddr("10.128.0.2") |
| 185 |
|
| 186 |
theirUdpAddr0 := netip.MustParseAddrPort("10.0.0.3:4242") |
| 187 |
theirUdpAddr1 := netip.MustParseAddrPort("192.168.0.3:4242") |
| 188 |
theirUdpAddr2 := netip.MustParseAddrPort("172.16.0.3:4242") |
| 189 |
theirUdpAddr3 := netip.MustParseAddrPort("100.152.0.3:4242") |
| 190 |
theirUdpAddr4 := netip.MustParseAddrPort("24.15.0.3:4242") |
| 191 |
theirVpnIp := netip.MustParseAddr("10.128.0.3") |
| 192 |
|
| 193 |
c := config.NewC(l) |
| 194 |
c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true} |
| 195 |
c.Settings["listen"] = map[string]any{"port": 4242} |
| 196 |
|
| 197 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/24") |
| 198 |
nt := new(bart.Lite) |
| 199 |
nt.Insert(myVpnNet) |
| 200 |
cs := &CertState{ |
| 201 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 202 |
myVpnNetworksTable: nt, |
| 203 |
} |
| 204 |
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 205 |
lh.ifce = &mockEncWriter{} |
| 206 |
require.NoError(t, err) |
| 207 |
lhh := lh.NewRequestHandler() |
| 208 |
|
| 209 |
// Test that my first update responds with just that |
| 210 |
newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr2}, lhh) |
| 211 |
r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh) |
| 212 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr2) |
| 213 |
|
| 214 |
// Ensure we don't accumulate addresses |
| 215 |
newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr3}, lhh) |
| 216 |
r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh) |
| 217 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr3) |
| 218 |
|
| 219 |
// Grow it back to 2 |
| 220 |
newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr4}, lhh) |
| 221 |
r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh) |
| 222 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4) |
| 223 |
|
| 224 |
// Update a different host and ask about it |
| 225 |
newLHHostUpdate(theirUdpAddr0, theirVpnIp, []netip.AddrPort{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh) |
| 226 |
r = newLHHostRequest(theirUdpAddr0, theirVpnIp, theirVpnIp, lhh) |
| 227 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4) |
| 228 |
|
| 229 |
// Have both hosts ask about the other |
| 230 |
r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh) |
| 231 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4) |
| 232 |
|
| 233 |
r = newLHHostRequest(myUdpAddr0, myVpnIp, theirVpnIp, lhh) |
| 234 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4) |
| 235 |
|
| 236 |
// Make sure we didn't get changed |
| 237 |
r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh) |
| 238 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4) |
| 239 |
|
| 240 |
// Ensure proper ordering and limiting |
| 241 |
// Send 12 addrs, get 10 back, the last 2 removed, allowing the duplicate to remain (clients dedupe) |
| 242 |
newLHHostUpdate( |
| 243 |
myUdpAddr0, |
| 244 |
myVpnIp, |
| 245 |
[]netip.AddrPort{ |
| 246 |
myUdpAddr1, |
| 247 |
myUdpAddr2, |
| 248 |
myUdpAddr3, |
| 249 |
myUdpAddr4, |
| 250 |
myUdpAddr5, |
| 251 |
myUdpAddr5, //Duplicated on purpose |
| 252 |
myUdpAddr6, |
| 253 |
myUdpAddr7, |
| 254 |
myUdpAddr8, |
| 255 |
myUdpAddr9, |
| 256 |
myUdpAddr10, |
| 257 |
myUdpAddr11, // This should get cut |
| 258 |
}, lhh) |
| 259 |
|
| 260 |
r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh) |
| 261 |
assertIp4InArray( |
| 262 |
t, |
| 263 |
r.msg.Details.V4AddrPorts, |
| 264 |
myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9, |
| 265 |
) |
| 266 |
|
| 267 |
// Make sure we won't add ips in our vpn network |
| 268 |
bad1 := netip.MustParseAddrPort("10.128.0.99:4242") |
| 269 |
bad2 := netip.MustParseAddrPort("10.128.0.100:4242") |
| 270 |
good := netip.MustParseAddrPort("1.128.0.99:4242") |
| 271 |
newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{bad1, bad2, good}, lhh) |
| 272 |
r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh) |
| 273 |
assertIp4InArray(t, r.msg.Details.V4AddrPorts, good) |
| 274 |
} |
| 275 |
|
| 276 |
func TestLighthouse_reload(t *testing.T) { |
| 277 |
l := test.NewLogger() |
| 278 |
c := config.NewC(l) |
| 279 |
c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true} |
| 280 |
c.Settings["listen"] = map[string]any{"port": 4242} |
| 281 |
|
| 282 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/24") |
| 283 |
nt := new(bart.Lite) |
| 284 |
nt.Insert(myVpnNet) |
| 285 |
cs := &CertState{ |
| 286 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 287 |
myVpnNetworksTable: nt, |
| 288 |
} |
| 289 |
|
| 290 |
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 291 |
require.NoError(t, err) |
| 292 |
|
| 293 |
nc := map[string]any{ |
| 294 |
"static_host_map": map[string]any{ |
| 295 |
"10.128.0.2": []any{"1.1.1.1:4242"}, |
| 296 |
}, |
| 297 |
} |
| 298 |
rc, err := yaml.Marshal(nc) |
| 299 |
require.NoError(t, err) |
| 300 |
c.ReloadConfigString(string(rc)) |
| 301 |
|
| 302 |
err = lh.reload(c, false) |
| 303 |
require.NoError(t, err) |
| 304 |
} |
| 305 |
|
| 306 |
// TestLighthouse_reloadStaticHostMap verifies that reloading static_host_map applies the new |
| 307 |
// config rather than appending to it. See issue #718. |
| 308 |
func TestLighthouse_reloadStaticHostMap(t *testing.T) { |
| 309 |
l := test.NewLogger() |
| 310 |
c := config.NewC(l) |
| 311 |
c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true} |
| 312 |
c.Settings["listen"] = map[string]any{"port": 4242} |
| 313 |
c.Settings["static_host_map"] = map[string]any{ |
| 314 |
"10.128.0.2": []any{"1.1.1.1:4242"}, |
| 315 |
} |
| 316 |
|
| 317 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/24") |
| 318 |
nt := new(bart.Lite) |
| 319 |
nt.Insert(myVpnNet) |
| 320 |
cs := &CertState{ |
| 321 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 322 |
myVpnNetworksTable: nt, |
| 323 |
} |
| 324 |
|
| 325 |
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 326 |
require.NoError(t, err) |
| 327 |
|
| 328 |
staticHost := netip.MustParseAddr("10.128.0.2") |
| 329 |
otherHost := netip.MustParseAddr("10.128.0.3") |
| 330 |
|
| 331 |
// Capture the RemoteList pointer up front; an in-flight handshake would hold the same one |
| 332 |
// on hostinfo.remotes, so it must reflect every reload below. |
| 333 |
pinned := lh.Query(staticHost) |
| 334 |
require.NotNil(t, pinned) |
| 335 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.1.1.1:4242")}, pinned.CopyAddrs([]netip.Prefix{})) |
| 336 |
|
| 337 |
// Replace the remote address. The new address should be the only entry. |
| 338 |
nc := map[string]any{ |
| 339 |
"static_host_map": map[string]any{ |
| 340 |
"10.128.0.2": []any{"2.2.2.2:4242"}, |
| 341 |
}, |
| 342 |
} |
| 343 |
rc, err := yaml.Marshal(nc) |
| 344 |
require.NoError(t, err) |
| 345 |
require.NoError(t, c.ReloadConfigString(string(rc))) |
| 346 |
|
| 347 |
rl := lh.Query(staticHost) |
| 348 |
require.NotNil(t, rl) |
| 349 |
assert.Same(t, pinned, rl, "RemoteList pointer must stay stable so in-flight handshakes pick up the change") |
| 350 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("2.2.2.2:4242")}, rl.CopyAddrs([]netip.Prefix{})) |
| 351 |
|
| 352 |
// Reload back to the original IP. Mirrors the round-trip in issue #718 step 6-8 where |
| 353 |
// the buggy reload produced [1.1.1.1, 2.2.2.2, 1.1.1.1] instead of [1.1.1.1]. |
| 354 |
nc = map[string]any{ |
| 355 |
"static_host_map": map[string]any{ |
| 356 |
"10.128.0.2": []any{"1.1.1.1:4242"}, |
| 357 |
}, |
| 358 |
} |
| 359 |
rc, err = yaml.Marshal(nc) |
| 360 |
require.NoError(t, err) |
| 361 |
require.NoError(t, c.ReloadConfigString(string(rc))) |
| 362 |
|
| 363 |
rl = lh.Query(staticHost) |
| 364 |
require.NotNil(t, rl) |
| 365 |
assert.Same(t, pinned, rl) |
| 366 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.1.1.1:4242")}, rl.CopyAddrs([]netip.Prefix{})) |
| 367 |
|
| 368 |
// Reload with the same config. An unchanged entry must not duplicate. |
| 369 |
require.NoError(t, c.ReloadConfigString(string(rc))) |
| 370 |
|
| 371 |
rl = lh.Query(staticHost) |
| 372 |
require.NotNil(t, rl) |
| 373 |
assert.Same(t, pinned, rl) |
| 374 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.1.1.1:4242")}, rl.CopyAddrs([]netip.Prefix{})) |
| 375 |
|
| 376 |
// Switch back to 2.2.2.2 so the rest of the test continues against a known address. |
| 377 |
nc = map[string]any{ |
| 378 |
"static_host_map": map[string]any{ |
| 379 |
"10.128.0.2": []any{"2.2.2.2:4242"}, |
| 380 |
}, |
| 381 |
} |
| 382 |
rc, err = yaml.Marshal(nc) |
| 383 |
require.NoError(t, err) |
| 384 |
require.NoError(t, c.ReloadConfigString(string(rc))) |
| 385 |
|
| 386 |
// Add a second host alongside the first. Both should be present, neither duplicated. |
| 387 |
nc = map[string]any{ |
| 388 |
"static_host_map": map[string]any{ |
| 389 |
"10.128.0.2": []any{"2.2.2.2:4242"}, |
| 390 |
"10.128.0.3": []any{"3.3.3.3:4242"}, |
| 391 |
}, |
| 392 |
} |
| 393 |
rc, err = yaml.Marshal(nc) |
| 394 |
require.NoError(t, err) |
| 395 |
require.NoError(t, c.ReloadConfigString(string(rc))) |
| 396 |
|
| 397 |
rl = lh.Query(staticHost) |
| 398 |
require.NotNil(t, rl) |
| 399 |
assert.Same(t, pinned, rl, "adding a sibling entry must not displace the existing RemoteList") |
| 400 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("2.2.2.2:4242")}, rl.CopyAddrs([]netip.Prefix{})) |
| 401 |
|
| 402 |
rl = lh.Query(otherHost) |
| 403 |
require.NotNil(t, rl) |
| 404 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("3.3.3.3:4242")}, rl.CopyAddrs([]netip.Prefix{})) |
| 405 |
|
| 406 |
// Drop the first host entirely. The vpnAddr is no longer marked static, our owner |
| 407 |
// contribution is cleared, but the addrMap entry stays in place so non-static cache |
| 408 |
// data (from lighthouse queries) on the same RemoteList isn't lost. In-flight handshakes |
| 409 |
// that already had the pointer see an empty address list rather than retrying stale ones. |
| 410 |
nc = map[string]any{ |
| 411 |
"static_host_map": map[string]any{ |
| 412 |
"10.128.0.3": []any{"3.3.3.3:4242"}, |
| 413 |
}, |
| 414 |
} |
| 415 |
rc, err = yaml.Marshal(nc) |
| 416 |
require.NoError(t, err) |
| 417 |
require.NoError(t, c.ReloadConfigString(string(rc))) |
| 418 |
|
| 419 |
_, isStatic := lh.GetStaticHostList()[staticHost] |
| 420 |
assert.False(t, isStatic) |
| 421 |
|
| 422 |
rl = lh.Query(staticHost) |
| 423 |
require.NotNil(t, rl) |
| 424 |
assert.Same(t, pinned, rl) |
| 425 |
assert.Empty(t, rl.CopyAddrs([]netip.Prefix{})) |
| 426 |
|
| 427 |
rl = lh.Query(otherHost) |
| 428 |
require.NotNil(t, rl) |
| 429 |
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("3.3.3.3:4242")}, rl.CopyAddrs([]netip.Prefix{})) |
| 430 |
} |
| 431 |
|
| 432 |
func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler) testLhReply { |
| 433 |
req := &NebulaMeta{ |
| 434 |
Type: NebulaMeta_HostQuery, |
| 435 |
Details: &NebulaMetaDetails{}, |
| 436 |
} |
| 437 |
|
| 438 |
if queryVpnIp.Is4() { |
| 439 |
bip := queryVpnIp.As4() |
| 440 |
req.Details.OldVpnAddr = binary.BigEndian.Uint32(bip[:]) |
| 441 |
} else { |
| 442 |
req.Details.VpnAddr = netAddrToProtoAddr(queryVpnIp) |
| 443 |
} |
| 444 |
|
| 445 |
b, err := req.Marshal() |
| 446 |
if err != nil { |
| 447 |
panic(err) |
| 448 |
} |
| 449 |
|
| 450 |
filter := NebulaMeta_HostQueryReply |
| 451 |
w := &testEncWriter{ |
| 452 |
metaFilter: &filter, |
| 453 |
} |
| 454 |
lhh.HandleRequest(fromAddr, []netip.Addr{myVpnIp}, b, w) |
| 455 |
return w.lastReply |
| 456 |
} |
| 457 |
|
| 458 |
func newLHHostUpdate(fromAddr netip.AddrPort, vpnIp netip.Addr, addrs []netip.AddrPort, lhh *LightHouseHandler) { |
| 459 |
req := &NebulaMeta{ |
| 460 |
Type: NebulaMeta_HostUpdateNotification, |
| 461 |
Details: &NebulaMetaDetails{}, |
| 462 |
} |
| 463 |
|
| 464 |
if vpnIp.Is4() { |
| 465 |
bip := vpnIp.As4() |
| 466 |
req.Details.OldVpnAddr = binary.BigEndian.Uint32(bip[:]) |
| 467 |
} else { |
| 468 |
req.Details.VpnAddr = netAddrToProtoAddr(vpnIp) |
| 469 |
} |
| 470 |
|
| 471 |
for _, v := range addrs { |
| 472 |
if v.Addr().Is4() { |
| 473 |
req.Details.V4AddrPorts = append(req.Details.V4AddrPorts, netAddrToProtoV4AddrPort(v.Addr(), v.Port())) |
| 474 |
} else { |
| 475 |
req.Details.V6AddrPorts = append(req.Details.V6AddrPorts, netAddrToProtoV6AddrPort(v.Addr(), v.Port())) |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
b, err := req.Marshal() |
| 480 |
if err != nil { |
| 481 |
panic(err) |
| 482 |
} |
| 483 |
|
| 484 |
w := &testEncWriter{} |
| 485 |
lhh.HandleRequest(fromAddr, []netip.Addr{vpnIp}, b, w) |
| 486 |
} |
| 487 |
|
| 488 |
type testLhReply struct { |
| 489 |
nebType header.MessageType |
| 490 |
nebSubType header.MessageSubType |
| 491 |
vpnIp netip.Addr |
| 492 |
msg *NebulaMeta |
| 493 |
} |
| 494 |
|
| 495 |
type testEncWriter struct { |
| 496 |
lastReply testLhReply |
| 497 |
metaFilter *NebulaMeta_MessageType |
| 498 |
protocolVersion cert.Version |
| 499 |
} |
| 500 |
|
| 501 |
func (tw *testEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) { |
| 502 |
} |
| 503 |
func (tw *testEncWriter) Handshake(vpnIp netip.Addr) { |
| 504 |
} |
| 505 |
|
| 506 |
func (tw *testEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, _, _ []byte) { |
| 507 |
msg := &NebulaMeta{} |
| 508 |
err := msg.Unmarshal(p) |
| 509 |
if tw.metaFilter == nil || msg.Type == *tw.metaFilter { |
| 510 |
tw.lastReply = testLhReply{ |
| 511 |
nebType: t, |
| 512 |
nebSubType: st, |
| 513 |
vpnIp: hostinfo.vpnAddrs[0], |
| 514 |
msg: msg, |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if err != nil { |
| 519 |
panic(err) |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
func (tw *testEncWriter) SendMessageToVpnAddr(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, _, _ []byte) { |
| 524 |
msg := &NebulaMeta{} |
| 525 |
err := msg.Unmarshal(p) |
| 526 |
if tw.metaFilter == nil || msg.Type == *tw.metaFilter { |
| 527 |
tw.lastReply = testLhReply{ |
| 528 |
nebType: t, |
| 529 |
nebSubType: st, |
| 530 |
vpnIp: vpnIp, |
| 531 |
msg: msg, |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
if err != nil { |
| 536 |
panic(err) |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
func (tw *testEncWriter) GetHostInfo(vpnIp netip.Addr) *HostInfo { |
| 541 |
return nil |
| 542 |
} |
| 543 |
|
| 544 |
func (tw *testEncWriter) GetCertState() *CertState { |
| 545 |
return &CertState{initiatingVersion: tw.protocolVersion} |
| 546 |
} |
| 547 |
|
| 548 |
// assertIp4InArray asserts every address in want is at the same position in have and that the lengths match |
| 549 |
func assertIp4InArray(t *testing.T, have []*V4AddrPort, want ...netip.AddrPort) { |
| 550 |
if !assert.Len(t, have, len(want)) { |
| 551 |
return |
| 552 |
} |
| 553 |
|
| 554 |
for k, w := range want { |
| 555 |
h := protoV4AddrPortToNetAddrPort(have[k]) |
| 556 |
if !(h == w) { |
| 557 |
assert.Fail(t, fmt.Sprintf("Response did not contain: %v at %v, found %v", w, k, h)) |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
func Test_findNetworkUnion(t *testing.T) { |
| 563 |
var out netip.Addr |
| 564 |
var ok bool |
| 565 |
|
| 566 |
tenDot := netip.MustParsePrefix("10.0.0.0/8") |
| 567 |
oneSevenTwo := netip.MustParsePrefix("172.16.0.0/16") |
| 568 |
fe80 := netip.MustParsePrefix("fe80::/8") |
| 569 |
fc00 := netip.MustParsePrefix("fc00::/7") |
| 570 |
|
| 571 |
a1 := netip.MustParseAddr("10.0.0.1") |
| 572 |
afe81 := netip.MustParseAddr("fe80::1") |
| 573 |
|
| 574 |
//simple |
| 575 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1}) |
| 576 |
assert.True(t, ok) |
| 577 |
assert.Equal(t, out, a1) |
| 578 |
|
| 579 |
//mixed lengths |
| 580 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1, afe81}) |
| 581 |
assert.True(t, ok) |
| 582 |
assert.Equal(t, out, a1) |
| 583 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo}, []netip.Addr{a1}) |
| 584 |
assert.True(t, ok) |
| 585 |
assert.Equal(t, out, a1) |
| 586 |
|
| 587 |
//mixed family |
| 588 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1}) |
| 589 |
assert.True(t, ok) |
| 590 |
assert.Equal(t, out, a1) |
| 591 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1, afe81}) |
| 592 |
assert.True(t, ok) |
| 593 |
assert.Equal(t, out, a1) |
| 594 |
|
| 595 |
//ordering |
| 596 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81, a1}) |
| 597 |
assert.True(t, ok) |
| 598 |
assert.Equal(t, out, a1) |
| 599 |
out, ok = findNetworkUnion([]netip.Prefix{fe80, tenDot, oneSevenTwo}, []netip.Addr{afe81, a1}) |
| 600 |
assert.True(t, ok) |
| 601 |
assert.Equal(t, out, afe81) |
| 602 |
|
| 603 |
//some mismatches |
| 604 |
out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81}) |
| 605 |
assert.True(t, ok) |
| 606 |
assert.Equal(t, out, afe81) |
| 607 |
out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1, afe81}) |
| 608 |
assert.True(t, ok) |
| 609 |
assert.Equal(t, out, afe81) |
| 610 |
|
| 611 |
//falsey cases |
| 612 |
out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1}) |
| 613 |
assert.False(t, ok) |
| 614 |
out, ok = findNetworkUnion([]netip.Prefix{fc00, fe80}, []netip.Addr{a1}) |
| 615 |
assert.False(t, ok) |
| 616 |
out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fc00}, []netip.Addr{a1, afe81}) |
| 617 |
assert.False(t, ok) |
| 618 |
out, ok = findNetworkUnion([]netip.Prefix{fc00}, []netip.Addr{a1, afe81}) |
| 619 |
assert.False(t, ok) |
| 620 |
} |
| 621 |
|
| 622 |
func TestLighthouse_Dont_Delete_Static_Hosts(t *testing.T) { |
| 623 |
l := test.NewLogger() |
| 624 |
|
| 625 |
myUdpAddr2 := netip.MustParseAddrPort("1.2.3.4:4242") |
| 626 |
|
| 627 |
testSameHostNotStatic := netip.MustParseAddr("10.128.0.41") |
| 628 |
testStaticHost := netip.MustParseAddr("10.128.0.42") |
| 629 |
//myVpnIp := netip.MustParseAddr("10.128.0.2") |
| 630 |
|
| 631 |
c := config.NewC(l) |
| 632 |
lh1 := "10.128.0.2" |
| 633 |
c.Settings["lighthouse"] = map[string]any{ |
| 634 |
"hosts": []any{lh1}, |
| 635 |
"interval": "1s", |
| 636 |
} |
| 637 |
|
| 638 |
c.Settings["listen"] = map[string]any{"port": 4242} |
| 639 |
c.Settings["static_host_map"] = map[string]any{ |
| 640 |
lh1: []any{"1.1.1.1:4242"}, |
| 641 |
"10.128.0.42": []any{"1.2.3.4:4242"}, |
| 642 |
} |
| 643 |
|
| 644 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/24") |
| 645 |
nt := new(bart.Lite) |
| 646 |
nt.Insert(myVpnNet) |
| 647 |
cs := &CertState{ |
| 648 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 649 |
myVpnNetworksTable: nt, |
| 650 |
} |
| 651 |
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 652 |
require.NoError(t, err) |
| 653 |
lh.ifce = &mockEncWriter{} |
| 654 |
|
| 655 |
//test that we actually have the static entry: |
| 656 |
out := lh.Query(testStaticHost) |
| 657 |
assert.NotNil(t, out) |
| 658 |
assert.Equal(t, out.vpnAddrs[0], testStaticHost) |
| 659 |
out.Rebuild([]netip.Prefix{}) //why tho |
| 660 |
assert.Equal(t, out.addrs[0], myUdpAddr2) |
| 661 |
|
| 662 |
//bolt on a lower numbered primary IP |
| 663 |
am := lh.unlockedGetRemoteList([]netip.Addr{testStaticHost}) |
| 664 |
am.vpnAddrs = []netip.Addr{testSameHostNotStatic, testStaticHost} |
| 665 |
lh.addrMap[testSameHostNotStatic] = am |
| 666 |
out.Rebuild([]netip.Prefix{}) //??? |
| 667 |
|
| 668 |
//test that we actually have the static entry: |
| 669 |
out = lh.Query(testStaticHost) |
| 670 |
assert.NotNil(t, out) |
| 671 |
assert.Equal(t, out.vpnAddrs[0], testSameHostNotStatic) |
| 672 |
assert.Equal(t, out.vpnAddrs[1], testStaticHost) |
| 673 |
assert.Equal(t, out.addrs[0], myUdpAddr2) |
| 674 |
|
| 675 |
//test that we actually have the static entry for BOTH: |
| 676 |
out2 := lh.Query(testSameHostNotStatic) |
| 677 |
assert.Same(t, out2, out) |
| 678 |
|
| 679 |
//now do the delete |
| 680 |
lh.DeleteVpnAddrs([]netip.Addr{testSameHostNotStatic, testStaticHost}) |
| 681 |
//verify |
| 682 |
out = lh.Query(testSameHostNotStatic) |
| 683 |
assert.NotNil(t, out) |
| 684 |
if out == nil { |
| 685 |
t.Fatal("expected non-nil query for the static host") |
| 686 |
} |
| 687 |
assert.Equal(t, out.vpnAddrs[0], testSameHostNotStatic) |
| 688 |
assert.Equal(t, out.vpnAddrs[1], testStaticHost) |
| 689 |
assert.Equal(t, out.addrs[0], myUdpAddr2) |
| 690 |
} |
| 691 |
|
| 692 |
func TestLighthouse_DeletesWork(t *testing.T) { |
| 693 |
l := test.NewLogger() |
| 694 |
|
| 695 |
myUdpAddr2 := netip.MustParseAddrPort("1.2.3.4:4242") |
| 696 |
testHost := netip.MustParseAddr("10.128.0.42") |
| 697 |
|
| 698 |
c := config.NewC(l) |
| 699 |
lh1 := "10.128.0.2" |
| 700 |
c.Settings["lighthouse"] = map[string]any{ |
| 701 |
"hosts": []any{lh1}, |
| 702 |
"interval": "1s", |
| 703 |
} |
| 704 |
|
| 705 |
c.Settings["listen"] = map[string]any{"port": 4242} |
| 706 |
c.Settings["static_host_map"] = map[string]any{ |
| 707 |
lh1: []any{"1.1.1.1:4242"}, |
| 708 |
} |
| 709 |
|
| 710 |
myVpnNet := netip.MustParsePrefix("10.128.0.1/24") |
| 711 |
nt := new(bart.Lite) |
| 712 |
nt.Insert(myVpnNet) |
| 713 |
cs := &CertState{ |
| 714 |
myVpnNetworks: []netip.Prefix{myVpnNet}, |
| 715 |
myVpnNetworksTable: nt, |
| 716 |
} |
| 717 |
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil) |
| 718 |
require.NoError(t, err) |
| 719 |
lh.ifce = &mockEncWriter{} |
| 720 |
|
| 721 |
//insert the host |
| 722 |
am := lh.unlockedGetRemoteList([]netip.Addr{testHost}) |
| 723 |
am.vpnAddrs = []netip.Addr{testHost} |
| 724 |
am.addrs = []netip.AddrPort{myUdpAddr2} |
| 725 |
lh.addrMap[testHost] = am |
| 726 |
am.Rebuild([]netip.Prefix{}) //??? |
| 727 |
|
| 728 |
//test that we actually have the entry: |
| 729 |
out := lh.Query(testHost) |
| 730 |
assert.NotNil(t, out) |
| 731 |
assert.Equal(t, out.vpnAddrs[0], testHost) |
| 732 |
out.Rebuild([]netip.Prefix{}) //why tho |
| 733 |
assert.Equal(t, out.addrs[0], myUdpAddr2) |
| 734 |
|
| 735 |
//now do the delete |
| 736 |
lh.DeleteVpnAddrs([]netip.Addr{testHost}) |
| 737 |
//verify |
| 738 |
out = lh.Query(testHost) |
| 739 |
assert.Nil(t, out) |
| 740 |
} |