acls.go 49 KB

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