connection_manager_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package nebula
  2. import (
  3. "context"
  4. "crypto/ed25519"
  5. "crypto/rand"
  6. "net"
  7. "testing"
  8. "time"
  9. "github.com/flynn/noise"
  10. "github.com/slackhq/nebula/cert"
  11. "github.com/slackhq/nebula/config"
  12. "github.com/slackhq/nebula/iputil"
  13. "github.com/slackhq/nebula/test"
  14. "github.com/slackhq/nebula/udp"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. var vpnIp iputil.VpnIp
  18. func newTestLighthouse() *LightHouse {
  19. lh := &LightHouse{
  20. l: test.NewLogger(),
  21. addrMap: map[iputil.VpnIp]*RemoteList{},
  22. }
  23. lighthouses := map[iputil.VpnIp]struct{}{}
  24. staticList := map[iputil.VpnIp]struct{}{}
  25. lh.lighthouses.Store(&lighthouses)
  26. lh.staticList.Store(&staticList)
  27. return lh
  28. }
  29. func Test_NewConnectionManagerTest(t *testing.T) {
  30. l := test.NewLogger()
  31. //_, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  32. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  33. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  34. vpnIp = iputil.Ip2VpnIp(net.ParseIP("172.1.1.2"))
  35. preferredRanges := []*net.IPNet{localrange}
  36. // Very incomplete mock objects
  37. hostMap := NewHostMap(l, vpncidr, preferredRanges)
  38. cs := &CertState{
  39. RawCertificate: []byte{},
  40. PrivateKey: []byte{},
  41. Certificate: &cert.NebulaCertificate{},
  42. RawCertificateNoKey: []byte{},
  43. }
  44. lh := newTestLighthouse()
  45. ifce := &Interface{
  46. hostMap: hostMap,
  47. inside: &test.NoopTun{},
  48. outside: &udp.NoopConn{},
  49. firewall: &Firewall{},
  50. lightHouse: lh,
  51. pki: &PKI{},
  52. handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  53. l: l,
  54. }
  55. ifce.pki.cs.Store(cs)
  56. // Create manager
  57. ctx, cancel := context.WithCancel(context.Background())
  58. defer cancel()
  59. punchy := NewPunchyFromConfig(l, config.NewC(l))
  60. nc := newConnectionManager(ctx, l, ifce, 5, 10, punchy)
  61. p := []byte("")
  62. nb := make([]byte, 12, 12)
  63. out := make([]byte, mtu)
  64. // Add an ip we have established a connection w/ to hostmap
  65. hostinfo := &HostInfo{
  66. vpnIp: vpnIp,
  67. localIndexId: 1099,
  68. remoteIndexId: 9901,
  69. }
  70. hostinfo.ConnectionState = &ConnectionState{
  71. certState: cs,
  72. H: &noise.HandshakeState{},
  73. }
  74. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  75. // We saw traffic out to vpnIp
  76. nc.Out(hostinfo.localIndexId)
  77. nc.In(hostinfo.localIndexId)
  78. assert.NotContains(t, nc.pendingDeletion, hostinfo.localIndexId)
  79. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnIp)
  80. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  81. assert.Contains(t, nc.out, hostinfo.localIndexId)
  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.NotContains(t, nc.pendingDeletion, hostinfo.localIndexId)
  85. assert.NotContains(t, nc.out, hostinfo.localIndexId)
  86. assert.NotContains(t, nc.in, hostinfo.localIndexId)
  87. // Do another traffic check tick, this host should be pending deletion now
  88. nc.Out(hostinfo.localIndexId)
  89. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  90. assert.Contains(t, nc.pendingDeletion, hostinfo.localIndexId)
  91. assert.NotContains(t, nc.out, hostinfo.localIndexId)
  92. assert.NotContains(t, nc.in, hostinfo.localIndexId)
  93. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  94. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnIp)
  95. // Do a final traffic check tick, the host should now be removed
  96. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  97. assert.NotContains(t, nc.pendingDeletion, hostinfo.localIndexId)
  98. assert.NotContains(t, nc.hostMap.Hosts, hostinfo.vpnIp)
  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. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  105. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  106. preferredRanges := []*net.IPNet{localrange}
  107. // Very incomplete mock objects
  108. hostMap := NewHostMap(l, vpncidr, preferredRanges)
  109. cs := &CertState{
  110. RawCertificate: []byte{},
  111. PrivateKey: []byte{},
  112. Certificate: &cert.NebulaCertificate{},
  113. RawCertificateNoKey: []byte{},
  114. }
  115. lh := newTestLighthouse()
  116. ifce := &Interface{
  117. hostMap: hostMap,
  118. inside: &test.NoopTun{},
  119. outside: &udp.NoopConn{},
  120. firewall: &Firewall{},
  121. lightHouse: lh,
  122. pki: &PKI{},
  123. handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  124. l: l,
  125. }
  126. ifce.pki.cs.Store(cs)
  127. // Create manager
  128. ctx, cancel := context.WithCancel(context.Background())
  129. defer cancel()
  130. punchy := NewPunchyFromConfig(l, config.NewC(l))
  131. nc := newConnectionManager(ctx, l, ifce, 5, 10, punchy)
  132. p := []byte("")
  133. nb := make([]byte, 12, 12)
  134. out := make([]byte, mtu)
  135. // Add an ip we have established a connection w/ to hostmap
  136. hostinfo := &HostInfo{
  137. vpnIp: vpnIp,
  138. localIndexId: 1099,
  139. remoteIndexId: 9901,
  140. }
  141. hostinfo.ConnectionState = &ConnectionState{
  142. certState: cs,
  143. H: &noise.HandshakeState{},
  144. }
  145. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  146. // We saw traffic out to vpnIp
  147. nc.Out(hostinfo.localIndexId)
  148. nc.In(hostinfo.localIndexId)
  149. assert.NotContains(t, nc.pendingDeletion, hostinfo.vpnIp)
  150. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnIp)
  151. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  152. // Do a traffic check tick, should not be pending deletion but should not have any in/out packets recorded
  153. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  154. assert.NotContains(t, nc.pendingDeletion, hostinfo.localIndexId)
  155. assert.NotContains(t, nc.out, hostinfo.localIndexId)
  156. assert.NotContains(t, nc.in, hostinfo.localIndexId)
  157. // Do another traffic check tick, this host should be pending deletion now
  158. nc.Out(hostinfo.localIndexId)
  159. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  160. assert.Contains(t, nc.pendingDeletion, hostinfo.localIndexId)
  161. assert.NotContains(t, nc.out, hostinfo.localIndexId)
  162. assert.NotContains(t, nc.in, hostinfo.localIndexId)
  163. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  164. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnIp)
  165. // We saw traffic, should no longer be pending deletion
  166. nc.In(hostinfo.localIndexId)
  167. nc.doTrafficCheck(hostinfo.localIndexId, p, nb, out, time.Now())
  168. assert.NotContains(t, nc.pendingDeletion, hostinfo.localIndexId)
  169. assert.NotContains(t, nc.out, hostinfo.localIndexId)
  170. assert.NotContains(t, nc.in, hostinfo.localIndexId)
  171. assert.Contains(t, nc.hostMap.Indexes, hostinfo.localIndexId)
  172. assert.Contains(t, nc.hostMap.Hosts, hostinfo.vpnIp)
  173. }
  174. // Check if we can disconnect the peer.
  175. // Validate if the peer's certificate is invalid (expired, etc.)
  176. // Disconnect only if disconnectInvalid: true is set.
  177. func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
  178. now := time.Now()
  179. l := test.NewLogger()
  180. ipNet := net.IPNet{
  181. IP: net.IPv4(172, 1, 1, 2),
  182. Mask: net.IPMask{255, 255, 255, 0},
  183. }
  184. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  185. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  186. preferredRanges := []*net.IPNet{localrange}
  187. hostMap := NewHostMap(l, vpncidr, preferredRanges)
  188. // Generate keys for CA and peer's cert.
  189. pubCA, privCA, _ := ed25519.GenerateKey(rand.Reader)
  190. caCert := cert.NebulaCertificate{
  191. Details: cert.NebulaCertificateDetails{
  192. Name: "ca",
  193. NotBefore: now,
  194. NotAfter: now.Add(1 * time.Hour),
  195. IsCA: true,
  196. PublicKey: pubCA,
  197. },
  198. }
  199. caCert.Sign(cert.Curve_CURVE25519, privCA)
  200. ncp := &cert.NebulaCAPool{
  201. CAs: cert.NewCAPool().CAs,
  202. }
  203. ncp.CAs["ca"] = &caCert
  204. pubCrt, _, _ := ed25519.GenerateKey(rand.Reader)
  205. peerCert := cert.NebulaCertificate{
  206. Details: cert.NebulaCertificateDetails{
  207. Name: "host",
  208. Ips: []*net.IPNet{&ipNet},
  209. Subnets: []*net.IPNet{},
  210. NotBefore: now,
  211. NotAfter: now.Add(60 * time.Second),
  212. PublicKey: pubCrt,
  213. IsCA: false,
  214. Issuer: "ca",
  215. },
  216. }
  217. peerCert.Sign(cert.Curve_CURVE25519, privCA)
  218. cs := &CertState{
  219. RawCertificate: []byte{},
  220. PrivateKey: []byte{},
  221. Certificate: &cert.NebulaCertificate{},
  222. RawCertificateNoKey: []byte{},
  223. }
  224. lh := newTestLighthouse()
  225. ifce := &Interface{
  226. hostMap: hostMap,
  227. inside: &test.NoopTun{},
  228. outside: &udp.NoopConn{},
  229. firewall: &Firewall{},
  230. lightHouse: lh,
  231. handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
  232. l: l,
  233. disconnectInvalid: true,
  234. pki: &PKI{},
  235. }
  236. ifce.pki.cs.Store(cs)
  237. ifce.pki.caPool.Store(ncp)
  238. // Create manager
  239. ctx, cancel := context.WithCancel(context.Background())
  240. defer cancel()
  241. punchy := NewPunchyFromConfig(l, config.NewC(l))
  242. nc := newConnectionManager(ctx, l, ifce, 5, 10, punchy)
  243. ifce.connectionManager = nc
  244. hostinfo := &HostInfo{
  245. vpnIp: vpnIp,
  246. ConnectionState: &ConnectionState{
  247. certState: cs,
  248. peerCert: &peerCert,
  249. H: &noise.HandshakeState{},
  250. },
  251. }
  252. nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
  253. // Move ahead 45s.
  254. // Check if to disconnect with invalid certificate.
  255. // Should be alive.
  256. nextTick := now.Add(45 * time.Second)
  257. invalid := nc.isInvalidCertificate(nextTick, hostinfo)
  258. assert.False(t, invalid)
  259. // Move ahead 61s.
  260. // Check if to disconnect with invalid certificate.
  261. // Should be disconnected.
  262. nextTick = now.Add(61 * time.Second)
  263. invalid = nc.isInvalidCertificate(nextTick, hostinfo)
  264. assert.True(t, invalid)
  265. }