modify.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package nodeacls
  2. import (
  3. "encoding/json"
  4. "github.com/gravitl/netmaker/database"
  5. "github.com/gravitl/netmaker/logic/acls"
  6. )
  7. // ChangeNodeACL - takes in two node IDs of a given network and changes them to specified allowed or not value
  8. // returns the total network's ACL and error
  9. func ChangeNodeACL(networkID acls.NetworkID, node1, node2 acls.NodeID, value byte) (acls.NetworkACL, error) {
  10. if value != acls.NotAllowed && value != acls.Allowed { // if invalid option make not allowed
  11. value = acls.NotAllowed
  12. }
  13. currentACL, err := FetchCurrentACL(networkID)
  14. if err != nil {
  15. return nil, err
  16. }
  17. // == make the access control change ==
  18. currentACL[node1][node2] = value
  19. currentACL[node2][node1] = value
  20. return UpsertNetworkACL(networkID, currentACL)
  21. }
  22. // CreateNodeACL - inserts or updates a node ACL on given network
  23. func CreateNodeACL(networkID acls.NetworkID, nodeID acls.NodeID, defaultVal byte) (acls.NodeACL, error) {
  24. if defaultVal != acls.NotAllowed && defaultVal != acls.Allowed {
  25. defaultVal = acls.NotAllowed
  26. }
  27. var currentNetworkACL, err = FetchCurrentACL(networkID)
  28. if err != nil {
  29. return nil, err
  30. }
  31. var newNodeACL = make(acls.NodeACL)
  32. for existingNodeID := range currentNetworkACL {
  33. currentNetworkACL[existingNodeID][nodeID] = defaultVal // set the old nodes to default value for new node
  34. newNodeACL[existingNodeID] = defaultVal // set the old nodes in new node ACL to default value
  35. }
  36. currentNetworkACL[nodeID] = newNodeACL // append the new node's ACL
  37. retNetworkACL, err := UpsertNetworkACL(networkID, currentNetworkACL) // insert into db, return result
  38. if err != nil {
  39. return nil, err
  40. }
  41. return retNetworkACL[nodeID], nil
  42. }
  43. // CreateNetworkACL - creates an empty ACL list in a given network
  44. func CreateNetworkACL(networkID acls.NetworkID) (acls.NetworkACL, error) {
  45. var networkACL = make(acls.NetworkACL)
  46. return networkACL, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(&networkACL)), database.NODE_ACLS_TABLE_NAME)
  47. }
  48. // UpsertNodeACL - applies a NodeACL to the db, overwrites or creates
  49. func UpsertNodeACL(networkID acls.NetworkID, nodeID acls.NodeID, nodeACL acls.NodeACL) (acls.NodeACL, error) {
  50. currentNetACL, err := FetchCurrentACL(networkID)
  51. if err != nil {
  52. return nodeACL, err
  53. }
  54. currentNetACL[nodeID] = nodeACL
  55. _, err = UpsertNetworkACL(networkID, currentNetACL)
  56. return nodeACL, err
  57. }
  58. // UpsertNetworkACL - Inserts or updates a network ACL given the json string of the ACL and the network name
  59. // if nil, create it
  60. func UpsertNetworkACL(networkID acls.NetworkID, networkACL acls.NetworkACL) (acls.NetworkACL, error) {
  61. if networkACL == nil {
  62. networkACL = make(acls.NetworkACL)
  63. }
  64. return networkACL, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(&networkACL)), database.NODE_ACLS_TABLE_NAME)
  65. }
  66. // RemoveNodeACL - removes a specific Node's ACL, returns the NetworkACL and error
  67. func RemoveNodeACL(networkID acls.NetworkID, nodeID acls.NodeID) (acls.NetworkACL, error) {
  68. var currentNeworkACL, err = FetchCurrentACL(networkID)
  69. if err != nil {
  70. return nil, err
  71. }
  72. for currentNodeID := range currentNeworkACL {
  73. if currentNodeID != nodeID {
  74. delete(currentNeworkACL[currentNodeID], nodeID)
  75. }
  76. }
  77. delete(currentNeworkACL, nodeID)
  78. return UpsertNetworkACL(networkID, currentNeworkACL)
  79. }
  80. // RemoveNetworkACL - just delete the network ACL
  81. func RemoveNetworkACL(networkID acls.NetworkID) error {
  82. return database.DeleteRecord(database.NODE_ACLS_TABLE_NAME, string(networkID))
  83. }
  84. func convertNetworkACLtoACLJson(networkACL *acls.NetworkACL) acls.ACLJson {
  85. data, err := json.Marshal(networkACL)
  86. if err != nil {
  87. return ""
  88. }
  89. return acls.ACLJson(data)
  90. }