acls.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "maps"
  7. "sort"
  8. "sync"
  9. "time"
  10. "github.com/gravitl/netmaker/database"
  11. "github.com/gravitl/netmaker/models"
  12. "github.com/gravitl/netmaker/servercfg"
  13. )
  14. var (
  15. aclCacheMutex = &sync.RWMutex{}
  16. aclCacheMap = make(map[string]models.Acl)
  17. aclTagsMutex = &sync.RWMutex{}
  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-remote-access-gws")) {
  93. defaultUserAcl := models.Acl{
  94. ID: fmt.Sprintf("%s.%s", netID, "all-remote-access-gws"),
  95. Default: true,
  96. Name: "All Remote Access 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.RemoteAccessTagName),
  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. // DeleteDefaultNetworkPolicies - deletes all default network acl policies
  124. func DeleteDefaultNetworkPolicies(netId models.NetworkID) {
  125. acls, _ := ListAclsByNetwork(netId)
  126. for _, acl := range acls {
  127. if acl.NetworkID == netId && acl.Default {
  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. // IsAclPolicyValid - validates if acl policy is valid
  210. func IsAclPolicyValid(acl models.Acl) bool {
  211. //check if src and dst are valid
  212. if acl.AllowedDirection != models.TrafficDirectionBi &&
  213. acl.AllowedDirection != models.TrafficDirectionUni {
  214. return false
  215. }
  216. switch acl.RuleType {
  217. case models.UserPolicy:
  218. // src list should only contain users
  219. for _, srcI := range acl.Src {
  220. if srcI.ID == "" || srcI.Value == "" {
  221. return false
  222. }
  223. if srcI.Value == "*" {
  224. continue
  225. }
  226. if srcI.ID != models.UserAclID && srcI.ID != models.UserGroupAclID {
  227. return false
  228. }
  229. // check if user group is valid
  230. if srcI.ID == models.UserAclID {
  231. _, err := GetUser(srcI.Value)
  232. if err != nil {
  233. return false
  234. }
  235. } else if srcI.ID == models.UserGroupAclID {
  236. err := IsGroupValid(models.UserGroupID(srcI.Value))
  237. if err != nil {
  238. return false
  239. }
  240. // check if group belongs to this network
  241. netGrps := GetUserGroupsInNetwork(acl.NetworkID)
  242. if _, ok := netGrps[models.UserGroupID(srcI.Value)]; !ok {
  243. return false
  244. }
  245. }
  246. }
  247. for _, dstI := range acl.Dst {
  248. if dstI.ID == "" || dstI.Value == "" {
  249. return false
  250. }
  251. if dstI.ID != models.NodeTagID && dstI.ID != models.NodeID {
  252. return false
  253. }
  254. if dstI.Value == "*" {
  255. continue
  256. }
  257. if dstI.ID == models.NodeTagID {
  258. // check if tag is valid
  259. _, err := GetTag(models.TagID(dstI.Value))
  260. if err != nil {
  261. return false
  262. }
  263. } else {
  264. _, nodeErr := GetNodeByID(dstI.Value)
  265. if nodeErr != nil {
  266. _, staticNodeErr := GetExtClient(dstI.Value, acl.NetworkID.String())
  267. if staticNodeErr != nil {
  268. return false
  269. }
  270. }
  271. }
  272. }
  273. case models.DevicePolicy:
  274. for _, srcI := range acl.Src {
  275. if srcI.ID == "" || srcI.Value == "" {
  276. return false
  277. }
  278. if srcI.ID != models.NodeTagID && srcI.ID != models.NodeID {
  279. return false
  280. }
  281. if srcI.Value == "*" {
  282. continue
  283. }
  284. if srcI.ID == models.NodeTagID {
  285. // check if tag is valid
  286. _, err := GetTag(models.TagID(srcI.Value))
  287. if err != nil {
  288. return false
  289. }
  290. } else {
  291. _, nodeErr := GetNodeByID(srcI.Value)
  292. if nodeErr != nil {
  293. _, staticNodeErr := GetExtClient(srcI.Value, acl.NetworkID.String())
  294. if staticNodeErr != nil {
  295. return false
  296. }
  297. }
  298. }
  299. }
  300. for _, dstI := range acl.Dst {
  301. if dstI.ID == "" || dstI.Value == "" {
  302. return false
  303. }
  304. if dstI.ID != models.NodeTagID && dstI.ID != models.NodeID {
  305. return false
  306. }
  307. if dstI.Value == "*" {
  308. continue
  309. }
  310. if dstI.ID == models.NodeTagID {
  311. // check if tag is valid
  312. _, err := GetTag(models.TagID(dstI.Value))
  313. if err != nil {
  314. return false
  315. }
  316. } else {
  317. _, nodeErr := GetNodeByID(dstI.Value)
  318. if nodeErr != nil {
  319. _, staticNodeErr := GetExtClient(dstI.Value, acl.NetworkID.String())
  320. if staticNodeErr != nil {
  321. return false
  322. }
  323. }
  324. }
  325. }
  326. }
  327. return true
  328. }
  329. // UpdateAcl - updates allowed fields on acls and commits to DB
  330. func UpdateAcl(newAcl, acl models.Acl) error {
  331. if !acl.Default {
  332. acl.Name = newAcl.Name
  333. acl.Src = newAcl.Src
  334. acl.Dst = newAcl.Dst
  335. acl.AllowedDirection = newAcl.AllowedDirection
  336. acl.Port = newAcl.Port
  337. acl.Proto = newAcl.Proto
  338. acl.ServiceType = newAcl.ServiceType
  339. }
  340. if newAcl.ServiceType == models.Any {
  341. acl.Port = []string{}
  342. acl.Proto = models.ALL
  343. }
  344. acl.Enabled = newAcl.Enabled
  345. d, err := json.Marshal(acl)
  346. if err != nil {
  347. return err
  348. }
  349. err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)
  350. if err == nil && servercfg.CacheEnabled() {
  351. storeAclInCache(acl)
  352. }
  353. return err
  354. }
  355. // UpsertAcl - upserts acl
  356. func UpsertAcl(acl models.Acl) error {
  357. d, err := json.Marshal(acl)
  358. if err != nil {
  359. return err
  360. }
  361. err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)
  362. if err == nil && servercfg.CacheEnabled() {
  363. storeAclInCache(acl)
  364. }
  365. return err
  366. }
  367. // DeleteAcl - deletes acl policy
  368. func DeleteAcl(a models.Acl) error {
  369. err := database.DeleteRecord(database.ACLS_TABLE_NAME, a.ID)
  370. if err == nil && servercfg.CacheEnabled() {
  371. removeAclFromCache(a)
  372. }
  373. return err
  374. }
  375. // GetDefaultPolicy - fetches default policy in the network by ruleType
  376. func GetDefaultPolicy(netID models.NetworkID, ruleType models.AclPolicyType) (models.Acl, error) {
  377. aclID := "all-users"
  378. if ruleType == models.DevicePolicy {
  379. aclID = "all-nodes"
  380. }
  381. acl, err := GetAcl(fmt.Sprintf("%s.%s", netID, aclID))
  382. if err != nil {
  383. return models.Acl{}, errors.New("default rule not found")
  384. }
  385. if acl.Enabled {
  386. return acl, nil
  387. }
  388. // check if there are any custom all policies
  389. srcMap := make(map[string]struct{})
  390. dstMap := make(map[string]struct{})
  391. defer func() {
  392. srcMap = nil
  393. dstMap = nil
  394. }()
  395. policies, _ := ListAclsByNetwork(netID)
  396. for _, policy := range policies {
  397. if !policy.Enabled {
  398. continue
  399. }
  400. if policy.RuleType == ruleType {
  401. dstMap = convAclTagToValueMap(policy.Dst)
  402. srcMap = convAclTagToValueMap(policy.Src)
  403. if _, ok := srcMap["*"]; ok {
  404. if _, ok := dstMap["*"]; ok {
  405. return policy, nil
  406. }
  407. }
  408. }
  409. }
  410. return acl, nil
  411. }
  412. func ListAcls() (acls []models.Acl) {
  413. if servercfg.CacheEnabled() && len(aclCacheMap) > 0 {
  414. return listAclFromCache()
  415. }
  416. data, err := database.FetchRecords(database.ACLS_TABLE_NAME)
  417. if err != nil && !database.IsEmptyRecord(err) {
  418. return []models.Acl{}
  419. }
  420. for _, dataI := range data {
  421. acl := models.Acl{}
  422. err := json.Unmarshal([]byte(dataI), &acl)
  423. if err != nil {
  424. continue
  425. }
  426. acls = append(acls, acl)
  427. if servercfg.CacheEnabled() {
  428. storeAclInCache(acl)
  429. }
  430. }
  431. return
  432. }
  433. // ListUserPolicies - lists all acl policies enforced on an user
  434. func ListUserPolicies(u models.User) []models.Acl {
  435. allAcls := ListAcls()
  436. userAcls := []models.Acl{}
  437. for _, acl := range allAcls {
  438. if acl.RuleType == models.UserPolicy {
  439. srcMap := convAclTagToValueMap(acl.Src)
  440. if _, ok := srcMap[u.UserName]; ok {
  441. userAcls = append(userAcls, acl)
  442. } else {
  443. // check for user groups
  444. for gID := range u.UserGroups {
  445. if _, ok := srcMap[gID.String()]; ok {
  446. userAcls = append(userAcls, acl)
  447. break
  448. }
  449. }
  450. }
  451. }
  452. }
  453. return userAcls
  454. }
  455. // listPoliciesOfUser - lists all user acl policies applied to user in an network
  456. func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {
  457. allAcls := ListAcls()
  458. userAcls := []models.Acl{}
  459. for _, acl := range allAcls {
  460. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  461. srcMap := convAclTagToValueMap(acl.Src)
  462. if _, ok := srcMap[user.UserName]; ok {
  463. userAcls = append(userAcls, acl)
  464. continue
  465. }
  466. for netRole := range user.NetworkRoles {
  467. if _, ok := srcMap[netRole.String()]; ok {
  468. userAcls = append(userAcls, acl)
  469. continue
  470. }
  471. }
  472. for userG := range user.UserGroups {
  473. if _, ok := srcMap[userG.String()]; ok {
  474. userAcls = append(userAcls, acl)
  475. continue
  476. }
  477. }
  478. }
  479. }
  480. return userAcls
  481. }
  482. // listDevicePolicies - lists all device policies in a network
  483. func listDevicePolicies(netID models.NetworkID) []models.Acl {
  484. allAcls := ListAcls()
  485. deviceAcls := []models.Acl{}
  486. for _, acl := range allAcls {
  487. if acl.NetworkID == netID && acl.RuleType == models.DevicePolicy {
  488. deviceAcls = append(deviceAcls, acl)
  489. }
  490. }
  491. return deviceAcls
  492. }
  493. // listUserPolicies - lists all user policies in a network
  494. func listUserPolicies(netID models.NetworkID) []models.Acl {
  495. allAcls := ListAcls()
  496. deviceAcls := []models.Acl{}
  497. for _, acl := range allAcls {
  498. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  499. deviceAcls = append(deviceAcls, acl)
  500. }
  501. }
  502. return deviceAcls
  503. }
  504. // ListAcls - lists all acl policies
  505. func ListAclsByNetwork(netID models.NetworkID) ([]models.Acl, error) {
  506. allAcls := ListAcls()
  507. netAcls := []models.Acl{}
  508. for _, acl := range allAcls {
  509. if acl.NetworkID == netID {
  510. netAcls = append(netAcls, acl)
  511. }
  512. }
  513. return netAcls, nil
  514. }
  515. func convAclTagToValueMap(acltags []models.AclPolicyTag) map[string]struct{} {
  516. aclValueMap := make(map[string]struct{})
  517. for _, aclTagI := range acltags {
  518. aclValueMap[aclTagI.Value] = struct{}{}
  519. }
  520. return aclValueMap
  521. }
  522. // IsUserAllowedToCommunicate - check if user is allowed to communicate with peer
  523. func IsUserAllowedToCommunicate(userName string, peer models.Node) (bool, []models.Acl) {
  524. if peer.IsStatic {
  525. peer = peer.StaticNode.ConvertToStaticNode()
  526. }
  527. peer.Tags[models.TagID(peer.ID.String())] = struct{}{}
  528. acl, _ := GetDefaultPolicy(models.NetworkID(peer.Network), models.UserPolicy)
  529. if acl.Enabled {
  530. return true, []models.Acl{acl}
  531. }
  532. user, err := GetUser(userName)
  533. if err != nil {
  534. return false, []models.Acl{}
  535. }
  536. allowedPolicies := []models.Acl{}
  537. policies := listPoliciesOfUser(*user, models.NetworkID(peer.Network))
  538. for _, policy := range policies {
  539. if !policy.Enabled {
  540. continue
  541. }
  542. dstMap := convAclTagToValueMap(policy.Dst)
  543. if _, ok := dstMap["*"]; ok {
  544. allowedPolicies = append(allowedPolicies, policy)
  545. continue
  546. }
  547. if _, ok := dstMap[peer.ID.String()]; ok {
  548. allowedPolicies = append(allowedPolicies, policy)
  549. continue
  550. }
  551. for tagID := range peer.Tags {
  552. if _, ok := dstMap[tagID.String()]; ok {
  553. allowedPolicies = append(allowedPolicies, policy)
  554. break
  555. }
  556. }
  557. }
  558. if len(allowedPolicies) > 0 {
  559. return true, allowedPolicies
  560. }
  561. return false, []models.Acl{}
  562. }
  563. // IsPeerAllowed - checks if peer needs to be added to the interface
  564. func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
  565. var nodeId, peerId string
  566. if node.IsStatic {
  567. nodeId = node.StaticNode.ClientID
  568. node = node.StaticNode.ConvertToStaticNode()
  569. } else {
  570. nodeId = node.ID.String()
  571. }
  572. if peer.IsStatic {
  573. peerId = peer.StaticNode.ClientID
  574. peer = peer.StaticNode.ConvertToStaticNode()
  575. } else {
  576. peerId = peer.ID.String()
  577. }
  578. aclTagsMutex.RLock()
  579. peerTags := maps.Clone(peer.Tags)
  580. nodeTags := maps.Clone(node.Tags)
  581. aclTagsMutex.RUnlock()
  582. nodeTags[models.TagID(nodeId)] = struct{}{}
  583. peerTags[models.TagID(peerId)] = struct{}{}
  584. if checkDefaultPolicy {
  585. // check default policy if all allowed return true
  586. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  587. if err == nil {
  588. if defaultPolicy.Enabled {
  589. return true
  590. }
  591. }
  592. }
  593. // list device policies
  594. policies := listDevicePolicies(models.NetworkID(peer.Network))
  595. srcMap := make(map[string]struct{})
  596. dstMap := make(map[string]struct{})
  597. defer func() {
  598. srcMap = nil
  599. dstMap = nil
  600. }()
  601. for _, policy := range policies {
  602. if !policy.Enabled {
  603. continue
  604. }
  605. srcMap = convAclTagToValueMap(policy.Src)
  606. dstMap = convAclTagToValueMap(policy.Dst)
  607. if checkTagGroupPolicy(srcMap, dstMap, node, peer, nodeTags, peerTags) {
  608. return true
  609. }
  610. }
  611. return false
  612. }
  613. func RemoveUserFromAclPolicy(userName string) {
  614. acls := ListAcls()
  615. for _, acl := range acls {
  616. delete := false
  617. update := false
  618. if acl.RuleType == models.UserPolicy {
  619. for i, srcI := range acl.Src {
  620. if srcI.ID == models.UserAclID && srcI.Value == userName {
  621. if len(acl.Src) == 1 {
  622. // delete policy
  623. delete = true
  624. break
  625. } else {
  626. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  627. update = true
  628. }
  629. }
  630. }
  631. if delete {
  632. DeleteAcl(acl)
  633. continue
  634. }
  635. if update {
  636. UpsertAcl(acl)
  637. }
  638. }
  639. }
  640. }
  641. func RemoveNodeFromAclPolicy(node models.Node) {
  642. var nodeID string
  643. if node.IsStatic {
  644. nodeID = node.StaticNode.ClientID
  645. } else {
  646. nodeID = node.ID.String()
  647. }
  648. acls, _ := ListAclsByNetwork(models.NetworkID(node.Network))
  649. for _, acl := range acls {
  650. delete := false
  651. update := false
  652. if acl.RuleType == models.DevicePolicy {
  653. for i, srcI := range acl.Src {
  654. if srcI.ID == models.NodeID && srcI.Value == nodeID {
  655. if len(acl.Src) == 1 {
  656. // delete policy
  657. delete = true
  658. break
  659. } else {
  660. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  661. update = true
  662. }
  663. }
  664. }
  665. if delete {
  666. DeleteAcl(acl)
  667. continue
  668. }
  669. for i, dstI := range acl.Dst {
  670. if dstI.ID == models.NodeID && dstI.Value == nodeID {
  671. if len(acl.Dst) == 1 {
  672. // delete policy
  673. delete = true
  674. break
  675. } else {
  676. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  677. update = true
  678. }
  679. }
  680. }
  681. if delete {
  682. DeleteAcl(acl)
  683. continue
  684. }
  685. if update {
  686. UpsertAcl(acl)
  687. }
  688. }
  689. if acl.RuleType == models.UserPolicy {
  690. for i, dstI := range acl.Dst {
  691. if dstI.ID == models.NodeID && dstI.Value == nodeID {
  692. if len(acl.Dst) == 1 {
  693. // delete policy
  694. delete = true
  695. break
  696. } else {
  697. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  698. update = true
  699. }
  700. }
  701. }
  702. if delete {
  703. DeleteAcl(acl)
  704. continue
  705. }
  706. if update {
  707. UpsertAcl(acl)
  708. }
  709. }
  710. }
  711. }
  712. func checkTagGroupPolicy(srcMap, dstMap map[string]struct{}, node, peer models.Node,
  713. nodeTags, peerTags map[models.TagID]struct{}) bool {
  714. // check for node ID
  715. if _, ok := srcMap[node.ID.String()]; ok {
  716. if _, ok = dstMap[peer.ID.String()]; ok {
  717. return true
  718. }
  719. }
  720. if _, ok := dstMap[node.ID.String()]; ok {
  721. if _, ok = srcMap[peer.ID.String()]; ok {
  722. return true
  723. }
  724. }
  725. for tagID := range nodeTags {
  726. if _, ok := dstMap[tagID.String()]; ok {
  727. if _, ok := srcMap["*"]; ok {
  728. return true
  729. }
  730. for tagID := range peerTags {
  731. if _, ok := srcMap[tagID.String()]; ok {
  732. return true
  733. }
  734. }
  735. }
  736. if _, ok := srcMap[tagID.String()]; ok {
  737. if _, ok := dstMap["*"]; ok {
  738. return true
  739. }
  740. for tagID := range peerTags {
  741. if _, ok := dstMap[tagID.String()]; ok {
  742. return true
  743. }
  744. }
  745. }
  746. }
  747. for tagID := range peerTags {
  748. if _, ok := dstMap[tagID.String()]; ok {
  749. if _, ok := srcMap["*"]; ok {
  750. return true
  751. }
  752. for tagID := range nodeTags {
  753. if _, ok := srcMap[tagID.String()]; ok {
  754. return true
  755. }
  756. }
  757. }
  758. if _, ok := srcMap[tagID.String()]; ok {
  759. if _, ok := dstMap["*"]; ok {
  760. return true
  761. }
  762. for tagID := range nodeTags {
  763. if _, ok := dstMap[tagID.String()]; ok {
  764. return true
  765. }
  766. }
  767. }
  768. }
  769. return false
  770. }
  771. func uniquePolicies(items []models.Acl) []models.Acl {
  772. if len(items) == 0 {
  773. return items
  774. }
  775. seen := make(map[string]bool)
  776. var result []models.Acl
  777. for _, item := range items {
  778. if !seen[item.ID] {
  779. seen[item.ID] = true
  780. result = append(result, item)
  781. }
  782. }
  783. return result
  784. }
  785. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer // ADD ALLOWED DIRECTION - 0 => node -> peer, 1 => peer-> node,
  786. func IsNodeAllowedToCommunicateV1(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
  787. var nodeId, peerId string
  788. if node.IsStatic {
  789. nodeId = node.StaticNode.ClientID
  790. node = node.StaticNode.ConvertToStaticNode()
  791. } else {
  792. nodeId = node.ID.String()
  793. }
  794. if peer.IsStatic {
  795. peerId = peer.StaticNode.ClientID
  796. peer = peer.StaticNode.ConvertToStaticNode()
  797. } else {
  798. peerId = peer.ID.String()
  799. }
  800. aclTagsMutex.RLock()
  801. peerTags := maps.Clone(peer.Tags)
  802. nodeTags := maps.Clone(node.Tags)
  803. aclTagsMutex.RUnlock()
  804. nodeTags[models.TagID(nodeId)] = struct{}{}
  805. peerTags[models.TagID(peerId)] = struct{}{}
  806. if checkDefaultPolicy {
  807. // check default policy if all allowed return true
  808. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  809. if err == nil {
  810. if defaultPolicy.Enabled {
  811. return true, []models.Acl{defaultPolicy}
  812. }
  813. }
  814. }
  815. allowedPolicies := []models.Acl{}
  816. defer func() {
  817. allowedPolicies = uniquePolicies(allowedPolicies)
  818. }()
  819. // list device policies
  820. policies := listDevicePolicies(models.NetworkID(peer.Network))
  821. srcMap := make(map[string]struct{})
  822. dstMap := make(map[string]struct{})
  823. defer func() {
  824. srcMap = nil
  825. dstMap = nil
  826. }()
  827. for _, policy := range policies {
  828. if !policy.Enabled {
  829. continue
  830. }
  831. allowed := false
  832. srcMap = convAclTagToValueMap(policy.Src)
  833. dstMap = convAclTagToValueMap(policy.Dst)
  834. _, srcAll := srcMap["*"]
  835. _, dstAll := dstMap["*"]
  836. if policy.AllowedDirection == models.TrafficDirectionBi {
  837. if _, ok := srcMap[nodeId]; ok || srcAll {
  838. if _, ok := dstMap[peerId]; ok || dstAll {
  839. allowedPolicies = append(allowedPolicies, policy)
  840. continue
  841. }
  842. }
  843. if _, ok := dstMap[nodeId]; ok || dstAll {
  844. if _, ok := srcMap[peerId]; ok || srcAll {
  845. allowedPolicies = append(allowedPolicies, policy)
  846. continue
  847. }
  848. }
  849. }
  850. if _, ok := dstMap[peerId]; ok || dstAll {
  851. if _, ok := srcMap[nodeId]; ok || srcAll {
  852. allowedPolicies = append(allowedPolicies, policy)
  853. continue
  854. }
  855. }
  856. if policy.AllowedDirection == models.TrafficDirectionBi {
  857. for tagID := range nodeTags {
  858. if _, ok := dstMap[tagID.String()]; ok {
  859. if srcAll {
  860. allowed = true
  861. break
  862. }
  863. for tagID := range peerTags {
  864. if _, ok := srcMap[tagID.String()]; ok {
  865. allowed = true
  866. break
  867. }
  868. }
  869. }
  870. if allowed {
  871. allowedPolicies = append(allowedPolicies, policy)
  872. break
  873. }
  874. if _, ok := srcMap[tagID.String()]; ok {
  875. if dstAll {
  876. allowed = true
  877. break
  878. }
  879. for tagID := range peerTags {
  880. if _, ok := dstMap[tagID.String()]; ok {
  881. allowed = true
  882. break
  883. }
  884. }
  885. }
  886. if allowed {
  887. break
  888. }
  889. }
  890. if allowed {
  891. allowedPolicies = append(allowedPolicies, policy)
  892. continue
  893. }
  894. }
  895. for tagID := range peerTags {
  896. if _, ok := dstMap[tagID.String()]; ok {
  897. if srcAll {
  898. allowed = true
  899. break
  900. }
  901. for tagID := range nodeTags {
  902. if _, ok := srcMap[tagID.String()]; ok {
  903. allowed = true
  904. break
  905. }
  906. }
  907. }
  908. if allowed {
  909. break
  910. }
  911. }
  912. if allowed {
  913. allowedPolicies = append(allowedPolicies, policy)
  914. }
  915. }
  916. if len(allowedPolicies) > 0 {
  917. return true, allowedPolicies
  918. }
  919. return false, allowedPolicies
  920. }
  921. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer
  922. func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
  923. var nodeId, peerId string
  924. if node.IsStatic {
  925. nodeId = node.StaticNode.ClientID
  926. node = node.StaticNode.ConvertToStaticNode()
  927. } else {
  928. nodeId = node.ID.String()
  929. }
  930. if peer.IsStatic {
  931. peerId = peer.StaticNode.ClientID
  932. peer = peer.StaticNode.ConvertToStaticNode()
  933. } else {
  934. peerId = peer.ID.String()
  935. }
  936. aclTagsMutex.RLock()
  937. peerTags := maps.Clone(peer.Tags)
  938. nodeTags := maps.Clone(node.Tags)
  939. aclTagsMutex.RUnlock()
  940. nodeTags[models.TagID(nodeId)] = struct{}{}
  941. peerTags[models.TagID(peerId)] = struct{}{}
  942. if checkDefaultPolicy {
  943. // check default policy if all allowed return true
  944. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  945. if err == nil {
  946. if defaultPolicy.Enabled {
  947. return true, []models.Acl{defaultPolicy}
  948. }
  949. }
  950. }
  951. allowedPolicies := []models.Acl{}
  952. // list device policies
  953. policies := listDevicePolicies(models.NetworkID(peer.Network))
  954. srcMap := make(map[string]struct{})
  955. dstMap := make(map[string]struct{})
  956. defer func() {
  957. srcMap = nil
  958. dstMap = nil
  959. }()
  960. for _, policy := range policies {
  961. if !policy.Enabled {
  962. continue
  963. }
  964. srcMap = convAclTagToValueMap(policy.Src)
  965. dstMap = convAclTagToValueMap(policy.Dst)
  966. _, srcAll := srcMap["*"]
  967. _, dstAll := dstMap["*"]
  968. if policy.AllowedDirection == models.TrafficDirectionBi {
  969. if _, ok := srcMap[nodeId]; ok || srcAll {
  970. if _, ok := dstMap[peerId]; ok || dstAll {
  971. allowedPolicies = append(allowedPolicies, policy)
  972. continue
  973. }
  974. }
  975. if _, ok := dstMap[nodeId]; ok || dstAll {
  976. if _, ok := srcMap[peerId]; ok || srcAll {
  977. allowedPolicies = append(allowedPolicies, policy)
  978. continue
  979. }
  980. }
  981. }
  982. if _, ok := dstMap[nodeId]; ok || dstAll {
  983. if _, ok := srcMap[peerId]; ok || srcAll {
  984. allowedPolicies = append(allowedPolicies, policy)
  985. continue
  986. }
  987. }
  988. for tagID := range nodeTags {
  989. allowed := false
  990. if _, ok := dstMap[tagID.String()]; policy.AllowedDirection == models.TrafficDirectionBi && ok {
  991. if srcAll {
  992. allowed = true
  993. allowedPolicies = append(allowedPolicies, policy)
  994. break
  995. }
  996. for tagID := range peerTags {
  997. if _, ok := srcMap[tagID.String()]; ok {
  998. allowed = true
  999. break
  1000. }
  1001. }
  1002. }
  1003. if allowed {
  1004. allowedPolicies = append(allowedPolicies, policy)
  1005. break
  1006. }
  1007. if _, ok := srcMap[tagID.String()]; ok {
  1008. if dstAll {
  1009. allowed = true
  1010. allowedPolicies = append(allowedPolicies, policy)
  1011. break
  1012. }
  1013. for tagID := range peerTags {
  1014. if _, ok := dstMap[tagID.String()]; ok {
  1015. allowed = true
  1016. break
  1017. }
  1018. }
  1019. }
  1020. if allowed {
  1021. allowedPolicies = append(allowedPolicies, policy)
  1022. break
  1023. }
  1024. }
  1025. for tagID := range peerTags {
  1026. allowed := false
  1027. if _, ok := dstMap[tagID.String()]; ok {
  1028. if srcAll {
  1029. allowed = true
  1030. allowedPolicies = append(allowedPolicies, policy)
  1031. break
  1032. }
  1033. for tagID := range nodeTags {
  1034. if _, ok := srcMap[tagID.String()]; ok {
  1035. allowed = true
  1036. break
  1037. }
  1038. }
  1039. }
  1040. if allowed {
  1041. allowedPolicies = append(allowedPolicies, policy)
  1042. break
  1043. }
  1044. if _, ok := srcMap[tagID.String()]; policy.AllowedDirection == models.TrafficDirectionBi && ok {
  1045. if dstAll {
  1046. allowed = true
  1047. allowedPolicies = append(allowedPolicies, policy)
  1048. break
  1049. }
  1050. for tagID := range nodeTags {
  1051. if _, ok := dstMap[tagID.String()]; ok {
  1052. allowed = true
  1053. break
  1054. }
  1055. }
  1056. }
  1057. if allowed {
  1058. allowedPolicies = append(allowedPolicies, policy)
  1059. break
  1060. }
  1061. }
  1062. }
  1063. if len(allowedPolicies) > 0 {
  1064. return true, allowedPolicies
  1065. }
  1066. return false, allowedPolicies
  1067. }
  1068. // SortTagEntrys - Sorts slice of Tag entries by their id
  1069. func SortAclEntrys(acls []models.Acl) {
  1070. sort.Slice(acls, func(i, j int) bool {
  1071. return acls[i].Name < acls[j].Name
  1072. })
  1073. }
  1074. // UpdateDeviceTag - updates device tag on acl policies
  1075. func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
  1076. acls := listDevicePolicies(netID)
  1077. update := false
  1078. for _, acl := range acls {
  1079. for i, srcTagI := range acl.Src {
  1080. if srcTagI.ID == models.NodeTagID {
  1081. if OldID.String() == srcTagI.Value {
  1082. acl.Src[i].Value = newID.String()
  1083. update = true
  1084. }
  1085. }
  1086. }
  1087. for i, dstTagI := range acl.Dst {
  1088. if dstTagI.ID == models.NodeTagID {
  1089. if OldID.String() == dstTagI.Value {
  1090. acl.Dst[i].Value = newID.String()
  1091. update = true
  1092. }
  1093. }
  1094. }
  1095. if update {
  1096. UpsertAcl(acl)
  1097. }
  1098. }
  1099. }
  1100. func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
  1101. acls := listDevicePolicies(netID)
  1102. for _, acl := range acls {
  1103. for _, srcTagI := range acl.Src {
  1104. if srcTagI.ID == models.NodeTagID {
  1105. if tagID.String() == srcTagI.Value {
  1106. return true
  1107. }
  1108. }
  1109. }
  1110. for _, dstTagI := range acl.Dst {
  1111. if dstTagI.ID == models.NodeTagID {
  1112. if tagID.String() == dstTagI.Value {
  1113. return true
  1114. }
  1115. }
  1116. }
  1117. }
  1118. return false
  1119. }
  1120. // RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
  1121. func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
  1122. acls := listDevicePolicies(netID)
  1123. update := false
  1124. for _, acl := range acls {
  1125. for i, srcTagI := range acl.Src {
  1126. if srcTagI.ID == models.NodeTagID {
  1127. if tagID.String() == srcTagI.Value {
  1128. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  1129. update = true
  1130. }
  1131. }
  1132. }
  1133. for i, dstTagI := range acl.Dst {
  1134. if dstTagI.ID == models.NodeTagID {
  1135. if tagID.String() == dstTagI.Value {
  1136. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  1137. update = true
  1138. }
  1139. }
  1140. }
  1141. if update {
  1142. UpsertAcl(acl)
  1143. }
  1144. }
  1145. return nil
  1146. }
  1147. func getUserAclRulesForNode(targetnode *models.Node,
  1148. rules map[string]models.AclRule) map[string]models.AclRule {
  1149. userNodes := GetStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  1150. userGrpMap := GetUserGrpMap()
  1151. allowedUsers := make(map[string][]models.Acl)
  1152. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  1153. for _, acl := range acls {
  1154. if !acl.Enabled {
  1155. continue
  1156. }
  1157. dstTags := convAclTagToValueMap(acl.Dst)
  1158. for nodeTag := range targetnode.Tags {
  1159. if _, ok := dstTags[nodeTag.String()]; !ok {
  1160. if _, ok = dstTags[targetnode.ID.String()]; !ok {
  1161. continue
  1162. }
  1163. }
  1164. // get all src tags
  1165. for _, srcAcl := range acl.Src {
  1166. if srcAcl.ID == models.UserAclID {
  1167. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  1168. } else if srcAcl.ID == models.UserGroupAclID {
  1169. // fetch all users in the group
  1170. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  1171. for userName := range usersMap {
  1172. allowedUsers[userName] = append(allowedUsers[userName], acl)
  1173. }
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. for _, userNode := range userNodes {
  1180. if !userNode.StaticNode.Enabled {
  1181. continue
  1182. }
  1183. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  1184. if !ok {
  1185. continue
  1186. }
  1187. for _, acl := range acls {
  1188. if !acl.Enabled {
  1189. continue
  1190. }
  1191. r := models.AclRule{
  1192. ID: acl.ID,
  1193. AllowedProtocol: acl.Proto,
  1194. AllowedPorts: acl.Port,
  1195. Direction: acl.AllowedDirection,
  1196. Allowed: true,
  1197. }
  1198. // Get peers in the tags and add allowed rules
  1199. if userNode.StaticNode.Address != "" {
  1200. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1201. }
  1202. if userNode.StaticNode.Address6 != "" {
  1203. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1204. }
  1205. if aclRule, ok := rules[acl.ID]; ok {
  1206. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  1207. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  1208. rules[acl.ID] = aclRule
  1209. } else {
  1210. rules[acl.ID] = r
  1211. }
  1212. }
  1213. }
  1214. return rules
  1215. }
  1216. func checkIfAnyPolicyisUniDirectional(targetNode models.Node) bool {
  1217. targetNode.Tags[models.TagID(targetNode.ID.String())] = struct{}{}
  1218. acls := listDevicePolicies(models.NetworkID(targetNode.Network))
  1219. for _, acl := range acls {
  1220. if !acl.Enabled {
  1221. continue
  1222. }
  1223. if acl.AllowedDirection == models.TrafficDirectionBi {
  1224. continue
  1225. }
  1226. srcTags := convAclTagToValueMap(acl.Src)
  1227. dstTags := convAclTagToValueMap(acl.Dst)
  1228. for nodeTag := range targetNode.Tags {
  1229. if _, ok := srcTags[nodeTag.String()]; ok {
  1230. return true
  1231. }
  1232. if _, ok := srcTags[targetNode.ID.String()]; ok {
  1233. return true
  1234. }
  1235. if _, ok := dstTags[nodeTag.String()]; ok {
  1236. return true
  1237. }
  1238. if _, ok := dstTags[targetNode.ID.String()]; ok {
  1239. return true
  1240. }
  1241. }
  1242. }
  1243. return false
  1244. }
  1245. func GetAclRulesForNode(targetnodeI *models.Node) (rules map[string]models.AclRule) {
  1246. targetnode := *targetnodeI
  1247. targetnode.Tags[models.TagID(targetnode.ID.String())] = struct{}{}
  1248. defer func() {
  1249. if !targetnode.IsIngressGateway {
  1250. rules = getUserAclRulesForNode(&targetnode, rules)
  1251. }
  1252. }()
  1253. rules = make(map[string]models.AclRule)
  1254. var taggedNodes map[models.TagID][]models.Node
  1255. if targetnode.IsIngressGateway {
  1256. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), false)
  1257. } else {
  1258. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  1259. }
  1260. acls := listDevicePolicies(models.NetworkID(targetnode.Network))
  1261. targetnode.Tags["*"] = struct{}{}
  1262. for _, acl := range acls {
  1263. if !acl.Enabled {
  1264. continue
  1265. }
  1266. srcTags := convAclTagToValueMap(acl.Src)
  1267. dstTags := convAclTagToValueMap(acl.Dst)
  1268. aclRule := models.AclRule{
  1269. ID: acl.ID,
  1270. AllowedProtocol: acl.Proto,
  1271. AllowedPorts: acl.Port,
  1272. Direction: acl.AllowedDirection,
  1273. Allowed: true,
  1274. }
  1275. for nodeTag := range targetnode.Tags {
  1276. if acl.AllowedDirection == models.TrafficDirectionBi {
  1277. var existsInSrcTag bool
  1278. var existsInDstTag bool
  1279. if _, ok := srcTags[nodeTag.String()]; ok {
  1280. existsInSrcTag = true
  1281. }
  1282. if _, ok := srcTags[targetnode.ID.String()]; ok {
  1283. existsInSrcTag = true
  1284. }
  1285. if _, ok := dstTags[nodeTag.String()]; ok {
  1286. existsInDstTag = true
  1287. }
  1288. if _, ok := dstTags[targetnode.ID.String()]; ok {
  1289. existsInDstTag = true
  1290. }
  1291. if existsInSrcTag && !existsInDstTag {
  1292. // get all dst tags
  1293. for dst := range dstTags {
  1294. if dst == nodeTag.String() {
  1295. continue
  1296. }
  1297. // Get peers in the tags and add allowed rules
  1298. nodes := taggedNodes[models.TagID(dst)]
  1299. if dst != targetnode.ID.String() {
  1300. node, err := GetNodeByID(dst)
  1301. if err == nil {
  1302. nodes = append(nodes, node)
  1303. }
  1304. }
  1305. for _, node := range nodes {
  1306. if node.ID == targetnode.ID {
  1307. continue
  1308. }
  1309. if node.Address.IP != nil {
  1310. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1311. }
  1312. if node.Address6.IP != nil {
  1313. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1314. }
  1315. if node.IsStatic && node.StaticNode.Address != "" {
  1316. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1317. }
  1318. if node.IsStatic && node.StaticNode.Address6 != "" {
  1319. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1320. }
  1321. }
  1322. }
  1323. }
  1324. if existsInDstTag && !existsInSrcTag {
  1325. // get all src tags
  1326. for src := range srcTags {
  1327. if src == nodeTag.String() {
  1328. continue
  1329. }
  1330. // Get peers in the tags and add allowed rules
  1331. nodes := taggedNodes[models.TagID(src)]
  1332. if src != targetnode.ID.String() {
  1333. node, err := GetNodeByID(src)
  1334. if err == nil {
  1335. nodes = append(nodes, node)
  1336. }
  1337. }
  1338. for _, node := range nodes {
  1339. if node.ID == targetnode.ID {
  1340. continue
  1341. }
  1342. if node.Address.IP != nil {
  1343. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1344. }
  1345. if node.Address6.IP != nil {
  1346. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1347. }
  1348. if node.IsStatic && node.StaticNode.Address != "" {
  1349. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1350. }
  1351. if node.IsStatic && node.StaticNode.Address6 != "" {
  1352. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1353. }
  1354. }
  1355. }
  1356. }
  1357. if existsInDstTag && existsInSrcTag {
  1358. nodes := taggedNodes[nodeTag]
  1359. for srcID := range srcTags {
  1360. if srcID == targetnode.ID.String() {
  1361. continue
  1362. }
  1363. node, err := GetNodeByID(srcID)
  1364. if err == nil {
  1365. nodes = append(nodes, node)
  1366. }
  1367. }
  1368. for dstID := range dstTags {
  1369. if dstID == targetnode.ID.String() {
  1370. continue
  1371. }
  1372. node, err := GetNodeByID(dstID)
  1373. if err == nil {
  1374. nodes = append(nodes, node)
  1375. }
  1376. }
  1377. for _, node := range nodes {
  1378. if node.ID == targetnode.ID {
  1379. continue
  1380. }
  1381. if node.Address.IP != nil {
  1382. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1383. }
  1384. if node.Address6.IP != nil {
  1385. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1386. }
  1387. if node.IsStatic && node.StaticNode.Address != "" {
  1388. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1389. }
  1390. if node.IsStatic && node.StaticNode.Address6 != "" {
  1391. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1392. }
  1393. }
  1394. }
  1395. } else {
  1396. _, all := dstTags["*"]
  1397. if _, ok := dstTags[nodeTag.String()]; ok || all {
  1398. // get all src tags
  1399. for src := range srcTags {
  1400. if src == nodeTag.String() {
  1401. continue
  1402. }
  1403. // Get peers in the tags and add allowed rules
  1404. nodes := taggedNodes[models.TagID(src)]
  1405. for _, node := range nodes {
  1406. if node.ID == targetnode.ID {
  1407. continue
  1408. }
  1409. if node.Address.IP != nil {
  1410. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1411. }
  1412. if node.Address6.IP != nil {
  1413. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1414. }
  1415. if node.IsStatic && node.StaticNode.Address != "" {
  1416. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1417. }
  1418. if node.IsStatic && node.StaticNode.Address6 != "" {
  1419. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. }
  1426. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  1427. rules[acl.ID] = aclRule
  1428. }
  1429. }
  1430. return rules
  1431. }