common.go 5.4 KB

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