retrieve.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package nodeacls
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gravitl/netmaker/logic/acls"
  6. )
  7. // AreNodesAllowed - checks if nodes are allowed to communicate in their network ACL
  8. func AreNodesAllowed(networkID NetworkID, node1, node2 NodeID) bool {
  9. var currentNetworkACL, err = FetchAllACLs(networkID)
  10. if err != nil {
  11. return false
  12. }
  13. nodeID1 := acls.AclID(node1)
  14. nodeID2 := acls.AclID(node2)
  15. return currentNetworkACL[nodeID1].IsAllowed(nodeID2) && currentNetworkACL[nodeID2].IsAllowed(nodeID1)
  16. }
  17. // FetchNodeACL - fetches a specific node's ACL in a given network
  18. func FetchNodeACL(networkID NetworkID, nodeID NodeID) (acls.ACL, error) {
  19. var currentNetworkACL, err = FetchAllACLs(networkID)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if currentNetworkACL[acls.AclID(nodeID)] == nil {
  24. return nil, fmt.Errorf("no node ACL present for node %s", nodeID)
  25. }
  26. return currentNetworkACL[acls.AclID(nodeID)], nil
  27. }
  28. // FetchNodeACLJson - fetches a node's acl in given network except returns the json string
  29. func FetchNodeACLJson(networkID NetworkID, nodeID NodeID) (acls.ACLJson, error) {
  30. currentNodeACL, err := FetchNodeACL(networkID, nodeID)
  31. if err != nil {
  32. return "", err
  33. }
  34. jsonData, err := json.Marshal(&currentNodeACL)
  35. if err != nil {
  36. return "", err
  37. }
  38. return acls.ACLJson(jsonData), nil
  39. }
  40. // FetchAllACLs - fetchs all node
  41. func FetchAllACLs(networkID NetworkID) (acls.ACLContainer, error) {
  42. var err error
  43. var currentNetworkACL acls.ACLContainer
  44. currentNetworkACL, err = currentNetworkACL.Get(acls.ContainerID(networkID))
  45. if err != nil {
  46. return nil, err
  47. }
  48. return currentNetworkACL, nil
  49. }