2023-05-21 12:37:59 -04:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
|
|
|
"gopkg.in/check.v1"
|
2023-11-21 12:20:06 -05:00
|
|
|
"tailscale.com/tailcfg"
|
2023-05-21 12:37:59 -04:00
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestGetNode(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := &types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestGetNodeByID(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestGetNodeByNodeKey(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByNodeKey(nodeKey.Public())
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestGetNodeByAnyNodeKey(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
oldNodeKey := key.NewNode()
|
|
|
|
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByAnyKey(machineKey.Public(), nodeKey.Public(), oldNodeKey.Public())
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestHardDeleteNode(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
2023-11-19 16:37:04 -05:00
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode3",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(1),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
err = db.DeleteNode(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNode(user.Name, "testnode3")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestListPeers(c *check.C) {
|
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
for index := 0; index <= 10; index++ {
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: uint64(index),
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode" + strconv.Itoa(index),
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node0ByID, err := db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
peersOfNode0, err := db.ListPeers(node0ByID)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(len(peersOfNode0), check.Equals, 9)
|
|
|
|
c.Assert(peersOfNode0[0].Hostname, check.Equals, "testnode2")
|
|
|
|
c.Assert(peersOfNode0[5].Hostname, check.Equals, "testnode7")
|
|
|
|
c.Assert(peersOfNode0[8].Hostname, check.Equals, "testnode10")
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
|
|
|
|
type base struct {
|
|
|
|
user *types.User
|
|
|
|
key *types.PreAuthKey
|
|
|
|
}
|
|
|
|
|
|
|
|
stor := make([]base, 0)
|
|
|
|
|
|
|
|
for _, name := range []string{"test", "admin"} {
|
|
|
|
user, err := db.CreateUser(name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
stor = append(stor, base{user, pak})
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err := db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
for index := 0; index <= 10; index++ {
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: uint64(index),
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
IPAddresses: types.NodeAddresses{
|
2023-05-21 12:37:59 -04:00
|
|
|
netip.MustParseAddr(fmt.Sprintf("100.64.0.%v", strconv.Itoa(index+1))),
|
|
|
|
},
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode" + strconv.Itoa(index),
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: stor[index%2].user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(stor[index%2].key.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
aclPolicy := &policy.ACLPolicy{
|
|
|
|
Groups: map[string][]string{
|
|
|
|
"group:test": {"admin"},
|
|
|
|
},
|
|
|
|
Hosts: map[string]netip.Prefix{},
|
|
|
|
TagOwners: map[string][]string{},
|
|
|
|
ACLs: []policy.ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"admin"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"test"},
|
|
|
|
Destinations: []string{"test:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Tests: []policy.ACLTest{},
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
adminNode, err := db.GetNodeByID(1)
|
|
|
|
c.Logf("Node(%v), user: %v", adminNode.Hostname, adminNode.User)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
testNode, err := db.GetNodeByID(2)
|
|
|
|
c.Logf("Node(%v), user: %v", testNode.Hostname, testNode.User)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
adminPeers, err := db.ListPeers(adminNode)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
testPeers, err := db.ListPeers(testNode)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
adminRules, _, err := policy.GenerateFilterAndSSHRules(aclPolicy, adminNode, adminPeers)
|
2023-06-08 13:10:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
testRules, _, err := policy.GenerateFilterAndSSHRules(aclPolicy, testNode, testPeers)
|
2023-06-08 13:10:09 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
peersOfAdminNode := policy.FilterNodesByACL(adminNode, adminPeers, adminRules)
|
|
|
|
peersOfTestNode := policy.FilterNodesByACL(testNode, testPeers, testRules)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Log(peersOfTestNode)
|
|
|
|
c.Assert(len(peersOfTestNode), check.Equals, 9)
|
|
|
|
c.Assert(peersOfTestNode[0].Hostname, check.Equals, "testnode1")
|
|
|
|
c.Assert(peersOfTestNode[1].Hostname, check.Equals, "testnode3")
|
|
|
|
c.Assert(peersOfTestNode[3].Hostname, check.Equals, "testnode5")
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Log(peersOfAdminNode)
|
|
|
|
c.Assert(len(peersOfAdminNode), check.Equals, 9)
|
|
|
|
c.Assert(peersOfAdminNode[0].Hostname, check.Equals, "testnode2")
|
|
|
|
c.Assert(peersOfAdminNode[2].Hostname, check.Equals, "testnode4")
|
|
|
|
c.Assert(peersOfAdminNode[5].Hostname, check.Equals, "testnode7")
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestExpireNode(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := &types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
Expiry: &time.Time{},
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
nodeFromDB, err := db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(nodeFromDB, check.NotNil)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(nodeFromDB.IsExpired(), check.Equals, false)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
now := time.Now()
|
2023-09-24 07:42:05 -04:00
|
|
|
err = db.NodeSetExpiry(nodeFromDB, now)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(nodeFromDB.IsExpired(), check.Equals, true)
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestSerdeAddressStrignSlice(c *check.C) {
|
2023-09-24 07:42:05 -04:00
|
|
|
input := types.NodeAddresses([]netip.Addr{
|
2023-05-21 12:37:59 -04:00
|
|
|
netip.MustParseAddr("192.0.2.1"),
|
|
|
|
netip.MustParseAddr("2001:db8::1"),
|
|
|
|
})
|
|
|
|
serialized, err := input.Value()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
if serial, ok := serialized.(string); ok {
|
|
|
|
c.Assert(serial, check.Equals, "192.0.2.1,2001:db8::1")
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
var deserialized types.NodeAddresses
|
2023-05-21 12:37:59 -04:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGenerateGivenName(c *check.C) {
|
|
|
|
user1, err := db.CreateUser("user-1")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user1.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNode("user-1", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
|
|
|
machineKey2 := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := &types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-05-21 12:37:59 -04:00
|
|
|
Hostname: "hostname-1",
|
|
|
|
GivenName: "hostname-1",
|
|
|
|
UserID: user1.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
givenName, err := db.GenerateGivenName(machineKey2.Public(), "hostname-2")
|
2023-09-24 07:42:05 -04:00
|
|
|
comment := check.Commentf("Same user, unique nodes, unique hostnames, no conflict")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil, comment)
|
|
|
|
c.Assert(givenName, check.Equals, "hostname-2", comment)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
givenName, err = db.GenerateGivenName(machineKey.Public(), "hostname-1")
|
2023-09-24 07:42:05 -04:00
|
|
|
comment = check.Commentf("Same user, same node, same hostname, no conflict")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil, comment)
|
|
|
|
c.Assert(givenName, check.Equals, "hostname-1", comment)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
givenName, err = db.GenerateGivenName(machineKey2.Public(), "hostname-1")
|
2023-09-24 07:42:05 -04:00
|
|
|
comment = check.Commentf("Same user, unique nodes, same hostname, conflict")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil, comment)
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(givenName, check.Matches, fmt.Sprintf("^hostname-1-[a-z0-9]{%d}$", NodeGivenNameHashLength), comment)
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestSetTags(c *check.C) {
|
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
_, err = db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 16:37:04 -05:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := &types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
|
|
|
// assign simple tags
|
|
|
|
sTags := []string{"tag:test", "tag:foo"}
|
2023-09-24 07:42:05 -04:00
|
|
|
err = db.SetTags(node, sTags)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 07:42:05 -04:00
|
|
|
node, err = db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(node.ForcedTags, check.DeepEquals, types.StringList(sTags))
|
2023-05-21 12:37:59 -04:00
|
|
|
|
|
|
|
// assign duplicat tags, expect no errors but no doubles in DB
|
|
|
|
eTags := []string{"tag:bar", "tag:test", "tag:unknown", "tag:test"}
|
2023-09-24 07:42:05 -04:00
|
|
|
err = db.SetTags(node, eTags)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 07:42:05 -04:00
|
|
|
node, err = db.GetNode("test", "testnode")
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(
|
2023-09-24 07:42:05 -04:00
|
|
|
node.ForcedTags,
|
2023-05-21 12:37:59 -04:00
|
|
|
check.DeepEquals,
|
|
|
|
types.StringList([]string{"tag:bar", "tag:test", "tag:unknown"}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadscale_generateGivenName(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
suppliedName string
|
|
|
|
randomSuffix bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *regexp.Regexp
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "simple node name generation",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
2023-09-24 07:42:05 -04:00
|
|
|
suppliedName: "testnode",
|
2023-05-21 12:37:59 -04:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
2023-09-24 07:42:05 -04:00
|
|
|
want: regexp.MustCompile("^testnode$"),
|
2023-05-21 12:37:59 -04:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "node name with 53 chars",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine",
|
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: regexp.MustCompile("^testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine$"),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "node name with 63 chars",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
2023-09-24 07:42:05 -04:00
|
|
|
suppliedName: "nodeeeeeee12345678901234567890123456789012345678901234567890123",
|
2023-05-21 12:37:59 -04:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
2023-09-24 07:42:05 -04:00
|
|
|
want: regexp.MustCompile("^nodeeeeeee12345678901234567890123456789012345678901234567890123$"),
|
2023-05-21 12:37:59 -04:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "node name with 64 chars",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
2023-09-24 07:42:05 -04:00
|
|
|
suppliedName: "nodeeeeeee123456789012345678901234567890123456789012345678901234",
|
2023-05-21 12:37:59 -04:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "node name with 73 chars",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
2023-09-24 07:42:05 -04:00
|
|
|
suppliedName: "nodeeeeeee123456789012345678901234567890123456789012345678901234567890123",
|
2023-05-21 12:37:59 -04:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "node name with random suffix",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
|
|
|
suppliedName: "test",
|
|
|
|
randomSuffix: true,
|
|
|
|
},
|
2023-09-24 07:42:05 -04:00
|
|
|
want: regexp.MustCompile(fmt.Sprintf("^test-[a-z0-9]{%d}$", NodeGivenNameHashLength)),
|
2023-05-21 12:37:59 -04:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 07:42:05 -04:00
|
|
|
name: "node name with 63 chars with random suffix",
|
2023-05-21 12:37:59 -04:00
|
|
|
args: args{
|
2023-09-24 07:42:05 -04:00
|
|
|
suppliedName: "nodeeee12345678901234567890123456789012345678901234567890123",
|
2023-05-21 12:37:59 -04:00
|
|
|
randomSuffix: true,
|
|
|
|
},
|
2023-09-24 07:42:05 -04:00
|
|
|
want: regexp.MustCompile(fmt.Sprintf("^nodeeee1234567890123456789012345678901234567890123456-[a-z0-9]{%d}$", NodeGivenNameHashLength)),
|
2023-05-21 12:37:59 -04:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-07-17 07:35:05 -04:00
|
|
|
got, err := generateGivenName(tt.args.suppliedName, tt.args.randomSuffix)
|
2023-05-21 12:37:59 -04:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf(
|
|
|
|
"Headscale.GenerateGivenName() error = %v, wantErr %v",
|
|
|
|
err,
|
|
|
|
tt.wantErr,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.want != nil && !tt.want.MatchString(got) {
|
|
|
|
t.Errorf(
|
|
|
|
"Headscale.GenerateGivenName() = %v, does not match %v",
|
|
|
|
tt.want,
|
|
|
|
got,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) > util.LabelHostnameLength {
|
|
|
|
t.Errorf(
|
|
|
|
"Headscale.GenerateGivenName() = %v is larger than allowed DNS segment %d",
|
|
|
|
got,
|
|
|
|
util.LabelHostnameLength,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestAutoApproveRoutes(c *check.C) {
|
|
|
|
acl := []byte(`
|
|
|
|
{
|
|
|
|
"tagOwners": {
|
|
|
|
"tag:exit": ["test"],
|
|
|
|
},
|
|
|
|
|
|
|
|
"groups": {
|
|
|
|
"group:test": ["test"]
|
|
|
|
},
|
|
|
|
|
|
|
|
"acls": [
|
|
|
|
{"action": "accept", "users": ["*"], "ports": ["*:*"]},
|
|
|
|
],
|
|
|
|
|
|
|
|
"autoApprovers": {
|
|
|
|
"exitNode": ["tag:exit"],
|
|
|
|
"routes": {
|
|
|
|
"10.10.0.0/16": ["group:test"],
|
|
|
|
"10.11.0.0/16": ["test"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
pol, err := policy.LoadACLPolicyFromBytes(acl, "hujson")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(pol, check.NotNil)
|
|
|
|
|
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
2023-11-19 16:37:04 -05:00
|
|
|
machineKey := key.NewMachine()
|
2023-05-21 12:37:59 -04:00
|
|
|
|
|
|
|
defaultRouteV4 := netip.MustParsePrefix("0.0.0.0/0")
|
|
|
|
defaultRouteV6 := netip.MustParsePrefix("::/0")
|
|
|
|
route1 := netip.MustParsePrefix("10.10.0.0/16")
|
|
|
|
// Check if a subprefix of an autoapproved route is approved
|
|
|
|
route2 := netip.MustParsePrefix("10.11.0.0/24")
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2023-05-21 12:37:59 -04:00
|
|
|
ID: 0,
|
2023-11-19 16:37:04 -05:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-05-21 12:37:59 -04:00
|
|
|
Hostname: "test",
|
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-11-21 12:20:06 -05:00
|
|
|
Hostinfo: &tailcfg.Hostinfo{
|
2023-05-21 12:37:59 -04:00
|
|
|
RequestTags: []string{"tag:exit"},
|
|
|
|
RoutableIPs: []netip.Prefix{defaultRouteV4, defaultRouteV6, route1, route2},
|
|
|
|
},
|
|
|
|
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.1")},
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
db.db.Save(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-12-09 12:09:24 -05:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 12:09:24 -05:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2023-05-21 12:37:59 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node0ByID, err := db.GetNodeByID(0)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
err = db.EnableAutoApprovedRoutes(pol, node0ByID)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
enabledRoutes, err := db.GetEnabledRoutes(node0ByID)
|
2023-05-21 12:37:59 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(enabledRoutes, check.HasLen, 4)
|
|
|
|
}
|