common.go 4.9 KB

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