common.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.RLock()
  56. allowed = acl[ID] == Allowed
  57. AclMutex.RUnlock()
  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. if _, ok := networkACL[ID1]; !ok {
  77. slog.Error("ACL missing for ", "id", ID1)
  78. return
  79. }
  80. if _, ok := networkACL[ID2]; !ok {
  81. slog.Error("ACL missing for ", "id", ID2)
  82. return
  83. }
  84. if _, ok := networkACL[ID1][ID2]; !ok {
  85. slog.Error("ACL missing for ", "id1", ID1, "id2", ID2)
  86. return
  87. }
  88. if _, ok := networkACL[ID2][ID1]; !ok {
  89. slog.Error("ACL missing for ", "id2", ID2, "id1", ID1)
  90. return
  91. }
  92. networkACL[ID1][ID2] = value
  93. networkACL[ID2][ID1] = value
  94. }
  95. // ACLContainer.Save - saves the state of a ACLContainer to the db
  96. func (aclContainer ACLContainer) Save(containerID ContainerID) (ACLContainer, error) {
  97. return upsertACLContainer(containerID, aclContainer)
  98. }
  99. // ACLContainer.New - saves the state of a ACLContainer to the db
  100. func (aclContainer ACLContainer) New(containerID ContainerID) (ACLContainer, error) {
  101. return upsertACLContainer(containerID, nil)
  102. }
  103. // ACLContainer.Get - saves the state of a ACLContainer to the db
  104. func (aclContainer ACLContainer) Get(containerID ContainerID) (ACLContainer, error) {
  105. return fetchACLContainer(containerID)
  106. }
  107. // == private ==
  108. // fetchACLContainer - fetches all current rules in given ACL container
  109. func fetchACLContainer(containerID ContainerID) (ACLContainer, error) {
  110. AclMutex.RLock()
  111. defer AclMutex.RUnlock()
  112. if servercfg.CacheEnabled() {
  113. if aclContainer, ok := fetchAclContainerFromCache(containerID); ok {
  114. return aclContainer, nil
  115. }
  116. }
  117. aclJson, err := fetchACLContainerJson(ContainerID(containerID))
  118. if err != nil {
  119. return nil, err
  120. }
  121. var currentNetworkACL ACLContainer
  122. if err := json.Unmarshal([]byte(aclJson), &currentNetworkACL); err != nil {
  123. return nil, err
  124. }
  125. if servercfg.CacheEnabled() {
  126. storeAclContainerInCache(containerID, currentNetworkACL)
  127. }
  128. return currentNetworkACL, nil
  129. }
  130. // fetchACLContainerJson - fetch the current ACL of given container except in json string
  131. func fetchACLContainerJson(containerID ContainerID) (ACLJson, error) {
  132. currentACLs, err := database.FetchRecord(database.NODE_ACLS_TABLE_NAME, string(containerID))
  133. if err != nil {
  134. return ACLJson(""), err
  135. }
  136. return ACLJson(currentACLs), nil
  137. }
  138. // upsertACL - applies a ACL to the db, overwrites or creates
  139. func upsertACL(containerID ContainerID, ID AclID, acl ACL) (ACL, error) {
  140. currentNetACL, err := fetchACLContainer(containerID)
  141. if err != nil {
  142. return acl, err
  143. }
  144. currentNetACL[ID] = acl
  145. _, err = upsertACLContainer(containerID, currentNetACL)
  146. return acl, err
  147. }
  148. // upsertACLContainer - Inserts or updates a network ACL given the json string of the ACL and the container ID
  149. // if nil, create it
  150. func upsertACLContainer(containerID ContainerID, aclContainer ACLContainer) (ACLContainer, error) {
  151. AclMutex.Lock()
  152. defer AclMutex.Unlock()
  153. if aclContainer == nil {
  154. aclContainer = make(ACLContainer)
  155. }
  156. err := database.Insert(string(containerID), string(convertNetworkACLtoACLJson(aclContainer)), database.NODE_ACLS_TABLE_NAME)
  157. if err != nil {
  158. return aclContainer, err
  159. }
  160. if servercfg.CacheEnabled() {
  161. storeAclContainerInCache(containerID, aclContainer)
  162. }
  163. return aclContainer, nil
  164. }
  165. func convertNetworkACLtoACLJson(networkACL ACLContainer) ACLJson {
  166. data, err := json.Marshal(networkACL)
  167. if err != nil {
  168. return ""
  169. }
  170. return ACLJson(data)
  171. }