acls.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. package logic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "maps"
  8. "net"
  9. "sort"
  10. "sync"
  11. "time"
  12. "github.com/gravitl/netmaker/database"
  13. "github.com/gravitl/netmaker/db"
  14. "github.com/gravitl/netmaker/models"
  15. "github.com/gravitl/netmaker/schema"
  16. "github.com/gravitl/netmaker/servercfg"
  17. )
  18. var GetFwRulesForNodeAndPeerOnGw = getFwRulesForNodeAndPeerOnGw
  19. var GetTagMapWithNodesByNetwork = getTagMapWithNodesByNetwork
  20. var GetEgressUserRulesForNode = func(targetnode *models.Node,
  21. rules map[string]models.AclRule) map[string]models.AclRule {
  22. return rules
  23. }
  24. var GetUserAclRulesForNode = func(targetnode *models.Node,
  25. rules map[string]models.AclRule) map[string]models.AclRule {
  26. return rules
  27. }
  28. var GetFwRulesForUserNodesOnGw = func(node models.Node, nodes []models.Node) (rules []models.FwRule) { return }
  29. func GetFwRulesOnIngressGateway(node models.Node) (rules []models.FwRule) {
  30. // fetch user access to static clients via policies
  31. defer func() {
  32. sort.Slice(rules, func(i, j int) bool {
  33. if !rules[i].SrcIP.IP.Equal(rules[j].SrcIP.IP) {
  34. return string(rules[i].SrcIP.IP.To16()) < string(rules[j].SrcIP.IP.To16())
  35. }
  36. return string(rules[i].DstIP.IP.To16()) < string(rules[j].DstIP.IP.To16())
  37. })
  38. }()
  39. defaultDevicePolicy, _ := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  40. nodes, _ := GetNetworkNodes(node.Network)
  41. nodes = append(nodes, GetStaticNodesByNetwork(models.NetworkID(node.Network), true)...)
  42. rules = GetFwRulesForUserNodesOnGw(node, nodes)
  43. if defaultDevicePolicy.Enabled {
  44. return
  45. }
  46. for _, nodeI := range nodes {
  47. if !nodeI.IsStatic || nodeI.IsUserNode {
  48. continue
  49. }
  50. if !node.StaticNode.Enabled {
  51. continue
  52. }
  53. // if nodeI.StaticNode.IngressGatewayID != node.ID.String() {
  54. // continue
  55. // }
  56. if IsNodeAllowedToCommunicateWithAllRsrcs(nodeI) {
  57. if nodeI.Address.IP != nil {
  58. rules = append(rules, models.FwRule{
  59. SrcIP: net.IPNet{
  60. IP: nodeI.Address.IP,
  61. Mask: net.CIDRMask(32, 32),
  62. },
  63. Allow: true,
  64. })
  65. rules = append(rules, models.FwRule{
  66. SrcIP: node.NetworkRange,
  67. DstIP: net.IPNet{
  68. IP: nodeI.Address.IP,
  69. Mask: net.CIDRMask(32, 32),
  70. },
  71. Allow: true,
  72. })
  73. }
  74. if nodeI.Address6.IP != nil {
  75. rules = append(rules, models.FwRule{
  76. SrcIP: net.IPNet{
  77. IP: nodeI.Address6.IP,
  78. Mask: net.CIDRMask(128, 128),
  79. },
  80. Allow: true,
  81. })
  82. rules = append(rules, models.FwRule{
  83. SrcIP: node.NetworkRange6,
  84. DstIP: net.IPNet{
  85. IP: nodeI.Address.IP,
  86. Mask: net.CIDRMask(128, 128),
  87. },
  88. Allow: true,
  89. })
  90. }
  91. continue
  92. }
  93. for _, peer := range nodes {
  94. if peer.StaticNode.ClientID == nodeI.StaticNode.ClientID || peer.IsUserNode {
  95. continue
  96. }
  97. if nodeI.StaticNode.IngressGatewayID != node.ID.String() &&
  98. ((!peer.IsStatic && peer.ID.String() != node.ID.String()) ||
  99. (peer.IsStatic && peer.StaticNode.IngressGatewayID != node.ID.String())) {
  100. continue
  101. }
  102. if peer.IsStatic {
  103. peer = peer.StaticNode.ConvertToStaticNode()
  104. }
  105. var allowedPolicies1 []models.Acl
  106. var ok bool
  107. if ok, allowedPolicies1 = IsNodeAllowedToCommunicate(nodeI.StaticNode.ConvertToStaticNode(), peer, true); ok {
  108. rules = append(rules, GetFwRulesForNodeAndPeerOnGw(nodeI.StaticNode.ConvertToStaticNode(), peer, allowedPolicies1)...)
  109. }
  110. if ok, allowedPolicies2 := IsNodeAllowedToCommunicate(peer, nodeI.StaticNode.ConvertToStaticNode(), true); ok {
  111. rules = append(rules,
  112. GetFwRulesForNodeAndPeerOnGw(peer, nodeI.StaticNode.ConvertToStaticNode(),
  113. getUniquePolicies(allowedPolicies1, allowedPolicies2))...)
  114. }
  115. }
  116. }
  117. if len(node.RelayedNodes) > 0 {
  118. for _, relayedNodeID := range node.RelayedNodes {
  119. relayedNode, err := GetNodeByID(relayedNodeID)
  120. if err != nil {
  121. continue
  122. }
  123. if relayedNode.Address.IP != nil {
  124. rules = append(rules, models.FwRule{
  125. AllowedProtocol: models.ALL,
  126. AllowedPorts: []string{},
  127. Allow: true,
  128. DstIP: relayedNode.AddressIPNet4(),
  129. SrcIP: node.NetworkRange,
  130. })
  131. rules = append(rules, models.FwRule{
  132. AllowedProtocol: models.ALL,
  133. AllowedPorts: []string{},
  134. Allow: true,
  135. DstIP: node.NetworkRange,
  136. SrcIP: relayedNode.AddressIPNet4(),
  137. })
  138. }
  139. if relayedNode.Address6.IP != nil {
  140. rules = append(rules, models.FwRule{
  141. AllowedProtocol: models.ALL,
  142. AllowedPorts: []string{},
  143. Allow: true,
  144. DstIP: relayedNode.AddressIPNet6(),
  145. SrcIP: node.NetworkRange6,
  146. })
  147. rules = append(rules, models.FwRule{
  148. AllowedProtocol: models.ALL,
  149. AllowedPorts: []string{},
  150. Allow: true,
  151. DstIP: node.NetworkRange6,
  152. SrcIP: relayedNode.AddressIPNet6(),
  153. })
  154. }
  155. }
  156. }
  157. return
  158. }
  159. func getFwRulesForNodeAndPeerOnGw(node, peer models.Node, allowedPolicies []models.Acl) (rules []models.FwRule) {
  160. for _, policy := range allowedPolicies {
  161. // if static peer dst rule not for ingress node -> skip
  162. if node.Address.IP != nil {
  163. rules = append(rules, models.FwRule{
  164. SrcIP: net.IPNet{
  165. IP: node.Address.IP,
  166. Mask: net.CIDRMask(32, 32),
  167. },
  168. DstIP: net.IPNet{
  169. IP: peer.Address.IP,
  170. Mask: net.CIDRMask(32, 32),
  171. },
  172. Allow: true,
  173. })
  174. }
  175. if node.Address6.IP != nil {
  176. rules = append(rules, models.FwRule{
  177. SrcIP: net.IPNet{
  178. IP: node.Address6.IP,
  179. Mask: net.CIDRMask(128, 128),
  180. },
  181. DstIP: net.IPNet{
  182. IP: peer.Address6.IP,
  183. Mask: net.CIDRMask(128, 128),
  184. },
  185. Allow: true,
  186. })
  187. }
  188. if policy.AllowedDirection == models.TrafficDirectionBi {
  189. if node.Address.IP != nil {
  190. rules = append(rules, models.FwRule{
  191. SrcIP: net.IPNet{
  192. IP: peer.Address.IP,
  193. Mask: net.CIDRMask(32, 32),
  194. },
  195. DstIP: net.IPNet{
  196. IP: node.Address.IP,
  197. Mask: net.CIDRMask(32, 32),
  198. },
  199. Allow: true,
  200. })
  201. }
  202. if node.Address6.IP != nil {
  203. rules = append(rules, models.FwRule{
  204. SrcIP: net.IPNet{
  205. IP: peer.Address6.IP,
  206. Mask: net.CIDRMask(128, 128),
  207. },
  208. DstIP: net.IPNet{
  209. IP: node.Address6.IP,
  210. Mask: net.CIDRMask(128, 128),
  211. },
  212. Allow: true,
  213. })
  214. }
  215. }
  216. if len(node.StaticNode.ExtraAllowedIPs) > 0 {
  217. for _, additionalAllowedIPNet := range node.StaticNode.ExtraAllowedIPs {
  218. _, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)
  219. if err != nil {
  220. continue
  221. }
  222. if ipNet.IP.To4() != nil && peer.Address.IP != nil {
  223. rules = append(rules, models.FwRule{
  224. SrcIP: net.IPNet{
  225. IP: peer.Address.IP,
  226. Mask: net.CIDRMask(32, 32),
  227. },
  228. DstIP: *ipNet,
  229. Allow: true,
  230. })
  231. } else if peer.Address6.IP != nil {
  232. rules = append(rules, models.FwRule{
  233. SrcIP: net.IPNet{
  234. IP: peer.Address6.IP,
  235. Mask: net.CIDRMask(128, 128),
  236. },
  237. DstIP: *ipNet,
  238. Allow: true,
  239. })
  240. }
  241. }
  242. }
  243. if len(peer.StaticNode.ExtraAllowedIPs) > 0 {
  244. for _, additionalAllowedIPNet := range peer.StaticNode.ExtraAllowedIPs {
  245. _, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)
  246. if err != nil {
  247. continue
  248. }
  249. if ipNet.IP.To4() != nil && node.Address.IP != nil {
  250. rules = append(rules, models.FwRule{
  251. SrcIP: net.IPNet{
  252. IP: node.Address.IP,
  253. Mask: net.CIDRMask(32, 32),
  254. },
  255. DstIP: *ipNet,
  256. Allow: true,
  257. })
  258. } else if node.Address6.IP != nil {
  259. rules = append(rules, models.FwRule{
  260. SrcIP: net.IPNet{
  261. IP: node.Address6.IP,
  262. Mask: net.CIDRMask(128, 128),
  263. },
  264. DstIP: *ipNet,
  265. Allow: true,
  266. })
  267. }
  268. }
  269. }
  270. // add egress range rules
  271. for _, dstI := range policy.Dst {
  272. if dstI.ID == models.EgressID {
  273. e := schema.Egress{ID: dstI.Value}
  274. err := e.Get(db.WithContext(context.TODO()))
  275. if err != nil {
  276. continue
  277. }
  278. if len(e.DomainAns) > 0 {
  279. for _, domainAnsI := range e.DomainAns {
  280. dstI.Value = domainAnsI
  281. ip, cidr, err := net.ParseCIDR(dstI.Value)
  282. if err == nil {
  283. if ip.To4() != nil {
  284. if node.Address.IP != nil {
  285. rules = append(rules, models.FwRule{
  286. SrcIP: net.IPNet{
  287. IP: node.Address.IP,
  288. Mask: net.CIDRMask(32, 32),
  289. },
  290. DstIP: *cidr,
  291. Allow: true,
  292. })
  293. }
  294. } else {
  295. if node.Address6.IP != nil {
  296. rules = append(rules, models.FwRule{
  297. SrcIP: net.IPNet{
  298. IP: node.Address6.IP,
  299. Mask: net.CIDRMask(128, 128),
  300. },
  301. DstIP: *cidr,
  302. Allow: true,
  303. })
  304. }
  305. }
  306. }
  307. }
  308. } else {
  309. dstI.Value = e.Range
  310. ip, cidr, err := net.ParseCIDR(dstI.Value)
  311. if err == nil {
  312. if ip.To4() != nil {
  313. if node.Address.IP != nil {
  314. rules = append(rules, models.FwRule{
  315. SrcIP: net.IPNet{
  316. IP: node.Address.IP,
  317. Mask: net.CIDRMask(32, 32),
  318. },
  319. DstIP: *cidr,
  320. Allow: true,
  321. })
  322. }
  323. } else {
  324. if node.Address6.IP != nil {
  325. rules = append(rules, models.FwRule{
  326. SrcIP: net.IPNet{
  327. IP: node.Address6.IP,
  328. Mask: net.CIDRMask(128, 128),
  329. },
  330. DstIP: *cidr,
  331. Allow: true,
  332. })
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. return
  341. }
  342. func getUniquePolicies(policies1, policies2 []models.Acl) []models.Acl {
  343. policies1Map := make(map[string]struct{})
  344. for _, policy1I := range policies1 {
  345. policies1Map[policy1I.ID] = struct{}{}
  346. }
  347. for i := len(policies2) - 1; i >= 0; i-- {
  348. if _, ok := policies1Map[policies2[i].ID]; ok {
  349. policies2 = append(policies2[:i], policies2[i+1:]...)
  350. }
  351. }
  352. return policies2
  353. }
  354. // Sort a slice of net.IP addresses
  355. func sortIPs(ips []net.IP) {
  356. sort.Slice(ips, func(i, j int) bool {
  357. ip1, ip2 := ips[i].To16(), ips[j].To16()
  358. return string(ip1) < string(ip2) // Compare as byte slices
  359. })
  360. }
  361. func GetStaticNodeIps(node models.Node) (ips []net.IP) {
  362. defer func() {
  363. sortIPs(ips)
  364. }()
  365. defaultUserPolicy, _ := GetDefaultPolicy(models.NetworkID(node.Network), models.UserPolicy)
  366. defaultDevicePolicy, _ := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  367. extclients := GetStaticNodesByNetwork(models.NetworkID(node.Network), false)
  368. for _, extclient := range extclients {
  369. if extclient.IsUserNode && defaultUserPolicy.Enabled {
  370. continue
  371. }
  372. if !extclient.IsUserNode && defaultDevicePolicy.Enabled {
  373. continue
  374. }
  375. if !extclient.StaticNode.Enabled {
  376. continue
  377. }
  378. if extclient.StaticNode.Address != "" {
  379. ips = append(ips, extclient.StaticNode.AddressIPNet4().IP)
  380. }
  381. if extclient.StaticNode.Address6 != "" {
  382. ips = append(ips, extclient.StaticNode.AddressIPNet6().IP)
  383. }
  384. }
  385. return
  386. }
  387. var MigrateToGws = func() {
  388. nodes, err := GetAllNodes()
  389. if err != nil {
  390. return
  391. }
  392. for _, node := range nodes {
  393. if node.IsIngressGateway || node.IsRelay || node.IsInternetGateway {
  394. node.IsGw = true
  395. node.IsIngressGateway = true
  396. node.IsRelay = true
  397. if node.Tags == nil {
  398. node.Tags = make(map[models.TagID]struct{})
  399. }
  400. UpsertNode(&node)
  401. }
  402. }
  403. }
  404. var CheckIfAnyPolicyisUniDirectional = func(targetNode models.Node, acls []models.Acl) bool {
  405. return false
  406. }
  407. func GetAclRulesForNode(targetnodeI *models.Node) (rules map[string]models.AclRule) {
  408. targetnode := *targetnodeI
  409. defer func() {
  410. //if !targetnode.IsIngressGateway {
  411. rules = GetUserAclRulesForNode(&targetnode, rules)
  412. //}
  413. }()
  414. rules = make(map[string]models.AclRule)
  415. if IsNodeAllowedToCommunicateWithAllRsrcs(targetnode) {
  416. aclRule := models.AclRule{
  417. ID: fmt.Sprintf("%s-all-allowed-node-rule", targetnode.ID.String()),
  418. AllowedProtocol: models.ALL,
  419. Direction: models.TrafficDirectionBi,
  420. Allowed: true,
  421. IPList: []net.IPNet{targetnode.NetworkRange},
  422. IP6List: []net.IPNet{targetnode.NetworkRange6},
  423. Dst: []net.IPNet{targetnode.AddressIPNet4()},
  424. Dst6: []net.IPNet{targetnode.AddressIPNet6()},
  425. }
  426. e := schema.Egress{Network: targetnode.Network}
  427. egressRanges4 := []net.IPNet{}
  428. egressRanges6 := []net.IPNet{}
  429. eli, _ := e.ListByNetwork(db.WithContext(context.Background()))
  430. for _, eI := range eli {
  431. if !eI.Status || len(eI.Nodes) == 0 {
  432. continue
  433. }
  434. if _, ok := eI.Nodes[targetnode.ID.String()]; ok {
  435. if eI.Range != "" {
  436. _, cidr, err := net.ParseCIDR(eI.Range)
  437. if err == nil {
  438. if cidr.IP.To4() != nil {
  439. egressRanges4 = append(egressRanges4, *cidr)
  440. } else {
  441. egressRanges6 = append(egressRanges6, *cidr)
  442. }
  443. }
  444. }
  445. }
  446. }
  447. if len(egressRanges4) > 0 {
  448. aclRule.Dst = append(aclRule.Dst, egressRanges4...)
  449. }
  450. if len(egressRanges6) > 0 {
  451. aclRule.Dst6 = append(aclRule.Dst6, egressRanges6...)
  452. }
  453. rules[aclRule.ID] = aclRule
  454. return
  455. }
  456. var taggedNodes map[models.TagID][]models.Node
  457. if targetnode.IsIngressGateway {
  458. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), false)
  459. } else {
  460. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  461. }
  462. acls := ListDevicePolicies(models.NetworkID(targetnode.Network))
  463. var targetNodeTags = make(map[models.TagID]struct{})
  464. if targetnode.Mutex != nil {
  465. targetnode.Mutex.Lock()
  466. targetNodeTags = maps.Clone(targetnode.Tags)
  467. targetnode.Mutex.Unlock()
  468. } else {
  469. targetNodeTags = maps.Clone(targetnode.Tags)
  470. }
  471. if targetNodeTags == nil {
  472. targetNodeTags = make(map[models.TagID]struct{})
  473. }
  474. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  475. targetNodeTags["*"] = struct{}{}
  476. for _, acl := range acls {
  477. if !acl.Enabled {
  478. continue
  479. }
  480. srcTags := ConvAclTagToValueMap(acl.Src)
  481. dstTags := ConvAclTagToValueMap(acl.Dst)
  482. egressRanges4 := []net.IPNet{}
  483. egressRanges6 := []net.IPNet{}
  484. for _, dst := range acl.Dst {
  485. if dst.Value == "*" {
  486. e := schema.Egress{Network: targetnode.Network}
  487. eli, _ := e.ListByNetwork(db.WithContext(context.Background()))
  488. for _, eI := range eli {
  489. if !eI.Status || len(eI.Nodes) == 0 {
  490. continue
  491. }
  492. if _, ok := eI.Nodes[targetnode.ID.String()]; ok {
  493. if eI.Range != "" {
  494. _, cidr, err := net.ParseCIDR(eI.Range)
  495. if err == nil {
  496. if cidr.IP.To4() != nil {
  497. egressRanges4 = append(egressRanges4, *cidr)
  498. } else {
  499. egressRanges6 = append(egressRanges6, *cidr)
  500. }
  501. }
  502. }
  503. }
  504. }
  505. break
  506. }
  507. if dst.ID == models.EgressID {
  508. e := schema.Egress{ID: dst.Value}
  509. err := e.Get(db.WithContext(context.TODO()))
  510. if err == nil && e.Status && len(e.Nodes) > 0 {
  511. if _, ok := e.Nodes[targetnode.ID.String()]; ok {
  512. if e.Range != "" {
  513. _, cidr, err := net.ParseCIDR(e.Range)
  514. if err == nil {
  515. if cidr.IP.To4() != nil {
  516. egressRanges4 = append(egressRanges4, *cidr)
  517. } else {
  518. egressRanges6 = append(egressRanges6, *cidr)
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. }
  526. _, srcAll := srcTags["*"]
  527. _, dstAll := dstTags["*"]
  528. aclRule := models.AclRule{
  529. ID: acl.ID,
  530. AllowedProtocol: acl.Proto,
  531. AllowedPorts: acl.Port,
  532. Direction: acl.AllowedDirection,
  533. Allowed: true,
  534. Dst: []net.IPNet{targetnode.AddressIPNet4()},
  535. Dst6: []net.IPNet{targetnode.AddressIPNet6()},
  536. }
  537. if len(egressRanges4) > 0 {
  538. aclRule.Dst = append(aclRule.Dst, egressRanges4...)
  539. }
  540. if len(egressRanges6) > 0 {
  541. aclRule.Dst6 = append(aclRule.Dst6, egressRanges6...)
  542. }
  543. for nodeTag := range targetNodeTags {
  544. if acl.AllowedDirection == models.TrafficDirectionBi {
  545. var existsInSrcTag bool
  546. var existsInDstTag bool
  547. if _, ok := srcTags[nodeTag.String()]; ok || srcAll {
  548. existsInSrcTag = true
  549. }
  550. if _, ok := srcTags[targetnode.ID.String()]; ok || srcAll {
  551. existsInSrcTag = true
  552. }
  553. if _, ok := dstTags[nodeTag.String()]; ok || dstAll {
  554. existsInDstTag = true
  555. }
  556. if _, ok := dstTags[targetnode.ID.String()]; ok || dstAll {
  557. existsInDstTag = true
  558. }
  559. if existsInSrcTag /* && !existsInDstTag*/ {
  560. // get all dst tags
  561. for dst := range dstTags {
  562. if dst == nodeTag.String() {
  563. continue
  564. }
  565. // Get peers in the tags and add allowed rules
  566. nodes := taggedNodes[models.TagID(dst)]
  567. if dst != targetnode.ID.String() {
  568. node, err := GetNodeByID(dst)
  569. if err == nil {
  570. nodes = append(nodes, node)
  571. }
  572. }
  573. for _, node := range nodes {
  574. if node.ID == targetnode.ID {
  575. continue
  576. }
  577. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  578. continue
  579. }
  580. if node.Address.IP != nil {
  581. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  582. }
  583. if node.Address6.IP != nil {
  584. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  585. }
  586. if node.IsStatic && node.StaticNode.Address != "" {
  587. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  588. }
  589. if node.IsStatic && node.StaticNode.Address6 != "" {
  590. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  591. }
  592. }
  593. }
  594. }
  595. if existsInDstTag /*&& !existsInSrcTag*/ {
  596. // get all src tags
  597. for src := range srcTags {
  598. if src == nodeTag.String() {
  599. continue
  600. }
  601. // Get peers in the tags and add allowed rules
  602. nodes := taggedNodes[models.TagID(src)]
  603. if src != targetnode.ID.String() {
  604. node, err := GetNodeByID(src)
  605. if err == nil {
  606. nodes = append(nodes, node)
  607. }
  608. }
  609. for _, node := range nodes {
  610. if node.ID == targetnode.ID {
  611. continue
  612. }
  613. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  614. continue
  615. }
  616. if node.Address.IP != nil {
  617. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  618. }
  619. if node.Address6.IP != nil {
  620. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  621. }
  622. if node.IsStatic && node.StaticNode.Address != "" {
  623. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  624. }
  625. if node.IsStatic && node.StaticNode.Address6 != "" {
  626. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  627. }
  628. }
  629. }
  630. }
  631. } else {
  632. _, all := dstTags["*"]
  633. if _, ok := dstTags[nodeTag.String()]; ok || all {
  634. // get all src tags
  635. for src := range srcTags {
  636. if src == nodeTag.String() {
  637. continue
  638. }
  639. // Get peers in the tags and add allowed rules
  640. nodes := taggedNodes[models.TagID(src)]
  641. for _, node := range nodes {
  642. if node.ID == targetnode.ID {
  643. continue
  644. }
  645. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  646. continue
  647. }
  648. if node.Address.IP != nil {
  649. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  650. }
  651. if node.Address6.IP != nil {
  652. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  653. }
  654. if node.IsStatic && node.StaticNode.Address != "" {
  655. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  656. }
  657. if node.IsStatic && node.StaticNode.Address6 != "" {
  658. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  659. }
  660. }
  661. }
  662. }
  663. }
  664. }
  665. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  666. aclRule.IPList = UniqueIPNetList(aclRule.IPList)
  667. aclRule.IP6List = UniqueIPNetList(aclRule.IP6List)
  668. rules[acl.ID] = aclRule
  669. }
  670. }
  671. return rules
  672. }
  673. func GetEgressRulesForNode(targetnode models.Node) (rules map[string]models.AclRule) {
  674. rules = make(map[string]models.AclRule)
  675. defer func() {
  676. rules = GetEgressUserRulesForNode(&targetnode, rules)
  677. }()
  678. taggedNodes := GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  679. acls := ListDevicePolicies(models.NetworkID(targetnode.Network))
  680. var targetNodeTags = make(map[models.TagID]struct{})
  681. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  682. targetNodeTags["*"] = struct{}{}
  683. if targetnode.IsGw && !servercfg.IsPro {
  684. targetNodeTags[models.TagID(fmt.Sprintf("%s.%s", targetnode.Network, models.GwTagName))] = struct{}{}
  685. }
  686. egs, _ := (&schema.Egress{Network: targetnode.Network}).ListByNetwork(db.WithContext(context.TODO()))
  687. if len(egs) == 0 {
  688. return
  689. }
  690. var egressIDMap = make(map[string]schema.Egress)
  691. for _, egI := range egs {
  692. if !egI.Status {
  693. continue
  694. }
  695. if _, ok := egI.Nodes[targetnode.ID.String()]; ok {
  696. egressIDMap[egI.ID] = egI
  697. }
  698. }
  699. if len(egressIDMap) == 0 {
  700. return
  701. }
  702. for _, acl := range acls {
  703. if !acl.Enabled {
  704. continue
  705. }
  706. srcTags := ConvAclTagToValueMap(acl.Src)
  707. dstTags := ConvAclTagToValueMap(acl.Dst)
  708. _, dstAll := dstTags["*"]
  709. aclRule := models.AclRule{
  710. ID: acl.ID,
  711. AllowedProtocol: acl.Proto,
  712. AllowedPorts: acl.Port,
  713. Direction: acl.AllowedDirection,
  714. Allowed: true,
  715. }
  716. for egressID, egI := range egressIDMap {
  717. if _, ok := dstTags[egressID]; ok || dstAll {
  718. if servercfg.IsPro && egI.Domain != "" && len(egI.DomainAns) > 0 {
  719. for _, domainAnsI := range egI.DomainAns {
  720. ip, cidr, err := net.ParseCIDR(domainAnsI)
  721. if err == nil {
  722. if ip.To4() != nil {
  723. aclRule.Dst = append(aclRule.Dst, *cidr)
  724. } else {
  725. aclRule.Dst6 = append(aclRule.Dst6, *cidr)
  726. }
  727. }
  728. }
  729. } else {
  730. ip, cidr, err := net.ParseCIDR(egI.Range)
  731. if err == nil {
  732. if ip.To4() != nil {
  733. aclRule.Dst = append(aclRule.Dst, *cidr)
  734. } else {
  735. aclRule.Dst6 = append(aclRule.Dst6, *cidr)
  736. }
  737. }
  738. }
  739. _, srcAll := srcTags["*"]
  740. if srcAll {
  741. if targetnode.NetworkRange.IP != nil {
  742. aclRule.IPList = append(aclRule.IPList, targetnode.NetworkRange)
  743. }
  744. if targetnode.NetworkRange6.IP != nil {
  745. aclRule.IP6List = append(aclRule.IP6List, targetnode.NetworkRange6)
  746. }
  747. continue
  748. }
  749. // get all src tags
  750. for src := range srcTags {
  751. // Get peers in the tags and add allowed rules
  752. nodes := taggedNodes[models.TagID(src)]
  753. for _, node := range nodes {
  754. if node.ID == targetnode.ID {
  755. continue
  756. }
  757. if node.Address.IP != nil {
  758. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  759. }
  760. if node.Address6.IP != nil {
  761. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  762. }
  763. if node.IsStatic && node.StaticNode.Address != "" {
  764. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  765. }
  766. if node.IsStatic && node.StaticNode.Address6 != "" {
  767. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  768. }
  769. }
  770. }
  771. }
  772. }
  773. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  774. aclRule.IPList = UniqueIPNetList(aclRule.IPList)
  775. aclRule.IP6List = UniqueIPNetList(aclRule.IP6List)
  776. rules[acl.ID] = aclRule
  777. }
  778. }
  779. return
  780. }
  781. func GetAclRuleForInetGw(targetnode models.Node) (rules map[string]models.AclRule) {
  782. rules = make(map[string]models.AclRule)
  783. if targetnode.IsInternetGateway {
  784. aclRule := models.AclRule{
  785. ID: fmt.Sprintf("%s-inet-gw-internal-rule", targetnode.ID.String()),
  786. AllowedProtocol: models.ALL,
  787. AllowedPorts: []string{},
  788. Direction: models.TrafficDirectionBi,
  789. Allowed: true,
  790. }
  791. if targetnode.NetworkRange.IP != nil {
  792. aclRule.IPList = append(aclRule.IPList, targetnode.NetworkRange)
  793. _, allIpv4, _ := net.ParseCIDR(IPv4Network)
  794. aclRule.Dst = append(aclRule.Dst, *allIpv4)
  795. }
  796. if targetnode.NetworkRange6.IP != nil {
  797. aclRule.IP6List = append(aclRule.IP6List, targetnode.NetworkRange6)
  798. _, allIpv6, _ := net.ParseCIDR(IPv6Network)
  799. aclRule.Dst6 = append(aclRule.Dst6, *allIpv6)
  800. }
  801. rules[aclRule.ID] = aclRule
  802. }
  803. return
  804. }
  805. // Compare two IPs and return true if ip1 < ip2
  806. func lessIP(ip1, ip2 net.IP) bool {
  807. ip1 = ip1.To16() // Ensure IPv4 is converted to IPv6-mapped format
  808. ip2 = ip2.To16()
  809. return string(ip1) < string(ip2)
  810. }
  811. // Sort by IP first, then by prefix length
  812. func sortIPNets(ipNets []net.IPNet) {
  813. sort.Slice(ipNets, func(i, j int) bool {
  814. ip1, ip2 := ipNets[i].IP, ipNets[j].IP
  815. mask1, _ := ipNets[i].Mask.Size()
  816. mask2, _ := ipNets[j].Mask.Size()
  817. // Compare IPs first
  818. if ip1.Equal(ip2) {
  819. return mask1 < mask2 // If same IP, sort by subnet mask size
  820. }
  821. return lessIP(ip1, ip2)
  822. })
  823. }
  824. func UniqueIPNetList(ipnets []net.IPNet) []net.IPNet {
  825. uniqueMap := make(map[string]net.IPNet)
  826. for _, ipnet := range ipnets {
  827. key := ipnet.String() // Uses CIDR notation as a unique key
  828. if _, exists := uniqueMap[key]; !exists {
  829. uniqueMap[key] = ipnet
  830. }
  831. }
  832. // Convert map back to slice
  833. uniqueList := make([]net.IPNet, 0, len(uniqueMap))
  834. for _, ipnet := range uniqueMap {
  835. uniqueList = append(uniqueList, ipnet)
  836. }
  837. sortIPNets(uniqueList)
  838. return uniqueList
  839. }
  840. func checkIfAclTagisValid(a models.Acl, t models.AclPolicyTag, isSrc bool) (err error) {
  841. switch t.ID {
  842. case models.NodeID:
  843. if a.RuleType == models.UserPolicy && isSrc {
  844. return errors.New("user policy source mismatch")
  845. }
  846. _, nodeErr := GetNodeByID(t.Value)
  847. if nodeErr != nil {
  848. _, staticNodeErr := GetExtClient(t.Value, a.NetworkID.String())
  849. if staticNodeErr != nil {
  850. return errors.New("invalid node " + t.Value)
  851. }
  852. }
  853. case models.EgressID, models.EgressRange:
  854. e := schema.Egress{
  855. ID: t.Value,
  856. }
  857. err := e.Get(db.WithContext(context.TODO()))
  858. if err != nil {
  859. return errors.New("invalid egress")
  860. }
  861. default:
  862. return errors.New("invalid policy")
  863. }
  864. return nil
  865. }
  866. var IsAclPolicyValid = func(acl models.Acl) (err error) {
  867. //check if src and dst are valid
  868. if acl.AllowedDirection == models.TrafficDirectionUni {
  869. return errors.New("uni traffic flow not allowed on CE")
  870. }
  871. switch acl.RuleType {
  872. case models.DevicePolicy:
  873. for _, srcI := range acl.Src {
  874. if srcI.Value == "*" {
  875. continue
  876. }
  877. if srcI.ID == models.NodeTagID && srcI.Value == fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName) {
  878. continue
  879. }
  880. if err = checkIfAclTagisValid(acl, srcI, true); err != nil {
  881. return err
  882. }
  883. }
  884. for _, dstI := range acl.Dst {
  885. if dstI.Value == "*" {
  886. continue
  887. }
  888. if dstI.ID == models.NodeTagID && dstI.Value == fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName) {
  889. continue
  890. }
  891. if err = checkIfAclTagisValid(acl, dstI, false); err != nil {
  892. return
  893. }
  894. }
  895. default:
  896. return errors.New("unknown acl policy type " + string(acl.RuleType))
  897. }
  898. return nil
  899. }
  900. var IsPeerAllowed = func(node, peer models.Node, checkDefaultPolicy bool) bool {
  901. var nodeId, peerId string
  902. // if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {
  903. // return true
  904. // }
  905. // if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {
  906. // return true
  907. // }
  908. if node.IsStatic {
  909. nodeId = node.StaticNode.ClientID
  910. node = node.StaticNode.ConvertToStaticNode()
  911. } else {
  912. nodeId = node.ID.String()
  913. }
  914. if peer.IsStatic {
  915. peerId = peer.StaticNode.ClientID
  916. peer = peer.StaticNode.ConvertToStaticNode()
  917. } else {
  918. peerId = peer.ID.String()
  919. }
  920. peerTags := make(map[models.TagID]struct{})
  921. nodeTags := make(map[models.TagID]struct{})
  922. nodeTags[models.TagID(nodeId)] = struct{}{}
  923. peerTags[models.TagID(peerId)] = struct{}{}
  924. if peer.IsGw {
  925. peerTags[models.TagID(fmt.Sprintf("%s.%s", peer.Network, models.GwTagName))] = struct{}{}
  926. }
  927. if node.IsGw {
  928. nodeTags[models.TagID(fmt.Sprintf("%s.%s", node.Network, models.GwTagName))] = struct{}{}
  929. }
  930. if checkDefaultPolicy {
  931. // check default policy if all allowed return true
  932. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  933. if err == nil {
  934. if defaultPolicy.Enabled {
  935. return true
  936. }
  937. }
  938. }
  939. // list device policies
  940. policies := ListDevicePolicies(models.NetworkID(peer.Network))
  941. srcMap := make(map[string]struct{})
  942. dstMap := make(map[string]struct{})
  943. defer func() {
  944. srcMap = nil
  945. dstMap = nil
  946. }()
  947. for _, policy := range policies {
  948. if !policy.Enabled {
  949. continue
  950. }
  951. srcMap = ConvAclTagToValueMap(policy.Src)
  952. dstMap = ConvAclTagToValueMap(policy.Dst)
  953. for _, dst := range policy.Dst {
  954. if dst.ID == models.EgressID {
  955. e := schema.Egress{ID: dst.Value}
  956. err := e.Get(db.WithContext(context.TODO()))
  957. if err == nil && e.Status {
  958. for nodeID := range e.Nodes {
  959. dstMap[nodeID] = struct{}{}
  960. }
  961. }
  962. }
  963. }
  964. if CheckTagGroupPolicy(srcMap, dstMap, node, peer, nodeTags, peerTags) {
  965. return true
  966. }
  967. }
  968. return false
  969. }
  970. func CheckTagGroupPolicy(srcMap, dstMap map[string]struct{}, node, peer models.Node,
  971. nodeTags, peerTags map[models.TagID]struct{}) bool {
  972. // check for node ID
  973. if _, ok := srcMap[node.ID.String()]; ok {
  974. if _, ok = dstMap[peer.ID.String()]; ok {
  975. return true
  976. }
  977. }
  978. if _, ok := dstMap[node.ID.String()]; ok {
  979. if _, ok = srcMap[peer.ID.String()]; ok {
  980. return true
  981. }
  982. }
  983. for tagID := range nodeTags {
  984. if _, ok := dstMap[tagID.String()]; ok {
  985. if _, ok := srcMap["*"]; ok {
  986. return true
  987. }
  988. for tagID := range peerTags {
  989. if _, ok := srcMap[tagID.String()]; ok {
  990. return true
  991. }
  992. }
  993. }
  994. if _, ok := srcMap[tagID.String()]; ok {
  995. if _, ok := dstMap["*"]; ok {
  996. return true
  997. }
  998. for tagID := range peerTags {
  999. if _, ok := dstMap[tagID.String()]; ok {
  1000. return true
  1001. }
  1002. }
  1003. }
  1004. }
  1005. for tagID := range peerTags {
  1006. if _, ok := dstMap[tagID.String()]; ok {
  1007. if _, ok := srcMap["*"]; ok {
  1008. return true
  1009. }
  1010. for tagID := range nodeTags {
  1011. if _, ok := srcMap[tagID.String()]; ok {
  1012. return true
  1013. }
  1014. }
  1015. }
  1016. if _, ok := srcMap[tagID.String()]; ok {
  1017. if _, ok := dstMap["*"]; ok {
  1018. return true
  1019. }
  1020. for tagID := range nodeTags {
  1021. if _, ok := dstMap[tagID.String()]; ok {
  1022. return true
  1023. }
  1024. }
  1025. }
  1026. }
  1027. return false
  1028. }
  1029. var (
  1030. CreateDefaultTags = func(netID models.NetworkID) {}
  1031. DeleteAllNetworkTags = func(networkID models.NetworkID) {}
  1032. IsUserAllowedToCommunicate = func(userName string, peer models.Node) (bool, []models.Acl) {
  1033. return false, []models.Acl{}
  1034. }
  1035. RemoveUserFromAclPolicy = func(userName string) {}
  1036. )
  1037. var (
  1038. aclCacheMutex = &sync.RWMutex{}
  1039. aclCacheMap = make(map[string]models.Acl)
  1040. )
  1041. func MigrateAclPolicies() {
  1042. acls := ListAcls()
  1043. for _, acl := range acls {
  1044. if acl.Proto.String() == "" {
  1045. acl.Proto = models.ALL
  1046. acl.ServiceType = models.Any
  1047. acl.Port = []string{}
  1048. UpsertAcl(acl)
  1049. }
  1050. if !servercfg.IsPro {
  1051. if acl.AllowedDirection == models.TrafficDirectionUni {
  1052. acl.AllowedDirection = models.TrafficDirectionBi
  1053. UpsertAcl(acl)
  1054. }
  1055. }
  1056. }
  1057. }
  1058. func IsNodeAllowedToCommunicateWithAllRsrcs(node models.Node) bool {
  1059. // check default policy if all allowed return true
  1060. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  1061. if err == nil {
  1062. if defaultPolicy.Enabled {
  1063. return true
  1064. }
  1065. }
  1066. var nodeId string
  1067. if node.IsStatic {
  1068. nodeId = node.StaticNode.ClientID
  1069. node = node.StaticNode.ConvertToStaticNode()
  1070. } else {
  1071. nodeId = node.ID.String()
  1072. }
  1073. var nodeTags map[models.TagID]struct{}
  1074. if node.Mutex != nil {
  1075. node.Mutex.Lock()
  1076. nodeTags = maps.Clone(node.Tags)
  1077. node.Mutex.Unlock()
  1078. } else {
  1079. nodeTags = maps.Clone(node.Tags)
  1080. }
  1081. if nodeTags == nil {
  1082. nodeTags = make(map[models.TagID]struct{})
  1083. }
  1084. nodeTags[models.TagID(node.ID.String())] = struct{}{}
  1085. nodeTags["*"] = struct{}{}
  1086. nodeTags[models.TagID(nodeId)] = struct{}{}
  1087. if !servercfg.IsPro && node.IsGw {
  1088. node.Tags[models.TagID(fmt.Sprintf("%s.%s", node.Network, models.GwTagName))] = struct{}{}
  1089. }
  1090. // list device policies
  1091. policies := ListDevicePolicies(models.NetworkID(node.Network))
  1092. srcMap := make(map[string]struct{})
  1093. dstMap := make(map[string]struct{})
  1094. defer func() {
  1095. srcMap = nil
  1096. dstMap = nil
  1097. }()
  1098. if CheckIfAnyPolicyisUniDirectional(node, policies) {
  1099. return false
  1100. }
  1101. for _, policy := range policies {
  1102. if !policy.Enabled {
  1103. continue
  1104. }
  1105. srcMap = ConvAclTagToValueMap(policy.Src)
  1106. dstMap = ConvAclTagToValueMap(policy.Dst)
  1107. _, srcAll := srcMap["*"]
  1108. _, dstAll := dstMap["*"]
  1109. for tagID := range nodeTags {
  1110. if srcAll {
  1111. if _, ok := dstMap[tagID.String()]; ok {
  1112. return true
  1113. }
  1114. }
  1115. if dstAll {
  1116. if _, ok := srcMap[tagID.String()]; ok {
  1117. return true
  1118. }
  1119. }
  1120. }
  1121. }
  1122. return false
  1123. }
  1124. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer // ADD ALLOWED DIRECTION - 0 => node -> peer, 1 => peer-> node,
  1125. func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
  1126. var nodeId, peerId string
  1127. // if peer.IsFailOver && node.FailedOverBy != uuid.Nil && node.FailedOverBy == peer.ID {
  1128. // return true, []models.Acl{}
  1129. // }
  1130. // if node.IsFailOver && peer.FailedOverBy != uuid.Nil && peer.FailedOverBy == node.ID {
  1131. // return true, []models.Acl{}
  1132. // }
  1133. // if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {
  1134. // return true, []models.Acl{}
  1135. // }
  1136. // if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {
  1137. // return true, []models.Acl{}
  1138. // }
  1139. if node.IsStatic {
  1140. nodeId = node.StaticNode.ClientID
  1141. node = node.StaticNode.ConvertToStaticNode()
  1142. } else {
  1143. nodeId = node.ID.String()
  1144. }
  1145. if peer.IsStatic {
  1146. peerId = peer.StaticNode.ClientID
  1147. peer = peer.StaticNode.ConvertToStaticNode()
  1148. } else {
  1149. peerId = peer.ID.String()
  1150. }
  1151. var nodeTags, peerTags map[models.TagID]struct{}
  1152. if node.Mutex != nil {
  1153. node.Mutex.Lock()
  1154. nodeTags = maps.Clone(node.Tags)
  1155. node.Mutex.Unlock()
  1156. } else {
  1157. nodeTags = node.Tags
  1158. }
  1159. if peer.Mutex != nil {
  1160. peer.Mutex.Lock()
  1161. peerTags = maps.Clone(peer.Tags)
  1162. peer.Mutex.Unlock()
  1163. } else {
  1164. peerTags = peer.Tags
  1165. }
  1166. if nodeTags == nil {
  1167. nodeTags = make(map[models.TagID]struct{})
  1168. }
  1169. if peerTags == nil {
  1170. peerTags = make(map[models.TagID]struct{})
  1171. }
  1172. nodeTags[models.TagID(nodeId)] = struct{}{}
  1173. peerTags[models.TagID(peerId)] = struct{}{}
  1174. if checkDefaultPolicy {
  1175. // check default policy if all allowed return true
  1176. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  1177. if err == nil {
  1178. if defaultPolicy.Enabled {
  1179. return true, []models.Acl{defaultPolicy}
  1180. }
  1181. }
  1182. }
  1183. allowedPolicies := []models.Acl{}
  1184. defer func() {
  1185. allowedPolicies = UniquePolicies(allowedPolicies)
  1186. }()
  1187. // list device policies
  1188. policies := ListDevicePolicies(models.NetworkID(peer.Network))
  1189. srcMap := make(map[string]struct{})
  1190. dstMap := make(map[string]struct{})
  1191. defer func() {
  1192. srcMap = nil
  1193. dstMap = nil
  1194. }()
  1195. for _, policy := range policies {
  1196. if !policy.Enabled {
  1197. continue
  1198. }
  1199. allowed := false
  1200. srcMap = ConvAclTagToValueMap(policy.Src)
  1201. dstMap = ConvAclTagToValueMap(policy.Dst)
  1202. for _, dst := range policy.Dst {
  1203. if dst.ID == models.EgressID {
  1204. e := schema.Egress{ID: dst.Value}
  1205. err := e.Get(db.WithContext(context.TODO()))
  1206. if err == nil && e.Status {
  1207. for nodeID := range e.Nodes {
  1208. dstMap[nodeID] = struct{}{}
  1209. }
  1210. }
  1211. }
  1212. }
  1213. _, srcAll := srcMap["*"]
  1214. _, dstAll := dstMap["*"]
  1215. if policy.AllowedDirection == models.TrafficDirectionBi {
  1216. if _, ok := srcMap[nodeId]; ok || srcAll {
  1217. if _, ok := dstMap[peerId]; ok || dstAll {
  1218. allowedPolicies = append(allowedPolicies, policy)
  1219. continue
  1220. }
  1221. }
  1222. if _, ok := dstMap[nodeId]; ok || dstAll {
  1223. if _, ok := srcMap[peerId]; ok || srcAll {
  1224. allowedPolicies = append(allowedPolicies, policy)
  1225. continue
  1226. }
  1227. }
  1228. }
  1229. if _, ok := dstMap[peerId]; ok || dstAll {
  1230. if _, ok := srcMap[nodeId]; ok || srcAll {
  1231. allowedPolicies = append(allowedPolicies, policy)
  1232. continue
  1233. }
  1234. }
  1235. if policy.AllowedDirection == models.TrafficDirectionBi {
  1236. for tagID := range nodeTags {
  1237. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  1238. if srcAll {
  1239. allowed = true
  1240. break
  1241. }
  1242. for tagID := range peerTags {
  1243. if _, ok := srcMap[tagID.String()]; ok {
  1244. allowed = true
  1245. break
  1246. }
  1247. }
  1248. }
  1249. if allowed {
  1250. allowedPolicies = append(allowedPolicies, policy)
  1251. break
  1252. }
  1253. if _, ok := srcMap[tagID.String()]; ok || srcAll {
  1254. if dstAll {
  1255. allowed = true
  1256. break
  1257. }
  1258. for tagID := range peerTags {
  1259. if _, ok := dstMap[tagID.String()]; ok {
  1260. allowed = true
  1261. break
  1262. }
  1263. }
  1264. }
  1265. if allowed {
  1266. break
  1267. }
  1268. }
  1269. if allowed {
  1270. allowedPolicies = append(allowedPolicies, policy)
  1271. continue
  1272. }
  1273. }
  1274. for tagID := range peerTags {
  1275. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  1276. if srcAll {
  1277. allowed = true
  1278. break
  1279. }
  1280. for tagID := range nodeTags {
  1281. if _, ok := srcMap[tagID.String()]; ok {
  1282. allowed = true
  1283. break
  1284. }
  1285. }
  1286. }
  1287. if allowed {
  1288. break
  1289. }
  1290. }
  1291. if allowed {
  1292. allowedPolicies = append(allowedPolicies, policy)
  1293. }
  1294. }
  1295. if len(allowedPolicies) > 0 {
  1296. return true, allowedPolicies
  1297. }
  1298. return false, allowedPolicies
  1299. }
  1300. // GetDefaultPolicy - fetches default policy in the network by ruleType
  1301. func GetDefaultPolicy(netID models.NetworkID, ruleType models.AclPolicyType) (models.Acl, error) {
  1302. aclID := "all-users"
  1303. if ruleType == models.DevicePolicy {
  1304. aclID = "all-nodes"
  1305. }
  1306. if !servercfg.IsPro && ruleType == models.UserPolicy {
  1307. return models.Acl{Enabled: true}, nil
  1308. }
  1309. acl, err := GetAcl(fmt.Sprintf("%s.%s", netID, aclID))
  1310. if err != nil {
  1311. return models.Acl{}, errors.New("default rule not found")
  1312. }
  1313. if acl.Enabled {
  1314. return acl, nil
  1315. }
  1316. // check if there are any custom all policies
  1317. srcMap := make(map[string]struct{})
  1318. dstMap := make(map[string]struct{})
  1319. defer func() {
  1320. srcMap = nil
  1321. dstMap = nil
  1322. }()
  1323. policies, _ := ListAclsByNetwork(netID)
  1324. for _, policy := range policies {
  1325. if !policy.Enabled {
  1326. continue
  1327. }
  1328. if policy.RuleType == ruleType {
  1329. dstMap = ConvAclTagToValueMap(policy.Dst)
  1330. srcMap = ConvAclTagToValueMap(policy.Src)
  1331. if _, ok := srcMap["*"]; ok {
  1332. if _, ok := dstMap["*"]; ok {
  1333. return policy, nil
  1334. }
  1335. }
  1336. }
  1337. }
  1338. return acl, nil
  1339. }
  1340. // ListAcls - lists all acl policies
  1341. func ListAclsByNetwork(netID models.NetworkID) ([]models.Acl, error) {
  1342. allAcls := ListAcls()
  1343. netAcls := []models.Acl{}
  1344. for _, acl := range allAcls {
  1345. if !servercfg.IsPro && acl.RuleType == models.UserPolicy {
  1346. continue
  1347. }
  1348. if acl.NetworkID == netID {
  1349. netAcls = append(netAcls, acl)
  1350. }
  1351. }
  1352. return netAcls, nil
  1353. }
  1354. // ListEgressAcls - list egress acl policies
  1355. func ListEgressAcls(eID string) ([]models.Acl, error) {
  1356. allAcls := ListAcls()
  1357. egressAcls := []models.Acl{}
  1358. for _, acl := range allAcls {
  1359. if !servercfg.IsPro && acl.RuleType == models.UserPolicy {
  1360. continue
  1361. }
  1362. for _, dst := range acl.Dst {
  1363. if dst.ID == models.EgressID && dst.Value == eID {
  1364. egressAcls = append(egressAcls, acl)
  1365. }
  1366. }
  1367. }
  1368. return egressAcls, nil
  1369. }
  1370. // ListDevicePolicies - lists all device policies in a network
  1371. func ListDevicePolicies(netID models.NetworkID) []models.Acl {
  1372. allAcls := ListAcls()
  1373. deviceAcls := []models.Acl{}
  1374. for _, acl := range allAcls {
  1375. if acl.NetworkID == netID && acl.RuleType == models.DevicePolicy {
  1376. deviceAcls = append(deviceAcls, acl)
  1377. }
  1378. }
  1379. return deviceAcls
  1380. }
  1381. func ConvAclTagToValueMap(acltags []models.AclPolicyTag) map[string]struct{} {
  1382. aclValueMap := make(map[string]struct{})
  1383. for _, aclTagI := range acltags {
  1384. aclValueMap[aclTagI.Value] = struct{}{}
  1385. }
  1386. return aclValueMap
  1387. }
  1388. func UniqueAclPolicyTags(tags []models.AclPolicyTag) []models.AclPolicyTag {
  1389. seen := make(map[string]bool)
  1390. var result []models.AclPolicyTag
  1391. for _, tag := range tags {
  1392. key := fmt.Sprintf("%v-%s", tag.ID, tag.Value)
  1393. if !seen[key] {
  1394. seen[key] = true
  1395. result = append(result, tag)
  1396. }
  1397. }
  1398. return result
  1399. }
  1400. // UpdateAcl - updates allowed fields on acls and commits to DB
  1401. func UpdateAcl(newAcl, acl models.Acl) error {
  1402. if !acl.Default {
  1403. acl.Name = newAcl.Name
  1404. acl.Src = newAcl.Src
  1405. acl.Dst = newAcl.Dst
  1406. acl.AllowedDirection = newAcl.AllowedDirection
  1407. acl.Port = newAcl.Port
  1408. acl.Proto = newAcl.Proto
  1409. acl.ServiceType = newAcl.ServiceType
  1410. }
  1411. if newAcl.ServiceType == models.Any {
  1412. acl.Port = []string{}
  1413. acl.Proto = models.ALL
  1414. }
  1415. acl.Enabled = newAcl.Enabled
  1416. d, err := json.Marshal(acl)
  1417. if err != nil {
  1418. return err
  1419. }
  1420. err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)
  1421. if err == nil && servercfg.CacheEnabled() {
  1422. storeAclInCache(acl)
  1423. }
  1424. return err
  1425. }
  1426. // UpsertAcl - upserts acl
  1427. func UpsertAcl(acl models.Acl) error {
  1428. d, err := json.Marshal(acl)
  1429. if err != nil {
  1430. return err
  1431. }
  1432. err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)
  1433. if err == nil && servercfg.CacheEnabled() {
  1434. storeAclInCache(acl)
  1435. }
  1436. return err
  1437. }
  1438. // DeleteAcl - deletes acl policy
  1439. func DeleteAcl(a models.Acl) error {
  1440. err := database.DeleteRecord(database.ACLS_TABLE_NAME, a.ID)
  1441. if err == nil && servercfg.CacheEnabled() {
  1442. removeAclFromCache(a)
  1443. }
  1444. return err
  1445. }
  1446. func ListAcls() (acls []models.Acl) {
  1447. if servercfg.CacheEnabled() && len(aclCacheMap) > 0 {
  1448. return listAclFromCache()
  1449. }
  1450. data, err := database.FetchRecords(database.ACLS_TABLE_NAME)
  1451. if err != nil && !database.IsEmptyRecord(err) {
  1452. return []models.Acl{}
  1453. }
  1454. for _, dataI := range data {
  1455. acl := models.Acl{}
  1456. err := json.Unmarshal([]byte(dataI), &acl)
  1457. if err != nil {
  1458. continue
  1459. }
  1460. if !servercfg.IsPro {
  1461. if acl.RuleType == models.UserPolicy {
  1462. continue
  1463. }
  1464. skip := false
  1465. for _, srcI := range acl.Src {
  1466. if srcI.ID == models.NodeTagID && (srcI.Value != "*" && srcI.Value != fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName)) {
  1467. skip = true
  1468. break
  1469. }
  1470. }
  1471. if skip {
  1472. continue
  1473. }
  1474. for _, dstI := range acl.Dst {
  1475. if dstI.ID == models.NodeTagID && (dstI.Value != "*" && dstI.Value != fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName)) {
  1476. skip = true
  1477. break
  1478. }
  1479. }
  1480. if skip {
  1481. continue
  1482. }
  1483. }
  1484. acls = append(acls, acl)
  1485. if servercfg.CacheEnabled() {
  1486. storeAclInCache(acl)
  1487. }
  1488. }
  1489. return
  1490. }
  1491. func UniquePolicies(items []models.Acl) []models.Acl {
  1492. if len(items) == 0 {
  1493. return items
  1494. }
  1495. seen := make(map[string]bool)
  1496. var result []models.Acl
  1497. for _, item := range items {
  1498. if !seen[item.ID] {
  1499. seen[item.ID] = true
  1500. result = append(result, item)
  1501. }
  1502. }
  1503. return result
  1504. }
  1505. // DeleteNetworkPolicies - deletes all default network acl policies
  1506. func DeleteNetworkPolicies(netId models.NetworkID) {
  1507. acls, _ := ListAclsByNetwork(netId)
  1508. for _, acl := range acls {
  1509. if acl.NetworkID == netId {
  1510. DeleteAcl(acl)
  1511. }
  1512. }
  1513. }
  1514. // SortTagEntrys - Sorts slice of Tag entries by their id
  1515. func SortAclEntrys(acls []models.Acl) {
  1516. sort.Slice(acls, func(i, j int) bool {
  1517. return acls[i].Name < acls[j].Name
  1518. })
  1519. }
  1520. // ValidateCreateAclReq - validates create req for acl
  1521. func ValidateCreateAclReq(req models.Acl) error {
  1522. // check if acl network exists
  1523. _, err := GetNetwork(req.NetworkID.String())
  1524. if err != nil {
  1525. return errors.New("failed to get network details for " + req.NetworkID.String())
  1526. }
  1527. // err = CheckIDSyntax(req.Name)
  1528. // if err != nil {
  1529. // return err
  1530. // }
  1531. for _, src := range req.Src {
  1532. if src.ID == models.UserGroupAclID {
  1533. userGroup, err := GetUserGroup(models.UserGroupID(src.Value))
  1534. if err != nil {
  1535. return err
  1536. }
  1537. _, ok := userGroup.NetworkRoles[models.AllNetworks]
  1538. if ok {
  1539. continue
  1540. }
  1541. _, ok = userGroup.NetworkRoles[req.NetworkID]
  1542. if !ok {
  1543. return fmt.Errorf("user group %s does not have access to network %s", src.Value, req.NetworkID)
  1544. }
  1545. }
  1546. }
  1547. return nil
  1548. }
  1549. func listAclFromCache() (acls []models.Acl) {
  1550. aclCacheMutex.RLock()
  1551. defer aclCacheMutex.RUnlock()
  1552. for _, acl := range aclCacheMap {
  1553. acls = append(acls, acl)
  1554. }
  1555. return
  1556. }
  1557. func storeAclInCache(a models.Acl) {
  1558. aclCacheMutex.Lock()
  1559. defer aclCacheMutex.Unlock()
  1560. aclCacheMap[a.ID] = a
  1561. }
  1562. func removeAclFromCache(a models.Acl) {
  1563. aclCacheMutex.Lock()
  1564. defer aclCacheMutex.Unlock()
  1565. delete(aclCacheMap, a.ID)
  1566. }
  1567. func getAclFromCache(aID string) (a models.Acl, ok bool) {
  1568. aclCacheMutex.RLock()
  1569. defer aclCacheMutex.RUnlock()
  1570. a, ok = aclCacheMap[aID]
  1571. return
  1572. }
  1573. // InsertAcl - creates acl policy
  1574. func InsertAcl(a models.Acl) error {
  1575. d, err := json.Marshal(a)
  1576. if err != nil {
  1577. return err
  1578. }
  1579. err = database.Insert(a.ID, string(d), database.ACLS_TABLE_NAME)
  1580. if err == nil && servercfg.CacheEnabled() {
  1581. storeAclInCache(a)
  1582. }
  1583. return err
  1584. }
  1585. // GetAcl - gets acl info by id
  1586. func GetAcl(aID string) (models.Acl, error) {
  1587. a := models.Acl{}
  1588. if servercfg.CacheEnabled() {
  1589. var ok bool
  1590. a, ok = getAclFromCache(aID)
  1591. if ok {
  1592. return a, nil
  1593. }
  1594. }
  1595. d, err := database.FetchRecord(database.ACLS_TABLE_NAME, aID)
  1596. if err != nil {
  1597. return a, err
  1598. }
  1599. err = json.Unmarshal([]byte(d), &a)
  1600. if err != nil {
  1601. return a, err
  1602. }
  1603. if servercfg.CacheEnabled() {
  1604. storeAclInCache(a)
  1605. }
  1606. return a, nil
  1607. }
  1608. // IsAclExists - checks if acl exists
  1609. func IsAclExists(aclID string) bool {
  1610. _, err := GetAcl(aclID)
  1611. return err == nil
  1612. }
  1613. func RemoveNodeFromAclPolicy(node models.Node) {
  1614. var nodeID string
  1615. if node.IsStatic {
  1616. nodeID = node.StaticNode.ClientID
  1617. } else {
  1618. nodeID = node.ID.String()
  1619. }
  1620. acls, _ := ListAclsByNetwork(models.NetworkID(node.Network))
  1621. for _, acl := range acls {
  1622. delete := false
  1623. update := false
  1624. if acl.RuleType == models.DevicePolicy {
  1625. for i := len(acl.Src) - 1; i >= 0; i-- {
  1626. if acl.Src[i].ID == models.NodeID && acl.Src[i].Value == nodeID {
  1627. if len(acl.Src) == 1 {
  1628. // delete policy
  1629. delete = true
  1630. break
  1631. } else {
  1632. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  1633. update = true
  1634. }
  1635. }
  1636. }
  1637. if delete {
  1638. DeleteAcl(acl)
  1639. continue
  1640. }
  1641. for i := len(acl.Dst) - 1; i >= 0; i-- {
  1642. if acl.Dst[i].ID == models.NodeID && acl.Dst[i].Value == nodeID {
  1643. if len(acl.Dst) == 1 {
  1644. // delete policy
  1645. delete = true
  1646. break
  1647. } else {
  1648. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  1649. update = true
  1650. }
  1651. }
  1652. }
  1653. if delete {
  1654. DeleteAcl(acl)
  1655. continue
  1656. }
  1657. if update {
  1658. UpsertAcl(acl)
  1659. }
  1660. }
  1661. if acl.RuleType == models.UserPolicy {
  1662. for i := len(acl.Dst) - 1; i >= 0; i-- {
  1663. if acl.Dst[i].ID == models.NodeID && acl.Dst[i].Value == nodeID {
  1664. if len(acl.Dst) == 1 {
  1665. // delete policy
  1666. delete = true
  1667. break
  1668. } else {
  1669. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  1670. update = true
  1671. }
  1672. }
  1673. }
  1674. if delete {
  1675. DeleteAcl(acl)
  1676. continue
  1677. }
  1678. if update {
  1679. UpsertAcl(acl)
  1680. }
  1681. }
  1682. }
  1683. }
  1684. // CreateDefaultAclNetworkPolicies - create default acl network policies
  1685. func CreateDefaultAclNetworkPolicies(netID models.NetworkID) {
  1686. if netID.String() == "" {
  1687. return
  1688. }
  1689. _, _ = ListAclsByNetwork(netID)
  1690. if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-nodes")) {
  1691. defaultDeviceAcl := models.Acl{
  1692. ID: fmt.Sprintf("%s.%s", netID, "all-nodes"),
  1693. Name: "All Nodes",
  1694. MetaData: "This Policy allows all nodes in the network to communicate with each other",
  1695. Default: true,
  1696. NetworkID: netID,
  1697. Proto: models.ALL,
  1698. ServiceType: models.Any,
  1699. Port: []string{},
  1700. RuleType: models.DevicePolicy,
  1701. Src: []models.AclPolicyTag{
  1702. {
  1703. ID: models.NodeTagID,
  1704. Value: "*",
  1705. }},
  1706. Dst: []models.AclPolicyTag{
  1707. {
  1708. ID: models.NodeTagID,
  1709. Value: "*",
  1710. }},
  1711. AllowedDirection: models.TrafficDirectionBi,
  1712. Enabled: true,
  1713. CreatedBy: "auto",
  1714. CreatedAt: time.Now().UTC(),
  1715. }
  1716. InsertAcl(defaultDeviceAcl)
  1717. }
  1718. if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-gateways")) {
  1719. defaultUserAcl := models.Acl{
  1720. ID: fmt.Sprintf("%s.%s", netID, "all-gateways"),
  1721. Default: true,
  1722. Name: "All Gateways",
  1723. NetworkID: netID,
  1724. Proto: models.ALL,
  1725. ServiceType: models.Any,
  1726. Port: []string{},
  1727. RuleType: models.DevicePolicy,
  1728. Src: []models.AclPolicyTag{
  1729. {
  1730. ID: models.NodeTagID,
  1731. Value: fmt.Sprintf("%s.%s", netID, models.GwTagName),
  1732. },
  1733. },
  1734. Dst: []models.AclPolicyTag{
  1735. {
  1736. ID: models.NodeTagID,
  1737. Value: "*",
  1738. },
  1739. },
  1740. AllowedDirection: models.TrafficDirectionBi,
  1741. Enabled: true,
  1742. CreatedBy: "auto",
  1743. CreatedAt: time.Now().UTC(),
  1744. }
  1745. InsertAcl(defaultUserAcl)
  1746. }
  1747. CreateDefaultUserPolicies(netID)
  1748. }
  1749. func getTagMapWithNodesByNetwork(netID models.NetworkID, withStaticNodes bool) (tagNodesMap map[models.TagID][]models.Node) {
  1750. tagNodesMap = make(map[models.TagID][]models.Node)
  1751. nodes, _ := GetNetworkNodes(netID.String())
  1752. netGwTag := models.TagID(fmt.Sprintf("%s.%s", netID.String(), models.GwTagName))
  1753. for _, nodeI := range nodes {
  1754. tagNodesMap[models.TagID(nodeI.ID.String())] = append(tagNodesMap[models.TagID(nodeI.ID.String())], nodeI)
  1755. if nodeI.IsGw {
  1756. tagNodesMap[netGwTag] = append(tagNodesMap[netGwTag], nodeI)
  1757. }
  1758. }
  1759. tagNodesMap["*"] = nodes
  1760. if !withStaticNodes {
  1761. return
  1762. }
  1763. return addTagMapWithStaticNodes(netID, tagNodesMap)
  1764. }
  1765. func addTagMapWithStaticNodes(netID models.NetworkID,
  1766. tagNodesMap map[models.TagID][]models.Node) map[models.TagID][]models.Node {
  1767. extclients, err := GetNetworkExtClients(netID.String())
  1768. if err != nil {
  1769. return tagNodesMap
  1770. }
  1771. for _, extclient := range extclients {
  1772. if extclient.RemoteAccessClientID != "" {
  1773. continue
  1774. }
  1775. tagNodesMap[models.TagID(extclient.ClientID)] = []models.Node{
  1776. {
  1777. IsStatic: true,
  1778. StaticNode: extclient,
  1779. },
  1780. }
  1781. tagNodesMap["*"] = append(tagNodesMap["*"], extclient.ConvertToStaticNode())
  1782. }
  1783. return tagNodesMap
  1784. }