common.go 4.2 KB

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