mirror of
https://github.com/slackhq/nebula.git
synced 2026-04-03 18:25:18 +02:00
split the client-snat-addr and the router-snat-addr to decrease confusion hopefully
This commit is contained in:
@@ -12,11 +12,12 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// mockDevice is a minimal Device implementation for testing prepareSnatAddr.
|
||||
// mockDevice is a minimal Device implementation for testing prepareUnsafeOriginAddr.
|
||||
type mockDevice struct {
|
||||
networks []netip.Prefix
|
||||
unsafeNetworks []netip.Prefix
|
||||
snatAddr netip.Prefix
|
||||
unsafeSnatAddr netip.Prefix
|
||||
}
|
||||
|
||||
func (d *mockDevice) Read([]byte) (int, error) { return 0, nil }
|
||||
@@ -26,6 +27,7 @@ func (d *mockDevice) Activate() error { return
|
||||
func (d *mockDevice) Networks() []netip.Prefix { return d.networks }
|
||||
func (d *mockDevice) UnsafeNetworks() []netip.Prefix { return d.unsafeNetworks }
|
||||
func (d *mockDevice) SNATAddress() netip.Prefix { return d.snatAddr }
|
||||
func (d *mockDevice) UnsafeIPv4OriginAddress() netip.Prefix { return d.unsafeSnatAddr }
|
||||
func (d *mockDevice) Name() string { return "mock" }
|
||||
func (d *mockDevice) RoutesFor(netip.Addr) routing.Gateways { return routing.Gateways{} }
|
||||
func (d *mockDevice) SupportsMultiqueue() bool { return false }
|
||||
@@ -40,7 +42,7 @@ func TestPrepareSnatAddr_V4Primary_NoSnat(t *testing.T) {
|
||||
d := &mockDevice{
|
||||
networks: []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareUnsafeOriginAddr(d, l, c, nil)
|
||||
assert.Equal(t, netip.Prefix{}, result, "should not assign SNAT addr when device has IPv4 primary")
|
||||
}
|
||||
|
||||
@@ -53,7 +55,7 @@ func TestPrepareSnatAddr_V6Primary_NoUnsafeOrRoutes(t *testing.T) {
|
||||
d := &mockDevice{
|
||||
networks: []netip.Prefix{netip.MustParsePrefix("fd00::1/128")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareUnsafeOriginAddr(d, l, c, nil)
|
||||
assert.Equal(t, netip.Prefix{}, result, "should not assign SNAT addr without IPv4 unsafe networks or routes")
|
||||
}
|
||||
|
||||
@@ -67,14 +69,17 @@ func TestPrepareSnatAddr_V6Primary_WithV4Unsafe(t *testing.T) {
|
||||
networks: []netip.Prefix{netip.MustParsePrefix("fd00::1/128")},
|
||||
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("192.168.0.0/16")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareSnatAddr(d, l, c)
|
||||
require.True(t, result.IsValid(), "should assign SNAT addr")
|
||||
assert.True(t, result.Addr().Is4(), "SNAT addr should be IPv4")
|
||||
assert.True(t, result.Addr().IsLinkLocalUnicast(), "SNAT addr should be link-local")
|
||||
assert.Equal(t, 32, result.Bits(), "SNAT addr should be /32")
|
||||
|
||||
result = prepareUnsafeOriginAddr(d, l, c, nil)
|
||||
require.False(t, result.IsValid(), "no routes = no origin addr needed")
|
||||
}
|
||||
|
||||
func TestPrepareSnatAddr_V6Primary_WithV4Route(t *testing.T) {
|
||||
func TestPrepareUnsafeOriginAddr_V6Primary_WithV4Route(t *testing.T) {
|
||||
l := logrus.New()
|
||||
l.SetLevel(logrus.PanicLevel)
|
||||
c := config.NewC(l)
|
||||
@@ -86,10 +91,13 @@ func TestPrepareSnatAddr_V6Primary_WithV4Route(t *testing.T) {
|
||||
routes := []Route{
|
||||
{Cidr: netip.MustParsePrefix("10.0.0.0/8")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, routes)
|
||||
result := prepareUnsafeOriginAddr(d, l, c, routes)
|
||||
require.True(t, result.IsValid(), "should assign SNAT addr when IPv4 route exists")
|
||||
assert.True(t, result.Addr().Is4())
|
||||
assert.True(t, result.Addr().IsLinkLocalUnicast())
|
||||
|
||||
result = prepareSnatAddr(d, l, c)
|
||||
require.False(t, result.IsValid(), "no UnsafeNetworks = no snat addr needed")
|
||||
}
|
||||
|
||||
func TestPrepareSnatAddr_V6Primary_V6UnsafeOnly(t *testing.T) {
|
||||
@@ -102,7 +110,7 @@ func TestPrepareSnatAddr_V6Primary_V6UnsafeOnly(t *testing.T) {
|
||||
networks: []netip.Prefix{netip.MustParsePrefix("fd00::1/128")},
|
||||
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("fd01::/64")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareUnsafeOriginAddr(d, l, c, nil)
|
||||
assert.Equal(t, netip.Prefix{}, result, "should not assign SNAT addr for IPv6-only unsafe networks")
|
||||
}
|
||||
|
||||
@@ -118,7 +126,7 @@ func TestPrepareSnatAddr_ManualAddress(t *testing.T) {
|
||||
networks: []netip.Prefix{netip.MustParsePrefix("fd00::1/128")},
|
||||
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("192.168.0.0/16")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareSnatAddr(d, l, c)
|
||||
require.True(t, result.IsValid())
|
||||
assert.Equal(t, netip.MustParseAddr("169.254.42.42"), result.Addr())
|
||||
assert.Equal(t, 32, result.Bits())
|
||||
@@ -136,7 +144,7 @@ func TestPrepareSnatAddr_InvalidManualAddress_Fallback(t *testing.T) {
|
||||
networks: []netip.Prefix{netip.MustParsePrefix("fd00::1/128")},
|
||||
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("192.168.0.0/16")},
|
||||
}
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareSnatAddr(d, l, c)
|
||||
// Should fall back to auto-assignment
|
||||
require.True(t, result.IsValid(), "should fall back to auto-assigned address")
|
||||
assert.True(t, result.Addr().Is4())
|
||||
@@ -155,7 +163,7 @@ func TestPrepareSnatAddr_AutoGenerated_Range(t *testing.T) {
|
||||
|
||||
// Generate several addresses and verify they're all in the expected range
|
||||
for i := 0; i < 100; i++ {
|
||||
result := prepareSnatAddr(d, l, c, nil)
|
||||
result := prepareSnatAddr(d, l, c)
|
||||
require.True(t, result.IsValid())
|
||||
addr := result.Addr()
|
||||
octets := addr.As4()
|
||||
|
||||
Reference in New Issue
Block a user