common.go 5.2 KB

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