common.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package acls
  2. import (
  3. "encoding/json"
  4. "github.com/gravitl/netmaker/database"
  5. )
  6. // == type functions ==
  7. // ACL.AllowNode - allows a node by ID in memory
  8. func (acl ACL) Allow(ID AclID) {
  9. acl[ID] = Allowed
  10. }
  11. // ACL.DisallowNode - disallows a node 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
  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.UpdateACL - saves the state of a ACL in the ACLContainer in memory
  28. func (aclContainer ACLContainer) UpdateACL(ID AclID, acl ACL) ACLContainer {
  29. aclContainer[ID] = acl
  30. return aclContainer
  31. }
  32. // ACLContainer.RemoveACL - removes the state of a ACL in the ACLContainer in memory
  33. func (aclContainer ACLContainer) RemoveACL(ID AclID) ACLContainer {
  34. delete(aclContainer, ID)
  35. return aclContainer
  36. }
  37. // ACLContainer.ChangeAccess - changes the relationship between two nodes in memory
  38. func (networkACL ACLContainer) ChangeAccess(ID1, ID2 AclID, value byte) {
  39. networkACL[ID1][ID2] = value
  40. networkACL[ID2][ID1] = value
  41. }
  42. // ACLContainer.Save - saves the state of a ACLContainer to the db
  43. func (aclContainer ACLContainer) Save(containerID ContainerID) (ACLContainer, error) {
  44. return upsertACLContainer(containerID, aclContainer)
  45. }
  46. // ACLContainer.New - saves the state of a ACLContainer to the db
  47. func (aclContainer ACLContainer) New(containerID ContainerID) (ACLContainer, error) {
  48. return upsertACLContainer(containerID, nil)
  49. }
  50. // ACLContainer.Get - saves the state of a ACLContainer to the db
  51. func (aclContainer ACLContainer) Get(containerID ContainerID) (ACLContainer, error) {
  52. return fetchACLContainer(containerID)
  53. }
  54. // == private ==
  55. // fetchACLContainer - fetches all current node rules in given network ACL
  56. func fetchACLContainer(containerID ContainerID) (ACLContainer, error) {
  57. aclJson, err := fetchACLContainerJson(ContainerID(containerID))
  58. if err != nil {
  59. return nil, err
  60. }
  61. var currentNetworkACL ACLContainer
  62. if err := json.Unmarshal([]byte(aclJson), &currentNetworkACL); err != nil {
  63. return nil, err
  64. }
  65. return currentNetworkACL, nil
  66. }
  67. // fetchACLContainerJson - fetch the current ACL of given network except in json string
  68. func fetchACLContainerJson(containerID ContainerID) (ACLJson, error) {
  69. currentACLs, err := database.FetchRecord(database.NODE_ACLS_TABLE_NAME, string(containerID))
  70. if err != nil {
  71. return ACLJson(""), err
  72. }
  73. return ACLJson(currentACLs), nil
  74. }
  75. // upsertACL - applies a ACL to the db, overwrites or creates
  76. func upsertACL(containerID ContainerID, ID AclID, acl ACL) (ACL, error) {
  77. currentNetACL, err := fetchACLContainer(containerID)
  78. if err != nil {
  79. return acl, err
  80. }
  81. currentNetACL[ID] = acl
  82. _, err = upsertACLContainer(containerID, currentNetACL)
  83. return acl, err
  84. }
  85. // upsertACLContainer - Inserts or updates a network ACL given the json string of the ACL and the network name
  86. // if nil, create it
  87. func upsertACLContainer(containerID ContainerID, aclContainer ACLContainer) (ACLContainer, error) {
  88. if aclContainer == nil {
  89. aclContainer = make(ACLContainer)
  90. }
  91. return aclContainer, database.Insert(string(containerID), string(convertNetworkACLtoACLJson(aclContainer)), database.NODE_ACLS_TABLE_NAME)
  92. }
  93. func convertNetworkACLtoACLJson(networkACL ACLContainer) ACLJson {
  94. data, err := json.Marshal(networkACL)
  95. if err != nil {
  96. return ""
  97. }
  98. return ACLJson(data)
  99. }