acls.go 50 KB

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