connection_manager_test.go 9.3 KB

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