1
0

hostmap_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package nebula
  2. import (
  3. "net"
  4. "net/netip"
  5. "testing"
  6. "github.com/slackhq/nebula/cert"
  7. "github.com/slackhq/nebula/config"
  8. "github.com/slackhq/nebula/test"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestHostMap_MakePrimary(t *testing.T) {
  13. l := test.NewLogger()
  14. hm := newHostMap(
  15. l,
  16. netip.MustParsePrefix("10.0.0.1/24"),
  17. )
  18. f := &Interface{}
  19. h1 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 1}
  20. h2 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 2}
  21. h3 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 3}
  22. h4 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 4}
  23. hm.unlockedAddHostInfo(h4, f)
  24. hm.unlockedAddHostInfo(h3, f)
  25. hm.unlockedAddHostInfo(h2, f)
  26. hm.unlockedAddHostInfo(h1, f)
  27. // Make sure we go h1 -> h2 -> h3 -> h4
  28. prim := hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  29. assert.Equal(t, h1.localIndexId, prim.localIndexId)
  30. assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
  31. assert.Nil(t, prim.prev)
  32. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  33. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  34. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  35. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  36. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  37. assert.Nil(t, h4.next)
  38. // Swap h3/middle to primary
  39. hm.MakePrimary(h3)
  40. // Make sure we go h3 -> h1 -> h2 -> h4
  41. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  42. assert.Equal(t, h3.localIndexId, prim.localIndexId)
  43. assert.Equal(t, h1.localIndexId, prim.next.localIndexId)
  44. assert.Nil(t, prim.prev)
  45. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  46. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  47. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  48. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  49. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  50. assert.Nil(t, h4.next)
  51. // Swap h4/tail to primary
  52. hm.MakePrimary(h4)
  53. // Make sure we go h4 -> h3 -> h1 -> h2
  54. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  55. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  56. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  57. assert.Nil(t, prim.prev)
  58. assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
  59. assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
  60. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  61. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  62. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  63. assert.Nil(t, h2.next)
  64. // Swap h4 again should be no-op
  65. hm.MakePrimary(h4)
  66. // Make sure we go h4 -> h3 -> h1 -> h2
  67. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  68. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  69. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  70. assert.Nil(t, prim.prev)
  71. assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
  72. assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
  73. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  74. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  75. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  76. assert.Nil(t, h2.next)
  77. }
  78. func TestHostInfo_CreateRemoteCIDR(t *testing.T) {
  79. h := HostInfo{}
  80. c := &cert.NebulaCertificate{
  81. Details: cert.NebulaCertificateDetails{
  82. Ips: []*net.IPNet{
  83. {
  84. IP: net.IPv4(1, 2, 3, 4),
  85. Mask: net.IPv4Mask(255, 255, 255, 0),
  86. },
  87. },
  88. },
  89. }
  90. // remoteCidr should be empty with only 1 ip address present in the certificate
  91. h.CreateRemoteCIDR(c)
  92. assert.Empty(t, h.remoteCidr)
  93. // remoteCidr should be populated if there is also a subnet in the certificate
  94. c.Details.Subnets = []*net.IPNet{
  95. {
  96. IP: net.IPv4(9, 2, 3, 4),
  97. Mask: net.IPv4Mask(255, 255, 255, 0),
  98. },
  99. }
  100. h.CreateRemoteCIDR(c)
  101. assert.NotEmpty(t, h.remoteCidr)
  102. _, ok := h.remoteCidr.Lookup(netip.MustParseAddr("1.2.3.0"))
  103. assert.False(t, ok, "An ip address within the certificates network should not be found")
  104. _, ok = h.remoteCidr.Lookup(netip.MustParseAddr("1.2.3.4"))
  105. assert.True(t, ok, "An exact ip address match should be found")
  106. _, ok = h.remoteCidr.Lookup(netip.MustParseAddr("9.2.3.4"))
  107. assert.True(t, ok, "An ip address within the subnets should be found")
  108. }
  109. func TestHostMap_DeleteHostInfo(t *testing.T) {
  110. l := test.NewLogger()
  111. hm := newHostMap(
  112. l,
  113. netip.MustParsePrefix("10.0.0.1/24"),
  114. )
  115. f := &Interface{}
  116. h1 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 1}
  117. h2 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 2}
  118. h3 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 3}
  119. h4 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 4}
  120. h5 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 5}
  121. h6 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 6}
  122. hm.unlockedAddHostInfo(h6, f)
  123. hm.unlockedAddHostInfo(h5, f)
  124. hm.unlockedAddHostInfo(h4, f)
  125. hm.unlockedAddHostInfo(h3, f)
  126. hm.unlockedAddHostInfo(h2, f)
  127. hm.unlockedAddHostInfo(h1, f)
  128. // h6 should be deleted
  129. assert.Nil(t, h6.next)
  130. assert.Nil(t, h6.prev)
  131. h := hm.QueryIndex(h6.localIndexId)
  132. assert.Nil(t, h)
  133. // Make sure we go h1 -> h2 -> h3 -> h4 -> h5
  134. prim := hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  135. assert.Equal(t, h1.localIndexId, prim.localIndexId)
  136. assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
  137. assert.Nil(t, prim.prev)
  138. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  139. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  140. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  141. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  142. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  143. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  144. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  145. assert.Nil(t, h5.next)
  146. // Delete primary
  147. hm.DeleteHostInfo(h1)
  148. assert.Nil(t, h1.prev)
  149. assert.Nil(t, h1.next)
  150. // Make sure we go h2 -> h3 -> h4 -> h5
  151. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  152. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  153. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  154. assert.Nil(t, prim.prev)
  155. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  156. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  157. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  158. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  159. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  160. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  161. assert.Nil(t, h5.next)
  162. // Delete in the middle
  163. hm.DeleteHostInfo(h3)
  164. assert.Nil(t, h3.prev)
  165. assert.Nil(t, h3.next)
  166. // Make sure we go h2 -> h4 -> h5
  167. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  168. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  169. assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
  170. assert.Nil(t, prim.prev)
  171. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  172. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  173. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  174. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  175. assert.Nil(t, h5.next)
  176. // Delete the tail
  177. hm.DeleteHostInfo(h5)
  178. assert.Nil(t, h5.prev)
  179. assert.Nil(t, h5.next)
  180. // Make sure we go h2 -> h4
  181. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  182. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  183. assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
  184. assert.Nil(t, prim.prev)
  185. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  186. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  187. assert.Nil(t, h4.next)
  188. // Delete the head
  189. hm.DeleteHostInfo(h2)
  190. assert.Nil(t, h2.prev)
  191. assert.Nil(t, h2.next)
  192. // Make sure we only have h4
  193. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  194. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  195. assert.Nil(t, prim.prev)
  196. assert.Nil(t, prim.next)
  197. assert.Nil(t, h4.next)
  198. // Delete the only item
  199. hm.DeleteHostInfo(h4)
  200. assert.Nil(t, h4.prev)
  201. assert.Nil(t, h4.next)
  202. // Make sure we have nil
  203. prim = hm.QueryVpnIp(netip.MustParseAddr("0.0.0.1"))
  204. assert.Nil(t, prim)
  205. }
  206. func TestHostMap_reload(t *testing.T) {
  207. l := test.NewLogger()
  208. c := config.NewC(l)
  209. hm := NewHostMapFromConfig(
  210. l,
  211. netip.MustParsePrefix("10.0.0.1/24"),
  212. c,
  213. )
  214. toS := func(ipn []netip.Prefix) []string {
  215. var s []string
  216. for _, n := range ipn {
  217. s = append(s, n.String())
  218. }
  219. return s
  220. }
  221. assert.Empty(t, hm.GetPreferredRanges())
  222. c.ReloadConfigString("preferred_ranges: [1.1.1.0/24, 10.1.1.0/24]")
  223. assert.EqualValues(t, []string{"1.1.1.0/24", "10.1.1.0/24"}, toS(hm.GetPreferredRanges()))
  224. c.ReloadConfigString("preferred_ranges: [1.1.1.1/32]")
  225. assert.EqualValues(t, []string{"1.1.1.1/32"}, toS(hm.GetPreferredRanges()))
  226. }
  227. func TestHostMap_RelayState(t *testing.T) {
  228. h1 := &HostInfo{vpnIp: netip.MustParseAddr("0.0.0.1"), localIndexId: 1}
  229. a1 := netip.MustParseAddr("::1")
  230. a2 := netip.MustParseAddr("2001::1")
  231. h1.relayState.InsertRelayTo(a1)
  232. assert.Equal(t, h1.relayState.relays, []netip.Addr{a1})
  233. h1.relayState.InsertRelayTo(a2)
  234. assert.Equal(t, h1.relayState.relays, []netip.Addr{a1, a2})
  235. // Ensure that the first relay added is the first one returned in the copy
  236. currentRelays := h1.relayState.CopyRelayIps()
  237. require.Len(t, currentRelays, 2)
  238. assert.Equal(t, currentRelays[0], a1)
  239. // Deleting the last one in the list works ok
  240. h1.relayState.DeleteRelay(a2)
  241. assert.Equal(t, h1.relayState.relays, []netip.Addr{a1})
  242. // Deleting an element not in the list works ok
  243. h1.relayState.DeleteRelay(a2)
  244. assert.Equal(t, h1.relayState.relays, []netip.Addr{a1})
  245. // Deleting the only element in the list works ok
  246. h1.relayState.DeleteRelay(a1)
  247. assert.Equal(t, h1.relayState.relays, []netip.Addr{})
  248. }