connection_manager_test.go 9.5 KB

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