modify.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package nodeacls
  2. import (
  3. "encoding/json"
  4. "github.com/gravitl/netmaker/database"
  5. )
  6. // CreateNodeACL - inserts or updates a node ACL on given network
  7. func CreateNodeACL(networkID NetworkID, nodeID NodeID, defaultVal byte) (NodeACL, error) {
  8. if defaultVal != NotAllowed && defaultVal != Allowed {
  9. defaultVal = NotAllowed
  10. }
  11. var currentNetworkACL, err = FetchCurrentACL(networkID)
  12. if err != nil {
  13. return nil, err
  14. }
  15. var newNodeACL = make(NodeACL)
  16. for existingNodeID := range currentNetworkACL {
  17. currentNetworkACL[existingNodeID][nodeID] = defaultVal // set the old nodes to default value for new node
  18. newNodeACL[existingNodeID] = defaultVal // set the old nodes in new node ACL to default value
  19. }
  20. currentNetworkACL[nodeID] = newNodeACL // append the new node's ACL
  21. retNetworkACL, err := upsertNetworkACL(networkID, currentNetworkACL) // insert into db, return result
  22. if err != nil {
  23. return nil, err
  24. }
  25. return retNetworkACL[nodeID], nil
  26. }
  27. // CreateNetworkACL - creates an empty ACL list in a given network
  28. func CreateNetworkACL(networkID NetworkID) (NetworkACL, error) {
  29. var networkACL = make(NetworkACL)
  30. return networkACL, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(&networkACL)), database.NODE_ACLS_TABLE_NAME)
  31. }
  32. // RemoveNodeACL - removes a specific Node's ACL, returns the NetworkACL and error
  33. func RemoveNodeACL(networkID NetworkID, nodeID NodeID) (NetworkACL, error) {
  34. var currentNeworkACL, err = FetchCurrentACL(networkID)
  35. if err != nil {
  36. return nil, err
  37. }
  38. for currentNodeID := range currentNeworkACL {
  39. if currentNodeID != nodeID {
  40. currentNeworkACL[currentNodeID].RemoveNode(nodeID)
  41. }
  42. }
  43. delete(currentNeworkACL, nodeID)
  44. return currentNeworkACL.Save(networkID)
  45. }
  46. // RemoveNetworkACL - just delete the network ACL
  47. func RemoveNetworkACL(networkID NetworkID) error {
  48. return database.DeleteRecord(database.NODE_ACLS_TABLE_NAME, string(networkID))
  49. }
  50. // NodeACL.AllowNode - allows a node by ID in memory
  51. func (nodeACL NodeACL) AllowNode(nodeID NodeID) {
  52. nodeACL[nodeID] = Allowed
  53. }
  54. // NodeACL.DisallowNode - disallows a node access by ID in memory
  55. func (nodeACL NodeACL) DisallowNode(nodeID NodeID) {
  56. nodeACL[nodeID] = NotAllowed
  57. }
  58. // NodeACL.RemoveNode - removes a node from a NodeACL
  59. func (nodeACL NodeACL) RemoveNode(nodeID NodeID) {
  60. delete(nodeACL, nodeID)
  61. }
  62. // NodeACL.Update - updates a nodeACL in DB
  63. func (nodeACL NodeACL) Save(networkID NetworkID, nodeID NodeID) (NodeACL, error) {
  64. return upsertNodeACL(networkID, nodeID, nodeACL)
  65. }
  66. // NodeACL.IsNodeAllowed - sees if nodeID is allowed in referring NodeACL
  67. func (nodeACL NodeACL) IsNodeAllowed(nodeID NodeID) bool {
  68. return nodeACL[nodeID] == Allowed
  69. }
  70. // NetworkACL.UpdateNodeACL - saves the state of a NodeACL in the NetworkACL in memory
  71. func (networkACL NetworkACL) UpdateNodeACL(nodeID NodeID, nodeACL NodeACL) NetworkACL {
  72. networkACL[nodeID] = nodeACL
  73. return networkACL
  74. }
  75. // NetworkACL.RemoveNodeACL - removes the state of a NodeACL in the NetworkACL in memory
  76. func (networkACL NetworkACL) RemoveNodeACL(nodeID NodeID) NetworkACL {
  77. delete(networkACL, nodeID)
  78. return networkACL
  79. }
  80. // NetworkACL.ChangeNodesAccess - changes the relationship between two nodes in memory
  81. func (networkACL NetworkACL) ChangeNodesAccess(nodeID1, nodeID2 NodeID, value byte) {
  82. networkACL[nodeID1][nodeID2] = value
  83. networkACL[nodeID2][nodeID1] = value
  84. }
  85. // NetworkACL.Save - saves the state of a NetworkACL to the db
  86. func (networkACL NetworkACL) Save(networkID NetworkID) (NetworkACL, error) {
  87. return upsertNetworkACL(networkID, networkACL)
  88. }
  89. // == private ==
  90. // upsertNodeACL - applies a NodeACL to the db, overwrites or creates
  91. func upsertNodeACL(networkID NetworkID, nodeID NodeID, nodeACL NodeACL) (NodeACL, error) {
  92. currentNetACL, err := FetchCurrentACL(networkID)
  93. if err != nil {
  94. return nodeACL, err
  95. }
  96. currentNetACL[nodeID] = nodeACL
  97. _, err = upsertNetworkACL(networkID, currentNetACL)
  98. return nodeACL, err
  99. }
  100. // upsertNetworkACL - Inserts or updates a network ACL given the json string of the ACL and the network name
  101. // if nil, create it
  102. func upsertNetworkACL(networkID NetworkID, networkACL NetworkACL) (NetworkACL, error) {
  103. if networkACL == nil {
  104. networkACL = make(NetworkACL)
  105. }
  106. return networkACL, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(&networkACL)), database.NODE_ACLS_TABLE_NAME)
  107. }
  108. func convertNetworkACLtoACLJson(networkACL *NetworkACL) ACLJson {
  109. data, err := json.Marshal(networkACL)
  110. if err != nil {
  111. return ""
  112. }
  113. return ACLJson(data)
  114. }