common.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package acls
  2. import (
  3. "encoding/json"
  4. "sync"
  5. "github.com/gravitl/netmaker/database"
  6. )
  7. var (
  8. aclCacheMutex = &sync.RWMutex{}
  9. aclCacheMap = make(map[ContainerID]ACLContainer)
  10. )
  11. func fetchAclContainerFromCache(containerID ContainerID) (aclCont ACLContainer, ok bool) {
  12. aclCacheMutex.RLock()
  13. aclCont, ok = aclCacheMap[containerID]
  14. aclCacheMutex.RUnlock()
  15. return
  16. }
  17. func storeAclContainerInCache(containerID ContainerID, aclContainer ACLContainer) {
  18. aclCacheMutex.Lock()
  19. aclCacheMap[containerID] = aclContainer
  20. aclCacheMutex.Unlock()
  21. }
  22. func DeleteAclFromCache(containerID ContainerID) {
  23. aclCacheMutex.Lock()
  24. delete(aclCacheMap, containerID)
  25. aclCacheMutex.Unlock()
  26. }
  27. // == type functions ==
  28. // ACL.Allow - allows access by ID in memory
  29. func (acl ACL) Allow(ID AclID) {
  30. acl[ID] = Allowed
  31. }
  32. // ACL.DisallowNode - disallows access by ID in memory
  33. func (acl ACL) Disallow(ID AclID) {
  34. acl[ID] = NotAllowed
  35. }
  36. // ACL.Remove - removes a node from a ACL in memory
  37. func (acl ACL) Remove(ID AclID) {
  38. delete(acl, ID)
  39. }
  40. // ACL.Update - updates a ACL in DB
  41. func (acl ACL) Save(containerID ContainerID, ID AclID) (ACL, error) {
  42. return upsertACL(containerID, ID, acl)
  43. }
  44. // ACL.IsAllowed - sees if ID is allowed in referring ACL
  45. func (acl ACL) IsAllowed(ID AclID) bool {
  46. return acl[ID] == Allowed
  47. }
  48. // ACLContainer.IsAllowed - returns if the current ACL container contains allowed ACLs between two IDs
  49. func (aclContainer ACLContainer) IsAllowed(ID1, ID2 AclID) bool {
  50. return aclContainer[ID1].IsAllowed(ID2) && aclContainer[ID2].IsAllowed(ID1)
  51. }
  52. // ACLContainer.UpdateACL - saves the state of a ACL in the ACLContainer in memory
  53. func (aclContainer ACLContainer) UpdateACL(ID AclID, acl ACL) ACLContainer {
  54. aclContainer[ID] = acl
  55. return aclContainer
  56. }
  57. // ACLContainer.RemoveACL - removes the state of a ACL in the ACLContainer in memory
  58. func (aclContainer ACLContainer) RemoveACL(ID AclID) ACLContainer {
  59. delete(aclContainer, ID)
  60. return aclContainer
  61. }
  62. // ACLContainer.ChangeAccess - changes the relationship between two nodes in memory
  63. func (networkACL ACLContainer) ChangeAccess(ID1, ID2 AclID, value byte) {
  64. networkACL[ID1][ID2] = value
  65. networkACL[ID2][ID1] = value
  66. }
  67. // ACLContainer.Save - saves the state of a ACLContainer to the db
  68. func (aclContainer ACLContainer) Save(containerID ContainerID) (ACLContainer, error) {
  69. return upsertACLContainer(containerID, aclContainer)
  70. }
  71. // ACLContainer.New - saves the state of a ACLContainer to the db
  72. func (aclContainer ACLContainer) New(containerID ContainerID) (ACLContainer, error) {
  73. return upsertACLContainer(containerID, nil)
  74. }
  75. // ACLContainer.Get - saves the state of a ACLContainer to the db
  76. func (aclContainer ACLContainer) Get(containerID ContainerID) (ACLContainer, error) {
  77. return fetchACLContainer(containerID)
  78. }
  79. // == private ==
  80. // fetchACLContainer - fetches all current rules in given ACL container
  81. func fetchACLContainer(containerID ContainerID) (ACLContainer, error) {
  82. if aclContainer, ok := fetchAclContainerFromCache(containerID); ok {
  83. return aclContainer, nil
  84. }
  85. aclJson, err := fetchACLContainerJson(ContainerID(containerID))
  86. if err != nil {
  87. return nil, err
  88. }
  89. var currentNetworkACL ACLContainer
  90. if err := json.Unmarshal([]byte(aclJson), &currentNetworkACL); err != nil {
  91. return nil, err
  92. }
  93. storeAclContainerInCache(containerID, currentNetworkACL)
  94. return currentNetworkACL, nil
  95. }
  96. // fetchACLContainerJson - fetch the current ACL of given container except in json string
  97. func fetchACLContainerJson(containerID ContainerID) (ACLJson, error) {
  98. currentACLs, err := database.FetchRecord(database.NODE_ACLS_TABLE_NAME, string(containerID))
  99. if err != nil {
  100. return ACLJson(""), err
  101. }
  102. return ACLJson(currentACLs), nil
  103. }
  104. // upsertACL - applies a ACL to the db, overwrites or creates
  105. func upsertACL(containerID ContainerID, ID AclID, acl ACL) (ACL, error) {
  106. currentNetACL, err := fetchACLContainer(containerID)
  107. if err != nil {
  108. return acl, err
  109. }
  110. currentNetACL[ID] = acl
  111. _, err = upsertACLContainer(containerID, currentNetACL)
  112. return acl, err
  113. }
  114. // upsertACLContainer - Inserts or updates a network ACL given the json string of the ACL and the container ID
  115. // if nil, create it
  116. func upsertACLContainer(containerID ContainerID, aclContainer ACLContainer) (ACLContainer, error) {
  117. if aclContainer == nil {
  118. aclContainer = make(ACLContainer)
  119. }
  120. err := database.Insert(string(containerID), string(convertNetworkACLtoACLJson(aclContainer)), database.NODE_ACLS_TABLE_NAME)
  121. if err != nil {
  122. return aclContainer, err
  123. }
  124. storeAclContainerInCache(containerID, aclContainer)
  125. return aclContainer, nil
  126. }
  127. func convertNetworkACLtoACLJson(networkACL ACLContainer) ACLJson {
  128. data, err := json.Marshal(networkACL)
  129. if err != nil {
  130. return ""
  131. }
  132. return ACLJson(data)
  133. }