common.go 5.2 KB

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