2021-05-10 23:44:19 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2023-01-21 06:38:32 +08:00
|
|
|
"net"
|
2021-09-10 01:59:43 +08:00
|
|
|
"testing"
|
|
|
|
|
2023-01-21 06:38:32 +08:00
|
|
|
"github.com/google/uuid"
|
2021-09-10 01:59:43 +08:00
|
|
|
"github.com/gravitl/netmaker/database"
|
2021-10-06 03:02:09 +08:00
|
|
|
"github.com/gravitl/netmaker/logic"
|
2022-02-26 00:06:03 +08:00
|
|
|
"github.com/gravitl/netmaker/logic/acls"
|
2022-03-11 21:46:01 +08:00
|
|
|
"github.com/gravitl/netmaker/logic/acls/nodeacls"
|
2021-05-10 23:44:19 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
2021-09-10 01:59:43 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-21 06:38:32 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2021-05-10 23:44:19 +08:00
|
|
|
)
|
|
|
|
|
2023-01-21 06:38:32 +08:00
|
|
|
var nonLinuxHost models.Host
|
|
|
|
var linuxHost models.Host
|
|
|
|
|
2021-09-10 01:59:43 +08:00
|
|
|
func TestCreateEgressGateway(t *testing.T) {
|
|
|
|
var gateway models.EgressGatewayRequest
|
|
|
|
gateway.Ranges = []string{"10.100.100.0/24"}
|
2022-01-26 22:04:30 +08:00
|
|
|
gateway.NetID = "skynet"
|
2021-09-10 01:59:43 +08:00
|
|
|
deleteAllNetworks()
|
|
|
|
createNet()
|
|
|
|
t.Run("NoNodes", func(t *testing.T) {
|
2021-12-08 01:46:55 +08:00
|
|
|
node, err := logic.CreateEgressGateway(gateway)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, models.Node{}, node)
|
2022-01-11 08:43:20 +08:00
|
|
|
assert.EqualError(t, err, "could not find any records")
|
2021-09-10 01:59:43 +08:00
|
|
|
})
|
2022-01-26 22:04:30 +08:00
|
|
|
t.Run("Non-linux node", func(t *testing.T) {
|
2023-01-21 06:38:32 +08:00
|
|
|
createnode := createNodeWithParams("", "")
|
|
|
|
createNodeHosts()
|
|
|
|
createnode.HostID = nonLinuxHost.ID
|
|
|
|
err := logic.AssociateNodeToHost(createnode, &nonLinuxHost)
|
2022-01-26 22:04:30 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
gateway.NodeID = createnode.ID.String()
|
2022-01-26 22:04:30 +08:00
|
|
|
node, err := logic.CreateEgressGateway(gateway)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, models.Node{}, node)
|
2022-05-12 00:15:54 +08:00
|
|
|
assert.EqualError(t, err, "windows is unsupported for egress gateways")
|
2022-01-26 22:04:30 +08:00
|
|
|
})
|
2022-07-20 19:01:28 +08:00
|
|
|
t.Run("Success-Nat-Enabled", func(t *testing.T) {
|
|
|
|
deleteAllNodes()
|
|
|
|
testnode := createTestNode()
|
2023-01-21 06:38:32 +08:00
|
|
|
gateway.NodeID = testnode.ID.String()
|
2022-07-20 19:01:28 +08:00
|
|
|
gateway.NatEnabled = "yes"
|
|
|
|
|
|
|
|
node, err := logic.CreateEgressGateway(gateway)
|
|
|
|
t.Log(node.EgressGatewayNatEnabled)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
t.Run("Success-Nat-Disabled", func(t *testing.T) {
|
|
|
|
deleteAllNodes()
|
|
|
|
testnode := createTestNode()
|
2023-01-21 06:38:32 +08:00
|
|
|
gateway.NodeID = testnode.ID.String()
|
2022-07-20 19:01:28 +08:00
|
|
|
gateway.NatEnabled = "no"
|
|
|
|
|
|
|
|
node, err := logic.CreateEgressGateway(gateway)
|
|
|
|
t.Log(node.EgressGatewayNatEnabled)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
2021-09-10 01:59:43 +08:00
|
|
|
t.Run("Success", func(t *testing.T) {
|
2022-07-20 19:01:28 +08:00
|
|
|
var gateway models.EgressGatewayRequest
|
|
|
|
gateway.Ranges = []string{"10.100.100.0/24"}
|
|
|
|
gateway.NetID = "skynet"
|
2022-02-06 04:33:57 +08:00
|
|
|
deleteAllNodes()
|
2021-09-10 01:59:43 +08:00
|
|
|
testnode := createTestNode()
|
2023-01-21 06:38:32 +08:00
|
|
|
gateway.NodeID = testnode.ID.String()
|
2021-09-10 01:59:43 +08:00
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
node, err := logic.CreateEgressGateway(gateway)
|
2022-02-06 04:33:57 +08:00
|
|
|
t.Log(node)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, true, node.IsEgressGateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Equal(t, gateway.Ranges, node.EgressGatewayRanges)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestDeleteEgressGateway(t *testing.T) {
|
|
|
|
var gateway models.EgressGatewayRequest
|
|
|
|
deleteAllNetworks()
|
|
|
|
createNet()
|
|
|
|
testnode := createTestNode()
|
|
|
|
gateway.Ranges = []string{"10.100.100.0/24"}
|
|
|
|
gateway.NetID = "skynet"
|
2023-01-21 06:38:32 +08:00
|
|
|
gateway.NodeID = testnode.ID.String()
|
2021-09-10 01:59:43 +08:00
|
|
|
t.Run("Success", func(t *testing.T) {
|
2021-12-08 01:46:55 +08:00
|
|
|
node, err := logic.CreateEgressGateway(gateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, true, node.IsEgressGateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Equal(t, []string{"10.100.100.0/24"}, node.EgressGatewayRanges)
|
2021-12-08 01:46:55 +08:00
|
|
|
node, err = logic.DeleteEgressGateway(gateway.NetID, gateway.NodeID)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, false, node.IsEgressGateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Equal(t, []string([]string{}), node.EgressGatewayRanges)
|
|
|
|
})
|
|
|
|
t.Run("NotGateway", func(t *testing.T) {
|
2021-12-08 01:46:55 +08:00
|
|
|
node, err := logic.DeleteEgressGateway(gateway.NetID, gateway.NodeID)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, false, node.IsEgressGateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Equal(t, []string([]string{}), node.EgressGatewayRanges)
|
|
|
|
})
|
|
|
|
t.Run("BadNode", func(t *testing.T) {
|
2021-12-08 01:46:55 +08:00
|
|
|
node, err := logic.DeleteEgressGateway(gateway.NetID, "01:02:03")
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.EqualError(t, err, "no result found")
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, models.Node{}, node)
|
2022-01-11 09:50:18 +08:00
|
|
|
deleteAllNodes()
|
2021-09-10 01:59:43 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNetworkNodes(t *testing.T) {
|
|
|
|
deleteAllNetworks()
|
|
|
|
createNet()
|
|
|
|
t.Run("BadNet", func(t *testing.T) {
|
2021-10-06 03:02:09 +08:00
|
|
|
node, err := logic.GetNetworkNodes("badnet")
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2023-03-09 06:50:21 +08:00
|
|
|
assert.Equal(t, []models.Node{}, node)
|
2021-09-10 01:59:43 +08:00
|
|
|
})
|
|
|
|
t.Run("NoNodes", func(t *testing.T) {
|
2021-10-06 03:02:09 +08:00
|
|
|
node, err := logic.GetNetworkNodes("skynet")
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2023-03-09 06:50:21 +08:00
|
|
|
assert.Equal(t, []models.Node{}, node)
|
2021-09-10 01:59:43 +08:00
|
|
|
})
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
|
createTestNode()
|
2021-10-06 03:02:09 +08:00
|
|
|
node, err := logic.GetNetworkNodes("skynet")
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
2022-12-20 04:15:35 +08:00
|
|
|
assert.NotEqual(t, []models.LegacyNode(nil), node)
|
2021-09-10 01:59:43 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateEgressGateway(t *testing.T) {
|
|
|
|
var gateway models.EgressGatewayRequest
|
|
|
|
t.Run("EmptyRange", func(t *testing.T) {
|
|
|
|
gateway.Ranges = []string{}
|
2021-12-08 01:46:55 +08:00
|
|
|
err := logic.ValidateEgressGateway(gateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.EqualError(t, err, "IP Ranges Cannot Be Empty")
|
|
|
|
})
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
|
gateway.Ranges = []string{"10.100.100.0/24"}
|
2021-12-08 01:46:55 +08:00
|
|
|
err := logic.ValidateEgressGateway(gateway)
|
2021-09-10 01:59:43 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:48:35 +08:00
|
|
|
func TestNodeACLs(t *testing.T) {
|
|
|
|
deleteAllNodes()
|
2023-01-21 06:38:32 +08:00
|
|
|
node1 := createNodeWithParams("", "10.0.0.50/32")
|
|
|
|
node2 := createNodeWithParams("", "10.0.0.100/32")
|
|
|
|
logic.AssociateNodeToHost(node1, &linuxHost)
|
|
|
|
logic.AssociateNodeToHost(node2, &linuxHost)
|
2022-02-25 02:48:35 +08:00
|
|
|
t.Run("acls not present", func(t *testing.T) {
|
2022-02-26 03:08:44 +08:00
|
|
|
currentACL, err := nodeacls.FetchAllACLs(nodeacls.NetworkID(node1.Network))
|
2022-02-25 02:48:35 +08:00
|
|
|
assert.Nil(t, err)
|
2022-02-26 05:28:58 +08:00
|
|
|
assert.NotNil(t, currentACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
node1ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID.String()))
|
2022-02-26 05:28:58 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, node1ACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.Allowed, node1ACL[acls.AclID(node2.ID.String())])
|
2022-02-25 02:48:35 +08:00
|
|
|
})
|
|
|
|
t.Run("node acls exists after creates", func(t *testing.T) {
|
2023-01-21 06:38:32 +08:00
|
|
|
node1ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID.String()))
|
2022-02-25 02:48:35 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, node1ACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
node2ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node2.Network), nodeacls.NodeID(node2.ID.String()))
|
2022-02-25 02:48:35 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, node2ACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.Allowed, node2ACL[acls.AclID(node1.ID.String())])
|
2022-02-25 02:48:35 +08:00
|
|
|
})
|
|
|
|
t.Run("node acls correct after fetch", func(t *testing.T) {
|
2023-01-21 06:38:32 +08:00
|
|
|
node1ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID.String()))
|
2022-02-25 02:48:35 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.Allowed, node1ACL[acls.AclID(node2.ID.String())])
|
2022-02-25 02:48:35 +08:00
|
|
|
})
|
|
|
|
t.Run("node acls correct after modify", func(t *testing.T) {
|
2023-01-21 06:38:32 +08:00
|
|
|
node1ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID.String()))
|
2022-02-25 04:21:20 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, node1ACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
node2ACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(node2.Network), nodeacls.NodeID(node2.ID.String()))
|
2022-02-26 05:28:58 +08:00
|
|
|
assert.Nil(t, err)
|
2022-02-25 04:21:20 +08:00
|
|
|
assert.NotNil(t, node2ACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
currentACL, err := nodeacls.DisallowNodes(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID.String()), nodeacls.NodeID(node2.ID.String()))
|
2022-02-25 04:21:20 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node1.ID.String())][acls.AclID(node2.ID.String())])
|
|
|
|
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node2.ID.String())][acls.AclID(node1.ID.String())])
|
2022-02-26 04:22:46 +08:00
|
|
|
currentACL.Save(acls.ContainerID(node1.Network))
|
|
|
|
})
|
|
|
|
t.Run("node acls correct after add new node not allowed", func(t *testing.T) {
|
2023-01-21 06:38:32 +08:00
|
|
|
node3 := createNodeWithParams("", "10.0.0.100/32")
|
|
|
|
createNodeHosts()
|
|
|
|
n, e := logic.GetNetwork(node3.Network)
|
|
|
|
assert.Nil(t, e)
|
|
|
|
n.DefaultACL = "no"
|
|
|
|
e = logic.SaveNetwork(&n)
|
|
|
|
assert.Nil(t, e)
|
|
|
|
err := logic.AssociateNodeToHost(node3, &linuxHost)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
currentACL, err := nodeacls.FetchAllACLs(nodeacls.NetworkID(node3.Network))
|
2022-02-26 04:22:46 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, currentACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node1.ID.String())][acls.AclID(node3.ID.String())])
|
|
|
|
nodeACL, err := nodeacls.CreateNodeACL(nodeacls.NetworkID(node3.Network), nodeacls.NodeID(node3.ID.String()), acls.NotAllowed)
|
2022-02-26 04:22:46 +08:00
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
nodeACL.Save(acls.ContainerID(node3.Network), acls.AclID(node3.ID.String()))
|
2022-02-26 04:22:46 +08:00
|
|
|
currentACL, err = nodeacls.FetchAllACLs(nodeacls.NetworkID(node3.Network))
|
|
|
|
assert.Nil(t, err)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node1.ID.String())][acls.AclID(node3.ID.String())])
|
|
|
|
assert.Equal(t, acls.NotAllowed, currentACL[acls.AclID(node2.ID.String())][acls.AclID(node3.ID.String())])
|
2022-02-25 02:48:35 +08:00
|
|
|
})
|
|
|
|
t.Run("node acls removed", func(t *testing.T) {
|
2023-01-21 06:38:32 +08:00
|
|
|
retNetworkACL, err := nodeacls.RemoveNodeACL(nodeacls.NetworkID(node1.Network), nodeacls.NodeID(node1.ID.String()))
|
2022-02-25 02:48:35 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, retNetworkACL)
|
2023-01-21 06:38:32 +08:00
|
|
|
assert.Equal(t, acls.NotPresent, retNetworkACL[acls.AclID(node2.ID.String())][acls.AclID(node1.ID.String())])
|
2022-02-25 02:48:35 +08:00
|
|
|
})
|
|
|
|
deleteAllNodes()
|
|
|
|
}
|
|
|
|
|
2021-09-07 03:30:39 +08:00
|
|
|
func deleteAllNodes() {
|
2022-01-11 21:40:51 +08:00
|
|
|
database.DeleteAllRecords(database.NODES_TABLE_NAME)
|
2021-09-07 03:30:39 +08:00
|
|
|
}
|
2021-12-08 04:51:57 +08:00
|
|
|
|
2023-01-21 06:38:32 +08:00
|
|
|
func createTestNode() *models.Node {
|
|
|
|
createNodeHosts()
|
|
|
|
n := createNodeWithParams("skynet", "")
|
|
|
|
_ = logic.AssociateNodeToHost(n, &linuxHost)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNodeWithParams(network, address string) *models.Node {
|
|
|
|
_, ipnet, _ := net.ParseCIDR("10.0.0.1/32")
|
|
|
|
tmpCNode := models.CommonNode{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Network: "skynet",
|
|
|
|
Address: *ipnet,
|
|
|
|
DNSOn: true,
|
|
|
|
}
|
|
|
|
if len(network) > 0 {
|
|
|
|
tmpCNode.Network = network
|
|
|
|
}
|
|
|
|
if len(address) > 0 {
|
|
|
|
_, ipnet2, _ := net.ParseCIDR(address)
|
|
|
|
tmpCNode.Address = *ipnet2
|
|
|
|
}
|
|
|
|
createnode := models.Node{
|
|
|
|
CommonNode: tmpCNode,
|
|
|
|
}
|
2021-12-11 10:09:42 +08:00
|
|
|
return &createnode
|
2021-12-08 04:51:57 +08:00
|
|
|
}
|
2023-01-21 06:38:32 +08:00
|
|
|
|
|
|
|
func createNodeHosts() {
|
|
|
|
k, _ := wgtypes.ParseKey("DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34=")
|
|
|
|
linuxHost = models.Host{
|
|
|
|
ID: uuid.New(),
|
|
|
|
PublicKey: k.PublicKey(),
|
|
|
|
HostPass: "password",
|
|
|
|
OS: "linux",
|
|
|
|
Name: "linuxhost",
|
|
|
|
}
|
|
|
|
_ = logic.CreateHost(&linuxHost)
|
|
|
|
nonLinuxHost = models.Host{
|
|
|
|
ID: uuid.New(),
|
|
|
|
OS: "windows",
|
|
|
|
PublicKey: k.PublicKey(),
|
|
|
|
Name: "windowshost",
|
|
|
|
HostPass: "password",
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = logic.CreateHost(&nonLinuxHost)
|
|
|
|
}
|