common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. networkACL[ID1][ID2] = value
  44. networkACL[ID2][ID1] = value
  45. }
  46. // ACLContainer.Save - saves the state of a ACLContainer to the db
  47. func (aclContainer ACLContainer) Save(containerID ContainerID) (ACLContainer, error) {
  48. return upsertACLContainer(containerID, aclContainer)
  49. }
  50. // ACLContainer.New - saves the state of a ACLContainer to the db
  51. func (aclContainer ACLContainer) New(containerID ContainerID) (ACLContainer, error) {
  52. return upsertACLContainer(containerID, nil)
  53. }
  54. // ACLContainer.Get - saves the state of a ACLContainer to the db
  55. func (aclContainer ACLContainer) Get(containerID ContainerID) (ACLContainer, error) {
  56. return fetchACLContainer(containerID)
  57. }
  58. // == private ==
  59. // fetchACLContainer - fetches all current rules in given ACL container
  60. func fetchACLContainer(containerID ContainerID) (ACLContainer, error) {
  61. aclJson, err := fetchACLContainerJson(ContainerID(containerID))
  62. if err != nil {
  63. return nil, err
  64. }
  65. var currentNetworkACL ACLContainer
  66. if err := json.Unmarshal([]byte(aclJson), &currentNetworkACL); err != nil {
  67. return nil, err
  68. }
  69. return currentNetworkACL, nil
  70. }
  71. // fetchACLContainerJson - fetch the current ACL of given container except in json string
  72. func fetchACLContainerJson(containerID ContainerID) (ACLJson, error) {
  73. currentACLs, err := database.FetchRecord(database.NODE_ACLS_TABLE_NAME, string(containerID))
  74. if err != nil {
  75. return ACLJson(""), err
  76. }
  77. return ACLJson(currentACLs), nil
  78. }
  79. // upsertACL - applies a ACL to the db, overwrites or creates
  80. func upsertACL(containerID ContainerID, ID AclID, acl ACL) (ACL, error) {
  81. currentNetACL, err := fetchACLContainer(containerID)
  82. if err != nil {
  83. return acl, err
  84. }
  85. currentNetACL[ID] = acl
  86. _, err = upsertACLContainer(containerID, currentNetACL)
  87. return acl, err
  88. }
  89. // upsertACLContainer - Inserts or updates a network ACL given the json string of the ACL and the container ID
  90. // if nil, create it
  91. func upsertACLContainer(containerID ContainerID, aclContainer ACLContainer) (ACLContainer, error) {
  92. if aclContainer == nil {
  93. aclContainer = make(ACLContainer)
  94. }
  95. return aclContainer, database.Insert(string(containerID), string(convertNetworkACLtoACLJson(aclContainer)), database.NODE_ACLS_TABLE_NAME)
  96. }
  97. func convertNetworkACLtoACLJson(networkACL ACLContainer) ACLJson {
  98. data, err := json.Marshal(networkACL)
  99. if err != nil {
  100. return ""
  101. }
  102. return ACLJson(data)
  103. }