common.go 5.4 KB

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