netmaker/logic/acls/nodeacls/retrieve.go

54 lines
1.6 KiB
Go
Raw Normal View History

2022-02-24 08:36:48 +08:00
package nodeacls
import (
"encoding/json"
2022-02-25 02:48:35 +08:00
"fmt"
2022-02-24 08:36:48 +08:00
2022-02-26 00:06:03 +08:00
"github.com/gravitl/netmaker/logic/acls"
2022-02-24 08:36:48 +08:00
)
// AreNodesAllowed - checks if nodes are allowed to communicate in their network ACL
func AreNodesAllowed(networkID NetworkID, node1, node2 NodeID) bool {
2022-02-26 03:08:44 +08:00
var currentNetworkACL, err = FetchAllACLs(networkID)
2022-02-24 08:36:48 +08:00
if err != nil {
return false
}
2022-02-26 03:08:44 +08:00
return currentNetworkACL[acls.AclID(node1)].IsAllowed(acls.AclID(node2)) && currentNetworkACL[acls.AclID(node2)].IsAllowed(acls.AclID(node1))
2022-02-24 08:36:48 +08:00
}
// FetchNodeACL - fetches a specific node's ACL in a given network
2022-02-26 00:06:03 +08:00
func FetchNodeACL(networkID NetworkID, nodeID NodeID) (acls.ACL, error) {
2022-02-26 03:08:44 +08:00
var currentNetworkACL, err = FetchAllACLs(networkID)
2022-02-24 08:36:48 +08:00
if err != nil {
return nil, err
}
2022-02-26 03:08:44 +08:00
if currentNetworkACL[acls.AclID(nodeID)] == nil {
2022-02-25 02:48:35 +08:00
return nil, fmt.Errorf("no node ACL present for node %s", nodeID)
}
2022-02-26 03:08:44 +08:00
return currentNetworkACL[acls.AclID(nodeID)], nil
2022-02-24 08:36:48 +08:00
}
// FetchNodeACLJson - fetches a node's acl in given network except returns the json string
2022-02-26 00:06:03 +08:00
func FetchNodeACLJson(networkID NetworkID, nodeID NodeID) (acls.ACLJson, error) {
2022-02-24 08:36:48 +08:00
currentNodeACL, err := FetchNodeACL(networkID, nodeID)
if err != nil {
return "", err
}
jsonData, err := json.Marshal(&currentNodeACL)
if err != nil {
return "", err
}
2022-02-26 00:06:03 +08:00
return acls.ACLJson(jsonData), nil
2022-02-24 08:36:48 +08:00
}
2022-02-26 03:08:44 +08:00
// FetchAllACLs - fetchs all node
func FetchAllACLs(networkID NetworkID) (acls.ACLContainer, error) {
var err error
var currentNetworkACL acls.ACLContainer
currentNetworkACL, err = currentNetworkACL.Get(acls.ContainerID(networkID))
if err != nil {
return nil, err
}
return currentNetworkACL, nil
}