2021-05-11 20:55:36 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-02-03 19:53:11 +01:00
|
|
|
"fmt"
|
2022-02-21 09:57:31 +01:00
|
|
|
"reflect"
|
2021-10-04 17:40:21 +00:00
|
|
|
"strconv"
|
2022-02-21 09:57:31 +01:00
|
|
|
"testing"
|
2021-11-21 13:40:19 +00:00
|
|
|
"time"
|
2021-07-25 17:59:48 +02:00
|
|
|
|
2021-05-11 20:55:36 -04:00
|
|
|
"gopkg.in/check.v1"
|
2022-01-16 14:16:59 +01:00
|
|
|
"inet.af/netaddr"
|
2022-02-21 09:57:31 +01:00
|
|
|
"tailscale.com/tailcfg"
|
2021-05-11 20:55:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachine(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := &Machine{
|
2021-05-11 20:55:36 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine",
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-11 20:55:36 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(machine)
|
2021-05-11 20:55:36 -04:00
|
|
|
|
2022-03-01 16:34:35 +00:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-17 00:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachineByID(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-05-11 20:55:36 -04:00
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := Machine{
|
2021-07-17 00:14:22 +02:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine",
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 00:14:22 +02:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(&machine)
|
2021-07-17 00:14:22 +02:00
|
|
|
|
2022-03-01 16:34:35 +00:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestDeleteMachine(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := Machine{
|
2021-07-17 00:14:22 +02:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine",
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 00:14:22 +02:00
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
err = app.DeleteMachine(&machine)
|
2021-07-17 00:29:14 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
|
|
|
|
_, err = app.GetMachine(namespace.Name, "testmachine")
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestHardDeleteMachine(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := Machine{
|
2021-07-17 00:14:22 +02:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine3",
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 00:14:22 +02:00
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
err = app.HardDeleteMachine(&machine)
|
2021-07-17 00:29:14 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
|
|
|
|
_, err = app.GetMachine(namespace.Name, "testmachine3")
|
2021-07-17 00:14:22 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
2021-05-11 20:55:36 -04:00
|
|
|
}
|
2021-10-04 17:40:21 +00:00
|
|
|
|
2022-02-25 10:26:34 +01:00
|
|
|
func (s *Suite) TestListPeers(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-10-04 17:40:21 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-10-04 17:40:21 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-10-04 17:40:21 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
for index := 0; index <= 10; index++ {
|
|
|
|
machine := Machine{
|
|
|
|
ID: uint64(index),
|
|
|
|
MachineKey: "foo" + strconv.Itoa(index),
|
|
|
|
NodeKey: "bar" + strconv.Itoa(index),
|
|
|
|
DiscoKey: "faa" + strconv.Itoa(index),
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine" + strconv.Itoa(index),
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-10-04 17:40:21 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(&machine)
|
2021-10-04 17:40:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine0ByID, err := app.GetMachineByID(0)
|
2021-10-04 17:40:21 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-25 10:26:34 +01:00
|
|
|
peersOfMachine0, err := app.ListPeers(machine0ByID)
|
2021-10-04 17:40:21 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(peersOfMachine0), check.Equals, 9)
|
2022-04-24 20:54:38 +01:00
|
|
|
c.Assert(peersOfMachine0[0].Hostname, check.Equals, "testmachine2")
|
|
|
|
c.Assert(peersOfMachine0[5].Hostname, check.Equals, "testmachine7")
|
|
|
|
c.Assert(peersOfMachine0[8].Hostname, check.Equals, "testmachine10")
|
2021-10-04 17:40:21 +00:00
|
|
|
}
|
2021-11-21 13:40:19 +00:00
|
|
|
|
2022-02-03 19:53:11 +01:00
|
|
|
func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
|
|
|
|
type base struct {
|
|
|
|
namespace *Namespace
|
|
|
|
key *PreAuthKey
|
|
|
|
}
|
|
|
|
|
2022-02-14 15:26:54 +01:00
|
|
|
stor := make([]base, 0)
|
2022-02-03 19:53:11 +01:00
|
|
|
|
|
|
|
for _, name := range []string{"test", "admin"} {
|
|
|
|
namespace, err := app.CreateNamespace(name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
stor = append(stor, base{namespace, pak})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := app.GetMachineByID(0)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
for index := 0; index <= 10; index++ {
|
|
|
|
machine := Machine{
|
2022-02-14 15:54:51 +01:00
|
|
|
ID: uint64(index),
|
|
|
|
MachineKey: "foo" + strconv.Itoa(index),
|
|
|
|
NodeKey: "bar" + strconv.Itoa(index),
|
|
|
|
DiscoKey: "faa" + strconv.Itoa(index),
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP(fmt.Sprintf("100.64.0.%v", strconv.Itoa(index+1))),
|
|
|
|
},
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine" + strconv.Itoa(index),
|
2022-02-03 19:53:11 +01:00
|
|
|
NamespaceID: stor[index%2].namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(stor[index%2].key.ID),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.aclPolicy = &ACLPolicy{
|
|
|
|
Groups: map[string][]string{
|
|
|
|
"group:test": {"admin"},
|
|
|
|
},
|
|
|
|
Hosts: map[string]netaddr.IPPrefix{},
|
|
|
|
TagOwners: map[string][]string{},
|
|
|
|
ACLs: []ACL{
|
|
|
|
{Action: "accept", Users: []string{"admin"}, Ports: []string{"*:*"}},
|
|
|
|
{Action: "accept", Users: []string{"test"}, Ports: []string{"test:*"}},
|
|
|
|
},
|
|
|
|
Tests: []ACLTest{},
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:00:41 +01:00
|
|
|
err = app.UpdateACLRules()
|
2022-02-03 19:53:11 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
adminMachine, err := app.GetMachineByID(1)
|
2022-04-24 20:54:38 +01:00
|
|
|
c.Logf("Machine(%v), namespace: %v", adminMachine.Hostname, adminMachine.Namespace)
|
2022-02-03 19:53:11 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
testMachine, err := app.GetMachineByID(2)
|
2022-04-24 20:54:38 +01:00
|
|
|
c.Logf("Machine(%v), namespace: %v", testMachine.Hostname, testMachine.Namespace)
|
2022-02-03 19:53:11 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-25 10:26:34 +01:00
|
|
|
machines, err := app.ListMachines()
|
2022-02-03 19:53:11 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-21 10:02:59 +01:00
|
|
|
peersOfTestMachine := getFilteredByACLPeers(machines, app.aclRules, testMachine)
|
|
|
|
peersOfAdminMachine := getFilteredByACLPeers(machines, app.aclRules, adminMachine)
|
2022-02-03 19:53:11 +01:00
|
|
|
|
|
|
|
c.Log(peersOfTestMachine)
|
|
|
|
c.Assert(len(peersOfTestMachine), check.Equals, 4)
|
2022-04-24 20:54:38 +01:00
|
|
|
c.Assert(peersOfTestMachine[0].Hostname, check.Equals, "testmachine4")
|
|
|
|
c.Assert(peersOfTestMachine[1].Hostname, check.Equals, "testmachine6")
|
|
|
|
c.Assert(peersOfTestMachine[3].Hostname, check.Equals, "testmachine10")
|
2022-02-03 19:53:11 +01:00
|
|
|
|
|
|
|
c.Log(peersOfAdminMachine)
|
|
|
|
c.Assert(len(peersOfAdminMachine), check.Equals, 9)
|
2022-04-24 20:54:38 +01:00
|
|
|
c.Assert(peersOfAdminMachine[0].Hostname, check.Equals, "testmachine2")
|
|
|
|
c.Assert(peersOfAdminMachine[2].Hostname, check.Equals, "testmachine4")
|
|
|
|
c.Assert(peersOfAdminMachine[5].Hostname, check.Equals, "testmachine7")
|
2022-02-03 19:53:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-21 13:40:19 +00:00
|
|
|
func (s *Suite) TestExpireMachine(c *check.C) {
|
|
|
|
namespace, err := app.CreateNamespace("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
machine := &Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 20:54:38 +01:00
|
|
|
Hostname: "testmachine",
|
2021-11-21 13:40:19 +00:00
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
Expiry: &time.Time{},
|
|
|
|
}
|
|
|
|
app.db.Save(machine)
|
|
|
|
|
|
|
|
machineFromDB, err := app.GetMachine("test", "testmachine")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(machineFromDB.isExpired(), check.Equals, false)
|
|
|
|
|
|
|
|
app.ExpireMachine(machineFromDB)
|
|
|
|
|
|
|
|
c.Assert(machineFromDB.isExpired(), check.Equals, true)
|
|
|
|
}
|
2022-01-16 14:16:59 +01:00
|
|
|
|
|
|
|
func (s *Suite) TestSerdeAddressStrignSlice(c *check.C) {
|
|
|
|
input := MachineAddresses([]netaddr.IP{
|
|
|
|
netaddr.MustParseIP("192.0.2.1"),
|
|
|
|
netaddr.MustParseIP("2001:db8::1"),
|
|
|
|
})
|
|
|
|
serialized, err := input.Value()
|
|
|
|
c.Assert(err, check.IsNil)
|
2022-01-30 08:35:10 +00:00
|
|
|
if serial, ok := serialized.(string); ok {
|
|
|
|
c.Assert(serial, check.Equals, "192.0.2.1,2001:db8::1")
|
|
|
|
}
|
2022-01-16 14:16:59 +01:00
|
|
|
|
|
|
|
var deserialized MachineAddresses
|
|
|
|
err = deserialized.Scan(serialized)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(len(deserialized), check.Equals, len(input))
|
|
|
|
for i := range deserialized {
|
|
|
|
c.Assert(deserialized[i], check.Equals, input[i])
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 09:57:31 +01:00
|
|
|
|
2022-03-01 22:43:25 +01:00
|
|
|
// nolint
|
2022-02-21 09:57:31 +01:00
|
|
|
func Test_getFilteredByACLPeers(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
machines []Machine
|
|
|
|
rules []tailcfg.FilterRule
|
|
|
|
machine *Machine
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2022-02-21 10:02:59 +01:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Machines
|
2022-02-21 09:57:31 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all hosts can talk to each other",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
2022-02-21 10:02:59 +01:00
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
2022-02-21 09:57:31 +01:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.2")},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.3")},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "One host can talk to another, but not all hosts",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
2022-02-21 10:02:59 +01:00
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
2022-02-21 09:57:31 +01:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.2")},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "host cannot directly talk to destination, but return path is authorized",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 16:06:20 +01:00
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-21 09:57:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
2022-02-21 10:02:59 +01:00
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.3"},
|
2022-02-21 09:57:31 +01:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
2022-03-01 22:43:25 +01:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.2")},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.3")},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rules allows all hosts to reach one destination",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"*"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rules allows all hosts to reach one destination, destination can reach all hosts",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"*"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rule allows all hosts to reach all destinations",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
{
|
|
|
|
SrcIPs: []string{"*"},
|
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 2,
|
2022-02-21 09:57:31 +01:00
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.2")},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
2022-03-01 22:43:25 +01:00
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
2022-02-21 09:57:31 +01:00
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.3")},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-01 22:43:25 +01:00
|
|
|
{
|
|
|
|
name: "without rule all communications are forbidden",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.1"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.2"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
|
|
|
netaddr.MustParseIP("100.64.0.3"),
|
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{netaddr.MustParseIP("100.64.0.2")},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{},
|
|
|
|
},
|
2022-02-21 09:57:31 +01:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-02-21 16:06:20 +01:00
|
|
|
got := getFilteredByACLPeers(
|
|
|
|
tt.args.machines,
|
|
|
|
tt.args.rules,
|
|
|
|
tt.args.machine,
|
|
|
|
)
|
2022-02-21 09:57:31 +01:00
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("getFilteredByACLPeers() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|