Debian Codegraph @

handshake_manager_test.go

raw
1 package nebula
2
3 import (
4 "net/netip"
5 "testing"
6 "time"
7
8 "github.com/gaissmai/bart"
9 "github.com/slackhq/nebula/cert"
10 "github.com/slackhq/nebula/header"
11 "github.com/slackhq/nebula/test"
12 "github.com/slackhq/nebula/udp"
13 "github.com/stretchr/testify/assert"
14 )
15
16 func Test_NewHandshakeManagerVpnIp(t *testing.T) {
17 l := test.NewLogger()
18 localrange := netip.MustParsePrefix("10.1.1.1/24")
19 ip := netip.MustParseAddr("172.1.1.2")
20
21 preferredRanges := []netip.Prefix{localrange}
22 mainHM := newHostMap(l)
23 mainHM.preferredRanges.Store(&preferredRanges)
24
25 lh := newTestLighthouse()
26
27 cs := &CertState{
28 initiatingVersion: cert.Version1,
29 privateKey: []byte{},
30 v1Cert: &dummyCert{version: cert.Version1},
31 v1Credential: nil,
32 }
33
34 blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
35 blah.f = &Interface{handshakeManager: blah, pki: &PKI{}, l: l}
36 blah.f.pki.cs.Store(cs)
37
38 now := time.Now()
39 blah.NextOutboundHandshakeTimerTick(now)
40
41 i := blah.StartHandshake(ip, nil)
42 i2 := blah.StartHandshake(ip, nil)
43 assert.Same(t, i, i2)
44
45 i.remotes = NewRemoteList([]netip.Addr{}, nil)
46
47 // Adding something to pending should not affect the main hostmap
48 assert.Empty(t, mainHM.Hosts)
49
50 // Confirm they are in the pending index list
51 assert.Contains(t, blah.vpnIps, ip)
52
53 // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
54 for i := 1; i <= DefaultHandshakeRetries+1; i++ {
55 now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
56 blah.NextOutboundHandshakeTimerTick(now)
57 }
58
59 // Confirm they are still in the pending index list
60 assert.Contains(t, blah.vpnIps, ip)
61
62 // Tick 1 more time, a minute will certainly flush it out
63 blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute))
64
65 // Confirm they have been removed
66 assert.NotContains(t, blah.vpnIps, ip)
67 }
68
69 func testCountTimerWheelEntries(tw *LockingTimerWheel[netip.Addr]) (c int) {
70 for _, i := range tw.t.wheel {
71 n := i.Head
72 for n != nil {
73 c++
74 n = n.Next
75 }
76 }
77 return c
78 }
79
80 type mockEncWriter struct {
81 }
82
83 func (mw *mockEncWriter) SendMessageToVpnAddr(_ header.MessageType, _ header.MessageSubType, _ netip.Addr, _, _, _ []byte) {
84 return
85 }
86
87 func (mw *mockEncWriter) SendVia(_ *HostInfo, _ *Relay, _, _, _ []byte, _ bool) {
88 return
89 }
90
91 func (mw *mockEncWriter) SendMessageToHostInfo(_ header.MessageType, _ header.MessageSubType, _ *HostInfo, _, _, _ []byte) {
92 return
93 }
94
95 func (mw *mockEncWriter) Handshake(_ netip.Addr) {}
96
97 func (mw *mockEncWriter) GetHostInfo(_ netip.Addr) *HostInfo {
98 return nil
99 }
100
101 func (mw *mockEncWriter) GetCertState() *CertState {
102 return &CertState{initiatingVersion: cert.Version2}
103 }
104
105 func TestValidatePeerCert(t *testing.T) {
106 l := test.NewLogger()
107
108 myNetwork := netip.MustParsePrefix("10.0.0.1/24")
109 myAddrTable := new(bart.Lite)
110 myAddrTable.Insert(netip.PrefixFrom(myNetwork.Addr(), myNetwork.Addr().BitLen()))
111 myNetTable := new(bart.Lite)
112 myNetTable.Insert(myNetwork.Masked())
113
114 newHM := func() *HandshakeManager {
115 hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), &udp.NoopConn{}, defaultHandshakeConfig)
116 hm.f = &Interface{
117 handshakeManager: hm,
118 pki: &PKI{},
119 l: l,
120 myVpnAddrsTable: myAddrTable,
121 myVpnNetworksTable: myNetTable,
122 lightHouse: hm.lightHouse,
123 }
124 return hm
125 }
126
127 cached := func(networks ...netip.Prefix) *cert.CachedCertificate {
128 return &cert.CachedCertificate{
129 Certificate: &dummyCert{name: "peer", networks: networks},
130 }
131 }
132
133 via := ViaSender{
134 UdpAddr: netip.MustParseAddrPort("198.51.100.7:4242"),
135 IsRelayed: true, // skip the remote allow list (covered separately)
136 }
137
138 t.Run("addr inside our networks sets anyVpnAddrsInCommon", func(t *testing.T) {
139 hm := newHM()
140 // 10.0.0.2 falls inside our 10.0.0.0/24
141 addrs, common, ok := hm.validatePeerCert(via, cached(netip.MustParsePrefix("10.0.0.2/24")))
142 assert.True(t, ok)
143 assert.True(t, common)
144 assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.0.0.2")}, addrs)
145 })
146
147 t.Run("addr outside our networks leaves anyVpnAddrsInCommon false", func(t *testing.T) {
148 hm := newHM()
149 addrs, common, ok := hm.validatePeerCert(via, cached(netip.MustParsePrefix("192.168.1.5/24")))
150 assert.True(t, ok)
151 assert.False(t, common)
152 assert.Equal(t, []netip.Addr{netip.MustParseAddr("192.168.1.5")}, addrs)
153 })
154
155 t.Run("any matching network is enough", func(t *testing.T) {
156 hm := newHM()
157 addrs, common, ok := hm.validatePeerCert(via, cached(
158 netip.MustParsePrefix("192.168.1.5/24"),
159 netip.MustParsePrefix("10.0.0.42/24"),
160 ))
161 assert.True(t, ok)
162 assert.True(t, common)
163 assert.Len(t, addrs, 2)
164 })
165
166 t.Run("self-handshake is rejected", func(t *testing.T) {
167 hm := newHM()
168 // 10.0.0.1 is in myVpnAddrsTable
169 addrs, common, ok := hm.validatePeerCert(via, cached(netip.MustParsePrefix("10.0.0.1/24")))
170 assert.False(t, ok)
171 assert.False(t, common)
172 assert.Nil(t, addrs)
173 })
174
175 t.Run("cert with no networks is rejected", func(t *testing.T) {
176 hm := newHM()
177 addrs, common, ok := hm.validatePeerCert(via, cached())
178 assert.False(t, ok)
179 assert.False(t, common)
180 assert.Nil(t, addrs)
181 })
182 }
183
184 func TestHandleIncomingDispatch(t *testing.T) {
185 l := test.NewLogger()
186
187 newHM := func() *HandshakeManager {
188 hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), &udp.NoopConn{}, defaultHandshakeConfig)
189 hm.f = &Interface{
190 handshakeManager: hm,
191 pki: &PKI{},
192 l: l,
193 }
194 return hm
195 }
196
197 via := ViaSender{
198 UdpAddr: netip.MustParseAddrPort("198.51.100.7:4242"),
199 IsRelayed: true, // bypass remote allow list
200 }
201
202 // A packet body of zero length is fine for these tests: dispatch is
203 // gated on header fields, and we assert that we never reach noise/cert
204 // processing for any of the malformed shapes here.
205 pkt := make([]byte, header.Len)
206
207 t.Run("unsupported subtype dropped", func(t *testing.T) {
208 hm := newHM()
209 h := &header.H{Type: header.Handshake, Subtype: header.MessageSubType(99), MessageCounter: 1}
210 hm.HandleIncoming(via, pkt, h)
211 assert.Empty(t, hm.indexes, "no pending handshake should be created")
212 })
213
214 t.Run("stage-1 with non-zero RemoteIndex dropped", func(t *testing.T) {
215 hm := newHM()
216 h := &header.H{
217 Type: header.Handshake,
218 Subtype: header.HandshakeIXPSK0,
219 RemoteIndex: 0xdeadbeef,
220 MessageCounter: 1,
221 }
222 hm.HandleIncoming(via, pkt, h)
223 assert.Empty(t, hm.indexes, "spoofed stage-1 must not create a pending machine")
224 })
225
226 t.Run("continuation with no matching pending index dropped", func(t *testing.T) {
227 hm := newHM()
228 h := &header.H{
229 Type: header.Handshake,
230 Subtype: header.HandshakeIXPSK0,
231 RemoteIndex: 0xcafef00d,
232 MessageCounter: 2,
233 }
234 hm.HandleIncoming(via, pkt, h)
235 assert.Empty(t, hm.indexes, "orphan stage-2 must not create state")
236 })
237 }