acls.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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. if nodeTags == nil {
  632. nodeTags = make(map[models.TagID]struct{})
  633. }
  634. if peerTags == nil {
  635. peerTags = make(map[models.TagID]struct{})
  636. }
  637. nodeTags[models.TagID(nodeId)] = struct{}{}
  638. peerTags[models.TagID(peerId)] = struct{}{}
  639. if checkDefaultPolicy {
  640. // check default policy if all allowed return true
  641. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  642. if err == nil {
  643. if defaultPolicy.Enabled {
  644. return true
  645. }
  646. }
  647. }
  648. // list device policies
  649. policies := listDevicePolicies(models.NetworkID(peer.Network))
  650. srcMap := make(map[string]struct{})
  651. dstMap := make(map[string]struct{})
  652. defer func() {
  653. srcMap = nil
  654. dstMap = nil
  655. }()
  656. for _, policy := range policies {
  657. if !policy.Enabled {
  658. continue
  659. }
  660. srcMap = convAclTagToValueMap(policy.Src)
  661. dstMap = convAclTagToValueMap(policy.Dst)
  662. if checkTagGroupPolicy(srcMap, dstMap, node, peer, nodeTags, peerTags) {
  663. return true
  664. }
  665. }
  666. return false
  667. }
  668. func RemoveUserFromAclPolicy(userName string) {
  669. acls := ListAcls()
  670. for _, acl := range acls {
  671. delete := false
  672. update := false
  673. if acl.RuleType == models.UserPolicy {
  674. for i := len(acl.Src) - 1; i >= 0; i-- {
  675. if acl.Src[i].ID == models.UserAclID && acl.Src[i].Value == userName {
  676. if len(acl.Src) == 1 {
  677. // delete policy
  678. delete = true
  679. break
  680. } else {
  681. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  682. update = true
  683. }
  684. }
  685. }
  686. if delete {
  687. DeleteAcl(acl)
  688. continue
  689. }
  690. if update {
  691. UpsertAcl(acl)
  692. }
  693. }
  694. }
  695. }
  696. func RemoveNodeFromAclPolicy(node models.Node) {
  697. var nodeID string
  698. if node.IsStatic {
  699. nodeID = node.StaticNode.ClientID
  700. } else {
  701. nodeID = node.ID.String()
  702. }
  703. acls, _ := ListAclsByNetwork(models.NetworkID(node.Network))
  704. for _, acl := range acls {
  705. delete := false
  706. update := false
  707. if acl.RuleType == models.DevicePolicy {
  708. for i := len(acl.Src) - 1; i >= 0; i-- {
  709. if acl.Src[i].ID == models.NodeID && acl.Src[i].Value == nodeID {
  710. if len(acl.Src) == 1 {
  711. // delete policy
  712. delete = true
  713. break
  714. } else {
  715. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  716. update = true
  717. }
  718. }
  719. }
  720. if delete {
  721. DeleteAcl(acl)
  722. continue
  723. }
  724. for i := len(acl.Dst) - 1; i >= 0; i-- {
  725. if acl.Dst[i].ID == models.NodeID && acl.Dst[i].Value == nodeID {
  726. if len(acl.Dst) == 1 {
  727. // delete policy
  728. delete = true
  729. break
  730. } else {
  731. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  732. update = true
  733. }
  734. }
  735. }
  736. if delete {
  737. DeleteAcl(acl)
  738. continue
  739. }
  740. if update {
  741. UpsertAcl(acl)
  742. }
  743. }
  744. if acl.RuleType == models.UserPolicy {
  745. for i := len(acl.Dst) - 1; i >= 0; i-- {
  746. if acl.Dst[i].ID == models.NodeID && acl.Dst[i].Value == nodeID {
  747. if len(acl.Dst) == 1 {
  748. // delete policy
  749. delete = true
  750. break
  751. } else {
  752. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  753. update = true
  754. }
  755. }
  756. }
  757. if delete {
  758. DeleteAcl(acl)
  759. continue
  760. }
  761. if update {
  762. UpsertAcl(acl)
  763. }
  764. }
  765. }
  766. }
  767. func checkTagGroupPolicy(srcMap, dstMap map[string]struct{}, node, peer models.Node,
  768. nodeTags, peerTags map[models.TagID]struct{}) bool {
  769. // check for node ID
  770. if _, ok := srcMap[node.ID.String()]; ok {
  771. if _, ok = dstMap[peer.ID.String()]; ok {
  772. return true
  773. }
  774. }
  775. if _, ok := dstMap[node.ID.String()]; ok {
  776. if _, ok = srcMap[peer.ID.String()]; ok {
  777. return true
  778. }
  779. }
  780. for tagID := range nodeTags {
  781. if _, ok := dstMap[tagID.String()]; ok {
  782. if _, ok := srcMap["*"]; ok {
  783. return true
  784. }
  785. for tagID := range peerTags {
  786. if _, ok := srcMap[tagID.String()]; ok {
  787. return true
  788. }
  789. }
  790. }
  791. if _, ok := srcMap[tagID.String()]; ok {
  792. if _, ok := dstMap["*"]; ok {
  793. return true
  794. }
  795. for tagID := range peerTags {
  796. if _, ok := dstMap[tagID.String()]; ok {
  797. return true
  798. }
  799. }
  800. }
  801. }
  802. for tagID := range peerTags {
  803. if _, ok := dstMap[tagID.String()]; ok {
  804. if _, ok := srcMap["*"]; ok {
  805. return true
  806. }
  807. for tagID := range nodeTags {
  808. if _, ok := srcMap[tagID.String()]; ok {
  809. return true
  810. }
  811. }
  812. }
  813. if _, ok := srcMap[tagID.String()]; ok {
  814. if _, ok := dstMap["*"]; ok {
  815. return true
  816. }
  817. for tagID := range nodeTags {
  818. if _, ok := dstMap[tagID.String()]; ok {
  819. return true
  820. }
  821. }
  822. }
  823. }
  824. return false
  825. }
  826. func uniquePolicies(items []models.Acl) []models.Acl {
  827. if len(items) == 0 {
  828. return items
  829. }
  830. seen := make(map[string]bool)
  831. var result []models.Acl
  832. for _, item := range items {
  833. if !seen[item.ID] {
  834. seen[item.ID] = true
  835. result = append(result, item)
  836. }
  837. }
  838. return result
  839. }
  840. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer // ADD ALLOWED DIRECTION - 0 => node -> peer, 1 => peer-> node,
  841. func IsNodeAllowedToCommunicateV1(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
  842. var nodeId, peerId string
  843. if node.IsStatic {
  844. nodeId = node.StaticNode.ClientID
  845. node = node.StaticNode.ConvertToStaticNode()
  846. } else {
  847. nodeId = node.ID.String()
  848. }
  849. if peer.IsStatic {
  850. peerId = peer.StaticNode.ClientID
  851. peer = peer.StaticNode.ConvertToStaticNode()
  852. } else {
  853. peerId = peer.ID.String()
  854. }
  855. var nodeTags, peerTags map[models.TagID]struct{}
  856. if node.Mutex != nil {
  857. node.Mutex.Lock()
  858. nodeTags = maps.Clone(node.Tags)
  859. node.Mutex.Unlock()
  860. } else {
  861. nodeTags = node.Tags
  862. }
  863. if peer.Mutex != nil {
  864. peer.Mutex.Lock()
  865. peerTags = maps.Clone(peer.Tags)
  866. peer.Mutex.Unlock()
  867. } else {
  868. peerTags = peer.Tags
  869. }
  870. if nodeTags == nil {
  871. nodeTags = make(map[models.TagID]struct{})
  872. }
  873. if peerTags == nil {
  874. peerTags = make(map[models.TagID]struct{})
  875. }
  876. nodeTags[models.TagID(nodeId)] = struct{}{}
  877. peerTags[models.TagID(peerId)] = struct{}{}
  878. if checkDefaultPolicy {
  879. // check default policy if all allowed return true
  880. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  881. if err == nil {
  882. if defaultPolicy.Enabled {
  883. return true, []models.Acl{defaultPolicy}
  884. }
  885. }
  886. }
  887. allowedPolicies := []models.Acl{}
  888. defer func() {
  889. allowedPolicies = uniquePolicies(allowedPolicies)
  890. }()
  891. // list device policies
  892. policies := listDevicePolicies(models.NetworkID(peer.Network))
  893. srcMap := make(map[string]struct{})
  894. dstMap := make(map[string]struct{})
  895. defer func() {
  896. srcMap = nil
  897. dstMap = nil
  898. }()
  899. for _, policy := range policies {
  900. if !policy.Enabled {
  901. continue
  902. }
  903. allowed := false
  904. srcMap = convAclTagToValueMap(policy.Src)
  905. dstMap = convAclTagToValueMap(policy.Dst)
  906. _, srcAll := srcMap["*"]
  907. _, dstAll := dstMap["*"]
  908. if policy.AllowedDirection == models.TrafficDirectionBi {
  909. if _, ok := srcMap[nodeId]; ok || srcAll {
  910. if _, ok := dstMap[peerId]; ok || dstAll {
  911. allowedPolicies = append(allowedPolicies, policy)
  912. continue
  913. }
  914. }
  915. if _, ok := dstMap[nodeId]; ok || dstAll {
  916. if _, ok := srcMap[peerId]; ok || srcAll {
  917. allowedPolicies = append(allowedPolicies, policy)
  918. continue
  919. }
  920. }
  921. }
  922. if _, ok := dstMap[peerId]; ok || dstAll {
  923. if _, ok := srcMap[nodeId]; ok || srcAll {
  924. allowedPolicies = append(allowedPolicies, policy)
  925. continue
  926. }
  927. }
  928. if policy.AllowedDirection == models.TrafficDirectionBi {
  929. for tagID := range nodeTags {
  930. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  931. if srcAll {
  932. allowed = true
  933. break
  934. }
  935. for tagID := range peerTags {
  936. if _, ok := srcMap[tagID.String()]; ok {
  937. allowed = true
  938. break
  939. }
  940. }
  941. }
  942. if allowed {
  943. allowedPolicies = append(allowedPolicies, policy)
  944. break
  945. }
  946. if _, ok := srcMap[tagID.String()]; ok || srcAll {
  947. if dstAll {
  948. allowed = true
  949. break
  950. }
  951. for tagID := range peerTags {
  952. if _, ok := dstMap[tagID.String()]; ok {
  953. allowed = true
  954. break
  955. }
  956. }
  957. }
  958. if allowed {
  959. break
  960. }
  961. }
  962. if allowed {
  963. allowedPolicies = append(allowedPolicies, policy)
  964. continue
  965. }
  966. }
  967. for tagID := range peerTags {
  968. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  969. if srcAll {
  970. allowed = true
  971. break
  972. }
  973. for tagID := range nodeTags {
  974. if _, ok := srcMap[tagID.String()]; ok {
  975. allowed = true
  976. break
  977. }
  978. }
  979. }
  980. if allowed {
  981. break
  982. }
  983. }
  984. if allowed {
  985. allowedPolicies = append(allowedPolicies, policy)
  986. }
  987. }
  988. if len(allowedPolicies) > 0 {
  989. return true, allowedPolicies
  990. }
  991. return false, allowedPolicies
  992. }
  993. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer
  994. func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
  995. var nodeId, peerId string
  996. if node.IsStatic {
  997. nodeId = node.StaticNode.ClientID
  998. node = node.StaticNode.ConvertToStaticNode()
  999. } else {
  1000. nodeId = node.ID.String()
  1001. }
  1002. if peer.IsStatic {
  1003. peerId = peer.StaticNode.ClientID
  1004. peer = peer.StaticNode.ConvertToStaticNode()
  1005. } else {
  1006. peerId = peer.ID.String()
  1007. }
  1008. var nodeTags, peerTags map[models.TagID]struct{}
  1009. if node.Mutex != nil {
  1010. node.Mutex.Lock()
  1011. nodeTags = maps.Clone(node.Tags)
  1012. node.Mutex.Unlock()
  1013. } else {
  1014. nodeTags = node.Tags
  1015. }
  1016. if peer.Mutex != nil {
  1017. peer.Mutex.Lock()
  1018. peerTags = maps.Clone(peer.Tags)
  1019. peer.Mutex.Unlock()
  1020. } else {
  1021. peerTags = peer.Tags
  1022. }
  1023. if nodeTags == nil {
  1024. nodeTags = make(map[models.TagID]struct{})
  1025. }
  1026. if peerTags == nil {
  1027. peerTags = make(map[models.TagID]struct{})
  1028. }
  1029. nodeTags[models.TagID(nodeId)] = struct{}{}
  1030. peerTags[models.TagID(peerId)] = struct{}{}
  1031. if checkDefaultPolicy {
  1032. // check default policy if all allowed return true
  1033. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  1034. if err == nil {
  1035. if defaultPolicy.Enabled {
  1036. return true, []models.Acl{defaultPolicy}
  1037. }
  1038. }
  1039. }
  1040. allowedPolicies := []models.Acl{}
  1041. // list device policies
  1042. policies := listDevicePolicies(models.NetworkID(peer.Network))
  1043. srcMap := make(map[string]struct{})
  1044. dstMap := make(map[string]struct{})
  1045. defer func() {
  1046. srcMap = nil
  1047. dstMap = nil
  1048. }()
  1049. for _, policy := range policies {
  1050. if !policy.Enabled {
  1051. continue
  1052. }
  1053. srcMap = convAclTagToValueMap(policy.Src)
  1054. dstMap = convAclTagToValueMap(policy.Dst)
  1055. _, srcAll := srcMap["*"]
  1056. _, dstAll := dstMap["*"]
  1057. if policy.AllowedDirection == models.TrafficDirectionBi {
  1058. if _, ok := srcMap[nodeId]; ok || srcAll {
  1059. if _, ok := dstMap[peerId]; ok || dstAll {
  1060. allowedPolicies = append(allowedPolicies, policy)
  1061. continue
  1062. }
  1063. }
  1064. if _, ok := dstMap[nodeId]; ok || dstAll {
  1065. if _, ok := srcMap[peerId]; ok || srcAll {
  1066. allowedPolicies = append(allowedPolicies, policy)
  1067. continue
  1068. }
  1069. }
  1070. }
  1071. if _, ok := dstMap[nodeId]; ok || dstAll {
  1072. if _, ok := srcMap[peerId]; ok || srcAll {
  1073. allowedPolicies = append(allowedPolicies, policy)
  1074. continue
  1075. }
  1076. }
  1077. for tagID := range nodeTags {
  1078. allowed := false
  1079. if _, ok := dstMap[tagID.String()]; policy.AllowedDirection == models.TrafficDirectionBi && ok || dstAll {
  1080. if srcAll {
  1081. allowed = true
  1082. allowedPolicies = append(allowedPolicies, policy)
  1083. break
  1084. }
  1085. for tagID := range peerTags {
  1086. if _, ok := srcMap[tagID.String()]; ok {
  1087. allowed = true
  1088. break
  1089. }
  1090. }
  1091. }
  1092. if allowed {
  1093. allowedPolicies = append(allowedPolicies, policy)
  1094. break
  1095. }
  1096. if _, ok := srcMap[tagID.String()]; ok || srcAll {
  1097. if dstAll {
  1098. allowed = true
  1099. allowedPolicies = append(allowedPolicies, policy)
  1100. break
  1101. }
  1102. for tagID := range peerTags {
  1103. if _, ok := dstMap[tagID.String()]; ok {
  1104. allowed = true
  1105. break
  1106. }
  1107. }
  1108. }
  1109. if allowed {
  1110. allowedPolicies = append(allowedPolicies, policy)
  1111. break
  1112. }
  1113. }
  1114. for tagID := range peerTags {
  1115. allowed := false
  1116. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  1117. if srcAll {
  1118. allowed = true
  1119. allowedPolicies = append(allowedPolicies, policy)
  1120. break
  1121. }
  1122. for tagID := range nodeTags {
  1123. if _, ok := srcMap[tagID.String()]; ok || srcAll {
  1124. allowed = true
  1125. break
  1126. }
  1127. }
  1128. }
  1129. if allowed {
  1130. allowedPolicies = append(allowedPolicies, policy)
  1131. break
  1132. }
  1133. if _, ok := srcMap[tagID.String()]; policy.AllowedDirection == models.TrafficDirectionBi && ok || srcAll {
  1134. if dstAll {
  1135. allowed = true
  1136. allowedPolicies = append(allowedPolicies, policy)
  1137. break
  1138. }
  1139. for tagID := range nodeTags {
  1140. if _, ok := dstMap[tagID.String()]; ok {
  1141. allowed = true
  1142. break
  1143. }
  1144. }
  1145. }
  1146. if allowed {
  1147. allowedPolicies = append(allowedPolicies, policy)
  1148. break
  1149. }
  1150. }
  1151. }
  1152. if len(allowedPolicies) > 0 {
  1153. return true, allowedPolicies
  1154. }
  1155. return false, allowedPolicies
  1156. }
  1157. // SortTagEntrys - Sorts slice of Tag entries by their id
  1158. func SortAclEntrys(acls []models.Acl) {
  1159. sort.Slice(acls, func(i, j int) bool {
  1160. return acls[i].Name < acls[j].Name
  1161. })
  1162. }
  1163. // UpdateDeviceTag - updates device tag on acl policies
  1164. func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
  1165. acls := listDevicePolicies(netID)
  1166. update := false
  1167. for _, acl := range acls {
  1168. for i, srcTagI := range acl.Src {
  1169. if srcTagI.ID == models.NodeTagID {
  1170. if OldID.String() == srcTagI.Value {
  1171. acl.Src[i].Value = newID.String()
  1172. update = true
  1173. }
  1174. }
  1175. }
  1176. for i, dstTagI := range acl.Dst {
  1177. if dstTagI.ID == models.NodeTagID {
  1178. if OldID.String() == dstTagI.Value {
  1179. acl.Dst[i].Value = newID.String()
  1180. update = true
  1181. }
  1182. }
  1183. }
  1184. if update {
  1185. UpsertAcl(acl)
  1186. }
  1187. }
  1188. }
  1189. func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
  1190. acls := listDevicePolicies(netID)
  1191. for _, acl := range acls {
  1192. for _, srcTagI := range acl.Src {
  1193. if srcTagI.ID == models.NodeTagID {
  1194. if tagID.String() == srcTagI.Value {
  1195. return true
  1196. }
  1197. }
  1198. }
  1199. for _, dstTagI := range acl.Dst {
  1200. if dstTagI.ID == models.NodeTagID {
  1201. if tagID.String() == dstTagI.Value {
  1202. return true
  1203. }
  1204. }
  1205. }
  1206. }
  1207. return false
  1208. }
  1209. // RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
  1210. func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
  1211. acls := listDevicePolicies(netID)
  1212. update := false
  1213. for _, acl := range acls {
  1214. for i := len(acl.Src) - 1; i >= 0; i-- {
  1215. if acl.Src[i].ID == models.NodeTagID {
  1216. if tagID.String() == acl.Src[i].Value {
  1217. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  1218. update = true
  1219. }
  1220. }
  1221. }
  1222. for i := len(acl.Dst) - 1; i >= 0; i-- {
  1223. if acl.Dst[i].ID == models.NodeTagID {
  1224. if tagID.String() == acl.Dst[i].Value {
  1225. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  1226. update = true
  1227. }
  1228. }
  1229. }
  1230. if update {
  1231. UpsertAcl(acl)
  1232. }
  1233. }
  1234. return nil
  1235. }
  1236. func getEgressUserRulesForNode(targetnode *models.Node,
  1237. rules map[string]models.AclRule) map[string]models.AclRule {
  1238. userNodes := GetStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  1239. userGrpMap := GetUserGrpMap()
  1240. allowedUsers := make(map[string][]models.Acl)
  1241. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  1242. var targetNodeTags = make(map[models.TagID]struct{})
  1243. targetNodeTags["*"] = struct{}{}
  1244. for _, rangeI := range targetnode.EgressGatewayRanges {
  1245. targetNodeTags[models.TagID(rangeI)] = struct{}{}
  1246. }
  1247. for _, acl := range acls {
  1248. if !acl.Enabled {
  1249. continue
  1250. }
  1251. dstTags := convAclTagToValueMap(acl.Dst)
  1252. _, all := dstTags["*"]
  1253. addUsers := false
  1254. if !all {
  1255. for nodeTag := range targetNodeTags {
  1256. if _, ok := dstTags[nodeTag.String()]; ok {
  1257. addUsers = true
  1258. break
  1259. }
  1260. }
  1261. } else {
  1262. addUsers = true
  1263. }
  1264. if addUsers {
  1265. // get all src tags
  1266. for _, srcAcl := range acl.Src {
  1267. if srcAcl.ID == models.UserAclID {
  1268. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  1269. } else if srcAcl.ID == models.UserGroupAclID {
  1270. // fetch all users in the group
  1271. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  1272. for userName := range usersMap {
  1273. allowedUsers[userName] = append(allowedUsers[userName], acl)
  1274. }
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. for _, userNode := range userNodes {
  1281. if !userNode.StaticNode.Enabled {
  1282. continue
  1283. }
  1284. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  1285. if !ok {
  1286. continue
  1287. }
  1288. for _, acl := range acls {
  1289. if !acl.Enabled {
  1290. continue
  1291. }
  1292. r := models.AclRule{
  1293. ID: acl.ID,
  1294. AllowedProtocol: acl.Proto,
  1295. AllowedPorts: acl.Port,
  1296. Direction: acl.AllowedDirection,
  1297. Allowed: true,
  1298. }
  1299. // Get peers in the tags and add allowed rules
  1300. if userNode.StaticNode.Address != "" {
  1301. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1302. }
  1303. if userNode.StaticNode.Address6 != "" {
  1304. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1305. }
  1306. for _, dstI := range acl.Dst {
  1307. if dstI.ID == models.EgressRange {
  1308. ip, cidr, err := net.ParseCIDR(dstI.Value)
  1309. if err == nil {
  1310. if ip.To4() != nil {
  1311. r.Dst = append(r.Dst, *cidr)
  1312. } else {
  1313. r.Dst6 = append(r.Dst6, *cidr)
  1314. }
  1315. }
  1316. }
  1317. }
  1318. if aclRule, ok := rules[acl.ID]; ok {
  1319. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  1320. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  1321. rules[acl.ID] = aclRule
  1322. } else {
  1323. rules[acl.ID] = r
  1324. }
  1325. }
  1326. }
  1327. return rules
  1328. }
  1329. func getUserAclRulesForNode(targetnode *models.Node,
  1330. rules map[string]models.AclRule) map[string]models.AclRule {
  1331. userNodes := GetStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  1332. userGrpMap := GetUserGrpMap()
  1333. allowedUsers := make(map[string][]models.Acl)
  1334. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  1335. var targetNodeTags = make(map[models.TagID]struct{})
  1336. if targetnode.Mutex != nil {
  1337. targetnode.Mutex.Lock()
  1338. targetNodeTags = maps.Clone(targetnode.Tags)
  1339. targetnode.Mutex.Unlock()
  1340. } else {
  1341. targetNodeTags = maps.Clone(targetnode.Tags)
  1342. }
  1343. if targetNodeTags == nil {
  1344. targetNodeTags = make(map[models.TagID]struct{})
  1345. }
  1346. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  1347. for _, acl := range acls {
  1348. if !acl.Enabled {
  1349. continue
  1350. }
  1351. dstTags := convAclTagToValueMap(acl.Dst)
  1352. _, all := dstTags["*"]
  1353. addUsers := false
  1354. if !all {
  1355. for nodeTag := range targetNodeTags {
  1356. if _, ok := dstTags[nodeTag.String()]; ok {
  1357. addUsers = true
  1358. break
  1359. }
  1360. }
  1361. } else {
  1362. addUsers = true
  1363. }
  1364. if addUsers {
  1365. // get all src tags
  1366. for _, srcAcl := range acl.Src {
  1367. if srcAcl.ID == models.UserAclID {
  1368. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  1369. } else if srcAcl.ID == models.UserGroupAclID {
  1370. // fetch all users in the group
  1371. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  1372. for userName := range usersMap {
  1373. allowedUsers[userName] = append(allowedUsers[userName], acl)
  1374. }
  1375. }
  1376. }
  1377. }
  1378. }
  1379. }
  1380. for _, userNode := range userNodes {
  1381. if !userNode.StaticNode.Enabled {
  1382. continue
  1383. }
  1384. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  1385. if !ok {
  1386. continue
  1387. }
  1388. for _, acl := range acls {
  1389. if !acl.Enabled {
  1390. continue
  1391. }
  1392. r := models.AclRule{
  1393. ID: acl.ID,
  1394. AllowedProtocol: acl.Proto,
  1395. AllowedPorts: acl.Port,
  1396. Direction: acl.AllowedDirection,
  1397. Allowed: true,
  1398. }
  1399. // Get peers in the tags and add allowed rules
  1400. if userNode.StaticNode.Address != "" {
  1401. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1402. }
  1403. if userNode.StaticNode.Address6 != "" {
  1404. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1405. }
  1406. if aclRule, ok := rules[acl.ID]; ok {
  1407. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  1408. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  1409. rules[acl.ID] = aclRule
  1410. } else {
  1411. rules[acl.ID] = r
  1412. }
  1413. }
  1414. }
  1415. return rules
  1416. }
  1417. func checkIfAnyPolicyisUniDirectional(targetNode models.Node) bool {
  1418. var targetNodeTags = make(map[models.TagID]struct{})
  1419. if targetNode.Mutex != nil {
  1420. targetNode.Mutex.Lock()
  1421. targetNodeTags = maps.Clone(targetNode.Tags)
  1422. targetNode.Mutex.Unlock()
  1423. } else {
  1424. targetNodeTags = maps.Clone(targetNode.Tags)
  1425. }
  1426. if targetNodeTags == nil {
  1427. targetNodeTags = make(map[models.TagID]struct{})
  1428. }
  1429. targetNodeTags[models.TagID(targetNode.ID.String())] = struct{}{}
  1430. targetNodeTags["*"] = struct{}{}
  1431. acls, _ := ListAclsByNetwork(models.NetworkID(targetNode.Network))
  1432. for _, acl := range acls {
  1433. if !acl.Enabled {
  1434. continue
  1435. }
  1436. if acl.AllowedDirection == models.TrafficDirectionBi {
  1437. continue
  1438. }
  1439. if acl.Proto != models.ALL || acl.ServiceType != models.Any {
  1440. return true
  1441. }
  1442. srcTags := convAclTagToValueMap(acl.Src)
  1443. dstTags := convAclTagToValueMap(acl.Dst)
  1444. for nodeTag := range targetNodeTags {
  1445. if acl.RuleType == models.DevicePolicy {
  1446. if _, ok := srcTags[nodeTag.String()]; ok {
  1447. return true
  1448. }
  1449. if _, ok := srcTags[targetNode.ID.String()]; ok {
  1450. return true
  1451. }
  1452. }
  1453. if _, ok := dstTags[nodeTag.String()]; ok {
  1454. return true
  1455. }
  1456. if _, ok := dstTags[targetNode.ID.String()]; ok {
  1457. return true
  1458. }
  1459. }
  1460. }
  1461. return false
  1462. }
  1463. func GetAclRulesForNode(targetnodeI *models.Node) (rules map[string]models.AclRule) {
  1464. targetnode := *targetnodeI
  1465. defer func() {
  1466. if !targetnode.IsIngressGateway {
  1467. rules = getUserAclRulesForNode(&targetnode, rules)
  1468. }
  1469. }()
  1470. rules = make(map[string]models.AclRule)
  1471. var taggedNodes map[models.TagID][]models.Node
  1472. if targetnode.IsIngressGateway {
  1473. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), false)
  1474. } else {
  1475. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  1476. }
  1477. acls := listDevicePolicies(models.NetworkID(targetnode.Network))
  1478. var targetNodeTags = make(map[models.TagID]struct{})
  1479. if targetnode.Mutex != nil {
  1480. targetnode.Mutex.Lock()
  1481. targetNodeTags = maps.Clone(targetnode.Tags)
  1482. targetnode.Mutex.Unlock()
  1483. } else {
  1484. targetNodeTags = maps.Clone(targetnode.Tags)
  1485. }
  1486. if targetNodeTags == nil {
  1487. targetNodeTags = make(map[models.TagID]struct{})
  1488. }
  1489. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  1490. targetNodeTags["*"] = struct{}{}
  1491. for _, acl := range acls {
  1492. if !acl.Enabled {
  1493. continue
  1494. }
  1495. srcTags := convAclTagToValueMap(acl.Src)
  1496. dstTags := convAclTagToValueMap(acl.Dst)
  1497. _, srcAll := srcTags["*"]
  1498. _, dstAll := dstTags["*"]
  1499. aclRule := models.AclRule{
  1500. ID: acl.ID,
  1501. AllowedProtocol: acl.Proto,
  1502. AllowedPorts: acl.Port,
  1503. Direction: acl.AllowedDirection,
  1504. Allowed: true,
  1505. }
  1506. for nodeTag := range targetNodeTags {
  1507. if acl.AllowedDirection == models.TrafficDirectionBi {
  1508. var existsInSrcTag bool
  1509. var existsInDstTag bool
  1510. if _, ok := srcTags[nodeTag.String()]; ok || srcAll {
  1511. existsInSrcTag = true
  1512. }
  1513. if _, ok := srcTags[targetnode.ID.String()]; ok || srcAll {
  1514. existsInSrcTag = true
  1515. }
  1516. if _, ok := dstTags[nodeTag.String()]; ok || dstAll {
  1517. existsInDstTag = true
  1518. }
  1519. if _, ok := dstTags[targetnode.ID.String()]; ok || dstAll {
  1520. existsInDstTag = true
  1521. }
  1522. if existsInSrcTag && !existsInDstTag {
  1523. // get all dst tags
  1524. for dst := range dstTags {
  1525. if dst == nodeTag.String() {
  1526. continue
  1527. }
  1528. // Get peers in the tags and add allowed rules
  1529. nodes := taggedNodes[models.TagID(dst)]
  1530. if dst != targetnode.ID.String() {
  1531. node, err := GetNodeByID(dst)
  1532. if err == nil {
  1533. nodes = append(nodes, node)
  1534. }
  1535. }
  1536. for _, node := range nodes {
  1537. if node.ID == targetnode.ID {
  1538. continue
  1539. }
  1540. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1541. continue
  1542. }
  1543. if node.Address.IP != nil {
  1544. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1545. }
  1546. if node.Address6.IP != nil {
  1547. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1548. }
  1549. if node.IsStatic && node.StaticNode.Address != "" {
  1550. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1551. }
  1552. if node.IsStatic && node.StaticNode.Address6 != "" {
  1553. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1554. }
  1555. }
  1556. }
  1557. }
  1558. if existsInDstTag && !existsInSrcTag {
  1559. // get all src tags
  1560. for src := range srcTags {
  1561. if src == nodeTag.String() {
  1562. continue
  1563. }
  1564. // Get peers in the tags and add allowed rules
  1565. nodes := taggedNodes[models.TagID(src)]
  1566. if src != targetnode.ID.String() {
  1567. node, err := GetNodeByID(src)
  1568. if err == nil {
  1569. nodes = append(nodes, node)
  1570. }
  1571. }
  1572. for _, node := range nodes {
  1573. if node.ID == targetnode.ID {
  1574. continue
  1575. }
  1576. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1577. continue
  1578. }
  1579. if node.Address.IP != nil {
  1580. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1581. }
  1582. if node.Address6.IP != nil {
  1583. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1584. }
  1585. if node.IsStatic && node.StaticNode.Address != "" {
  1586. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1587. }
  1588. if node.IsStatic && node.StaticNode.Address6 != "" {
  1589. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1590. }
  1591. }
  1592. }
  1593. }
  1594. if existsInDstTag && existsInSrcTag {
  1595. nodes := taggedNodes[nodeTag]
  1596. for srcID := range srcTags {
  1597. if srcID == targetnode.ID.String() {
  1598. continue
  1599. }
  1600. node, err := GetNodeByID(srcID)
  1601. if err == nil {
  1602. nodes = append(nodes, node)
  1603. }
  1604. }
  1605. for dstID := range dstTags {
  1606. if dstID == targetnode.ID.String() {
  1607. continue
  1608. }
  1609. node, err := GetNodeByID(dstID)
  1610. if err == nil {
  1611. nodes = append(nodes, node)
  1612. }
  1613. }
  1614. for _, node := range nodes {
  1615. if node.ID == targetnode.ID {
  1616. continue
  1617. }
  1618. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1619. continue
  1620. }
  1621. if node.Address.IP != nil {
  1622. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1623. }
  1624. if node.Address6.IP != nil {
  1625. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1626. }
  1627. if node.IsStatic && node.StaticNode.Address != "" {
  1628. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1629. }
  1630. if node.IsStatic && node.StaticNode.Address6 != "" {
  1631. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1632. }
  1633. }
  1634. }
  1635. } else {
  1636. _, all := dstTags["*"]
  1637. if _, ok := dstTags[nodeTag.String()]; ok || all {
  1638. // get all src tags
  1639. for src := range srcTags {
  1640. if src == nodeTag.String() {
  1641. continue
  1642. }
  1643. // Get peers in the tags and add allowed rules
  1644. nodes := taggedNodes[models.TagID(src)]
  1645. for _, node := range nodes {
  1646. if node.ID == targetnode.ID {
  1647. continue
  1648. }
  1649. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1650. continue
  1651. }
  1652. if node.Address.IP != nil {
  1653. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1654. }
  1655. if node.Address6.IP != nil {
  1656. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1657. }
  1658. if node.IsStatic && node.StaticNode.Address != "" {
  1659. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1660. }
  1661. if node.IsStatic && node.StaticNode.Address6 != "" {
  1662. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1663. }
  1664. }
  1665. }
  1666. }
  1667. }
  1668. }
  1669. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  1670. aclRule.IPList = UniqueIPNetList(aclRule.IPList)
  1671. aclRule.IP6List = UniqueIPNetList(aclRule.IP6List)
  1672. rules[acl.ID] = aclRule
  1673. }
  1674. }
  1675. return rules
  1676. }
  1677. func UniqueIPNetList(ipnets []net.IPNet) []net.IPNet {
  1678. uniqueMap := make(map[string]net.IPNet)
  1679. for _, ipnet := range ipnets {
  1680. key := ipnet.String() // Uses CIDR notation as a unique key
  1681. if _, exists := uniqueMap[key]; !exists {
  1682. uniqueMap[key] = ipnet
  1683. }
  1684. }
  1685. // Convert map back to slice
  1686. uniqueList := make([]net.IPNet, 0, len(uniqueMap))
  1687. for _, ipnet := range uniqueMap {
  1688. uniqueList = append(uniqueList, ipnet)
  1689. }
  1690. return uniqueList
  1691. }
  1692. func GetEgressRulesForNode(targetnode models.Node) (rules map[string]models.AclRule) {
  1693. rules = make(map[string]models.AclRule)
  1694. defer func() {
  1695. rules = getEgressUserRulesForNode(&targetnode, rules)
  1696. }()
  1697. taggedNodes := GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  1698. acls := listDevicePolicies(models.NetworkID(targetnode.Network))
  1699. var targetNodeTags = make(map[models.TagID]struct{})
  1700. targetNodeTags["*"] = struct{}{}
  1701. /*
  1702. if target node is egress gateway
  1703. if acl policy has egress route and it is present in target node egress ranges
  1704. fetches all the nodes in that policy and add rules
  1705. */
  1706. for _, rangeI := range targetnode.EgressGatewayRanges {
  1707. targetNodeTags[models.TagID(rangeI)] = struct{}{}
  1708. }
  1709. for _, acl := range acls {
  1710. if !acl.Enabled {
  1711. continue
  1712. }
  1713. srcTags := convAclTagToValueMap(acl.Src)
  1714. dstTags := convAclTagToValueMap(acl.Dst)
  1715. _, srcAll := srcTags["*"]
  1716. _, dstAll := dstTags["*"]
  1717. for nodeTag := range targetNodeTags {
  1718. aclRule := models.AclRule{
  1719. ID: acl.ID,
  1720. AllowedProtocol: acl.Proto,
  1721. AllowedPorts: acl.Port,
  1722. Direction: acl.AllowedDirection,
  1723. Allowed: true,
  1724. }
  1725. if nodeTag != "*" {
  1726. ip, cidr, err := net.ParseCIDR(nodeTag.String())
  1727. if err != nil {
  1728. continue
  1729. }
  1730. if ip.To4() != nil {
  1731. aclRule.Dst = append(aclRule.Dst, *cidr)
  1732. } else {
  1733. aclRule.Dst6 = append(aclRule.Dst6, *cidr)
  1734. }
  1735. } else {
  1736. aclRule.Dst = append(aclRule.Dst, net.IPNet{
  1737. IP: net.IPv4zero, // 0.0.0.0
  1738. Mask: net.CIDRMask(0, 32), // /0 means match all IPv4
  1739. })
  1740. aclRule.Dst6 = append(aclRule.Dst6, net.IPNet{
  1741. IP: net.IPv6zero, // ::
  1742. Mask: net.CIDRMask(0, 128), // /0 means match all IPv6
  1743. })
  1744. }
  1745. if acl.AllowedDirection == models.TrafficDirectionBi {
  1746. var existsInSrcTag bool
  1747. var existsInDstTag bool
  1748. if _, ok := srcTags[nodeTag.String()]; ok || srcAll {
  1749. existsInSrcTag = true
  1750. }
  1751. if _, ok := dstTags[nodeTag.String()]; ok || dstAll {
  1752. existsInDstTag = true
  1753. }
  1754. if existsInSrcTag && !existsInDstTag {
  1755. // get all dst tags
  1756. for dst := range dstTags {
  1757. if dst == nodeTag.String() {
  1758. continue
  1759. }
  1760. // Get peers in the tags and add allowed rules
  1761. nodes := taggedNodes[models.TagID(dst)]
  1762. if dst != targetnode.ID.String() {
  1763. node, err := GetNodeByID(dst)
  1764. if err == nil {
  1765. nodes = append(nodes, node)
  1766. }
  1767. }
  1768. for _, node := range nodes {
  1769. if node.ID == targetnode.ID {
  1770. continue
  1771. }
  1772. if node.Address.IP != nil {
  1773. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1774. }
  1775. if node.Address6.IP != nil {
  1776. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1777. }
  1778. if node.IsStatic && node.StaticNode.Address != "" {
  1779. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1780. }
  1781. if node.IsStatic && node.StaticNode.Address6 != "" {
  1782. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1783. }
  1784. }
  1785. }
  1786. }
  1787. if existsInDstTag && !existsInSrcTag {
  1788. // get all src tags
  1789. for src := range srcTags {
  1790. if src == nodeTag.String() {
  1791. continue
  1792. }
  1793. // Get peers in the tags and add allowed rules
  1794. nodes := taggedNodes[models.TagID(src)]
  1795. if src != targetnode.ID.String() {
  1796. node, err := GetNodeByID(src)
  1797. if err == nil {
  1798. nodes = append(nodes, node)
  1799. }
  1800. }
  1801. for _, node := range nodes {
  1802. if node.ID == targetnode.ID {
  1803. continue
  1804. }
  1805. if node.Address.IP != nil {
  1806. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1807. }
  1808. if node.Address6.IP != nil {
  1809. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1810. }
  1811. if node.IsStatic && node.StaticNode.Address != "" {
  1812. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1813. }
  1814. if node.IsStatic && node.StaticNode.Address6 != "" {
  1815. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1816. }
  1817. }
  1818. }
  1819. }
  1820. if existsInDstTag && existsInSrcTag {
  1821. nodes := taggedNodes[nodeTag]
  1822. for srcID := range srcTags {
  1823. if srcID == targetnode.ID.String() {
  1824. continue
  1825. }
  1826. node, err := GetNodeByID(srcID)
  1827. if err == nil {
  1828. nodes = append(nodes, node)
  1829. }
  1830. }
  1831. for dstID := range dstTags {
  1832. if dstID == targetnode.ID.String() {
  1833. continue
  1834. }
  1835. node, err := GetNodeByID(dstID)
  1836. if err == nil {
  1837. nodes = append(nodes, node)
  1838. }
  1839. }
  1840. for _, node := range nodes {
  1841. if node.ID == targetnode.ID {
  1842. continue
  1843. }
  1844. if node.Address.IP != nil {
  1845. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1846. }
  1847. if node.Address6.IP != nil {
  1848. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1849. }
  1850. if node.IsStatic && node.StaticNode.Address != "" {
  1851. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1852. }
  1853. if node.IsStatic && node.StaticNode.Address6 != "" {
  1854. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1855. }
  1856. }
  1857. }
  1858. } else {
  1859. _, all := dstTags["*"]
  1860. if _, ok := dstTags[nodeTag.String()]; ok || all {
  1861. // get all src tags
  1862. for src := range srcTags {
  1863. if src == nodeTag.String() {
  1864. continue
  1865. }
  1866. // Get peers in the tags and add allowed rules
  1867. nodes := taggedNodes[models.TagID(src)]
  1868. for _, node := range nodes {
  1869. if node.ID == targetnode.ID {
  1870. continue
  1871. }
  1872. if node.Address.IP != nil {
  1873. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1874. }
  1875. if node.Address6.IP != nil {
  1876. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1877. }
  1878. if node.IsStatic && node.StaticNode.Address != "" {
  1879. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1880. }
  1881. if node.IsStatic && node.StaticNode.Address6 != "" {
  1882. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1883. }
  1884. }
  1885. }
  1886. }
  1887. }
  1888. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  1889. aclRule.IPList = UniqueIPNetList(aclRule.IPList)
  1890. aclRule.IP6List = UniqueIPNetList(aclRule.IP6List)
  1891. rules[acl.ID] = aclRule
  1892. }
  1893. }
  1894. }
  1895. return
  1896. }