3
0

firewall_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. package nebula
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "math"
  7. "net"
  8. "testing"
  9. "time"
  10. "github.com/rcrowley/go-metrics"
  11. "github.com/slackhq/nebula/cert"
  12. "github.com/slackhq/nebula/config"
  13. "github.com/slackhq/nebula/firewall"
  14. "github.com/slackhq/nebula/iputil"
  15. "github.com/slackhq/nebula/test"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestNewFirewall(t *testing.T) {
  19. l := test.NewLogger()
  20. c := &cert.NebulaCertificate{}
  21. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  22. conntrack := fw.Conntrack
  23. assert.NotNil(t, conntrack)
  24. assert.NotNil(t, conntrack.Conns)
  25. assert.NotNil(t, conntrack.TimerWheel)
  26. assert.NotNil(t, fw.InRules)
  27. assert.NotNil(t, fw.OutRules)
  28. assert.Equal(t, time.Second, fw.TCPTimeout)
  29. assert.Equal(t, time.Minute, fw.UDPTimeout)
  30. assert.Equal(t, time.Hour, fw.DefaultTimeout)
  31. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  32. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  33. assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
  34. fw = NewFirewall(l, time.Second, time.Hour, time.Minute, c)
  35. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  36. assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
  37. fw = NewFirewall(l, time.Hour, time.Second, time.Minute, c)
  38. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  39. assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
  40. fw = NewFirewall(l, time.Hour, time.Minute, time.Second, c)
  41. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  42. assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
  43. fw = NewFirewall(l, time.Minute, time.Hour, time.Second, c)
  44. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  45. assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
  46. fw = NewFirewall(l, time.Minute, time.Second, time.Hour, c)
  47. assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
  48. assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
  49. }
  50. func TestFirewall_AddRule(t *testing.T) {
  51. l := test.NewLogger()
  52. ob := &bytes.Buffer{}
  53. l.SetOutput(ob)
  54. c := &cert.NebulaCertificate{}
  55. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  56. assert.NotNil(t, fw.InRules)
  57. assert.NotNil(t, fw.OutRules)
  58. _, ti, _ := net.ParseCIDR("1.2.3.4/32")
  59. assert.Nil(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", nil, nil, "", ""))
  60. // An empty rule is any
  61. assert.True(t, fw.InRules.TCP[1].Any.Any)
  62. assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
  63. assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
  64. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  65. assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "", ""))
  66. assert.False(t, fw.InRules.UDP[1].Any.Any)
  67. assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0], "g1")
  68. assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
  69. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  70. assert.Nil(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", nil, nil, "", ""))
  71. assert.False(t, fw.InRules.ICMP[1].Any.Any)
  72. assert.Empty(t, fw.InRules.ICMP[1].Any.Groups)
  73. assert.Contains(t, fw.InRules.ICMP[1].Any.Hosts, "h1")
  74. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  75. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti, nil, "", ""))
  76. assert.False(t, fw.OutRules.AnyProto[1].Any.Any)
  77. assert.Empty(t, fw.OutRules.AnyProto[1].Any.Groups)
  78. assert.Empty(t, fw.OutRules.AnyProto[1].Any.Hosts)
  79. assert.NotNil(t, fw.OutRules.AnyProto[1].Any.CIDR.Match(iputil.Ip2VpnIp(ti.IP)))
  80. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  81. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", nil, ti, "", ""))
  82. assert.False(t, fw.OutRules.AnyProto[1].Any.Any)
  83. assert.Empty(t, fw.OutRules.AnyProto[1].Any.Groups)
  84. assert.Empty(t, fw.OutRules.AnyProto[1].Any.Hosts)
  85. assert.NotNil(t, fw.OutRules.AnyProto[1].Any.LocalCIDR.Match(iputil.Ip2VpnIp(ti.IP)))
  86. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  87. assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "ca-name", ""))
  88. assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
  89. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  90. assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "", "ca-sha"))
  91. assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
  92. // Set any and clear fields
  93. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  94. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"g1", "g2"}, "h1", ti, ti, "", ""))
  95. assert.Equal(t, []string{"g1", "g2"}, fw.OutRules.AnyProto[0].Any.Groups[0])
  96. assert.Contains(t, fw.OutRules.AnyProto[0].Any.Hosts, "h1")
  97. assert.NotNil(t, fw.OutRules.AnyProto[0].Any.CIDR.Match(iputil.Ip2VpnIp(ti.IP)))
  98. assert.NotNil(t, fw.OutRules.AnyProto[0].Any.LocalCIDR.Match(iputil.Ip2VpnIp(ti.IP)))
  99. // run twice just to make sure
  100. //TODO: these ANY rules should clear the CA firewall portion
  101. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
  102. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, nil, "", ""))
  103. assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
  104. assert.Empty(t, fw.OutRules.AnyProto[0].Any.Groups)
  105. assert.Empty(t, fw.OutRules.AnyProto[0].Any.Hosts)
  106. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  107. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, nil, "", ""))
  108. assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
  109. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  110. _, anyIp, _ := net.ParseCIDR("0.0.0.0/0")
  111. assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, nil, "", ""))
  112. assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
  113. // Test error conditions
  114. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  115. assert.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", nil, nil, "", ""))
  116. assert.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", nil, nil, "", ""))
  117. }
  118. func TestFirewall_Drop(t *testing.T) {
  119. l := test.NewLogger()
  120. ob := &bytes.Buffer{}
  121. l.SetOutput(ob)
  122. p := firewall.Packet{
  123. LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  124. RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  125. LocalPort: 10,
  126. RemotePort: 90,
  127. Protocol: firewall.ProtoUDP,
  128. Fragment: false,
  129. }
  130. ipNet := net.IPNet{
  131. IP: net.IPv4(1, 2, 3, 4),
  132. Mask: net.IPMask{255, 255, 255, 0},
  133. }
  134. c := cert.NebulaCertificate{
  135. Details: cert.NebulaCertificateDetails{
  136. Name: "host1",
  137. Ips: []*net.IPNet{&ipNet},
  138. Groups: []string{"default-group"},
  139. InvertedGroups: map[string]struct{}{"default-group": {}},
  140. Issuer: "signer-shasum",
  141. },
  142. }
  143. h := HostInfo{
  144. ConnectionState: &ConnectionState{
  145. peerCert: &c,
  146. },
  147. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  148. }
  149. h.CreateRemoteCIDR(&c)
  150. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  151. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
  152. cp := cert.NewCAPool()
  153. // Drop outbound
  154. assert.Equal(t, fw.Drop([]byte{}, p, false, &h, cp, nil), ErrNoMatchingRule)
  155. // Allow inbound
  156. resetConntrack(fw)
  157. assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
  158. // Allow outbound because conntrack
  159. assert.NoError(t, fw.Drop([]byte{}, p, false, &h, cp, nil))
  160. // test remote mismatch
  161. oldRemote := p.RemoteIP
  162. p.RemoteIP = iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 10))
  163. assert.Equal(t, fw.Drop([]byte{}, p, false, &h, cp, nil), ErrInvalidRemoteIP)
  164. p.RemoteIP = oldRemote
  165. // ensure signer doesn't get in the way of group checks
  166. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  167. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "", "signer-shasum"))
  168. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "", "signer-shasum-bad"))
  169. assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
  170. // test caSha doesn't drop on match
  171. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  172. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "", "signer-shasum-bad"))
  173. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "", "signer-shasum"))
  174. assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
  175. // ensure ca name doesn't get in the way of group checks
  176. cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
  177. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  178. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "ca-good", ""))
  179. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "ca-good-bad", ""))
  180. assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
  181. // test caName doesn't drop on match
  182. cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
  183. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  184. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "ca-good-bad", ""))
  185. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "ca-good", ""))
  186. assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
  187. }
  188. func BenchmarkFirewallTable_match(b *testing.B) {
  189. ft := FirewallTable{
  190. TCP: firewallPort{},
  191. }
  192. _, n, _ := net.ParseCIDR("172.1.1.1/32")
  193. _ = ft.TCP.addRule(10, 10, []string{"good-group"}, "good-host", n, n, "", "")
  194. _ = ft.TCP.addRule(10, 10, []string{"good-group2"}, "good-host", n, n, "", "")
  195. _ = ft.TCP.addRule(10, 10, []string{"good-group3"}, "good-host", n, n, "", "")
  196. _ = ft.TCP.addRule(10, 10, []string{"good-group4"}, "good-host", n, n, "", "")
  197. _ = ft.TCP.addRule(10, 10, []string{"good-group, good-group1"}, "good-host", n, n, "", "")
  198. cp := cert.NewCAPool()
  199. b.Run("fail on proto", func(b *testing.B) {
  200. c := &cert.NebulaCertificate{}
  201. for n := 0; n < b.N; n++ {
  202. ft.match(firewall.Packet{Protocol: firewall.ProtoUDP}, true, c, cp)
  203. }
  204. })
  205. b.Run("fail on port", func(b *testing.B) {
  206. c := &cert.NebulaCertificate{}
  207. for n := 0; n < b.N; n++ {
  208. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 1}, true, c, cp)
  209. }
  210. })
  211. b.Run("fail all group, name, and cidr", func(b *testing.B) {
  212. _, ip, _ := net.ParseCIDR("9.254.254.254/32")
  213. c := &cert.NebulaCertificate{
  214. Details: cert.NebulaCertificateDetails{
  215. InvertedGroups: map[string]struct{}{"nope": {}},
  216. Name: "nope",
  217. Ips: []*net.IPNet{ip},
  218. },
  219. }
  220. for n := 0; n < b.N; n++ {
  221. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
  222. }
  223. })
  224. b.Run("pass on group", func(b *testing.B) {
  225. c := &cert.NebulaCertificate{
  226. Details: cert.NebulaCertificateDetails{
  227. InvertedGroups: map[string]struct{}{"good-group": {}},
  228. Name: "nope",
  229. },
  230. }
  231. for n := 0; n < b.N; n++ {
  232. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
  233. }
  234. })
  235. b.Run("pass on name", func(b *testing.B) {
  236. c := &cert.NebulaCertificate{
  237. Details: cert.NebulaCertificateDetails{
  238. InvertedGroups: map[string]struct{}{"nope": {}},
  239. Name: "good-host",
  240. },
  241. }
  242. for n := 0; n < b.N; n++ {
  243. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
  244. }
  245. })
  246. b.Run("pass on ip", func(b *testing.B) {
  247. ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
  248. c := &cert.NebulaCertificate{
  249. Details: cert.NebulaCertificateDetails{
  250. InvertedGroups: map[string]struct{}{"nope": {}},
  251. Name: "good-host",
  252. },
  253. }
  254. for n := 0; n < b.N; n++ {
  255. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10, RemoteIP: ip}, true, c, cp)
  256. }
  257. })
  258. b.Run("pass on local ip", func(b *testing.B) {
  259. ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
  260. c := &cert.NebulaCertificate{
  261. Details: cert.NebulaCertificateDetails{
  262. InvertedGroups: map[string]struct{}{"nope": {}},
  263. Name: "good-host",
  264. },
  265. }
  266. for n := 0; n < b.N; n++ {
  267. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10, LocalIP: ip}, true, c, cp)
  268. }
  269. })
  270. _ = ft.TCP.addRule(0, 0, []string{"good-group"}, "good-host", n, n, "", "")
  271. b.Run("pass on ip with any port", func(b *testing.B) {
  272. ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
  273. c := &cert.NebulaCertificate{
  274. Details: cert.NebulaCertificateDetails{
  275. InvertedGroups: map[string]struct{}{"nope": {}},
  276. Name: "good-host",
  277. },
  278. }
  279. for n := 0; n < b.N; n++ {
  280. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, RemoteIP: ip}, true, c, cp)
  281. }
  282. })
  283. b.Run("pass on local ip with any port", func(b *testing.B) {
  284. ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
  285. c := &cert.NebulaCertificate{
  286. Details: cert.NebulaCertificateDetails{
  287. InvertedGroups: map[string]struct{}{"nope": {}},
  288. Name: "good-host",
  289. },
  290. }
  291. for n := 0; n < b.N; n++ {
  292. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalIP: ip}, true, c, cp)
  293. }
  294. })
  295. }
  296. func TestFirewall_Drop2(t *testing.T) {
  297. l := test.NewLogger()
  298. ob := &bytes.Buffer{}
  299. l.SetOutput(ob)
  300. p := firewall.Packet{
  301. LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  302. RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  303. LocalPort: 10,
  304. RemotePort: 90,
  305. Protocol: firewall.ProtoUDP,
  306. Fragment: false,
  307. }
  308. ipNet := net.IPNet{
  309. IP: net.IPv4(1, 2, 3, 4),
  310. Mask: net.IPMask{255, 255, 255, 0},
  311. }
  312. c := cert.NebulaCertificate{
  313. Details: cert.NebulaCertificateDetails{
  314. Name: "host1",
  315. Ips: []*net.IPNet{&ipNet},
  316. InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
  317. },
  318. }
  319. h := HostInfo{
  320. ConnectionState: &ConnectionState{
  321. peerCert: &c,
  322. },
  323. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  324. }
  325. h.CreateRemoteCIDR(&c)
  326. c1 := cert.NebulaCertificate{
  327. Details: cert.NebulaCertificateDetails{
  328. Name: "host1",
  329. Ips: []*net.IPNet{&ipNet},
  330. InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
  331. },
  332. }
  333. h1 := HostInfo{
  334. ConnectionState: &ConnectionState{
  335. peerCert: &c1,
  336. },
  337. }
  338. h1.CreateRemoteCIDR(&c1)
  339. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  340. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", nil, nil, "", ""))
  341. cp := cert.NewCAPool()
  342. // h1/c1 lacks the proper groups
  343. assert.Error(t, fw.Drop([]byte{}, p, true, &h1, cp, nil), ErrNoMatchingRule)
  344. // c has the proper groups
  345. resetConntrack(fw)
  346. assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
  347. }
  348. func TestFirewall_Drop3(t *testing.T) {
  349. l := test.NewLogger()
  350. ob := &bytes.Buffer{}
  351. l.SetOutput(ob)
  352. p := firewall.Packet{
  353. LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  354. RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  355. LocalPort: 1,
  356. RemotePort: 1,
  357. Protocol: firewall.ProtoUDP,
  358. Fragment: false,
  359. }
  360. ipNet := net.IPNet{
  361. IP: net.IPv4(1, 2, 3, 4),
  362. Mask: net.IPMask{255, 255, 255, 0},
  363. }
  364. c := cert.NebulaCertificate{
  365. Details: cert.NebulaCertificateDetails{
  366. Name: "host-owner",
  367. Ips: []*net.IPNet{&ipNet},
  368. },
  369. }
  370. c1 := cert.NebulaCertificate{
  371. Details: cert.NebulaCertificateDetails{
  372. Name: "host1",
  373. Ips: []*net.IPNet{&ipNet},
  374. Issuer: "signer-sha-bad",
  375. },
  376. }
  377. h1 := HostInfo{
  378. ConnectionState: &ConnectionState{
  379. peerCert: &c1,
  380. },
  381. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  382. }
  383. h1.CreateRemoteCIDR(&c1)
  384. c2 := cert.NebulaCertificate{
  385. Details: cert.NebulaCertificateDetails{
  386. Name: "host2",
  387. Ips: []*net.IPNet{&ipNet},
  388. Issuer: "signer-sha",
  389. },
  390. }
  391. h2 := HostInfo{
  392. ConnectionState: &ConnectionState{
  393. peerCert: &c2,
  394. },
  395. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  396. }
  397. h2.CreateRemoteCIDR(&c2)
  398. c3 := cert.NebulaCertificate{
  399. Details: cert.NebulaCertificateDetails{
  400. Name: "host3",
  401. Ips: []*net.IPNet{&ipNet},
  402. Issuer: "signer-sha-bad",
  403. },
  404. }
  405. h3 := HostInfo{
  406. ConnectionState: &ConnectionState{
  407. peerCert: &c3,
  408. },
  409. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  410. }
  411. h3.CreateRemoteCIDR(&c3)
  412. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  413. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", nil, nil, "", ""))
  414. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", nil, nil, "", "signer-sha"))
  415. cp := cert.NewCAPool()
  416. // c1 should pass because host match
  417. assert.NoError(t, fw.Drop([]byte{}, p, true, &h1, cp, nil))
  418. // c2 should pass because ca sha match
  419. resetConntrack(fw)
  420. assert.NoError(t, fw.Drop([]byte{}, p, true, &h2, cp, nil))
  421. // c3 should fail because no match
  422. resetConntrack(fw)
  423. assert.Equal(t, fw.Drop([]byte{}, p, true, &h3, cp, nil), ErrNoMatchingRule)
  424. }
  425. func TestFirewall_DropConntrackReload(t *testing.T) {
  426. l := test.NewLogger()
  427. ob := &bytes.Buffer{}
  428. l.SetOutput(ob)
  429. p := firewall.Packet{
  430. LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  431. RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
  432. LocalPort: 10,
  433. RemotePort: 90,
  434. Protocol: firewall.ProtoUDP,
  435. Fragment: false,
  436. }
  437. ipNet := net.IPNet{
  438. IP: net.IPv4(1, 2, 3, 4),
  439. Mask: net.IPMask{255, 255, 255, 0},
  440. }
  441. c := cert.NebulaCertificate{
  442. Details: cert.NebulaCertificateDetails{
  443. Name: "host1",
  444. Ips: []*net.IPNet{&ipNet},
  445. Groups: []string{"default-group"},
  446. InvertedGroups: map[string]struct{}{"default-group": {}},
  447. Issuer: "signer-shasum",
  448. },
  449. }
  450. h := HostInfo{
  451. ConnectionState: &ConnectionState{
  452. peerCert: &c,
  453. },
  454. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  455. }
  456. h.CreateRemoteCIDR(&c)
  457. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  458. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
  459. cp := cert.NewCAPool()
  460. // Drop outbound
  461. assert.Equal(t, fw.Drop([]byte{}, p, false, &h, cp, nil), ErrNoMatchingRule)
  462. // Allow inbound
  463. resetConntrack(fw)
  464. assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
  465. // Allow outbound because conntrack
  466. assert.NoError(t, fw.Drop([]byte{}, p, false, &h, cp, nil))
  467. oldFw := fw
  468. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  469. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", nil, nil, "", ""))
  470. fw.Conntrack = oldFw.Conntrack
  471. fw.rulesVersion = oldFw.rulesVersion + 1
  472. // Allow outbound because conntrack and new rules allow port 10
  473. assert.NoError(t, fw.Drop([]byte{}, p, false, &h, cp, nil))
  474. oldFw = fw
  475. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  476. assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", nil, nil, "", ""))
  477. fw.Conntrack = oldFw.Conntrack
  478. fw.rulesVersion = oldFw.rulesVersion + 1
  479. // Drop outbound because conntrack doesn't match new ruleset
  480. assert.Equal(t, fw.Drop([]byte{}, p, false, &h, cp, nil), ErrNoMatchingRule)
  481. }
  482. func BenchmarkLookup(b *testing.B) {
  483. ml := func(m map[string]struct{}, a [][]string) {
  484. for n := 0; n < b.N; n++ {
  485. for _, sg := range a {
  486. found := false
  487. for _, g := range sg {
  488. if _, ok := m[g]; !ok {
  489. found = false
  490. break
  491. }
  492. found = true
  493. }
  494. if found {
  495. return
  496. }
  497. }
  498. }
  499. }
  500. b.Run("array to map best", func(b *testing.B) {
  501. m := map[string]struct{}{
  502. "1ne": {},
  503. "2wo": {},
  504. "3hr": {},
  505. "4ou": {},
  506. "5iv": {},
  507. "6ix": {},
  508. }
  509. a := [][]string{
  510. {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
  511. {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
  512. {"one", "two", "3hr", "4ou", "5iv", "6ix"},
  513. {"one", "two", "thr", "4ou", "5iv", "6ix"},
  514. {"one", "two", "thr", "fou", "5iv", "6ix"},
  515. {"one", "two", "thr", "fou", "fiv", "6ix"},
  516. {"one", "two", "thr", "fou", "fiv", "six"},
  517. }
  518. for n := 0; n < b.N; n++ {
  519. ml(m, a)
  520. }
  521. })
  522. b.Run("array to map worst", func(b *testing.B) {
  523. m := map[string]struct{}{
  524. "one": {},
  525. "two": {},
  526. "thr": {},
  527. "fou": {},
  528. "fiv": {},
  529. "six": {},
  530. }
  531. a := [][]string{
  532. {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
  533. {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
  534. {"one", "two", "3hr", "4ou", "5iv", "6ix"},
  535. {"one", "two", "thr", "4ou", "5iv", "6ix"},
  536. {"one", "two", "thr", "fou", "5iv", "6ix"},
  537. {"one", "two", "thr", "fou", "fiv", "6ix"},
  538. {"one", "two", "thr", "fou", "fiv", "six"},
  539. }
  540. for n := 0; n < b.N; n++ {
  541. ml(m, a)
  542. }
  543. })
  544. //TODO: only way array lookup in array will help is if both are sorted, then maybe it's faster
  545. }
  546. func Test_parsePort(t *testing.T) {
  547. _, _, err := parsePort("")
  548. assert.EqualError(t, err, "was not a number; ``")
  549. _, _, err = parsePort(" ")
  550. assert.EqualError(t, err, "was not a number; ` `")
  551. _, _, err = parsePort("-")
  552. assert.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
  553. _, _, err = parsePort(" - ")
  554. assert.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
  555. _, _, err = parsePort("a-b")
  556. assert.EqualError(t, err, "beginning range was not a number; `a`")
  557. _, _, err = parsePort("1-b")
  558. assert.EqualError(t, err, "ending range was not a number; `b`")
  559. s, e, err := parsePort(" 1 - 2 ")
  560. assert.Equal(t, int32(1), s)
  561. assert.Equal(t, int32(2), e)
  562. assert.Nil(t, err)
  563. s, e, err = parsePort("0-1")
  564. assert.Equal(t, int32(0), s)
  565. assert.Equal(t, int32(0), e)
  566. assert.Nil(t, err)
  567. s, e, err = parsePort("9919")
  568. assert.Equal(t, int32(9919), s)
  569. assert.Equal(t, int32(9919), e)
  570. assert.Nil(t, err)
  571. s, e, err = parsePort("any")
  572. assert.Equal(t, int32(0), s)
  573. assert.Equal(t, int32(0), e)
  574. assert.Nil(t, err)
  575. }
  576. func TestNewFirewallFromConfig(t *testing.T) {
  577. l := test.NewLogger()
  578. // Test a bad rule definition
  579. c := &cert.NebulaCertificate{}
  580. conf := config.NewC(l)
  581. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": "asdf"}
  582. _, err := NewFirewallFromConfig(l, c, conf)
  583. assert.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
  584. // Test both port and code
  585. conf = config.NewC(l)
  586. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "code": "2"}}}
  587. _, err = NewFirewallFromConfig(l, c, conf)
  588. assert.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided")
  589. // Test missing host, group, cidr, ca_name and ca_sha
  590. conf = config.NewC(l)
  591. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{}}}
  592. _, err = NewFirewallFromConfig(l, c, conf)
  593. assert.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, local_cidr, ca_name, or ca_sha must be provided")
  594. // Test code/port error
  595. conf = config.NewC(l)
  596. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "a", "host": "testh"}}}
  597. _, err = NewFirewallFromConfig(l, c, conf)
  598. assert.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
  599. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "a", "host": "testh"}}}
  600. _, err = NewFirewallFromConfig(l, c, conf)
  601. assert.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
  602. // Test proto error
  603. conf = config.NewC(l)
  604. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "host": "testh"}}}
  605. _, err = NewFirewallFromConfig(l, c, conf)
  606. assert.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
  607. // Test cidr parse error
  608. conf = config.NewC(l)
  609. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "cidr": "testh", "proto": "any"}}}
  610. _, err = NewFirewallFromConfig(l, c, conf)
  611. assert.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; invalid CIDR address: testh")
  612. // Test local_cidr parse error
  613. conf = config.NewC(l)
  614. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "local_cidr": "testh", "proto": "any"}}}
  615. _, err = NewFirewallFromConfig(l, c, conf)
  616. assert.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; invalid CIDR address: testh")
  617. // Test both group and groups
  618. conf = config.NewC(l)
  619. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
  620. _, err = NewFirewallFromConfig(l, c, conf)
  621. assert.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
  622. }
  623. func TestAddFirewallRulesFromConfig(t *testing.T) {
  624. l := test.NewLogger()
  625. // Test adding tcp rule
  626. conf := config.NewC(l)
  627. mf := &mockFirewall{}
  628. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "tcp", "host": "a"}}}
  629. assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  630. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
  631. // Test adding udp rule
  632. conf = config.NewC(l)
  633. mf = &mockFirewall{}
  634. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "udp", "host": "a"}}}
  635. assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  636. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
  637. // Test adding icmp rule
  638. conf = config.NewC(l)
  639. mf = &mockFirewall{}
  640. conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "icmp", "host": "a"}}}
  641. assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  642. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
  643. // Test adding any rule
  644. conf = config.NewC(l)
  645. mf = &mockFirewall{}
  646. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
  647. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  648. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
  649. // Test adding rule with cidr
  650. cidr := &net.IPNet{IP: net.ParseIP("10.0.0.0").To4(), Mask: net.IPv4Mask(255, 0, 0, 0)}
  651. conf = config.NewC(l)
  652. mf = &mockFirewall{}
  653. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "cidr": cidr.String()}}}
  654. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  655. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr, localIp: nil}, mf.lastCall)
  656. // Test adding rule with local_cidr
  657. conf = config.NewC(l)
  658. mf = &mockFirewall{}
  659. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "local_cidr": cidr.String()}}}
  660. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  661. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: cidr}, mf.lastCall)
  662. // Test adding rule with ca_sha
  663. conf = config.NewC(l)
  664. mf = &mockFirewall{}
  665. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
  666. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  667. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: nil, caSha: "12312313123"}, mf.lastCall)
  668. // Test adding rule with ca_name
  669. conf = config.NewC(l)
  670. mf = &mockFirewall{}
  671. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_name": "root01"}}}
  672. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  673. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: nil, caName: "root01"}, mf.lastCall)
  674. // Test single group
  675. conf = config.NewC(l)
  676. mf = &mockFirewall{}
  677. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a"}}}
  678. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  679. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil, localIp: nil}, mf.lastCall)
  680. // Test single groups
  681. conf = config.NewC(l)
  682. mf = &mockFirewall{}
  683. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": "a"}}}
  684. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  685. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil, localIp: nil}, mf.lastCall)
  686. // Test multiple AND groups
  687. conf = config.NewC(l)
  688. mf = &mockFirewall{}
  689. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
  690. assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  691. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: nil, localIp: nil}, mf.lastCall)
  692. // Test Add error
  693. conf = config.NewC(l)
  694. mf = &mockFirewall{}
  695. mf.nextCallReturn = errors.New("test error")
  696. conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
  697. assert.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
  698. }
  699. func TestTCPRTTTracking(t *testing.T) {
  700. b := make([]byte, 200)
  701. // Max ip IHL (60 bytes) and tcp IHL (60 bytes)
  702. b[0] = 15
  703. b[60+12] = 15 << 4
  704. f := Firewall{
  705. metricTCPRTT: metrics.GetOrRegisterHistogram("nope", nil, metrics.NewExpDecaySample(1028, 0.015)),
  706. }
  707. // Set SEQ to 1
  708. binary.BigEndian.PutUint32(b[60+4:60+8], 1)
  709. c := &conn{}
  710. setTCPRTTTracking(c, b)
  711. assert.Equal(t, uint32(1), c.Seq)
  712. // Bad ack - no ack flag
  713. binary.BigEndian.PutUint32(b[60+8:60+12], 80)
  714. assert.False(t, f.checkTCPRTT(c, b))
  715. // Bad ack, number is too low
  716. binary.BigEndian.PutUint32(b[60+8:60+12], 0)
  717. b[60+13] = uint8(0x10)
  718. assert.False(t, f.checkTCPRTT(c, b))
  719. // Good ack
  720. binary.BigEndian.PutUint32(b[60+8:60+12], 80)
  721. assert.True(t, f.checkTCPRTT(c, b))
  722. assert.Equal(t, uint32(0), c.Seq)
  723. // Set SEQ to 1
  724. binary.BigEndian.PutUint32(b[60+4:60+8], 1)
  725. c = &conn{}
  726. setTCPRTTTracking(c, b)
  727. assert.Equal(t, uint32(1), c.Seq)
  728. // Good acks
  729. binary.BigEndian.PutUint32(b[60+8:60+12], 81)
  730. assert.True(t, f.checkTCPRTT(c, b))
  731. assert.Equal(t, uint32(0), c.Seq)
  732. // Set SEQ to max uint32 - 20
  733. binary.BigEndian.PutUint32(b[60+4:60+8], ^uint32(0)-20)
  734. c = &conn{}
  735. setTCPRTTTracking(c, b)
  736. assert.Equal(t, ^uint32(0)-20, c.Seq)
  737. // Good acks
  738. binary.BigEndian.PutUint32(b[60+8:60+12], 81)
  739. assert.True(t, f.checkTCPRTT(c, b))
  740. assert.Equal(t, uint32(0), c.Seq)
  741. // Set SEQ to max uint32 / 2
  742. binary.BigEndian.PutUint32(b[60+4:60+8], ^uint32(0)/2)
  743. c = &conn{}
  744. setTCPRTTTracking(c, b)
  745. assert.Equal(t, ^uint32(0)/2, c.Seq)
  746. // Below
  747. binary.BigEndian.PutUint32(b[60+8:60+12], ^uint32(0)/2-1)
  748. assert.False(t, f.checkTCPRTT(c, b))
  749. assert.Equal(t, ^uint32(0)/2, c.Seq)
  750. // Halfway below
  751. binary.BigEndian.PutUint32(b[60+8:60+12], uint32(0))
  752. assert.False(t, f.checkTCPRTT(c, b))
  753. assert.Equal(t, ^uint32(0)/2, c.Seq)
  754. // Halfway above is ok
  755. binary.BigEndian.PutUint32(b[60+8:60+12], ^uint32(0))
  756. assert.True(t, f.checkTCPRTT(c, b))
  757. assert.Equal(t, uint32(0), c.Seq)
  758. // Set SEQ to max uint32
  759. binary.BigEndian.PutUint32(b[60+4:60+8], ^uint32(0))
  760. c = &conn{}
  761. setTCPRTTTracking(c, b)
  762. assert.Equal(t, ^uint32(0), c.Seq)
  763. // Halfway + 1 above
  764. binary.BigEndian.PutUint32(b[60+8:60+12], ^uint32(0)/2+1)
  765. assert.False(t, f.checkTCPRTT(c, b))
  766. assert.Equal(t, ^uint32(0), c.Seq)
  767. // Halfway above
  768. binary.BigEndian.PutUint32(b[60+8:60+12], ^uint32(0)/2)
  769. assert.True(t, f.checkTCPRTT(c, b))
  770. assert.Equal(t, uint32(0), c.Seq)
  771. }
  772. func TestFirewall_convertRule(t *testing.T) {
  773. l := test.NewLogger()
  774. ob := &bytes.Buffer{}
  775. l.SetOutput(ob)
  776. // Ensure group array of 1 is converted and a warning is printed
  777. c := map[interface{}]interface{}{
  778. "group": []interface{}{"group1"},
  779. }
  780. r, err := convertRule(l, c, "test", 1)
  781. assert.Contains(t, ob.String(), "test rule #1; group was an array with a single value, converting to simple value")
  782. assert.Nil(t, err)
  783. assert.Equal(t, "group1", r.Group)
  784. // Ensure group array of > 1 is errord
  785. ob.Reset()
  786. c = map[interface{}]interface{}{
  787. "group": []interface{}{"group1", "group2"},
  788. }
  789. r, err = convertRule(l, c, "test", 1)
  790. assert.Equal(t, "", ob.String())
  791. assert.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
  792. // Make sure a well formed group is alright
  793. ob.Reset()
  794. c = map[interface{}]interface{}{
  795. "group": "group1",
  796. }
  797. r, err = convertRule(l, c, "test", 1)
  798. assert.Nil(t, err)
  799. assert.Equal(t, "group1", r.Group)
  800. }
  801. type addRuleCall struct {
  802. incoming bool
  803. proto uint8
  804. startPort int32
  805. endPort int32
  806. groups []string
  807. host string
  808. ip *net.IPNet
  809. localIp *net.IPNet
  810. caName string
  811. caSha string
  812. }
  813. type mockFirewall struct {
  814. lastCall addRuleCall
  815. nextCallReturn error
  816. }
  817. func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip *net.IPNet, localIp *net.IPNet, caName string, caSha string) error {
  818. mf.lastCall = addRuleCall{
  819. incoming: incoming,
  820. proto: proto,
  821. startPort: startPort,
  822. endPort: endPort,
  823. groups: groups,
  824. host: host,
  825. ip: ip,
  826. localIp: localIp,
  827. caName: caName,
  828. caSha: caSha,
  829. }
  830. err := mf.nextCallReturn
  831. mf.nextCallReturn = nil
  832. return err
  833. }
  834. func resetConntrack(fw *Firewall) {
  835. fw.Conntrack.Lock()
  836. fw.Conntrack.Conns = map[firewall.Packet]*conn{}
  837. fw.Conntrack.Unlock()
  838. }