retrieve.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. return currentNetworkACL[acls.AclID(node1)].IsAllowed(acls.AclID(node2)) && currentNetworkACL[acls.AclID(node2)].IsAllowed(acls.AclID(node1))
  14. }
  15. // FetchNodeACL - fetches a specific node's ACL in a given network
  16. func FetchNodeACL(networkID NetworkID, nodeID NodeID) (acls.ACL, error) {
  17. var currentNetworkACL, err = FetchAllACLs(networkID)
  18. if err != nil {
  19. return nil, err
  20. }
  21. if currentNetworkACL[acls.AclID(nodeID)] == nil {
  22. return nil, fmt.Errorf("no node ACL present for node %s", nodeID)
  23. }
  24. return currentNetworkACL[acls.AclID(nodeID)], nil
  25. }
  26. // FetchNodeACLJson - fetches a node's acl in given network except returns the json string
  27. func FetchNodeACLJson(networkID NetworkID, nodeID NodeID) (acls.ACLJson, error) {
  28. currentNodeACL, err := FetchNodeACL(networkID, nodeID)
  29. if err != nil {
  30. return "", err
  31. }
  32. jsonData, err := json.Marshal(&currentNodeACL)
  33. if err != nil {
  34. return "", err
  35. }
  36. return acls.ACLJson(jsonData), nil
  37. }
  38. // FetchAllACLs - fetchs all node
  39. func FetchAllACLs(networkID NetworkID) (acls.ACLContainer, error) {
  40. var err error
  41. var currentNetworkACL acls.ACLContainer
  42. currentNetworkACL, err = currentNetworkACL.Get(acls.ContainerID(networkID))
  43. if err != nil {
  44. return nil, err
  45. }
  46. return currentNetworkACL, nil
  47. }