common.go 4.2 KB

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