connection_manager_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. package nebula
  2. import (
  3. "crypto/ed25519"
  4. "crypto/rand"
  5. "net/netip"
  6. "testing"
  7. "time"
  8. "github.com/flynn/noise"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/config"
  11. "github.com/slackhq/nebula/test"
  12. "github.com/slackhq/nebula/udp"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func newTestLighthouse() *LightHouse {
  17. lh := &LightHouse{
  18. l: test.NewLogger(),
  19. addrMap: map[netip.Addr]*RemoteList{},
  20. queryChan: make(chan netip.Addr, 10),
  21. }
  22. lighthouses := map[netip.Addr]struct{}{}
  23. staticList := map[netip.Addr]struct{}{}
  24. lh.lighthouses.Store(&lighthouses)
  25. lh.staticList.Store(&staticList)
  26. return lh
  27. }
  28. func Test_NewConnectionManagerTest(t *testing.T) {
  29. l := test.NewLogger()
  30. //_, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  31. localrange := netip.MustParsePrefix("10.1.1.1/24")
  32. vpnIp := netip.MustParseAddr("172.1.1.2")
  33. preferredRanges := []netip.Prefix{localrange}
  34. // Very incomplete mock objects
  35. hostMap := newHostMap(l)
  36. hostMap.preferredRanges.Store(&preferredRanges)
  37. cs := &CertState{
  38. initiatingVersion: cert.Version1,
  39. privateKey: []byte{},
  40. v1Cert: &dummyCert{version: cert.Version1},
  41. v1HandshakeBytes: []byte{},
  42. }
  43. lh := newTestLighthouse()
  44. ifce := &Interface{
  45. hostMap: hostMap,
  46. inside: &test.NoopTun{},
  47. outside: &udp.NoopConn{},
  48. firewall: &Firewall{},
  49. lightHouse: lh,
  50. pki: &PKI{},
  51. handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  52. l: l,
  53. }
  54. ifce.pki.cs.Store(cs)
  55. // Create manager
  56. conf := config.NewC(l)
  57. punchy := NewPunchyFromConfig(l, conf)
  58. nc := newConnectionManagerFromConfig(l, conf, hostMap, punchy)
  59. nc.intf = ifce
  60. p := []byte("")
  61. nb := make([]byte, 12, 12)
  62. out := make([]byte, mtu)
  63. // Add an ip we have established a connection w/ to hostmap
  64. hostinfo := &HostInfo{
  65. vpnAddrs: []netip.Addr{vpnIp},
  66. localIndexId: 1099,
  67. remoteIndexId: 9901,
  68. }
  69. hostinfo.ConnectionState = &ConnectionState{
  70. myCert: &dummyCert{version: cert.Version1},
  71. H: &noise.HandshakeState{},
  72. }
  73. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  74. // We saw traffic out to vpnIp
  75. nc.Out(hostinfo)
  76. nc.In(hostinfo)
  77. assert.False(t, hostinfo.pendingDeletion.Load())
  78. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  79. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  80. assert.True(t, hostinfo.out.Load())
  81. assert.True(t, hostinfo.in.Load())
  82. // Do a traffic check tick, should not be pending deletion but should not have any in/out packets recorded
  83. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  84. assert.False(t, hostinfo.pendingDeletion.Load())
  85. assert.False(t, hostinfo.out.Load())
  86. assert.False(t, hostinfo.in.Load())
  87. // Do another traffic check tick, this host should be pending deletion now
  88. nc.Out(hostinfo)
  89. assert.True(t, hostinfo.out.Load())
  90. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  91. assert.True(t, hostinfo.pendingDeletion.Load())
  92. assert.False(t, hostinfo.out.Load())
  93. assert.False(t, hostinfo.in.Load())
  94. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  95. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  96. // Do a final traffic check tick, the host should now be removed
  97. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  98. assert.NotContains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs)
  99. assert.NotContains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  100. }
  101. func Test_NewConnectionManagerTest2(t *testing.T) {
  102. l := test.NewLogger()
  103. //_, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  104. localrange := netip.MustParsePrefix("10.1.1.1/24")
  105. vpnIp := netip.MustParseAddr("172.1.1.2")
  106. preferredRanges := []netip.Prefix{localrange}
  107. // Very incomplete mock objects
  108. hostMap := newHostMap(l)
  109. hostMap.preferredRanges.Store(&preferredRanges)
  110. cs := &CertState{
  111. initiatingVersion: cert.Version1,
  112. privateKey: []byte{},
  113. v1Cert: &dummyCert{version: cert.Version1},
  114. v1HandshakeBytes: []byte{},
  115. }
  116. lh := newTestLighthouse()
  117. ifce := &Interface{
  118. hostMap: hostMap,
  119. inside: &test.NoopTun{},
  120. outside: &udp.NoopConn{},
  121. firewall: &Firewall{},
  122. lightHouse: lh,
  123. pki: &PKI{},
  124. handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  125. l: l,
  126. }
  127. ifce.pki.cs.Store(cs)
  128. // Create manager
  129. conf := config.NewC(l)
  130. punchy := NewPunchyFromConfig(l, conf)
  131. nc := newConnectionManagerFromConfig(l, conf, hostMap, punchy)
  132. nc.intf = ifce
  133. p := []byte("")
  134. nb := make([]byte, 12, 12)
  135. out := make([]byte, mtu)
  136. // Add an ip we have established a connection w/ to hostmap
  137. hostinfo := &HostInfo{
  138. vpnAddrs: []netip.Addr{vpnIp},
  139. localIndexId: 1099,
  140. remoteIndexId: 9901,
  141. }
  142. hostinfo.ConnectionState = &ConnectionState{
  143. myCert: &dummyCert{version: cert.Version1},
  144. H: &noise.HandshakeState{},
  145. }
  146. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  147. // We saw traffic out to vpnIp
  148. nc.Out(hostinfo)
  149. nc.In(hostinfo)
  150. assert.True(t, hostinfo.in.Load())
  151. assert.True(t, hostinfo.out.Load())
  152. assert.False(t, hostinfo.pendingDeletion.Load())
  153. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  154. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  155. // Do a traffic check tick, should not be pending deletion but should not have any in/out packets recorded
  156. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  157. assert.False(t, hostinfo.pendingDeletion.Load())
  158. assert.False(t, hostinfo.out.Load())
  159. assert.False(t, hostinfo.in.Load())
  160. // Do another traffic check tick, this host should be pending deletion now
  161. nc.Out(hostinfo)
  162. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  163. assert.True(t, hostinfo.pendingDeletion.Load())
  164. assert.False(t, hostinfo.out.Load())
  165. assert.False(t, hostinfo.in.Load())
  166. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  167. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  168. // We saw traffic, should no longer be pending deletion
  169. nc.In(hostinfo)
  170. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  171. assert.False(t, hostinfo.pendingDeletion.Load())
  172. assert.False(t, hostinfo.out.Load())
  173. assert.False(t, hostinfo.in.Load())
  174. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  175. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  176. }
  177. func Test_NewConnectionManager_DisconnectInactive(t *testing.T) {
  178. l := test.NewLogger()
  179. localrange := netip.MustParsePrefix("10.1.1.1/24")
  180. vpnAddrs := []netip.Addr{netip.MustParseAddr("172.1.1.2")}
  181. preferredRanges := []netip.Prefix{localrange}
  182. // Very incomplete mock objects
  183. hostMap := newHostMap(l)
  184. hostMap.preferredRanges.Store(&preferredRanges)
  185. cs := &CertState{
  186. initiatingVersion: cert.Version1,
  187. privateKey: []byte{},
  188. v1Cert: &dummyCert{version: cert.Version1},
  189. v1HandshakeBytes: []byte{},
  190. }
  191. lh := newTestLighthouse()
  192. ifce := &Interface{
  193. hostMap: hostMap,
  194. inside: &test.NoopTun{},
  195. outside: &udp.NoopConn{},
  196. firewall: &Firewall{},
  197. lightHouse: lh,
  198. pki: &PKI{},
  199. handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  200. l: l,
  201. }
  202. ifce.pki.cs.Store(cs)
  203. // Create manager
  204. conf := config.NewC(l)
  205. conf.Settings["tunnels"] = map[string]any{
  206. "drop_inactive": true,
  207. }
  208. punchy := NewPunchyFromConfig(l, conf)
  209. nc := newConnectionManagerFromConfig(l, conf, hostMap, punchy)
  210. assert.True(t, nc.dropInactive.Load())
  211. nc.intf = ifce
  212. // Add an ip we have established a connection w/ to hostmap
  213. hostinfo := &HostInfo{
  214. vpnAddrs: vpnAddrs,
  215. localIndexId: 1099,
  216. remoteIndexId: 9901,
  217. }
  218. hostinfo.ConnectionState = &ConnectionState{
  219. myCert: &dummyCert{version: cert.Version1},
  220. H: &noise.HandshakeState{},
  221. }
  222. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  223. // Do a traffic check tick, in and out should be cleared but should not be pending deletion
  224. nc.Out(hostinfo)
  225. nc.In(hostinfo)
  226. assert.True(t, hostinfo.out.Load())
  227. assert.True(t, hostinfo.in.Load())
  228. now := time.Now()
  229. decision, _, _ := nc.makeTrafficDecision(hostinfo.localIndexId, now)
  230. assert.Equal(t, tryRehandshake, decision)
  231. assert.Equal(t, now, hostinfo.lastUsed)
  232. assert.False(t, hostinfo.pendingDeletion.Load())
  233. assert.False(t, hostinfo.out.Load())
  234. assert.False(t, hostinfo.in.Load())
  235. decision, _, _ = nc.makeTrafficDecision(hostinfo.localIndexId, now.Add(time.Second*5))
  236. assert.Equal(t, doNothing, decision)
  237. assert.Equal(t, now, hostinfo.lastUsed)
  238. assert.False(t, hostinfo.pendingDeletion.Load())
  239. assert.False(t, hostinfo.out.Load())
  240. assert.False(t, hostinfo.in.Load())
  241. // Do another traffic check tick, should still not be pending deletion
  242. decision, _, _ = nc.makeTrafficDecision(hostinfo.localIndexId, now.Add(time.Second*10))
  243. assert.Equal(t, doNothing, decision)
  244. assert.Equal(t, now, hostinfo.lastUsed)
  245. assert.False(t, hostinfo.pendingDeletion.Load())
  246. assert.False(t, hostinfo.out.Load())
  247. assert.False(t, hostinfo.in.Load())
  248. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  249. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  250. // Finally advance beyond the inactivity timeout
  251. decision, _, _ = nc.makeTrafficDecision(hostinfo.localIndexId, now.Add(time.Minute*10))
  252. assert.Equal(t, closeTunnel, decision)
  253. assert.Equal(t, now, hostinfo.lastUsed)
  254. assert.False(t, hostinfo.pendingDeletion.Load())
  255. assert.False(t, hostinfo.out.Load())
  256. assert.False(t, hostinfo.in.Load())
  257. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  258. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnAddrs[0])
  259. }
  260. // Check if we can disconnect the peer.
  261. // Validate if the peer's certificate is invalid (expired, etc.)
  262. // Disconnect only if disconnectInvalid: true is set.
  263. func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
  264. now := time.Now()
  265. l := test.NewLogger()
  266. vpncidr := netip.MustParsePrefix("172.1.1.1/24")
  267. localrange := netip.MustParsePrefix("10.1.1.1/24")
  268. vpnIp := netip.MustParseAddr("172.1.1.2")
  269. preferredRanges := []netip.Prefix{localrange}
  270. hostMap := newHostMap(l)
  271. hostMap.preferredRanges.Store(&preferredRanges)
  272. // Generate keys for CA and peer's cert.
  273. pubCA, privCA, _ := ed25519.GenerateKey(rand.Reader)
  274. tbs := &cert.TBSCertificate{
  275. Version: 1,
  276. Name: "ca",
  277. IsCA: true,
  278. NotBefore: now,
  279. NotAfter: now.Add(1 * time.Hour),
  280. PublicKey: pubCA,
  281. }
  282. caCert, err := tbs.Sign(nil, cert.Curve_CURVE25519, privCA)
  283. require.NoError(t, err)
  284. ncp := cert.NewCAPool()
  285. require.NoError(t, ncp.AddCA(caCert))
  286. pubCrt, _, _ := ed25519.GenerateKey(rand.Reader)
  287. tbs = &cert.TBSCertificate{
  288. Version: 1,
  289. Name: "host",
  290. Networks: []netip.Prefix{vpncidr},
  291. NotBefore: now,
  292. NotAfter: now.Add(60 * time.Second),
  293. PublicKey: pubCrt,
  294. }
  295. peerCert, err := tbs.Sign(caCert, cert.Curve_CURVE25519, privCA)
  296. require.NoError(t, err)
  297. cachedPeerCert, err := ncp.VerifyCertificate(now.Add(time.Second), peerCert)
  298. cs := &CertState{
  299. privateKey: []byte{},
  300. v1Cert: &dummyCert{},
  301. v1HandshakeBytes: []byte{},
  302. }
  303. lh := newTestLighthouse()
  304. ifce := &Interface{
  305. hostMap: hostMap,
  306. inside: &test.NoopTun{},
  307. outside: &udp.NoopConn{},
  308. firewall: &Firewall{},
  309. lightHouse: lh,
  310. handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  311. l: l,
  312. pki: &PKI{},
  313. }
  314. ifce.pki.cs.Store(cs)
  315. ifce.pki.caPool.Store(ncp)
  316. ifce.disconnectInvalid.Store(true)
  317. // Create manager
  318. conf := config.NewC(l)
  319. punchy := NewPunchyFromConfig(l, conf)
  320. nc := newConnectionManagerFromConfig(l, conf, hostMap, punchy)
  321. nc.intf = ifce
  322. ifce.connectionManager = nc
  323. hostinfo := &HostInfo{
  324. vpnAddrs: []netip.Addr{vpnIp},
  325. ConnectionState: &ConnectionState{
  326. myCert: &dummyCert{},
  327. peerCert: cachedPeerCert,
  328. H: &noise.HandshakeState{},
  329. },
  330. }
  331. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  332. // Move ahead 45s.
  333. // Check if to disconnect with invalid certificate.
  334. // Should be alive.
  335. nextTick := now.Add(45 * time.Second)
  336. invalid := nc.isInvalidCertificate(nextTick, hostinfo)
  337. assert.False(t, invalid)
  338. // Move ahead 61s.
  339. // Check if to disconnect with invalid certificate.
  340. // Should be disconnected.
  341. nextTick = now.Add(61 * time.Second)
  342. invalid = nc.isInvalidCertificate(nextTick, hostinfo)
  343. assert.True(t, invalid)
  344. }
  345. type dummyCert struct {
  346. version cert.Version
  347. curve cert.Curve
  348. groups []string
  349. isCa bool
  350. issuer string
  351. name string
  352. networks []netip.Prefix
  353. notAfter time.Time
  354. notBefore time.Time
  355. publicKey []byte
  356. signature []byte
  357. unsafeNetworks []netip.Prefix
  358. }
  359. func (d *dummyCert) Version() cert.Version {
  360. return d.version
  361. }
  362. func (d *dummyCert) Curve() cert.Curve {
  363. return d.curve
  364. }
  365. func (d *dummyCert) Groups() []string {
  366. return d.groups
  367. }
  368. func (d *dummyCert) IsCA() bool {
  369. return d.isCa
  370. }
  371. func (d *dummyCert) Issuer() string {
  372. return d.issuer
  373. }
  374. func (d *dummyCert) Name() string {
  375. return d.name
  376. }
  377. func (d *dummyCert) Networks() []netip.Prefix {
  378. return d.networks
  379. }
  380. func (d *dummyCert) NotAfter() time.Time {
  381. return d.notAfter
  382. }
  383. func (d *dummyCert) NotBefore() time.Time {
  384. return d.notBefore
  385. }
  386. func (d *dummyCert) PublicKey() []byte {
  387. return d.publicKey
  388. }
  389. func (d *dummyCert) Signature() []byte {
  390. return d.signature
  391. }
  392. func (d *dummyCert) UnsafeNetworks() []netip.Prefix {
  393. return d.unsafeNetworks
  394. }
  395. func (d *dummyCert) MarshalForHandshakes() ([]byte, error) {
  396. return nil, nil
  397. }
  398. func (d *dummyCert) Sign(curve cert.Curve, key []byte) error {
  399. return nil
  400. }
  401. func (d *dummyCert) CheckSignature(key []byte) bool {
  402. return true
  403. }
  404. func (d *dummyCert) Expired(t time.Time) bool {
  405. return false
  406. }
  407. func (d *dummyCert) CheckRootConstraints(signer cert.Certificate) error {
  408. return nil
  409. }
  410. func (d *dummyCert) VerifyPrivateKey(curve cert.Curve, key []byte) error {
  411. return nil
  412. }
  413. func (d *dummyCert) String() string {
  414. return ""
  415. }
  416. func (d *dummyCert) Marshal() ([]byte, error) {
  417. return nil, nil
  418. }
  419. func (d *dummyCert) MarshalPEM() ([]byte, error) {
  420. return nil, nil
  421. }
  422. func (d *dummyCert) Fingerprint() (string, error) {
  423. return "", nil
  424. }
  425. func (d *dummyCert) MarshalJSON() ([]byte, error) {
  426. return nil, nil
  427. }
  428. func (d *dummyCert) Copy() cert.Certificate {
  429. return d
  430. }