2021-05-12 08:55:36 +08:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-02-04 02:53:11 +08:00
|
|
|
"fmt"
|
2022-09-02 06:04:31 +08:00
|
|
|
"net/netip"
|
2022-02-21 16:57:31 +08:00
|
|
|
"reflect"
|
2021-10-05 01:40:21 +08:00
|
|
|
"strconv"
|
2022-05-17 02:30:43 +08:00
|
|
|
"strings"
|
2022-02-21 16:57:31 +08:00
|
|
|
"testing"
|
2021-11-21 21:40:19 +08:00
|
|
|
"time"
|
2021-07-25 23:59:48 +08:00
|
|
|
|
2021-05-12 08:55:36 +08:00
|
|
|
"gopkg.in/check.v1"
|
2022-02-21 16:57:31 +08:00
|
|
|
"tailscale.com/tailcfg"
|
2022-08-14 03:03:02 +08:00
|
|
|
"tailscale.com/types/key"
|
2021-05-12 08:55:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachine(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := &Machine{
|
2021-05-12 08:55:36 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-12 08:55:36 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(machine)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2022-03-02 00:34:35 +08:00
|
|
|
_, err = app.GetMachine("test", "testmachine")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-07-17 06:14:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachineByID(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-07-17 06:14:22 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 06:14:22 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-07-17 06:14:22 +08:00
|
|
|
|
2022-03-02 00:34:35 +08:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2022-08-14 03:03:02 +08:00
|
|
|
func (s *Suite) TestGetMachineByNodeKey(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.GetMachineByID(0)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
|
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: NodePublicKeyStripPrefix(nodeKey.Public()),
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
_, err = app.GetMachineByNodeKey(nodeKey.Public())
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMachineByAnyNodeKey(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.GetMachineByID(0)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
oldNodeKey := key.NewNode()
|
|
|
|
|
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: NodePublicKeyStripPrefix(nodeKey.Public()),
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
_, err = app.GetMachineByAnyNodeKey(nodeKey.Public(), oldNodeKey.Public())
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2021-07-17 06:14:22 +08:00
|
|
|
func (s *Suite) TestDeleteMachine(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-07-17 06:14:22 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 06:14:22 +08:00
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
err = app.DeleteMachine(&machine)
|
2021-07-17 06:29:14 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
|
|
|
|
_, err = app.GetMachine(namespace.Name, "testmachine")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestHardDeleteMachine(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-07-17 06:14:22 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine3",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-07-17 06:14:22 +08:00
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
err = app.HardDeleteMachine(&machine)
|
2021-07-17 06:29:14 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
|
|
|
|
_, err = app.GetMachine(namespace.Name, "testmachine3")
|
2021-07-17 06:14:22 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
2021-05-12 08:55:36 +08:00
|
|
|
}
|
2021-10-05 01:40:21 +08:00
|
|
|
|
2022-02-25 17:26:34 +08:00
|
|
|
func (s *Suite) TestListPeers(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachineByID(0)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08: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-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine" + strconv.Itoa(index),
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-10-05 01:40:21 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-10-05 01:40:21 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine0ByID, err := app.GetMachineByID(0)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-25 17:26:34 +08:00
|
|
|
peersOfMachine0, err := app.ListPeers(machine0ByID)
|
2021-10-05 01:40:21 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(peersOfMachine0), check.Equals, 9)
|
2022-04-25 03:54:38 +08: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-05 01:40:21 +08:00
|
|
|
}
|
2021-11-21 21:40:19 +08:00
|
|
|
|
2022-02-04 02:53:11 +08:00
|
|
|
func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
|
|
|
|
type base struct {
|
|
|
|
namespace *Namespace
|
|
|
|
key *PreAuthKey
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:26:54 +08:00
|
|
|
stor := make([]base, 0)
|
2022-02-04 02:53:11 +08: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 22:54:51 +08:00
|
|
|
ID: uint64(index),
|
|
|
|
MachineKey: "foo" + strconv.Itoa(index),
|
|
|
|
NodeKey: "bar" + strconv.Itoa(index),
|
|
|
|
DiscoKey: "faa" + strconv.Itoa(index),
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr(fmt.Sprintf("100.64.0.%v", strconv.Itoa(index+1))),
|
2022-02-14 22:54:51 +08:00
|
|
|
},
|
2022-04-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine" + strconv.Itoa(index),
|
2022-02-04 02:53:11 +08: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"},
|
|
|
|
},
|
2022-09-02 06:04:31 +08:00
|
|
|
Hosts: map[string]netip.Prefix{},
|
2022-02-04 02:53:11 +08:00
|
|
|
TagOwners: map[string][]string{},
|
|
|
|
ACLs: []ACL{
|
2022-08-04 16:47:00 +08:00
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"admin"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"test"},
|
|
|
|
Destinations: []string{"test:*"},
|
|
|
|
},
|
2022-02-04 02:53:11 +08:00
|
|
|
},
|
|
|
|
Tests: []ACLTest{},
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:00:41 +08:00
|
|
|
err = app.UpdateACLRules()
|
2022-02-04 02:53:11 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
adminMachine, err := app.GetMachineByID(1)
|
2022-04-25 03:54:38 +08:00
|
|
|
c.Logf("Machine(%v), namespace: %v", adminMachine.Hostname, adminMachine.Namespace)
|
2022-02-04 02:53:11 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
testMachine, err := app.GetMachineByID(2)
|
2022-04-25 03:54:38 +08:00
|
|
|
c.Logf("Machine(%v), namespace: %v", testMachine.Hostname, testMachine.Namespace)
|
2022-02-04 02:53:11 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-25 17:26:34 +08:00
|
|
|
machines, err := app.ListMachines()
|
2022-02-04 02:53:11 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-21 17:02:59 +08:00
|
|
|
peersOfTestMachine := getFilteredByACLPeers(machines, app.aclRules, testMachine)
|
|
|
|
peersOfAdminMachine := getFilteredByACLPeers(machines, app.aclRules, adminMachine)
|
2022-02-04 02:53:11 +08:00
|
|
|
|
|
|
|
c.Log(peersOfTestMachine)
|
|
|
|
c.Assert(len(peersOfTestMachine), check.Equals, 4)
|
2022-04-25 03:54:38 +08: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-04 02:53:11 +08:00
|
|
|
|
|
|
|
c.Log(peersOfAdminMachine)
|
|
|
|
c.Assert(len(peersOfAdminMachine), check.Equals, 9)
|
2022-04-25 03:54:38 +08: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-04 02:53:11 +08:00
|
|
|
}
|
|
|
|
|
2021-11-21 21:40:19 +08: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-25 03:54:38 +08:00
|
|
|
Hostname: "testmachine",
|
2021-11-21 21:40:19 +08: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)
|
2022-06-26 18:30:52 +08:00
|
|
|
c.Assert(machineFromDB, check.NotNil)
|
2021-11-21 21:40:19 +08:00
|
|
|
|
|
|
|
c.Assert(machineFromDB.isExpired(), check.Equals, false)
|
|
|
|
|
2022-06-26 18:30:52 +08:00
|
|
|
err = app.ExpireMachine(machineFromDB)
|
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-21 21:40:19 +08:00
|
|
|
|
|
|
|
c.Assert(machineFromDB.isExpired(), check.Equals, true)
|
|
|
|
}
|
2022-01-16 21:16:59 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestSerdeAddressStrignSlice(c *check.C) {
|
2022-09-02 06:04:31 +08:00
|
|
|
input := MachineAddresses([]netip.Addr{
|
|
|
|
netip.MustParseAddr("192.0.2.1"),
|
|
|
|
netip.MustParseAddr("2001:db8::1"),
|
2022-01-16 21:16:59 +08:00
|
|
|
})
|
|
|
|
serialized, err := input.Value()
|
|
|
|
c.Assert(err, check.IsNil)
|
2022-01-30 16:35:10 +08:00
|
|
|
if serial, ok := serialized.(string); ok {
|
|
|
|
c.Assert(serial, check.Equals, "192.0.2.1,2001:db8::1")
|
|
|
|
}
|
2022-01-16 21:16:59 +08: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 16:57:31 +08:00
|
|
|
|
2022-07-25 16:54:51 +08:00
|
|
|
func (s *Suite) TestSetTags(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",
|
|
|
|
Hostname: "testmachine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
|
|
|
app.db.Save(machine)
|
|
|
|
|
|
|
|
// assign simple tags
|
|
|
|
sTags := []string{"tag:test", "tag:foo"}
|
|
|
|
err = app.SetTags(machine, sTags)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
machine, err = app.GetMachine("test", "testmachine")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(machine.ForcedTags, check.DeepEquals, StringList(sTags))
|
|
|
|
|
|
|
|
// assign duplicat tags, expect no errors but no doubles in DB
|
|
|
|
eTags := []string{"tag:bar", "tag:test", "tag:unknown", "tag:test"}
|
|
|
|
err = app.SetTags(machine, eTags)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
machine, err = app.GetMachine("test", "testmachine")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(
|
|
|
|
machine.ForcedTags,
|
|
|
|
check.DeepEquals,
|
|
|
|
StringList([]string{"tag:bar", "tag:test", "tag:unknown"}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-16 19:15:04 +08:00
|
|
|
func Test_getTags(t *testing.T) {
|
|
|
|
type args struct {
|
2022-05-05 04:56:55 +08:00
|
|
|
aclPolicy *ACLPolicy
|
2022-04-16 19:15:04 +08:00
|
|
|
machine Machine
|
|
|
|
stripEmailDomain bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantInvalid []string
|
|
|
|
wantValid []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid tag one machine",
|
|
|
|
args: args{
|
2022-05-05 04:56:55 +08:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-04-16 19:15:04 +08:00
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: Machine{
|
|
|
|
Namespace: Namespace{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: HostInfo{
|
|
|
|
RequestTags: []string{"tag:valid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: []string{"tag:valid"},
|
|
|
|
wantInvalid: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid tag and valid tag one machine",
|
|
|
|
args: args{
|
2022-05-05 04:56:55 +08:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-04-16 19:15:04 +08:00
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: Machine{
|
|
|
|
Namespace: Namespace{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: HostInfo{
|
|
|
|
RequestTags: []string{"tag:valid", "tag:invalid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: []string{"tag:valid"},
|
|
|
|
wantInvalid: []string{"tag:invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple invalid and identical tags, should return only one invalid tag",
|
|
|
|
args: args{
|
2022-05-05 04:56:55 +08:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-04-16 19:15:04 +08:00
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: Machine{
|
|
|
|
Namespace: Namespace{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: HostInfo{
|
|
|
|
RequestTags: []string{
|
|
|
|
"tag:invalid",
|
|
|
|
"tag:valid",
|
|
|
|
"tag:invalid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: []string{"tag:valid"},
|
|
|
|
wantInvalid: []string{"tag:invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only invalid tags",
|
|
|
|
args: args{
|
2022-05-05 04:56:55 +08:00
|
|
|
aclPolicy: &ACLPolicy{
|
2022-04-16 19:15:04 +08:00
|
|
|
TagOwners: TagOwners{
|
|
|
|
"tag:valid": []string{"joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: Machine{
|
|
|
|
Namespace: Namespace{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: HostInfo{
|
|
|
|
RequestTags: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: nil,
|
|
|
|
wantInvalid: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
2022-05-05 04:56:55 +08:00
|
|
|
{
|
|
|
|
name: "empty ACLPolicy should return empty tags and should not panic",
|
|
|
|
args: args{
|
|
|
|
aclPolicy: nil,
|
|
|
|
machine: Machine{
|
|
|
|
Namespace: Namespace{
|
|
|
|
Name: "joe",
|
|
|
|
},
|
|
|
|
HostInfo: HostInfo{
|
|
|
|
RequestTags: []string{"tag:invalid", "very-invalid"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
|
|
|
wantValid: nil,
|
|
|
|
wantInvalid: nil,
|
|
|
|
},
|
2022-04-16 19:15:04 +08:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
gotValid, gotInvalid := getTags(
|
|
|
|
test.args.aclPolicy,
|
|
|
|
test.args.machine,
|
|
|
|
test.args.stripEmailDomain,
|
|
|
|
)
|
2022-04-26 04:07:44 +08:00
|
|
|
for _, valid := range gotValid {
|
|
|
|
if !contains(test.wantValid, valid) {
|
2022-04-26 04:27:44 +08:00
|
|
|
t.Errorf(
|
|
|
|
"valids: getTags() = %v, want %v",
|
|
|
|
gotValid,
|
|
|
|
test.wantValid,
|
|
|
|
)
|
|
|
|
|
2022-04-26 04:07:44 +08:00
|
|
|
break
|
|
|
|
}
|
2022-04-16 19:15:04 +08:00
|
|
|
}
|
2022-04-26 04:07:44 +08:00
|
|
|
for _, invalid := range gotInvalid {
|
|
|
|
if !contains(test.wantInvalid, invalid) {
|
2022-04-26 04:27:44 +08:00
|
|
|
t.Errorf(
|
|
|
|
"invalids: getTags() = %v, want %v",
|
|
|
|
gotInvalid,
|
|
|
|
test.wantInvalid,
|
|
|
|
)
|
|
|
|
|
2022-04-26 04:07:44 +08:00
|
|
|
break
|
|
|
|
}
|
2022-04-16 19:15:04 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 16:57:31 +08:00
|
|
|
func Test_getFilteredByACLPeers(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
machines []Machine
|
|
|
|
rules []tailcfg.FilterRule
|
|
|
|
machine *Machine
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2022-02-21 17:02:59 +08:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Machines
|
2022-02-21 16:57:31 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all hosts can talk to each other",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
2022-02-21 17:02:59 +08:00
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
2022-02-21 16:57:31 +08:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 1,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2022-02-21 16:57:31 +08:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
2022-02-21 16:57:31 +08:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.3")},
|
2022-02-21 16:57:31 +08:00
|
|
|
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 23:06:20 +08:00
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
2022-02-21 17:02:59 +08:00
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.1", "100.64.0.2", "100.64.0.3"},
|
2022-02-21 16:57:31 +08:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 1,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.1")},
|
2022-02-21 16:57:31 +08:00
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
2022-02-21 16:57:31 +08:00
|
|
|
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 23:06:20 +08:00
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 23:06:20 +08:00
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-02-21 23:06:20 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
2022-02-21 16:57:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
2022-02-21 17:02:59 +08:00
|
|
|
{
|
|
|
|
SrcIPs: []string{"100.64.0.3"},
|
2022-02-21 16:57:31 +08:00
|
|
|
DstPorts: []tailcfg.NetPortRange{
|
|
|
|
{IP: "100.64.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
2022-03-02 05:43:25 +08:00
|
|
|
ID: 2,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
2022-03-02 05:43:25 +08:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 3,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.3")},
|
2022-03-02 05:43:25 +08:00
|
|
|
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{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
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{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
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{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
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{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
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{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
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-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
2022-02-21 16:57:31 +08:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{
|
2022-03-02 05:43:25 +08:00
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
2022-02-21 16:57:31 +08:00
|
|
|
{
|
|
|
|
ID: 3,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.3")},
|
2022-02-21 16:57:31 +08:00
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-02 05:43:25 +08:00
|
|
|
{
|
|
|
|
name: "without rule all communications are forbidden",
|
|
|
|
args: args{
|
|
|
|
machines: []Machine{ // list of all machines in the database
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.1"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "joe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.2"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
IPAddresses: MachineAddresses{
|
2022-09-02 06:04:31 +08:00
|
|
|
netip.MustParseAddr("100.64.0.3"),
|
2022-03-02 05:43:25 +08:00
|
|
|
},
|
|
|
|
Namespace: Namespace{Name: "mickael"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rules: []tailcfg.FilterRule{ // list of all ACLRules registered
|
|
|
|
},
|
|
|
|
machine: &Machine{ // current machine
|
|
|
|
ID: 2,
|
2022-09-02 06:04:31 +08:00
|
|
|
IPAddresses: MachineAddresses{netip.MustParseAddr("100.64.0.2")},
|
2022-03-02 05:43:25 +08:00
|
|
|
Namespace: Namespace{Name: "marc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: Machines{},
|
|
|
|
},
|
2022-02-21 16:57:31 +08:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-02-21 23:06:20 +08:00
|
|
|
got := getFilteredByACLPeers(
|
|
|
|
tt.args.machines,
|
|
|
|
tt.args.rules,
|
|
|
|
tt.args.machine,
|
|
|
|
)
|
2022-02-21 16:57:31 +08:00
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("getFilteredByACLPeers() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 02:30:43 +08:00
|
|
|
|
|
|
|
func TestHeadscale_GenerateGivenName(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
suppliedName string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
h *Headscale
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple machine name generation",
|
|
|
|
h: &Headscale{
|
2022-06-05 23:47:26 +08:00
|
|
|
cfg: &Config{
|
2022-05-17 02:30:43 +08:00
|
|
|
OIDC: OIDCConfig{
|
|
|
|
StripEmaildomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
suppliedName: "testmachine",
|
|
|
|
},
|
|
|
|
want: "testmachine",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "machine name with 53 chars",
|
|
|
|
h: &Headscale{
|
2022-06-05 23:47:26 +08:00
|
|
|
cfg: &Config{
|
2022-05-17 02:30:43 +08:00
|
|
|
OIDC: OIDCConfig{
|
|
|
|
StripEmaildomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine",
|
|
|
|
},
|
|
|
|
want: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "machine name with 60 chars",
|
|
|
|
h: &Headscale{
|
2022-06-05 23:47:26 +08:00
|
|
|
cfg: &Config{
|
2022-05-17 02:30:43 +08:00
|
|
|
OIDC: OIDCConfig{
|
|
|
|
StripEmaildomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine1234567",
|
|
|
|
},
|
|
|
|
want: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "machine name with 63 chars",
|
|
|
|
h: &Headscale{
|
2022-06-05 23:47:26 +08:00
|
|
|
cfg: &Config{
|
2022-05-17 02:30:43 +08:00
|
|
|
OIDC: OIDCConfig{
|
|
|
|
StripEmaildomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine1234567890",
|
|
|
|
},
|
|
|
|
want: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "machine name with 64 chars",
|
|
|
|
h: &Headscale{
|
2022-06-05 23:47:26 +08:00
|
|
|
cfg: &Config{
|
2022-05-17 02:30:43 +08:00
|
|
|
OIDC: OIDCConfig{
|
|
|
|
StripEmaildomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine1234567891",
|
|
|
|
},
|
|
|
|
want: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "machine name with 73 chars",
|
|
|
|
h: &Headscale{
|
2022-06-05 23:47:26 +08:00
|
|
|
cfg: &Config{
|
2022-05-17 02:30:43 +08:00
|
|
|
OIDC: OIDCConfig{
|
|
|
|
StripEmaildomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine12345678901234567890",
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := tt.h.GenerateGivenName(tt.args.suppliedName)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf(
|
|
|
|
"Headscale.GenerateGivenName() error = %v, wantErr %v",
|
|
|
|
err,
|
|
|
|
tt.wantErr,
|
|
|
|
)
|
2022-06-26 17:43:17 +08:00
|
|
|
|
2022-05-17 02:30:43 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.want != "" && strings.Contains(tt.want, got) {
|
|
|
|
t.Errorf(
|
|
|
|
"Headscale.GenerateGivenName() = %v, is not a substring of %v",
|
|
|
|
tt.want,
|
|
|
|
got,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) > labelHostnameLength {
|
|
|
|
t.Errorf(
|
|
|
|
"Headscale.GenerateGivenName() = %v is larger than allowed DNS segment %d",
|
|
|
|
got,
|
|
|
|
labelHostnameLength,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 20:09:06 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestAutoApproveRoutes(c *check.C) {
|
|
|
|
err := app.LoadACLPolicy("./tests/acls/acl_policy_autoapprovers.hujson")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
namespace, err := app.CreateNamespace("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
|
|
|
|
defaultRoute := netaddr.MustParseIPPrefix("0.0.0.0/0")
|
|
|
|
route1 := netaddr.MustParseIPPrefix("10.10.0.0/16")
|
|
|
|
route2 := netaddr.MustParseIPPrefix("10.11.0.0/16")
|
|
|
|
|
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: NodePublicKeyStripPrefix(nodeKey.Public()),
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "test",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: HostInfo{
|
|
|
|
RequestTags: []string{"tag:exit"},
|
|
|
|
RoutableIPs: []netaddr.IPPrefix{defaultRoute, route1, route2},
|
|
|
|
},
|
|
|
|
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.1")},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.db.Save(&machine)
|
|
|
|
|
|
|
|
machine0ByID, err := app.GetMachineByID(0)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
app.EnableAutoApprovedRoutes(machine0ByID)
|
|
|
|
c.Assert(machine0ByID.GetEnabledRoutes(), check.HasLen, 3)
|
|
|
|
}
|