common.go 5.2 KB

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