acls.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. fmt.Println("===> ACL Db DATA: ", data)
  358. for _, dataI := range data {
  359. acl := models.Acl{}
  360. err := json.Unmarshal([]byte(dataI), &acl)
  361. if err != nil {
  362. fmt.Println("======> UNMARSHAL ERROR ACLS ", err)
  363. continue
  364. }
  365. acls = append(acls, acl)
  366. if servercfg.CacheEnabled() {
  367. storeAclInCache(acl)
  368. }
  369. }
  370. return
  371. }
  372. // ListUserPolicies - lists all acl policies enforced on an user
  373. func ListUserPolicies(u models.User) []models.Acl {
  374. allAcls := listAcls()
  375. userAcls := []models.Acl{}
  376. for _, acl := range allAcls {
  377. if acl.RuleType == models.UserPolicy {
  378. srcMap := convAclTagToValueMap(acl.Src)
  379. if _, ok := srcMap[u.UserName]; ok {
  380. userAcls = append(userAcls, acl)
  381. } else {
  382. // check for user groups
  383. for gID := range u.UserGroups {
  384. if _, ok := srcMap[gID.String()]; ok {
  385. userAcls = append(userAcls, acl)
  386. break
  387. }
  388. }
  389. }
  390. }
  391. }
  392. return userAcls
  393. }
  394. // listPoliciesOfUser - lists all user acl policies applied to user in an network
  395. func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {
  396. allAcls := listAcls()
  397. userAcls := []models.Acl{}
  398. for _, acl := range allAcls {
  399. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  400. srcMap := convAclTagToValueMap(acl.Src)
  401. if _, ok := srcMap[user.UserName]; ok {
  402. userAcls = append(userAcls, acl)
  403. continue
  404. }
  405. for netRole := range user.NetworkRoles {
  406. if _, ok := srcMap[netRole.String()]; ok {
  407. userAcls = append(userAcls, acl)
  408. continue
  409. }
  410. }
  411. for userG := range user.UserGroups {
  412. if _, ok := srcMap[userG.String()]; ok {
  413. userAcls = append(userAcls, acl)
  414. continue
  415. }
  416. }
  417. }
  418. }
  419. return userAcls
  420. }
  421. // listDevicePolicies - lists all device policies in a network
  422. func listDevicePolicies(netID models.NetworkID) []models.Acl {
  423. allAcls := listAcls()
  424. deviceAcls := []models.Acl{}
  425. for _, acl := range allAcls {
  426. if acl.NetworkID == netID && acl.RuleType == models.DevicePolicy {
  427. deviceAcls = append(deviceAcls, acl)
  428. }
  429. }
  430. return deviceAcls
  431. }
  432. // ListAcls - lists all acl policies
  433. func ListAcls(netID models.NetworkID) ([]models.Acl, error) {
  434. allAcls := listAcls()
  435. netAcls := []models.Acl{}
  436. for _, acl := range allAcls {
  437. if acl.NetworkID == netID {
  438. netAcls = append(netAcls, acl)
  439. }
  440. }
  441. return netAcls, nil
  442. }
  443. func convAclTagToValueMap(acltags []models.AclPolicyTag) map[string]struct{} {
  444. aclValueMap := make(map[string]struct{})
  445. for _, aclTagI := range acltags {
  446. aclValueMap[aclTagI.Value] = struct{}{}
  447. }
  448. return aclValueMap
  449. }
  450. // IsUserAllowedToCommunicate - check if user is allowed to communicate with peer
  451. func IsUserAllowedToCommunicate(userName string, peer models.Node) bool {
  452. if peer.IsStatic {
  453. peer = peer.StaticNode.ConvertToStaticNode()
  454. }
  455. acl, _ := GetDefaultPolicy(models.NetworkID(peer.Network), models.UserPolicy)
  456. if acl.Enabled {
  457. return true
  458. }
  459. user, err := GetUser(userName)
  460. if err != nil {
  461. return false
  462. }
  463. policies := listPoliciesOfUser(*user, models.NetworkID(peer.Network))
  464. for _, policy := range policies {
  465. if !policy.Enabled {
  466. continue
  467. }
  468. dstMap := convAclTagToValueMap(policy.Dst)
  469. if _, ok := dstMap["*"]; ok {
  470. return true
  471. }
  472. for tagID := range peer.Tags {
  473. if _, ok := dstMap[tagID.String()]; ok {
  474. return true
  475. }
  476. }
  477. }
  478. return false
  479. }
  480. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer
  481. func IsNodeAllowedToCommunicate(node, peer models.Node) bool {
  482. if node.IsStatic {
  483. node = node.StaticNode.ConvertToStaticNode()
  484. }
  485. if peer.IsStatic {
  486. peer = peer.StaticNode.ConvertToStaticNode()
  487. }
  488. // check default policy if all allowed return true
  489. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  490. if err == nil {
  491. if defaultPolicy.Enabled {
  492. return true
  493. }
  494. }
  495. // list device policies
  496. policies := listDevicePolicies(models.NetworkID(peer.Network))
  497. for _, policy := range policies {
  498. if !policy.Enabled {
  499. continue
  500. }
  501. srcMap := convAclTagToValueMap(policy.Src)
  502. dstMap := convAclTagToValueMap(policy.Dst)
  503. // fmt.Printf("\n======> SRCMAP: %+v\n", srcMap)
  504. // fmt.Printf("\n======> DSTMAP: %+v\n", dstMap)
  505. // fmt.Printf("\n======> node Tags: %+v\n", node.Tags)
  506. // fmt.Printf("\n======> peer Tags: %+v\n", peer.Tags)
  507. for tagID := range node.Tags {
  508. if _, ok := dstMap[tagID.String()]; ok {
  509. if _, ok := srcMap["*"]; ok {
  510. return true
  511. }
  512. for tagID := range peer.Tags {
  513. if _, ok := srcMap[tagID.String()]; ok {
  514. return true
  515. }
  516. }
  517. }
  518. if _, ok := srcMap[tagID.String()]; ok {
  519. if _, ok := dstMap["*"]; ok {
  520. return true
  521. }
  522. for tagID := range peer.Tags {
  523. if _, ok := dstMap[tagID.String()]; ok {
  524. return true
  525. }
  526. }
  527. }
  528. }
  529. for tagID := range peer.Tags {
  530. if _, ok := dstMap[tagID.String()]; ok {
  531. if _, ok := srcMap["*"]; ok {
  532. return true
  533. }
  534. for tagID := range node.Tags {
  535. if _, ok := srcMap[tagID.String()]; ok {
  536. return true
  537. }
  538. }
  539. }
  540. if _, ok := srcMap[tagID.String()]; ok {
  541. if _, ok := dstMap["*"]; ok {
  542. return true
  543. }
  544. for tagID := range node.Tags {
  545. if _, ok := dstMap[tagID.String()]; ok {
  546. return true
  547. }
  548. }
  549. }
  550. }
  551. }
  552. return false
  553. }
  554. // SortTagEntrys - Sorts slice of Tag entries by their id
  555. func SortAclEntrys(acls []models.Acl) {
  556. sort.Slice(acls, func(i, j int) bool {
  557. return acls[i].Name < acls[j].Name
  558. })
  559. }
  560. // UpdateDeviceTag - updates device tag on acl policies
  561. func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
  562. acls := listDevicePolicies(netID)
  563. update := false
  564. for _, acl := range acls {
  565. for i, srcTagI := range acl.Src {
  566. if srcTagI.ID == models.DeviceAclID {
  567. if OldID.String() == srcTagI.Value {
  568. acl.Src[i].Value = newID.String()
  569. update = true
  570. }
  571. }
  572. }
  573. for i, dstTagI := range acl.Dst {
  574. if dstTagI.ID == models.DeviceAclID {
  575. if OldID.String() == dstTagI.Value {
  576. acl.Dst[i].Value = newID.String()
  577. update = true
  578. }
  579. }
  580. }
  581. if update {
  582. UpsertAcl(acl)
  583. }
  584. }
  585. }
  586. func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
  587. acls := listDevicePolicies(netID)
  588. for _, acl := range acls {
  589. for _, srcTagI := range acl.Src {
  590. if srcTagI.ID == models.DeviceAclID {
  591. if tagID.String() == srcTagI.Value {
  592. return true
  593. }
  594. }
  595. }
  596. for _, dstTagI := range acl.Dst {
  597. if dstTagI.ID == models.DeviceAclID {
  598. return true
  599. }
  600. }
  601. }
  602. return false
  603. }
  604. // RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
  605. func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
  606. acls := listDevicePolicies(netID)
  607. update := false
  608. for _, acl := range acls {
  609. for i, srcTagI := range acl.Src {
  610. if srcTagI.ID == models.DeviceAclID {
  611. if tagID.String() == srcTagI.Value {
  612. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  613. update = true
  614. }
  615. }
  616. }
  617. for i, dstTagI := range acl.Dst {
  618. if dstTagI.ID == models.DeviceAclID {
  619. if tagID.String() == dstTagI.Value {
  620. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  621. update = true
  622. }
  623. }
  624. }
  625. if update {
  626. UpsertAcl(acl)
  627. }
  628. }
  629. return nil
  630. }
  631. func GetAclRulesForNode(node *models.Node) (rules map[string]models.AclRule) {
  632. defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  633. rules = make(map[string]models.AclRule)
  634. if err == nil && defaultPolicy.Enabled {
  635. return map[string]models.AclRule{
  636. defaultPolicy.ID: {
  637. IPList: []net.IPNet{node.NetworkRange},
  638. IP6List: []net.IPNet{node.NetworkRange6},
  639. AllowedProtocols: []models.Protocol{models.ALL},
  640. Direction: models.TrafficDirectionBi,
  641. Allowed: true,
  642. },
  643. }
  644. }
  645. taggedNodes := GetTagMapWithNodesByNetwork(models.NetworkID(node.Network))
  646. acls := listDevicePolicies(models.NetworkID(node.Network))
  647. //allowedNodeUniqueMap := make(map[string]struct{})
  648. for nodeTag := range node.Tags {
  649. for _, acl := range acls {
  650. if acl.Default || !acl.Enabled {
  651. continue
  652. }
  653. srcTags := convAclTagToValueMap(acl.Src)
  654. dstTags := convAclTagToValueMap(acl.Dst)
  655. aclRule := models.AclRule{
  656. ID: acl.ID,
  657. AllowedProtocols: acl.Proto,
  658. AllowedPorts: acl.Port,
  659. Direction: acl.AllowedDirection,
  660. Allowed: true,
  661. }
  662. if acl.AllowedDirection == models.TrafficDirectionBi {
  663. var existsInSrcTag bool
  664. var existsInDstTag bool
  665. // if contains all resources, return entire cidr
  666. if _, ok := srcTags["*"]; ok {
  667. return map[string]models.AclRule{
  668. acl.ID: {
  669. IPList: []net.IPNet{node.NetworkRange},
  670. IP6List: []net.IPNet{node.NetworkRange6},
  671. AllowedProtocols: []models.Protocol{models.ALL},
  672. AllowedPorts: acl.Port,
  673. Direction: acl.AllowedDirection,
  674. Allowed: true,
  675. },
  676. }
  677. }
  678. if _, ok := dstTags["*"]; ok {
  679. return map[string]models.AclRule{
  680. acl.ID: {
  681. IPList: []net.IPNet{node.NetworkRange},
  682. IP6List: []net.IPNet{node.NetworkRange6},
  683. AllowedProtocols: []models.Protocol{models.ALL},
  684. AllowedPorts: acl.Port,
  685. Direction: acl.AllowedDirection,
  686. Allowed: true,
  687. },
  688. }
  689. }
  690. if _, ok := srcTags[nodeTag.String()]; ok {
  691. existsInSrcTag = true
  692. }
  693. if _, ok := dstTags[nodeTag.String()]; ok {
  694. existsInDstTag = true
  695. }
  696. if existsInSrcTag {
  697. // get all dst tags
  698. for dst := range dstTags {
  699. if dst == nodeTag.String() {
  700. continue
  701. }
  702. // Get peers in the tags and add allowed rules
  703. nodes := taggedNodes[models.TagID(dst)]
  704. for _, node := range nodes {
  705. if node.Address.IP != nil {
  706. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  707. }
  708. if node.Address6.IP != nil {
  709. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  710. }
  711. }
  712. }
  713. }
  714. if existsInDstTag {
  715. // get all src tags
  716. for src := range srcTags {
  717. if src == nodeTag.String() {
  718. continue
  719. }
  720. // Get peers in the tags and add allowed rules
  721. nodes := taggedNodes[models.TagID(src)]
  722. for _, node := range nodes {
  723. if node.Address.IP != nil {
  724. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  725. }
  726. if node.Address6.IP != nil {
  727. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  728. }
  729. }
  730. }
  731. }
  732. if existsInDstTag && existsInSrcTag {
  733. nodes := taggedNodes[nodeTag]
  734. for _, node := range nodes {
  735. if node.Address.IP != nil {
  736. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  737. }
  738. if node.Address6.IP != nil {
  739. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  740. }
  741. }
  742. }
  743. } else {
  744. if _, ok := dstTags[nodeTag.String()]; ok {
  745. // get all src tags
  746. for src := range srcTags {
  747. if src == nodeTag.String() {
  748. continue
  749. }
  750. // Get peers in the tags and add allowed rules
  751. nodes := taggedNodes[models.TagID(src)]
  752. for _, node := range nodes {
  753. if node.Address.IP != nil {
  754. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  755. }
  756. if node.Address6.IP != nil {
  757. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  758. }
  759. }
  760. }
  761. }
  762. }
  763. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  764. rules[acl.ID] = aclRule
  765. }
  766. }
  767. }
  768. return rules
  769. }