acls.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net"
  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. )
  18. // CreateDefaultAclNetworkPolicies - create default acl network policies
  19. func CreateDefaultAclNetworkPolicies(netID models.NetworkID) {
  20. if netID.String() == "" {
  21. return
  22. }
  23. if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-nodes")) {
  24. defaultDeviceAcl := models.Acl{
  25. ID: fmt.Sprintf("%s.%s", netID, "all-nodes"),
  26. Name: "All Nodes",
  27. MetaData: "This Policy allows all nodes in the network to communicate with each other",
  28. Default: true,
  29. NetworkID: netID,
  30. RuleType: models.DevicePolicy,
  31. Src: []models.AclPolicyTag{
  32. {
  33. ID: models.DeviceAclID,
  34. Value: "*",
  35. }},
  36. Dst: []models.AclPolicyTag{
  37. {
  38. ID: models.DeviceAclID,
  39. Value: "*",
  40. }},
  41. AllowedDirection: models.TrafficDirectionBi,
  42. Enabled: true,
  43. CreatedBy: "auto",
  44. CreatedAt: time.Now().UTC(),
  45. }
  46. InsertAcl(defaultDeviceAcl)
  47. }
  48. if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-users")) {
  49. defaultUserAcl := models.Acl{
  50. ID: fmt.Sprintf("%s.%s", netID, "all-users"),
  51. Default: true,
  52. Name: "All Users",
  53. MetaData: "This policy gives access to everything in the network for an user",
  54. NetworkID: netID,
  55. RuleType: models.UserPolicy,
  56. Src: []models.AclPolicyTag{
  57. {
  58. ID: models.UserAclID,
  59. Value: "*",
  60. },
  61. },
  62. Dst: []models.AclPolicyTag{{
  63. ID: models.DeviceAclID,
  64. Value: "*",
  65. }},
  66. AllowedDirection: models.TrafficDirectionUni,
  67. Enabled: true,
  68. CreatedBy: "auto",
  69. CreatedAt: time.Now().UTC(),
  70. }
  71. InsertAcl(defaultUserAcl)
  72. }
  73. if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-remote-access-gws")) {
  74. defaultUserAcl := models.Acl{
  75. ID: fmt.Sprintf("%s.%s", netID, "all-remote-access-gws"),
  76. Default: true,
  77. Name: "All Remote Access Gateways",
  78. NetworkID: netID,
  79. RuleType: models.DevicePolicy,
  80. Src: []models.AclPolicyTag{
  81. {
  82. ID: models.DeviceAclID,
  83. Value: fmt.Sprintf("%s.%s", netID, models.RemoteAccessTagName),
  84. },
  85. },
  86. Dst: []models.AclPolicyTag{
  87. {
  88. ID: models.DeviceAclID,
  89. Value: "*",
  90. },
  91. },
  92. AllowedDirection: models.TrafficDirectionBi,
  93. Enabled: true,
  94. CreatedBy: "auto",
  95. CreatedAt: time.Now().UTC(),
  96. }
  97. InsertAcl(defaultUserAcl)
  98. }
  99. CreateDefaultUserPolicies(netID)
  100. }
  101. // DeleteDefaultNetworkPolicies - deletes all default network acl policies
  102. func DeleteDefaultNetworkPolicies(netId models.NetworkID) {
  103. acls, _ := ListAcls(netId)
  104. for _, acl := range acls {
  105. if acl.NetworkID == netId && acl.Default {
  106. DeleteAcl(acl)
  107. }
  108. }
  109. }
  110. // ValidateCreateAclReq - validates create req for acl
  111. func ValidateCreateAclReq(req models.Acl) error {
  112. // check if acl network exists
  113. _, err := GetNetwork(req.NetworkID.String())
  114. if err != nil {
  115. return errors.New("failed to get network details for " + req.NetworkID.String())
  116. }
  117. // err = CheckIDSyntax(req.Name)
  118. // if err != nil {
  119. // return err
  120. // }
  121. return nil
  122. }
  123. func listAclFromCache() (acls []models.Acl) {
  124. aclCacheMutex.RLock()
  125. defer aclCacheMutex.RUnlock()
  126. for _, acl := range aclCacheMap {
  127. acls = append(acls, acl)
  128. }
  129. return
  130. }
  131. func storeAclInCache(a models.Acl) {
  132. aclCacheMutex.Lock()
  133. defer aclCacheMutex.Unlock()
  134. aclCacheMap[a.ID] = a
  135. }
  136. func removeAclFromCache(a models.Acl) {
  137. aclCacheMutex.Lock()
  138. defer aclCacheMutex.Unlock()
  139. delete(aclCacheMap, a.ID)
  140. }
  141. func getAclFromCache(aID string) (a models.Acl, ok bool) {
  142. aclCacheMutex.RLock()
  143. defer aclCacheMutex.RUnlock()
  144. a, ok = aclCacheMap[aID]
  145. return
  146. }
  147. // InsertAcl - creates acl policy
  148. func InsertAcl(a models.Acl) error {
  149. d, err := json.Marshal(a)
  150. if err != nil {
  151. return err
  152. }
  153. err = database.Insert(a.ID, string(d), database.ACLS_TABLE_NAME)
  154. if err == nil && servercfg.CacheEnabled() {
  155. storeAclInCache(a)
  156. }
  157. return err
  158. }
  159. // GetAcl - gets acl info by id
  160. func GetAcl(aID string) (models.Acl, error) {
  161. a := models.Acl{}
  162. if servercfg.CacheEnabled() {
  163. var ok bool
  164. a, ok = getAclFromCache(aID)
  165. if ok {
  166. return a, nil
  167. }
  168. }
  169. d, err := database.FetchRecord(database.ACLS_TABLE_NAME, aID)
  170. if err != nil {
  171. return a, err
  172. }
  173. err = json.Unmarshal([]byte(d), &a)
  174. if err != nil {
  175. return a, err
  176. }
  177. if servercfg.CacheEnabled() {
  178. storeAclInCache(a)
  179. }
  180. return a, nil
  181. }
  182. // IsAclExists - checks if acl exists
  183. func IsAclExists(aclID string) bool {
  184. _, err := GetAcl(aclID)
  185. return err == nil
  186. }
  187. // IsAclPolicyValid - validates if acl policy is valid
  188. func IsAclPolicyValid(acl models.Acl) bool {
  189. //check if src and dst are valid
  190. if acl.AllowedDirection != models.TrafficDirectionBi &&
  191. acl.AllowedDirection != models.TrafficDirectionUni {
  192. return false
  193. }
  194. switch acl.RuleType {
  195. case models.UserPolicy:
  196. // src list should only contain users
  197. for _, srcI := range acl.Src {
  198. if srcI.ID == "" || srcI.Value == "" {
  199. return false
  200. }
  201. if srcI.Value == "*" {
  202. continue
  203. }
  204. if srcI.ID != models.UserAclID && srcI.ID != models.UserGroupAclID {
  205. return false
  206. }
  207. // check if user group is valid
  208. if srcI.ID == models.UserAclID {
  209. _, err := GetUser(srcI.Value)
  210. if err != nil {
  211. return false
  212. }
  213. } else if srcI.ID == models.UserGroupAclID {
  214. err := IsGroupValid(models.UserGroupID(srcI.Value))
  215. if err != nil {
  216. return false
  217. }
  218. // check if group belongs to this network
  219. netGrps := GetUserGroupsInNetwork(acl.NetworkID)
  220. if _, ok := netGrps[models.UserGroupID(srcI.Value)]; !ok {
  221. return false
  222. }
  223. }
  224. }
  225. for _, dstI := range acl.Dst {
  226. if dstI.ID == "" || dstI.Value == "" {
  227. return false
  228. }
  229. if dstI.ID != models.DeviceAclID {
  230. return false
  231. }
  232. if dstI.Value == "*" {
  233. continue
  234. }
  235. // check if tag is valid
  236. _, err := GetTag(models.TagID(dstI.Value))
  237. if err != nil {
  238. return false
  239. }
  240. }
  241. case models.DevicePolicy:
  242. for _, srcI := range acl.Src {
  243. if srcI.ID == "" || srcI.Value == "" {
  244. return false
  245. }
  246. if srcI.ID != models.DeviceAclID {
  247. return false
  248. }
  249. if srcI.Value == "*" {
  250. continue
  251. }
  252. // check if tag is valid
  253. _, err := GetTag(models.TagID(srcI.Value))
  254. if err != nil {
  255. return false
  256. }
  257. }
  258. for _, dstI := range acl.Dst {
  259. if dstI.ID == "" || dstI.Value == "" {
  260. return false
  261. }
  262. if dstI.ID != models.DeviceAclID {
  263. return false
  264. }
  265. if dstI.Value == "*" {
  266. continue
  267. }
  268. // check if tag is valid
  269. _, err := GetTag(models.TagID(dstI.Value))
  270. if err != nil {
  271. return false
  272. }
  273. }
  274. }
  275. return true
  276. }
  277. // UpdateAcl - updates allowed fields on acls and commits to DB
  278. func UpdateAcl(newAcl, acl models.Acl) error {
  279. if !acl.Default {
  280. acl.Name = newAcl.Name
  281. acl.Src = newAcl.Src
  282. acl.Dst = newAcl.Dst
  283. acl.AllowedDirection = newAcl.AllowedDirection
  284. acl.Port = newAcl.Port
  285. acl.Proto = newAcl.Proto
  286. }
  287. acl.Enabled = newAcl.Enabled
  288. d, err := json.Marshal(acl)
  289. if err != nil {
  290. return err
  291. }
  292. err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)
  293. if err == nil && servercfg.CacheEnabled() {
  294. storeAclInCache(acl)
  295. }
  296. return err
  297. }
  298. // UpsertAcl - upserts acl
  299. func UpsertAcl(acl models.Acl) error {
  300. d, err := json.Marshal(acl)
  301. if err != nil {
  302. return err
  303. }
  304. err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)
  305. if err == nil && servercfg.CacheEnabled() {
  306. storeAclInCache(acl)
  307. }
  308. return err
  309. }
  310. // DeleteAcl - deletes acl policy
  311. func DeleteAcl(a models.Acl) error {
  312. err := database.DeleteRecord(database.ACLS_TABLE_NAME, a.ID)
  313. if err == nil && servercfg.CacheEnabled() {
  314. removeAclFromCache(a)
  315. }
  316. return err
  317. }
  318. // GetDefaultPolicy - fetches default policy in the network by ruleType
  319. func GetDefaultPolicy(netID models.NetworkID, ruleType models.AclPolicyType) (models.Acl, error) {
  320. aclID := "all-users"
  321. if ruleType == models.DevicePolicy {
  322. aclID = "all-nodes"
  323. }
  324. acl, err := GetAcl(fmt.Sprintf("%s.%s", netID, aclID))
  325. if err != nil {
  326. return models.Acl{}, errors.New("default rule not found")
  327. }
  328. if acl.Enabled {
  329. return acl, nil
  330. }
  331. // check if there are any custom all policies
  332. policies, _ := ListAcls(netID)
  333. for _, policy := range policies {
  334. if !policy.Enabled {
  335. continue
  336. }
  337. if policy.RuleType == ruleType {
  338. dstMap := convAclTagToValueMap(policy.Dst)
  339. srcMap := convAclTagToValueMap(policy.Src)
  340. if _, ok := srcMap["*"]; ok {
  341. if _, ok := dstMap["*"]; ok {
  342. return policy, nil
  343. }
  344. }
  345. }
  346. }
  347. return acl, nil
  348. }
  349. func listAcls() (acls []models.Acl) {
  350. if servercfg.CacheEnabled() && len(aclCacheMap) > 0 {
  351. return listAclFromCache()
  352. }
  353. data, err := database.FetchRecords(database.ACLS_TABLE_NAME)
  354. if err != nil && !database.IsEmptyRecord(err) {
  355. return []models.Acl{}
  356. }
  357. for _, dataI := range data {
  358. acl := models.Acl{}
  359. err := json.Unmarshal([]byte(dataI), &acl)
  360. if err != nil {
  361. continue
  362. }
  363. acls = append(acls, acl)
  364. if servercfg.CacheEnabled() {
  365. storeAclInCache(acl)
  366. }
  367. }
  368. return
  369. }
  370. // ListUserPolicies - lists all acl policies enforced on an user
  371. func ListUserPolicies(u models.User) []models.Acl {
  372. allAcls := listAcls()
  373. userAcls := []models.Acl{}
  374. for _, acl := range allAcls {
  375. if acl.RuleType == models.UserPolicy {
  376. srcMap := convAclTagToValueMap(acl.Src)
  377. if _, ok := srcMap[u.UserName]; ok {
  378. userAcls = append(userAcls, acl)
  379. } else {
  380. // check for user groups
  381. for gID := range u.UserGroups {
  382. if _, ok := srcMap[gID.String()]; ok {
  383. userAcls = append(userAcls, acl)
  384. break
  385. }
  386. }
  387. }
  388. }
  389. }
  390. return userAcls
  391. }
  392. // listPoliciesOfUser - lists all user acl policies applied to user in an network
  393. func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {
  394. allAcls := listAcls()
  395. userAcls := []models.Acl{}
  396. for _, acl := range allAcls {
  397. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  398. srcMap := convAclTagToValueMap(acl.Src)
  399. if _, ok := srcMap[user.UserName]; ok {
  400. userAcls = append(userAcls, acl)
  401. continue
  402. }
  403. for netRole := range user.NetworkRoles {
  404. if _, ok := srcMap[netRole.String()]; ok {
  405. userAcls = append(userAcls, acl)
  406. continue
  407. }
  408. }
  409. for userG := range user.UserGroups {
  410. if _, ok := srcMap[userG.String()]; ok {
  411. userAcls = append(userAcls, acl)
  412. continue
  413. }
  414. }
  415. }
  416. }
  417. return userAcls
  418. }
  419. // listDevicePolicies - lists all device policies in a network
  420. func listDevicePolicies(netID models.NetworkID) []models.Acl {
  421. allAcls := listAcls()
  422. deviceAcls := []models.Acl{}
  423. for _, acl := range allAcls {
  424. if acl.NetworkID == netID && acl.RuleType == models.DevicePolicy {
  425. deviceAcls = append(deviceAcls, acl)
  426. }
  427. }
  428. return deviceAcls
  429. }
  430. // ListAcls - lists all acl policies
  431. func ListAcls(netID models.NetworkID) ([]models.Acl, error) {
  432. allAcls := listAcls()
  433. netAcls := []models.Acl{}
  434. for _, acl := range allAcls {
  435. if acl.NetworkID == netID {
  436. netAcls = append(netAcls, acl)
  437. }
  438. }
  439. return netAcls, nil
  440. }
  441. func convAclTagToValueMap(acltags []models.AclPolicyTag) map[string]struct{} {
  442. aclValueMap := make(map[string]struct{})
  443. for _, aclTagI := range acltags {
  444. aclValueMap[aclTagI.Value] = struct{}{}
  445. }
  446. return aclValueMap
  447. }
  448. // IsUserAllowedToCommunicate - check if user is allowed to communicate with peer
  449. func IsUserAllowedToCommunicate(userName string, peer models.Node) bool {
  450. if peer.IsStatic {
  451. peer = peer.StaticNode.ConvertToStaticNode()
  452. }
  453. acl, _ := GetDefaultPolicy(models.NetworkID(peer.Network), models.UserPolicy)
  454. if acl.Enabled {
  455. return true
  456. }
  457. user, err := GetUser(userName)
  458. if err != nil {
  459. return false
  460. }
  461. policies := listPoliciesOfUser(*user, models.NetworkID(peer.Network))
  462. for _, policy := range policies {
  463. if !policy.Enabled {
  464. continue
  465. }
  466. dstMap := convAclTagToValueMap(policy.Dst)
  467. if _, ok := dstMap["*"]; ok {
  468. return true
  469. }
  470. for tagID := range peer.Tags {
  471. if _, ok := dstMap[tagID.String()]; ok {
  472. return true
  473. }
  474. }
  475. }
  476. return false
  477. }
  478. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer
  479. func IsNodeAllowedToCommunicate(node, peer models.Node) bool {
  480. if node.IsStatic {
  481. node = node.StaticNode.ConvertToStaticNode()
  482. }
  483. if peer.IsStatic {
  484. peer = peer.StaticNode.ConvertToStaticNode()
  485. }
  486. // check default policy if all allowed return true
  487. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  488. if err == nil {
  489. if defaultPolicy.Enabled {
  490. return true
  491. }
  492. }
  493. // list device policies
  494. policies := listDevicePolicies(models.NetworkID(peer.Network))
  495. for _, policy := range policies {
  496. if !policy.Enabled {
  497. continue
  498. }
  499. srcMap := convAclTagToValueMap(policy.Src)
  500. dstMap := convAclTagToValueMap(policy.Dst)
  501. // fmt.Printf("\n======> SRCMAP: %+v\n", srcMap)
  502. // fmt.Printf("\n======> DSTMAP: %+v\n", dstMap)
  503. // fmt.Printf("\n======> node Tags: %+v\n", node.Tags)
  504. // fmt.Printf("\n======> peer Tags: %+v\n", peer.Tags)
  505. for tagID := range node.Tags {
  506. if _, ok := dstMap[tagID.String()]; ok {
  507. if _, ok := srcMap["*"]; ok {
  508. return true
  509. }
  510. for tagID := range peer.Tags {
  511. if _, ok := srcMap[tagID.String()]; ok {
  512. return true
  513. }
  514. }
  515. }
  516. if _, ok := srcMap[tagID.String()]; ok {
  517. if _, ok := dstMap["*"]; ok {
  518. return true
  519. }
  520. for tagID := range peer.Tags {
  521. if _, ok := dstMap[tagID.String()]; ok {
  522. return true
  523. }
  524. }
  525. }
  526. }
  527. for tagID := range peer.Tags {
  528. if _, ok := dstMap[tagID.String()]; ok {
  529. if _, ok := srcMap["*"]; ok {
  530. return true
  531. }
  532. for tagID := range node.Tags {
  533. if _, ok := srcMap[tagID.String()]; ok {
  534. return true
  535. }
  536. }
  537. }
  538. if _, ok := srcMap[tagID.String()]; ok {
  539. if _, ok := dstMap["*"]; ok {
  540. return true
  541. }
  542. for tagID := range node.Tags {
  543. if _, ok := dstMap[tagID.String()]; ok {
  544. return true
  545. }
  546. }
  547. }
  548. }
  549. }
  550. return false
  551. }
  552. // SortTagEntrys - Sorts slice of Tag entries by their id
  553. func SortAclEntrys(acls []models.Acl) {
  554. sort.Slice(acls, func(i, j int) bool {
  555. return acls[i].Name < acls[j].Name
  556. })
  557. }
  558. // UpdateDeviceTag - updates device tag on acl policies
  559. func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
  560. acls := listDevicePolicies(netID)
  561. update := false
  562. for _, acl := range acls {
  563. for i, srcTagI := range acl.Src {
  564. if srcTagI.ID == models.DeviceAclID {
  565. if OldID.String() == srcTagI.Value {
  566. acl.Src[i].Value = newID.String()
  567. update = true
  568. }
  569. }
  570. }
  571. for i, dstTagI := range acl.Dst {
  572. if dstTagI.ID == models.DeviceAclID {
  573. if OldID.String() == dstTagI.Value {
  574. acl.Dst[i].Value = newID.String()
  575. update = true
  576. }
  577. }
  578. }
  579. if update {
  580. UpsertAcl(acl)
  581. }
  582. }
  583. }
  584. func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
  585. acls := listDevicePolicies(netID)
  586. for _, acl := range acls {
  587. for _, srcTagI := range acl.Src {
  588. if srcTagI.ID == models.DeviceAclID {
  589. if tagID.String() == srcTagI.Value {
  590. return true
  591. }
  592. }
  593. }
  594. for _, dstTagI := range acl.Dst {
  595. if dstTagI.ID == models.DeviceAclID {
  596. return true
  597. }
  598. }
  599. }
  600. return false
  601. }
  602. // RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
  603. func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
  604. acls := listDevicePolicies(netID)
  605. update := false
  606. for _, acl := range acls {
  607. for i, srcTagI := range acl.Src {
  608. if srcTagI.ID == models.DeviceAclID {
  609. if tagID.String() == srcTagI.Value {
  610. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  611. update = true
  612. }
  613. }
  614. }
  615. for i, dstTagI := range acl.Dst {
  616. if dstTagI.ID == models.DeviceAclID {
  617. if tagID.String() == dstTagI.Value {
  618. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  619. update = true
  620. }
  621. }
  622. }
  623. if update {
  624. UpsertAcl(acl)
  625. }
  626. }
  627. return nil
  628. }
  629. func GetAclRulesForNode(node *models.Node) (rules map[string]models.AclRule) {
  630. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  631. rules = make(map[string]models.AclRule)
  632. if err == nil && defaultPolicy.Enabled {
  633. return map[string]models.AclRule{
  634. defaultPolicy.ID: {
  635. IPList: []net.IPNet{node.NetworkRange},
  636. IP6List: []net.IPNet{node.NetworkRange6},
  637. AllowedProtocols: []models.Protocol{models.ALL},
  638. Direction: models.TrafficDirectionBi,
  639. Allowed: true,
  640. },
  641. }
  642. }
  643. taggedNodes := GetTagMapWithNodesByNetwork(models.NetworkID(node.Network))
  644. acls := listDevicePolicies(models.NetworkID(node.Network))
  645. //allowedNodeUniqueMap := make(map[string]struct{})
  646. for nodeTag := range node.Tags {
  647. for _, acl := range acls {
  648. if acl.Default || !acl.Enabled {
  649. continue
  650. }
  651. srcTags := convAclTagToValueMap(acl.Src)
  652. dstTags := convAclTagToValueMap(acl.Dst)
  653. aclRule := models.AclRule{
  654. ID: acl.ID,
  655. AllowedProtocols: acl.Proto,
  656. AllowedPorts: acl.Port,
  657. Direction: acl.AllowedDirection,
  658. Allowed: true,
  659. }
  660. if acl.AllowedDirection == models.TrafficDirectionBi {
  661. var existsInSrcTag bool
  662. var existsInDstTag bool
  663. // if contains all resources, return entire cidr
  664. if _, ok := srcTags["*"]; ok {
  665. return map[string]models.AclRule{
  666. acl.ID: {
  667. IPList: []net.IPNet{node.NetworkRange},
  668. IP6List: []net.IPNet{node.NetworkRange6},
  669. AllowedProtocols: []models.Protocol{models.ALL},
  670. AllowedPorts: acl.Port,
  671. Direction: acl.AllowedDirection,
  672. Allowed: true,
  673. },
  674. }
  675. }
  676. if _, ok := dstTags["*"]; ok {
  677. return map[string]models.AclRule{
  678. acl.ID: {
  679. IPList: []net.IPNet{node.NetworkRange},
  680. IP6List: []net.IPNet{node.NetworkRange6},
  681. AllowedProtocols: []models.Protocol{models.ALL},
  682. AllowedPorts: acl.Port,
  683. Direction: acl.AllowedDirection,
  684. Allowed: true,
  685. },
  686. }
  687. }
  688. if _, ok := srcTags[nodeTag.String()]; ok {
  689. existsInSrcTag = true
  690. }
  691. if _, ok := dstTags[nodeTag.String()]; ok {
  692. existsInDstTag = true
  693. }
  694. if existsInSrcTag {
  695. // get all dst tags
  696. for dst := range dstTags {
  697. if dst == nodeTag.String() {
  698. continue
  699. }
  700. // Get peers in the tags and add allowed rules
  701. nodes := taggedNodes[models.TagID(dst)]
  702. for _, node := range nodes {
  703. if node.Address.IP != nil {
  704. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  705. }
  706. if node.Address6.IP != nil {
  707. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  708. }
  709. }
  710. }
  711. }
  712. if existsInDstTag {
  713. // get all src tags
  714. for src := range srcTags {
  715. if src == nodeTag.String() {
  716. continue
  717. }
  718. // Get peers in the tags and add allowed rules
  719. nodes := taggedNodes[models.TagID(src)]
  720. for _, node := range nodes {
  721. if node.Address.IP != nil {
  722. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  723. }
  724. if node.Address6.IP != nil {
  725. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  726. }
  727. }
  728. }
  729. }
  730. if existsInDstTag && existsInSrcTag {
  731. nodes := taggedNodes[nodeTag]
  732. for _, node := range nodes {
  733. if node.Address.IP != nil {
  734. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  735. }
  736. if node.Address6.IP != nil {
  737. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  738. }
  739. }
  740. }
  741. } else {
  742. if _, ok := dstTags[nodeTag.String()]; ok {
  743. // get all src tags
  744. for src := range srcTags {
  745. if src == nodeTag.String() {
  746. continue
  747. }
  748. // Get peers in the tags and add allowed rules
  749. nodes := taggedNodes[models.TagID(src)]
  750. for _, node := range nodes {
  751. if node.Address.IP != nil {
  752. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  753. }
  754. if node.Address6.IP != nil {
  755. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  756. }
  757. }
  758. }
  759. }
  760. }
  761. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  762. rules[acl.ID] = aclRule
  763. }
  764. }
  765. }
  766. return rules
  767. }