firewall_test.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. package nebula
  2. import (
  3. "bytes"
  4. "errors"
  5. "math"
  6. "net/netip"
  7. "testing"
  8. "time"
  9. "github.com/gaissmai/bart"
  10. "github.com/sirupsen/logrus"
  11. "github.com/slackhq/nebula/cert"
  12. "github.com/slackhq/nebula/config"
  13. "github.com/slackhq/nebula/firewall"
  14. "github.com/slackhq/nebula/test"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. )
  18. func TestNewFirewall(t *testing.T) {
  19. l := test.NewLogger()
  20. c := &dummyCert{}
  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 := &dummyCert{}
  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, err := netip.ParsePrefix("1.2.3.4/32")
  59. require.NoError(t, err)
  60. ti6, err := netip.ParsePrefix("fd12::34/128")
  61. require.NoError(t, err)
  62. require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", "", "", "", ""))
  63. // An empty rule is any
  64. assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
  65. assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
  66. assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
  67. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  68. require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", ""))
  69. assert.Nil(t, fw.InRules.UDP[1].Any.Any)
  70. assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0].Groups, "g1")
  71. assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
  72. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  73. require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", "", "", "", ""))
  74. //no matter what port is given for icmp, it should end up as "any"
  75. assert.Nil(t, fw.InRules.ICMP[firewall.PortAny].Any.Any)
  76. assert.Empty(t, fw.InRules.ICMP[firewall.PortAny].Any.Groups)
  77. assert.Contains(t, fw.InRules.ICMP[firewall.PortAny].Any.Hosts, "h1")
  78. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  79. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti.String(), "", "", ""))
  80. assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
  81. _, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti)
  82. assert.True(t, ok)
  83. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  84. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti6.String(), "", "", ""))
  85. assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
  86. _, ok = fw.OutRules.AnyProto[1].Any.CIDR.Get(ti6)
  87. assert.True(t, ok)
  88. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  89. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti.String(), "", ""))
  90. assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
  91. ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti)
  92. assert.True(t, ok)
  93. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  94. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti6.String(), "", ""))
  95. assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
  96. ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti6)
  97. assert.True(t, ok)
  98. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  99. require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", ""))
  100. assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
  101. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  102. require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha"))
  103. assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
  104. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  105. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", "", "", "", ""))
  106. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  107. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  108. anyIp, err := netip.ParsePrefix("0.0.0.0/0")
  109. require.NoError(t, err)
  110. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp.String(), "", "", ""))
  111. assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
  112. table, ok := fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
  113. assert.True(t, table.Any)
  114. table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
  115. assert.False(t, ok)
  116. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  117. anyIp6, err := netip.ParsePrefix("::/0")
  118. require.NoError(t, err)
  119. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp6.String(), "", "", ""))
  120. assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
  121. table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
  122. assert.True(t, table.Any)
  123. table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
  124. assert.False(t, ok)
  125. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  126. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "any", "", "", ""))
  127. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  128. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  129. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp.String(), "", ""))
  130. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  131. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
  132. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
  133. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  134. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp6.String(), "", ""))
  135. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  136. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
  137. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
  138. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  139. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", "any", "", ""))
  140. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  141. // Test error conditions
  142. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  143. require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", "", "", "", ""))
  144. require.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", "", "", "", ""))
  145. }
  146. func TestFirewall_Drop(t *testing.T) {
  147. l := test.NewLogger()
  148. ob := &bytes.Buffer{}
  149. l.SetOutput(ob)
  150. myVpnNetworksTable := new(bart.Lite)
  151. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  152. p := firewall.Packet{
  153. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  154. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  155. LocalPort: 10,
  156. RemotePort: 90,
  157. Protocol: firewall.ProtoUDP,
  158. Fragment: false,
  159. }
  160. c := dummyCert{
  161. name: "host1",
  162. networks: []netip.Prefix{netip.MustParsePrefix("1.2.3.4/24")},
  163. groups: []string{"default-group"},
  164. issuer: "signer-shasum",
  165. }
  166. h := HostInfo{
  167. ConnectionState: &ConnectionState{
  168. peerCert: &cert.CachedCertificate{
  169. Certificate: &c,
  170. InvertedGroups: map[string]struct{}{"default-group": {}},
  171. },
  172. },
  173. vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")},
  174. }
  175. h.buildNetworks(myVpnNetworksTable, &c)
  176. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  177. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  178. cp := cert.NewCAPool()
  179. // Drop outbound
  180. assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
  181. // Allow inbound
  182. resetConntrack(fw)
  183. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  184. // Allow outbound because conntrack
  185. require.NoError(t, fw.Drop(p, false, &h, cp, nil))
  186. // test remote mismatch
  187. oldRemote := p.RemoteAddr
  188. p.RemoteAddr = netip.MustParseAddr("1.2.3.10")
  189. assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
  190. p.RemoteAddr = oldRemote
  191. // ensure signer doesn't get in the way of group checks
  192. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  193. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
  194. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
  195. assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
  196. // test caSha doesn't drop on match
  197. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  198. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
  199. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
  200. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  201. // ensure ca name doesn't get in the way of group checks
  202. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  203. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  204. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
  205. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
  206. assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
  207. // test caName doesn't drop on match
  208. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  209. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  210. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
  211. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
  212. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  213. }
  214. func TestFirewall_DropV6(t *testing.T) {
  215. l := test.NewLogger()
  216. ob := &bytes.Buffer{}
  217. l.SetOutput(ob)
  218. myVpnNetworksTable := new(bart.Lite)
  219. myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
  220. p := firewall.Packet{
  221. LocalAddr: netip.MustParseAddr("fd12::34"),
  222. RemoteAddr: netip.MustParseAddr("fd12::34"),
  223. LocalPort: 10,
  224. RemotePort: 90,
  225. Protocol: firewall.ProtoUDP,
  226. Fragment: false,
  227. }
  228. c := dummyCert{
  229. name: "host1",
  230. networks: []netip.Prefix{netip.MustParsePrefix("fd12::34/120")},
  231. groups: []string{"default-group"},
  232. issuer: "signer-shasum",
  233. }
  234. h := HostInfo{
  235. ConnectionState: &ConnectionState{
  236. peerCert: &cert.CachedCertificate{
  237. Certificate: &c,
  238. InvertedGroups: map[string]struct{}{"default-group": {}},
  239. },
  240. },
  241. vpnAddrs: []netip.Addr{netip.MustParseAddr("fd12::34")},
  242. }
  243. h.buildNetworks(myVpnNetworksTable, &c)
  244. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  245. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  246. cp := cert.NewCAPool()
  247. // Drop outbound
  248. assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
  249. // Allow inbound
  250. resetConntrack(fw)
  251. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  252. // Allow outbound because conntrack
  253. require.NoError(t, fw.Drop(p, false, &h, cp, nil))
  254. // test remote mismatch
  255. oldRemote := p.RemoteAddr
  256. p.RemoteAddr = netip.MustParseAddr("fd12::56")
  257. assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
  258. p.RemoteAddr = oldRemote
  259. // ensure signer doesn't get in the way of group checks
  260. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  261. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
  262. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
  263. assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
  264. // test caSha doesn't drop on match
  265. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  266. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
  267. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
  268. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  269. // ensure ca name doesn't get in the way of group checks
  270. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  271. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  272. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
  273. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
  274. assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
  275. // test caName doesn't drop on match
  276. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  277. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  278. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
  279. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
  280. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  281. }
  282. func BenchmarkFirewallTable_match(b *testing.B) {
  283. f := &Firewall{}
  284. ft := FirewallTable{
  285. TCP: firewallPort{},
  286. }
  287. pfix := netip.MustParsePrefix("172.1.1.1/32")
  288. _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix.String(), "", "", "")
  289. _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix.String(), "", "")
  290. pfix6 := netip.MustParsePrefix("fd11::11/128")
  291. _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix6.String(), "", "", "")
  292. _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix6.String(), "", "")
  293. cp := cert.NewCAPool()
  294. b.Run("fail on proto", func(b *testing.B) {
  295. // This benchmark is showing us the cost of failing to match the protocol
  296. c := &cert.CachedCertificate{
  297. Certificate: &dummyCert{},
  298. }
  299. for n := 0; n < b.N; n++ {
  300. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoUDP}, true, c, cp))
  301. }
  302. })
  303. b.Run("pass proto, fail on port", func(b *testing.B) {
  304. // This benchmark is showing us the cost of matching a specific protocol but failing to match the port
  305. c := &cert.CachedCertificate{
  306. Certificate: &dummyCert{},
  307. }
  308. for n := 0; n < b.N; n++ {
  309. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 1}, true, c, cp))
  310. }
  311. })
  312. b.Run("pass proto, port, fail on local CIDR", func(b *testing.B) {
  313. c := &cert.CachedCertificate{
  314. Certificate: &dummyCert{},
  315. }
  316. ip := netip.MustParsePrefix("9.254.254.254/32")
  317. for n := 0; n < b.N; n++ {
  318. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
  319. }
  320. })
  321. b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) {
  322. c := &cert.CachedCertificate{
  323. Certificate: &dummyCert{},
  324. }
  325. ip := netip.MustParsePrefix("fd99::99/128")
  326. for n := 0; n < b.N; n++ {
  327. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
  328. }
  329. })
  330. b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) {
  331. c := &cert.CachedCertificate{
  332. Certificate: &dummyCert{
  333. name: "nope",
  334. networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
  335. },
  336. InvertedGroups: map[string]struct{}{"nope": {}},
  337. }
  338. for n := 0; n < b.N; n++ {
  339. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
  340. }
  341. })
  342. b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
  343. c := &cert.CachedCertificate{
  344. Certificate: &dummyCert{
  345. name: "nope",
  346. networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
  347. },
  348. InvertedGroups: map[string]struct{}{"nope": {}},
  349. }
  350. for n := 0; n < b.N; n++ {
  351. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
  352. }
  353. })
  354. b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
  355. c := &cert.CachedCertificate{
  356. Certificate: &dummyCert{
  357. name: "nope",
  358. networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
  359. },
  360. InvertedGroups: map[string]struct{}{"nope": {}},
  361. }
  362. for n := 0; n < b.N; n++ {
  363. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
  364. }
  365. })
  366. b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
  367. c := &cert.CachedCertificate{
  368. Certificate: &dummyCert{
  369. name: "nope",
  370. networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
  371. },
  372. InvertedGroups: map[string]struct{}{"nope": {}},
  373. }
  374. for n := 0; n < b.N; n++ {
  375. assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
  376. }
  377. })
  378. b.Run("pass on group on any local cidr", func(b *testing.B) {
  379. c := &cert.CachedCertificate{
  380. Certificate: &dummyCert{
  381. name: "nope",
  382. },
  383. InvertedGroups: map[string]struct{}{"good-group": {}},
  384. }
  385. for n := 0; n < b.N; n++ {
  386. assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
  387. }
  388. })
  389. b.Run("pass on group on specific local cidr", func(b *testing.B) {
  390. c := &cert.CachedCertificate{
  391. Certificate: &dummyCert{
  392. name: "nope",
  393. },
  394. InvertedGroups: map[string]struct{}{"good-group": {}},
  395. }
  396. for n := 0; n < b.N; n++ {
  397. assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
  398. }
  399. })
  400. b.Run("pass on group on specific local cidr6", func(b *testing.B) {
  401. c := &cert.CachedCertificate{
  402. Certificate: &dummyCert{
  403. name: "nope",
  404. },
  405. InvertedGroups: map[string]struct{}{"good-group": {}},
  406. }
  407. for n := 0; n < b.N; n++ {
  408. assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
  409. }
  410. })
  411. b.Run("pass on name", func(b *testing.B) {
  412. c := &cert.CachedCertificate{
  413. Certificate: &dummyCert{
  414. name: "good-host",
  415. },
  416. InvertedGroups: map[string]struct{}{"nope": {}},
  417. }
  418. for n := 0; n < b.N; n++ {
  419. ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
  420. }
  421. })
  422. }
  423. func TestFirewall_Drop2(t *testing.T) {
  424. l := test.NewLogger()
  425. ob := &bytes.Buffer{}
  426. l.SetOutput(ob)
  427. myVpnNetworksTable := new(bart.Lite)
  428. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  429. p := firewall.Packet{
  430. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  431. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  432. LocalPort: 10,
  433. RemotePort: 90,
  434. Protocol: firewall.ProtoUDP,
  435. Fragment: false,
  436. }
  437. network := netip.MustParsePrefix("1.2.3.4/24")
  438. c := cert.CachedCertificate{
  439. Certificate: &dummyCert{
  440. name: "host1",
  441. networks: []netip.Prefix{network},
  442. },
  443. InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
  444. }
  445. h := HostInfo{
  446. ConnectionState: &ConnectionState{
  447. peerCert: &c,
  448. },
  449. vpnAddrs: []netip.Addr{network.Addr()},
  450. }
  451. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  452. c1 := cert.CachedCertificate{
  453. Certificate: &dummyCert{
  454. name: "host1",
  455. networks: []netip.Prefix{network},
  456. },
  457. InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
  458. }
  459. h1 := HostInfo{
  460. vpnAddrs: []netip.Addr{network.Addr()},
  461. ConnectionState: &ConnectionState{
  462. peerCert: &c1,
  463. },
  464. }
  465. h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
  466. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  467. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", "", "", "", ""))
  468. cp := cert.NewCAPool()
  469. // h1/c1 lacks the proper groups
  470. require.ErrorIs(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule)
  471. // c has the proper groups
  472. resetConntrack(fw)
  473. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  474. }
  475. func TestFirewall_Drop3(t *testing.T) {
  476. l := test.NewLogger()
  477. ob := &bytes.Buffer{}
  478. l.SetOutput(ob)
  479. myVpnNetworksTable := new(bart.Lite)
  480. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  481. p := firewall.Packet{
  482. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  483. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  484. LocalPort: 1,
  485. RemotePort: 1,
  486. Protocol: firewall.ProtoUDP,
  487. Fragment: false,
  488. }
  489. network := netip.MustParsePrefix("1.2.3.4/24")
  490. c := cert.CachedCertificate{
  491. Certificate: &dummyCert{
  492. name: "host-owner",
  493. networks: []netip.Prefix{network},
  494. },
  495. }
  496. c1 := cert.CachedCertificate{
  497. Certificate: &dummyCert{
  498. name: "host1",
  499. networks: []netip.Prefix{network},
  500. issuer: "signer-sha-bad",
  501. },
  502. }
  503. h1 := HostInfo{
  504. ConnectionState: &ConnectionState{
  505. peerCert: &c1,
  506. },
  507. vpnAddrs: []netip.Addr{network.Addr()},
  508. }
  509. h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
  510. c2 := cert.CachedCertificate{
  511. Certificate: &dummyCert{
  512. name: "host2",
  513. networks: []netip.Prefix{network},
  514. issuer: "signer-sha",
  515. },
  516. }
  517. h2 := HostInfo{
  518. ConnectionState: &ConnectionState{
  519. peerCert: &c2,
  520. },
  521. vpnAddrs: []netip.Addr{network.Addr()},
  522. }
  523. h2.buildNetworks(myVpnNetworksTable, c2.Certificate)
  524. c3 := cert.CachedCertificate{
  525. Certificate: &dummyCert{
  526. name: "host3",
  527. networks: []netip.Prefix{network},
  528. issuer: "signer-sha-bad",
  529. },
  530. }
  531. h3 := HostInfo{
  532. ConnectionState: &ConnectionState{
  533. peerCert: &c3,
  534. },
  535. vpnAddrs: []netip.Addr{network.Addr()},
  536. }
  537. h3.buildNetworks(myVpnNetworksTable, c3.Certificate)
  538. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  539. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", "", "", "", ""))
  540. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", "signer-sha"))
  541. cp := cert.NewCAPool()
  542. // c1 should pass because host match
  543. require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
  544. // c2 should pass because ca sha match
  545. resetConntrack(fw)
  546. require.NoError(t, fw.Drop(p, true, &h2, cp, nil))
  547. // c3 should fail because no match
  548. resetConntrack(fw)
  549. assert.Equal(t, fw.Drop(p, true, &h3, cp, nil), ErrNoMatchingRule)
  550. // Test a remote address match
  551. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  552. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "1.2.3.4/24", "", "", ""))
  553. require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
  554. }
  555. func TestFirewall_Drop3V6(t *testing.T) {
  556. l := test.NewLogger()
  557. ob := &bytes.Buffer{}
  558. l.SetOutput(ob)
  559. myVpnNetworksTable := new(bart.Lite)
  560. myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
  561. p := firewall.Packet{
  562. LocalAddr: netip.MustParseAddr("fd12::34"),
  563. RemoteAddr: netip.MustParseAddr("fd12::34"),
  564. LocalPort: 1,
  565. RemotePort: 1,
  566. Protocol: firewall.ProtoUDP,
  567. Fragment: false,
  568. }
  569. network := netip.MustParsePrefix("fd12::34/120")
  570. c := cert.CachedCertificate{
  571. Certificate: &dummyCert{
  572. name: "host-owner",
  573. networks: []netip.Prefix{network},
  574. },
  575. }
  576. h := HostInfo{
  577. ConnectionState: &ConnectionState{
  578. peerCert: &c,
  579. },
  580. vpnAddrs: []netip.Addr{network.Addr()},
  581. }
  582. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  583. // Test a remote address match
  584. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  585. cp := cert.NewCAPool()
  586. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "fd12::34/120", "", "", ""))
  587. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  588. }
  589. func TestFirewall_DropConntrackReload(t *testing.T) {
  590. l := test.NewLogger()
  591. ob := &bytes.Buffer{}
  592. l.SetOutput(ob)
  593. myVpnNetworksTable := new(bart.Lite)
  594. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  595. p := firewall.Packet{
  596. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  597. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  598. LocalPort: 10,
  599. RemotePort: 90,
  600. Protocol: firewall.ProtoUDP,
  601. Fragment: false,
  602. }
  603. network := netip.MustParsePrefix("1.2.3.4/24")
  604. c := cert.CachedCertificate{
  605. Certificate: &dummyCert{
  606. name: "host1",
  607. networks: []netip.Prefix{network},
  608. groups: []string{"default-group"},
  609. issuer: "signer-shasum",
  610. },
  611. InvertedGroups: map[string]struct{}{"default-group": {}},
  612. }
  613. h := HostInfo{
  614. ConnectionState: &ConnectionState{
  615. peerCert: &c,
  616. },
  617. vpnAddrs: []netip.Addr{network.Addr()},
  618. }
  619. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  620. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  621. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  622. cp := cert.NewCAPool()
  623. // Drop outbound
  624. assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
  625. // Allow inbound
  626. resetConntrack(fw)
  627. require.NoError(t, fw.Drop(p, true, &h, cp, nil))
  628. // Allow outbound because conntrack
  629. require.NoError(t, fw.Drop(p, false, &h, cp, nil))
  630. oldFw := fw
  631. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  632. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", "", "", "", ""))
  633. fw.Conntrack = oldFw.Conntrack
  634. fw.rulesVersion = oldFw.rulesVersion + 1
  635. // Allow outbound because conntrack and new rules allow port 10
  636. require.NoError(t, fw.Drop(p, false, &h, cp, nil))
  637. oldFw = fw
  638. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  639. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", "", "", "", ""))
  640. fw.Conntrack = oldFw.Conntrack
  641. fw.rulesVersion = oldFw.rulesVersion + 1
  642. // Drop outbound because conntrack doesn't match new ruleset
  643. assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
  644. }
  645. func TestFirewall_DropIPSpoofing(t *testing.T) {
  646. l := test.NewLogger()
  647. ob := &bytes.Buffer{}
  648. l.SetOutput(ob)
  649. myVpnNetworksTable := new(bart.Lite)
  650. myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
  651. c := cert.CachedCertificate{
  652. Certificate: &dummyCert{
  653. name: "host-owner",
  654. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
  655. },
  656. }
  657. c1 := cert.CachedCertificate{
  658. Certificate: &dummyCert{
  659. name: "host",
  660. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
  661. unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
  662. },
  663. }
  664. h1 := HostInfo{
  665. ConnectionState: &ConnectionState{
  666. peerCert: &c1,
  667. },
  668. vpnAddrs: []netip.Addr{c1.Certificate.Networks()[0].Addr()},
  669. }
  670. h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
  671. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  672. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", ""))
  673. cp := cert.NewCAPool()
  674. // Packet spoofed by `c1`. Note that the remote addr is not a valid one.
  675. p := firewall.Packet{
  676. LocalAddr: netip.MustParseAddr("192.0.2.1"),
  677. RemoteAddr: netip.MustParseAddr("192.0.2.3"),
  678. LocalPort: 1,
  679. RemotePort: 1,
  680. Protocol: firewall.ProtoUDP,
  681. Fragment: false,
  682. }
  683. assert.Equal(t, fw.Drop(p, true, &h1, cp, nil), ErrInvalidRemoteIP)
  684. }
  685. func BenchmarkLookup(b *testing.B) {
  686. ml := func(m map[string]struct{}, a [][]string) {
  687. for n := 0; n < b.N; n++ {
  688. for _, sg := range a {
  689. found := false
  690. for _, g := range sg {
  691. if _, ok := m[g]; !ok {
  692. found = false
  693. break
  694. }
  695. found = true
  696. }
  697. if found {
  698. return
  699. }
  700. }
  701. }
  702. }
  703. b.Run("array to map best", func(b *testing.B) {
  704. m := map[string]struct{}{
  705. "1ne": {},
  706. "2wo": {},
  707. "3hr": {},
  708. "4ou": {},
  709. "5iv": {},
  710. "6ix": {},
  711. }
  712. a := [][]string{
  713. {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
  714. {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
  715. {"one", "two", "3hr", "4ou", "5iv", "6ix"},
  716. {"one", "two", "thr", "4ou", "5iv", "6ix"},
  717. {"one", "two", "thr", "fou", "5iv", "6ix"},
  718. {"one", "two", "thr", "fou", "fiv", "6ix"},
  719. {"one", "two", "thr", "fou", "fiv", "six"},
  720. }
  721. for n := 0; n < b.N; n++ {
  722. ml(m, a)
  723. }
  724. })
  725. b.Run("array to map worst", func(b *testing.B) {
  726. m := map[string]struct{}{
  727. "one": {},
  728. "two": {},
  729. "thr": {},
  730. "fou": {},
  731. "fiv": {},
  732. "six": {},
  733. }
  734. a := [][]string{
  735. {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
  736. {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
  737. {"one", "two", "3hr", "4ou", "5iv", "6ix"},
  738. {"one", "two", "thr", "4ou", "5iv", "6ix"},
  739. {"one", "two", "thr", "fou", "5iv", "6ix"},
  740. {"one", "two", "thr", "fou", "fiv", "6ix"},
  741. {"one", "two", "thr", "fou", "fiv", "six"},
  742. }
  743. for n := 0; n < b.N; n++ {
  744. ml(m, a)
  745. }
  746. })
  747. }
  748. func Test_parsePort(t *testing.T) {
  749. _, _, err := parsePort("")
  750. require.EqualError(t, err, "was not a number; ``")
  751. _, _, err = parsePort(" ")
  752. require.EqualError(t, err, "was not a number; ` `")
  753. _, _, err = parsePort("-")
  754. require.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
  755. _, _, err = parsePort(" - ")
  756. require.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
  757. _, _, err = parsePort("a-b")
  758. require.EqualError(t, err, "beginning range was not a number; `a`")
  759. _, _, err = parsePort("1-b")
  760. require.EqualError(t, err, "ending range was not a number; `b`")
  761. s, e, err := parsePort(" 1 - 2 ")
  762. assert.Equal(t, int32(1), s)
  763. assert.Equal(t, int32(2), e)
  764. require.NoError(t, err)
  765. s, e, err = parsePort("0-1")
  766. assert.Equal(t, int32(0), s)
  767. assert.Equal(t, int32(0), e)
  768. require.NoError(t, err)
  769. s, e, err = parsePort("9919")
  770. assert.Equal(t, int32(9919), s)
  771. assert.Equal(t, int32(9919), e)
  772. require.NoError(t, err)
  773. s, e, err = parsePort("any")
  774. assert.Equal(t, int32(0), s)
  775. assert.Equal(t, int32(0), e)
  776. require.NoError(t, err)
  777. }
  778. func TestNewFirewallFromConfig(t *testing.T) {
  779. l := test.NewLogger()
  780. // Test a bad rule definition
  781. c := &dummyCert{}
  782. cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil)
  783. require.NoError(t, err)
  784. conf := config.NewC(l)
  785. conf.Settings["firewall"] = map[string]any{"outbound": "asdf"}
  786. _, err = NewFirewallFromConfig(l, cs, conf)
  787. require.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
  788. // Test both port and code
  789. conf = config.NewC(l)
  790. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "code": "2"}}}
  791. _, err = NewFirewallFromConfig(l, cs, conf)
  792. require.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided")
  793. // Test missing host, group, cidr, ca_name and ca_sha
  794. conf = config.NewC(l)
  795. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{}}}
  796. _, err = NewFirewallFromConfig(l, cs, conf)
  797. require.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, local_cidr, ca_name, or ca_sha must be provided")
  798. // Test code/port error
  799. conf = config.NewC(l)
  800. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "a", "host": "testh", "proto": "any"}}}
  801. _, err = NewFirewallFromConfig(l, cs, conf)
  802. require.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
  803. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "a", "host": "testh", "proto": "any"}}}
  804. _, err = NewFirewallFromConfig(l, cs, conf)
  805. require.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
  806. // Test proto error
  807. conf = config.NewC(l)
  808. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "host": "testh"}}}
  809. _, err = NewFirewallFromConfig(l, cs, conf)
  810. require.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
  811. // Test cidr parse error
  812. conf = config.NewC(l)
  813. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "cidr": "testh", "proto": "any"}}}
  814. _, err = NewFirewallFromConfig(l, cs, conf)
  815. require.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
  816. // Test local_cidr parse error
  817. conf = config.NewC(l)
  818. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "local_cidr": "testh", "proto": "any"}}}
  819. _, err = NewFirewallFromConfig(l, cs, conf)
  820. require.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
  821. // Test both group and groups
  822. conf = config.NewC(l)
  823. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
  824. _, err = NewFirewallFromConfig(l, cs, conf)
  825. require.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
  826. }
  827. func TestAddFirewallRulesFromConfig(t *testing.T) {
  828. l := test.NewLogger()
  829. // Test adding tcp rule
  830. conf := config.NewC(l)
  831. mf := &mockFirewall{}
  832. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "tcp", "host": "a"}}}
  833. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  834. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  835. // Test adding udp rule
  836. conf = config.NewC(l)
  837. mf = &mockFirewall{}
  838. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "udp", "host": "a"}}}
  839. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  840. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  841. // Test adding icmp rule
  842. conf = config.NewC(l)
  843. mf = &mockFirewall{}
  844. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "icmp", "host": "a"}}}
  845. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  846. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  847. // Test adding icmp rule no port
  848. conf = config.NewC(l)
  849. mf = &mockFirewall{}
  850. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"proto": "icmp", "host": "a"}}}
  851. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  852. assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  853. // Test adding any rule
  854. conf = config.NewC(l)
  855. mf = &mockFirewall{}
  856. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
  857. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  858. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  859. // Test adding rule with cidr
  860. cidr := netip.MustParsePrefix("10.0.0.0/8")
  861. conf = config.NewC(l)
  862. mf = &mockFirewall{}
  863. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr.String()}}}
  864. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  865. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr.String(), localIp: ""}, mf.lastCall)
  866. // Test adding rule with local_cidr
  867. conf = config.NewC(l)
  868. mf = &mockFirewall{}
  869. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr.String()}}}
  870. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  871. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr.String()}, mf.lastCall)
  872. // Test adding rule with cidr ipv6
  873. cidr6 := netip.MustParsePrefix("fd00::/8")
  874. conf = config.NewC(l)
  875. mf = &mockFirewall{}
  876. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr6.String()}}}
  877. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  878. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr6.String(), localIp: ""}, mf.lastCall)
  879. // Test adding rule with any cidr
  880. conf = config.NewC(l)
  881. mf = &mockFirewall{}
  882. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "any"}}}
  883. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  884. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "any", localIp: ""}, mf.lastCall)
  885. // Test adding rule with junk cidr
  886. conf = config.NewC(l)
  887. mf = &mockFirewall{}
  888. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "junk/junk"}}}
  889. require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP")
  890. // Test adding rule with local_cidr ipv6
  891. conf = config.NewC(l)
  892. mf = &mockFirewall{}
  893. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr6.String()}}}
  894. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  895. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr6.String()}, mf.lastCall)
  896. // Test adding rule with any local_cidr
  897. conf = config.NewC(l)
  898. mf = &mockFirewall{}
  899. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "any"}}}
  900. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  901. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, localIp: "any"}, mf.lastCall)
  902. // Test adding rule with junk local_cidr
  903. conf = config.NewC(l)
  904. mf = &mockFirewall{}
  905. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "junk/junk"}}}
  906. require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP")
  907. // Test adding rule with ca_sha
  908. conf = config.NewC(l)
  909. mf = &mockFirewall{}
  910. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
  911. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  912. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caSha: "12312313123"}, mf.lastCall)
  913. // Test adding rule with ca_name
  914. conf = config.NewC(l)
  915. mf = &mockFirewall{}
  916. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_name": "root01"}}}
  917. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  918. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caName: "root01"}, mf.lastCall)
  919. // Test single group
  920. conf = config.NewC(l)
  921. mf = &mockFirewall{}
  922. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a"}}}
  923. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  924. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
  925. // Test single groups
  926. conf = config.NewC(l)
  927. mf = &mockFirewall{}
  928. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": "a"}}}
  929. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  930. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
  931. // Test multiple AND groups
  932. conf = config.NewC(l)
  933. mf = &mockFirewall{}
  934. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
  935. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  936. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall)
  937. // Test Add error
  938. conf = config.NewC(l)
  939. mf = &mockFirewall{}
  940. mf.nextCallReturn = errors.New("test error")
  941. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
  942. require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
  943. }
  944. func TestFirewall_convertRule(t *testing.T) {
  945. l := test.NewLogger()
  946. ob := &bytes.Buffer{}
  947. l.SetOutput(ob)
  948. // Ensure group array of 1 is converted and a warning is printed
  949. c := map[string]any{
  950. "group": []any{"group1"},
  951. }
  952. r, err := convertRule(l, c, "test", 1)
  953. assert.Contains(t, ob.String(), "test rule #1; group was an array with a single value, converting to simple value")
  954. require.NoError(t, err)
  955. assert.Equal(t, []string{"group1"}, r.Groups)
  956. // Ensure group array of > 1 is errord
  957. ob.Reset()
  958. c = map[string]any{
  959. "group": []any{"group1", "group2"},
  960. }
  961. r, err = convertRule(l, c, "test", 1)
  962. assert.Empty(t, ob.String())
  963. require.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
  964. // Make sure a well formed group is alright
  965. ob.Reset()
  966. c = map[string]any{
  967. "group": "group1",
  968. }
  969. r, err = convertRule(l, c, "test", 1)
  970. require.NoError(t, err)
  971. assert.Equal(t, []string{"group1"}, r.Groups)
  972. }
  973. func TestFirewall_convertRuleSanity(t *testing.T) {
  974. l := test.NewLogger()
  975. ob := &bytes.Buffer{}
  976. l.SetOutput(ob)
  977. noWarningPlease := []map[string]any{
  978. {"group": "group1"},
  979. {"groups": []any{"group2"}},
  980. {"host": "bob"},
  981. {"cidr": "1.1.1.1/1"},
  982. {"groups": []any{"group2"}, "host": "bob"},
  983. {"cidr": "1.1.1.1/1", "host": "bob"},
  984. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
  985. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
  986. }
  987. for _, c := range noWarningPlease {
  988. r, err := convertRule(l, c, "test", 1)
  989. require.NoError(t, err)
  990. require.NoError(t, r.sanity(), "should not generate a sanity warning, %+v", c)
  991. }
  992. yesWarningPlease := []map[string]any{
  993. {"group": "group1"},
  994. {"groups": []any{"group2"}},
  995. {"cidr": "1.1.1.1/1"},
  996. {"groups": []any{"group2"}, "host": "bob"},
  997. {"cidr": "1.1.1.1/1", "host": "bob"},
  998. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
  999. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
  1000. }
  1001. for _, c := range yesWarningPlease {
  1002. c["host"] = "any"
  1003. r, err := convertRule(l, c, "test", 1)
  1004. require.NoError(t, err)
  1005. err = r.sanity()
  1006. require.Error(t, err, "I wanted a warning: %+v", c)
  1007. }
  1008. //reset the list
  1009. yesWarningPlease = []map[string]any{
  1010. {"group": "group1"},
  1011. {"groups": []any{"group2"}},
  1012. {"cidr": "1.1.1.1/1"},
  1013. {"groups": []any{"group2"}, "host": "bob"},
  1014. {"cidr": "1.1.1.1/1", "host": "bob"},
  1015. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
  1016. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
  1017. }
  1018. for _, c := range yesWarningPlease {
  1019. r, err := convertRule(l, c, "test", 1)
  1020. require.NoError(t, err)
  1021. r.Groups = append(r.Groups, "any")
  1022. err = r.sanity()
  1023. require.Error(t, err, "I wanted a warning: %+v", c)
  1024. }
  1025. }
  1026. type testcase struct {
  1027. h *HostInfo
  1028. p firewall.Packet
  1029. c cert.Certificate
  1030. err error
  1031. }
  1032. func (c *testcase) Test(t *testing.T, fw *Firewall) {
  1033. t.Helper()
  1034. cp := cert.NewCAPool()
  1035. resetConntrack(fw)
  1036. err := fw.Drop(c.p, true, c.h, cp, nil)
  1037. if c.err == nil {
  1038. require.NoError(t, err, "failed to not drop remote address %s", c.p.RemoteAddr)
  1039. } else {
  1040. require.ErrorIs(t, c.err, err, "failed to drop remote address %s", c.p.RemoteAddr)
  1041. }
  1042. }
  1043. func buildTestCase(setup testsetup, err error, theirPrefixes ...netip.Prefix) testcase {
  1044. c1 := dummyCert{
  1045. name: "host1",
  1046. networks: theirPrefixes,
  1047. groups: []string{"default-group"},
  1048. issuer: "signer-shasum",
  1049. }
  1050. h := HostInfo{
  1051. ConnectionState: &ConnectionState{
  1052. peerCert: &cert.CachedCertificate{
  1053. Certificate: &c1,
  1054. InvertedGroups: map[string]struct{}{"default-group": {}},
  1055. },
  1056. },
  1057. vpnAddrs: make([]netip.Addr, len(theirPrefixes)),
  1058. }
  1059. for i := range theirPrefixes {
  1060. h.vpnAddrs[i] = theirPrefixes[i].Addr()
  1061. }
  1062. h.buildNetworks(setup.myVpnNetworksTable, &c1)
  1063. p := firewall.Packet{
  1064. LocalAddr: setup.c.Networks()[0].Addr(), //todo?
  1065. RemoteAddr: theirPrefixes[0].Addr(),
  1066. LocalPort: 10,
  1067. RemotePort: 90,
  1068. Protocol: firewall.ProtoUDP,
  1069. Fragment: false,
  1070. }
  1071. return testcase{
  1072. h: &h,
  1073. p: p,
  1074. c: &c1,
  1075. err: err,
  1076. }
  1077. }
  1078. type testsetup struct {
  1079. c dummyCert
  1080. myVpnNetworksTable *bart.Lite
  1081. fw *Firewall
  1082. }
  1083. func newSetup(t *testing.T, l *logrus.Logger, myPrefixes ...netip.Prefix) testsetup {
  1084. c := dummyCert{
  1085. name: "me",
  1086. networks: myPrefixes,
  1087. groups: []string{"default-group"},
  1088. issuer: "signer-shasum",
  1089. }
  1090. return newSetupFromCert(t, l, c)
  1091. }
  1092. func newSetupFromCert(t *testing.T, l *logrus.Logger, c dummyCert) testsetup {
  1093. myVpnNetworksTable := new(bart.Lite)
  1094. for _, prefix := range c.Networks() {
  1095. myVpnNetworksTable.Insert(prefix)
  1096. }
  1097. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  1098. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  1099. return testsetup{
  1100. c: c,
  1101. fw: fw,
  1102. myVpnNetworksTable: myVpnNetworksTable,
  1103. }
  1104. }
  1105. func TestFirewall_Drop_EnforceIPMatch(t *testing.T) {
  1106. t.Parallel()
  1107. l := test.NewLogger()
  1108. ob := &bytes.Buffer{}
  1109. l.SetOutput(ob)
  1110. myPrefix := netip.MustParsePrefix("1.1.1.1/8")
  1111. // for now, it's okay that these are all "incoming", the logic this test tries to check doesn't care about in/out
  1112. t.Run("allow inbound all matching", func(t *testing.T) {
  1113. t.Parallel()
  1114. setup := newSetup(t, l, myPrefix)
  1115. tc := buildTestCase(setup, nil, netip.MustParsePrefix("1.2.3.4/24"))
  1116. tc.Test(t, setup.fw)
  1117. })
  1118. t.Run("allow inbound local matching", func(t *testing.T) {
  1119. t.Parallel()
  1120. setup := newSetup(t, l, myPrefix)
  1121. tc := buildTestCase(setup, ErrInvalidLocalIP, netip.MustParsePrefix("1.2.3.4/24"))
  1122. tc.p.LocalAddr = netip.MustParseAddr("1.2.3.8")
  1123. tc.Test(t, setup.fw)
  1124. })
  1125. t.Run("block inbound remote mismatched", func(t *testing.T) {
  1126. t.Parallel()
  1127. setup := newSetup(t, l, myPrefix)
  1128. tc := buildTestCase(setup, ErrInvalidRemoteIP, netip.MustParsePrefix("1.2.3.4/24"))
  1129. tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
  1130. tc.Test(t, setup.fw)
  1131. })
  1132. t.Run("Block a vpn peer packet", func(t *testing.T) {
  1133. t.Parallel()
  1134. setup := newSetup(t, l, myPrefix)
  1135. tc := buildTestCase(setup, ErrPeerRejected, netip.MustParsePrefix("2.2.2.2/24"))
  1136. tc.Test(t, setup.fw)
  1137. })
  1138. twoPrefixes := []netip.Prefix{
  1139. netip.MustParsePrefix("1.2.3.4/24"), netip.MustParsePrefix("2.2.2.2/24"),
  1140. }
  1141. t.Run("allow inbound one matching", func(t *testing.T) {
  1142. t.Parallel()
  1143. setup := newSetup(t, l, myPrefix)
  1144. tc := buildTestCase(setup, nil, twoPrefixes...)
  1145. tc.Test(t, setup.fw)
  1146. })
  1147. t.Run("block inbound multimismatch", func(t *testing.T) {
  1148. t.Parallel()
  1149. setup := newSetup(t, l, myPrefix)
  1150. tc := buildTestCase(setup, ErrInvalidRemoteIP, twoPrefixes...)
  1151. tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
  1152. tc.Test(t, setup.fw)
  1153. })
  1154. t.Run("allow inbound 2nd one matching", func(t *testing.T) {
  1155. t.Parallel()
  1156. setup2 := newSetup(t, l, netip.MustParsePrefix("2.2.2.1/24"))
  1157. tc := buildTestCase(setup2, nil, twoPrefixes...)
  1158. tc.p.RemoteAddr = twoPrefixes[1].Addr()
  1159. tc.Test(t, setup2.fw)
  1160. })
  1161. t.Run("allow inbound unsafe route", func(t *testing.T) {
  1162. t.Parallel()
  1163. unsafePrefix := netip.MustParsePrefix("192.168.0.0/24")
  1164. c := dummyCert{
  1165. name: "me",
  1166. networks: []netip.Prefix{myPrefix},
  1167. unsafeNetworks: []netip.Prefix{unsafePrefix},
  1168. groups: []string{"default-group"},
  1169. issuer: "signer-shasum",
  1170. }
  1171. unsafeSetup := newSetupFromCert(t, l, c)
  1172. tc := buildTestCase(unsafeSetup, nil, twoPrefixes...)
  1173. tc.p.LocalAddr = netip.MustParseAddr("192.168.0.3")
  1174. tc.err = ErrNoMatchingRule
  1175. tc.Test(t, unsafeSetup.fw) //should hit firewall and bounce off
  1176. require.NoError(t, unsafeSetup.fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", unsafePrefix.String(), "", ""))
  1177. tc.err = nil
  1178. tc.Test(t, unsafeSetup.fw) //should pass
  1179. })
  1180. }
  1181. type addRuleCall struct {
  1182. incoming bool
  1183. proto uint8
  1184. startPort int32
  1185. endPort int32
  1186. groups []string
  1187. host string
  1188. ip string
  1189. localIp string
  1190. caName string
  1191. caSha string
  1192. }
  1193. type mockFirewall struct {
  1194. lastCall addRuleCall
  1195. nextCallReturn error
  1196. }
  1197. func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip, localIp, caName string, caSha string) error {
  1198. mf.lastCall = addRuleCall{
  1199. incoming: incoming,
  1200. proto: proto,
  1201. startPort: startPort,
  1202. endPort: endPort,
  1203. groups: groups,
  1204. host: host,
  1205. ip: ip,
  1206. localIp: localIp,
  1207. caName: caName,
  1208. caSha: caSha,
  1209. }
  1210. err := mf.nextCallReturn
  1211. mf.nextCallReturn = nil
  1212. return err
  1213. }
  1214. func resetConntrack(fw *Firewall) {
  1215. fw.Conntrack.Lock()
  1216. fw.Conntrack.Conns = map[firewall.Packet]*conn{}
  1217. fw.Conntrack.Unlock()
  1218. }