common.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package acls
  2. import (
  3. "encoding/json"
  4. "github.com/gravitl/netmaker/database"
  5. )
  6. // CreateACLContainer - creates an empty ACL list in a given network
  7. func CreateACLContainer(networkID ContainerID) (ACLContainer, error) {
  8. var aclContainer = make(ACLContainer)
  9. return aclContainer, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(aclContainer)), database.NODE_ACLS_TABLE_NAME)
  10. }
  11. // FetchACLContainer - fetches all current node rules in given network ACL
  12. func FetchACLContainer(networkID ContainerID) (ACLContainer, error) {
  13. aclJson, err := FetchACLContainerJson(ContainerID(networkID))
  14. if err != nil {
  15. return nil, err
  16. }
  17. var currentNetworkACL ACLContainer
  18. if err := json.Unmarshal([]byte(aclJson), &currentNetworkACL); err != nil {
  19. return nil, err
  20. }
  21. return currentNetworkACL, nil
  22. }
  23. // FetchACLContainerJson - fetch the current ACL of given network except in json string
  24. func FetchACLContainerJson(networkID ContainerID) (ACLJson, error) {
  25. currentACLs, err := database.FetchRecord(database.NODE_ACLS_TABLE_NAME, string(networkID))
  26. if err != nil {
  27. return ACLJson(""), err
  28. }
  29. return ACLJson(currentACLs), nil
  30. }
  31. // == type functions ==
  32. // ACL.AllowNode - allows a node by ID in memory
  33. func (acl ACL) Allow(ID AclID) {
  34. acl[ID] = Allowed
  35. }
  36. // ACL.DisallowNode - disallows a node access by ID in memory
  37. func (acl ACL) Disallow(ID AclID) {
  38. acl[ID] = NotAllowed
  39. }
  40. // ACL.Remove - removes a node from a ACL
  41. func (acl ACL) Remove(ID AclID) {
  42. delete(acl, ID)
  43. }
  44. // ACL.Update - updates a ACL in DB
  45. func (acl ACL) Save(networkID ContainerID, ID AclID) (ACL, error) {
  46. return upsertACL(networkID, ID, acl)
  47. }
  48. // ACL.IsNodeAllowed - sees if ID is allowed in referring ACL
  49. func (acl ACL) IsNodeAllowed(ID AclID) bool {
  50. return acl[ID] == Allowed
  51. }
  52. // ACLContainer.UpdateNodeACL - saves the state of a ACL in the ACLContainer in memory
  53. func (aclContainer ACLContainer) UpdateNodeACL(ID AclID, acl ACL) ACLContainer {
  54. aclContainer[ID] = acl
  55. return aclContainer
  56. }
  57. // ACLContainer.RemoveNodeACL - removes the state of a ACL in the ACLContainer in memory
  58. func (aclContainer ACLContainer) RemoveNodeACL(ID AclID) ACLContainer {
  59. delete(aclContainer, ID)
  60. return aclContainer
  61. }
  62. // ACLContainer.ChangeNodesAccess - changes the relationship between two nodes in memory
  63. func (networkACL ACLContainer) ChangeNodesAccess(ID1, ID2 AclID, value byte) {
  64. networkACL[ID1][ID2] = value
  65. networkACL[ID2][ID1] = value
  66. }
  67. // ACLContainer.Save - saves the state of a ACLContainer to the db
  68. func (aclContainer ACLContainer) Save(networkID ContainerID) (ACLContainer, error) {
  69. return upsertACLContainer(networkID, aclContainer)
  70. }
  71. // == private ==
  72. // upsertACL - applies a ACL to the db, overwrites or creates
  73. func upsertACL(networkID ContainerID, ID AclID, acl ACL) (ACL, error) {
  74. currentNetACL, err := FetchACLContainer(networkID)
  75. if err != nil {
  76. return acl, err
  77. }
  78. currentNetACL[ID] = acl
  79. _, err = upsertACLContainer(networkID, currentNetACL)
  80. return acl, err
  81. }
  82. // upsertACLContainer - Inserts or updates a network ACL given the json string of the ACL and the network name
  83. // if nil, create it
  84. func upsertACLContainer(networkID ContainerID, aclContainer ACLContainer) (ACLContainer, error) {
  85. if aclContainer == nil {
  86. aclContainer = make(ACLContainer)
  87. }
  88. return aclContainer, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(aclContainer)), database.NODE_ACLS_TABLE_NAME)
  89. }
  90. func convertNetworkACLtoACLJson(networkACL ACLContainer) ACLJson {
  91. data, err := json.Marshal(networkACL)
  92. if err != nil {
  93. return ""
  94. }
  95. return ACLJson(data)
  96. }