2023-05-21 16:37:59 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2024-07-18 08:01:59 +00:00
|
|
|
"crypto/rand"
|
2023-05-21 16:37:59 +00:00
|
|
|
"fmt"
|
2024-07-18 08:01:59 +00:00
|
|
|
"math/big"
|
2023-05-21 16:37:59 +00:00
|
|
|
"net/netip"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2024-07-18 08:01:59 +00:00
|
|
|
"sync"
|
2023-05-21 16:37:59 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-07-18 08:01:59 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2024-07-22 06:56:00 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2024-04-21 16:28:17 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2024-07-18 08:01:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-21 16:37:59 +00:00
|
|
|
"gopkg.in/check.v1"
|
2024-09-11 16:27:49 +00:00
|
|
|
"gorm.io/gorm"
|
2024-10-02 07:06:09 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2023-11-21 17:20:06 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2023-05-21 16:37:59 +00:00
|
|
|
"tailscale.com/types/key"
|
2024-07-18 08:01:59 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2023-05-21 16:37:59 +00:00
|
|
|
)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (s *Suite) TestGetNode(c *check.C) {
|
2023-05-21 16:37:59 +00: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)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := &types.Node{
|
2023-05-21 16:37:59 +00:00
|
|
|
ID: 0,
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode",
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (s *Suite) TestGetNodeByID(c *check.C) {
|
2023-05-21 16:37:59 +00: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 11:42:05 +00:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2023-05-21 16:37:59 +00:00
|
|
|
ID: 0,
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode",
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (s *Suite) TestGetNodeByAnyNodeKey(c *check.C) {
|
2023-05-21 16:37:59 +00: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 11:42:05 +00:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
oldNodeKey := key.NewNode()
|
|
|
|
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2023-05-21 16:37:59 +00:00
|
|
|
ID: 0,
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode",
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNodeByAnyKey(machineKey.Public(), nodeKey.Public(), oldNodeKey.Public())
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (s *Suite) TestHardDeleteNode(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
2023-11-19 21:37:04 +00:00
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2023-05-21 16:37:59 +00:00
|
|
|
ID: 0,
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode3",
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
_, err = db.DeleteNode(&node, xsync.NewMapOf[types.NodeID, bool]())
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode(user.Name, "testnode3")
|
2023-05-21 16:37:59 +00: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 11:42:05 +00:00
|
|
|
_, err = db.GetNodeByID(0)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
for index := 0; index <= 10; index++ {
|
2023-11-19 21:37:04 +00:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: types.NodeID(index),
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode" + strconv.Itoa(index),
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node0ByID, err := db.GetNodeByID(0)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
peersOfNode0, err := db.ListPeers(node0ByID.ID)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00: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 16:37:59 +00: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 11:42:05 +00:00
|
|
|
_, err := db.GetNodeByID(0)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
for index := 0; index <= 10; index++ {
|
2023-11-19 21:37:04 +00:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
v4 := netip.MustParseAddr(fmt.Sprintf("100.64.0.%d", index+1))
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2024-04-17 05:03:06 +00:00
|
|
|
ID: types.NodeID(index),
|
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
|
|
|
IPv4: &v4,
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode" + strconv.Itoa(index),
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: stor[index%2].user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(stor[index%2].key.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00: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 11:42:05 +00:00
|
|
|
adminNode, err := db.GetNodeByID(1)
|
|
|
|
c.Logf("Node(%v), user: %v", adminNode.Hostname, adminNode.User)
|
2024-10-02 09:41:58 +00:00
|
|
|
c.Assert(adminNode.IPv4, check.NotNil)
|
|
|
|
c.Assert(adminNode.IPv6, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
testNode, err := db.GetNodeByID(2)
|
|
|
|
c.Logf("Node(%v), user: %v", testNode.Hostname, testNode.User)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
adminPeers, err := db.ListPeers(adminNode.ID)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2024-10-02 09:41:58 +00:00
|
|
|
c.Assert(len(adminPeers), check.Equals, 9)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
testPeers, err := db.ListPeers(testNode.ID)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2024-10-02 09:41:58 +00:00
|
|
|
c.Assert(len(testPeers), check.Equals, 9)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
adminRules, _, err := policy.GenerateFilterAndSSHRulesForTests(aclPolicy, adminNode, adminPeers)
|
2023-06-08 17:10:09 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
testRules, _, err := policy.GenerateFilterAndSSHRulesForTests(aclPolicy, testNode, testPeers)
|
2023-06-08 17:10:09 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
peersOfAdminNode := policy.FilterNodesByACL(adminNode, adminPeers, adminRules)
|
|
|
|
peersOfTestNode := policy.FilterNodesByACL(testNode, testPeers, testRules)
|
2024-10-02 09:41:58 +00:00
|
|
|
c.Log(peersOfAdminNode)
|
2023-09-24 11:42:05 +00:00
|
|
|
c.Log(peersOfTestNode)
|
2024-10-02 09:41:58 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
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 16:37:59 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
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 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (s *Suite) TestExpireNode(c *check.C) {
|
2023-05-21 16:37:59 +00: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)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := &types.Node{
|
2023-05-21 16:37:59 +00:00
|
|
|
ID: 0,
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode",
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
Expiry: &time.Time{},
|
|
|
|
}
|
2024-02-08 16:28:19 +00:00
|
|
|
db.DB.Save(node)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
nodeFromDB, err := db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 11:42:05 +00:00
|
|
|
c.Assert(nodeFromDB, check.NotNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
c.Assert(nodeFromDB.IsExpired(), check.Equals, false)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2023-07-17 11:35:05 +00:00
|
|
|
now := time.Now()
|
2024-02-08 16:28:19 +00:00
|
|
|
err = db.NodeSetExpiry(nodeFromDB.ID, now)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
nodeFromDB, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
c.Assert(nodeFromDB.IsExpired(), check.Equals, true)
|
2023-05-21 16:37:59 +00: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)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := &types.Node{
|
2023-05-21 16:37:59 +00:00
|
|
|
ID: 0,
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "testnode",
|
2023-05-21 16:37:59 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
|
|
|
|
trx := db.DB.Save(node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
|
|
|
// assign simple tags
|
|
|
|
sTags := []string{"tag:test", "tag:foo"}
|
2024-02-08 16:28:19 +00:00
|
|
|
err = db.SetTags(node.ID, sTags)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2024-02-08 16:28:19 +00:00
|
|
|
node, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2024-10-02 09:41:58 +00:00
|
|
|
c.Assert(node.ForcedTags, check.DeepEquals, sTags)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-05-19 21:49:27 +00:00
|
|
|
// assign duplicate tags, expect no errors but no doubles in DB
|
2023-05-21 16:37:59 +00:00
|
|
|
eTags := []string{"tag:bar", "tag:test", "tag:unknown", "tag:test"}
|
2024-02-08 16:28:19 +00:00
|
|
|
err = db.SetTags(node.ID, eTags)
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2024-02-08 16:28:19 +00:00
|
|
|
node, err = db.getNode("test", "testnode")
|
2023-05-21 16:37:59 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(
|
2023-09-24 11:42:05 +00:00
|
|
|
node.ForcedTags,
|
2023-05-21 16:37:59 +00:00
|
|
|
check.DeepEquals,
|
2024-10-02 09:41:58 +00:00
|
|
|
[]string{"tag:bar", "tag:test", "tag:unknown"},
|
2023-05-21 16:37:59 +00:00
|
|
|
)
|
2024-04-17 22:08:32 +00:00
|
|
|
|
|
|
|
// test removing tags
|
|
|
|
err = db.SetTags(node.ID, []string{})
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
node, err = db.getNode("test", "testnode")
|
|
|
|
c.Assert(err, check.IsNil)
|
2024-10-02 09:41:58 +00:00
|
|
|
c.Assert(node.ForcedTags, check.DeepEquals, []string{})
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:42:05 +00:00
|
|
|
name: "simple node name generation",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
2023-09-24 11:42:05 +00:00
|
|
|
suppliedName: "testnode",
|
2023-05-21 16:37:59 +00:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
2023-09-24 11:42:05 +00:00
|
|
|
want: regexp.MustCompile("^testnode$"),
|
2023-05-21 16:37:59 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2024-10-15 16:33:03 +00:00
|
|
|
{
|
|
|
|
name: "UPPERCASE node name generation",
|
|
|
|
args: args{
|
|
|
|
suppliedName: "TestNode",
|
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: regexp.MustCompile("^testnode$"),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-05-21 16:37:59 +00:00
|
|
|
{
|
2023-09-24 11:42:05 +00:00
|
|
|
name: "node name with 53 chars",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
|
|
|
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine",
|
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: regexp.MustCompile("^testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine$"),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 11:42:05 +00:00
|
|
|
name: "node name with 63 chars",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
2023-09-24 11:42:05 +00:00
|
|
|
suppliedName: "nodeeeeeee12345678901234567890123456789012345678901234567890123",
|
2023-05-21 16:37:59 +00:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
2023-09-24 11:42:05 +00:00
|
|
|
want: regexp.MustCompile("^nodeeeeeee12345678901234567890123456789012345678901234567890123$"),
|
2023-05-21 16:37:59 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 11:42:05 +00:00
|
|
|
name: "node name with 64 chars",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
2023-09-24 11:42:05 +00:00
|
|
|
suppliedName: "nodeeeeeee123456789012345678901234567890123456789012345678901234",
|
2023-05-21 16:37:59 +00:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 11:42:05 +00:00
|
|
|
name: "node name with 73 chars",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
2023-09-24 11:42:05 +00:00
|
|
|
suppliedName: "nodeeeeeee123456789012345678901234567890123456789012345678901234567890123",
|
2023-05-21 16:37:59 +00:00
|
|
|
randomSuffix: false,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 11:42:05 +00:00
|
|
|
name: "node name with random suffix",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
|
|
|
suppliedName: "test",
|
|
|
|
randomSuffix: true,
|
|
|
|
},
|
2023-09-24 11:42:05 +00:00
|
|
|
want: regexp.MustCompile(fmt.Sprintf("^test-[a-z0-9]{%d}$", NodeGivenNameHashLength)),
|
2023-05-21 16:37:59 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-09-24 11:42:05 +00:00
|
|
|
name: "node name with 63 chars with random suffix",
|
2023-05-21 16:37:59 +00:00
|
|
|
args: args{
|
2023-09-24 11:42:05 +00:00
|
|
|
suppliedName: "nodeeee12345678901234567890123456789012345678901234567890123",
|
2023-05-21 16:37:59 +00:00
|
|
|
randomSuffix: true,
|
|
|
|
},
|
2023-09-24 11:42:05 +00:00
|
|
|
want: regexp.MustCompile(fmt.Sprintf("^nodeeee1234567890123456789012345678901234567890123456-[a-z0-9]{%d}$", NodeGivenNameHashLength)),
|
2023-05-21 16:37:59 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-07-17 11:35:05 +00:00
|
|
|
got, err := generateGivenName(tt.args.suppliedName, tt.args.randomSuffix)
|
2023-05-21 16:37:59 +00: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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
func TestAutoApproveRoutes(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
acl string
|
|
|
|
routes []netip.Prefix
|
|
|
|
want []netip.Prefix
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "2068-approve-issue-sub",
|
|
|
|
acl: `
|
|
|
|
{
|
|
|
|
"groups": {
|
|
|
|
"group:k8s": ["test"]
|
|
|
|
},
|
|
|
|
|
|
|
|
"acls": [
|
|
|
|
{"action": "accept", "users": ["*"], "ports": ["*:*"]},
|
|
|
|
],
|
|
|
|
|
|
|
|
"autoApprovers": {
|
|
|
|
"routes": {
|
|
|
|
"10.42.0.0/16": ["test"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
routes: []netip.Prefix{netip.MustParsePrefix("10.42.7.0/24")},
|
|
|
|
want: []netip.Prefix{netip.MustParsePrefix("10.42.7.0/24")},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2068-approve-issue-sub",
|
|
|
|
acl: `
|
2023-05-21 16:37:59 +00:00
|
|
|
{
|
|
|
|
"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"],
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 14:46:20 +00:00
|
|
|
}`,
|
|
|
|
routes: []netip.Prefix{
|
2024-10-02 07:06:09 +00:00
|
|
|
tsaddr.AllIPv4(),
|
|
|
|
tsaddr.AllIPv6(),
|
2024-09-05 14:46:20 +00:00
|
|
|
netip.MustParsePrefix("10.10.0.0/16"),
|
|
|
|
netip.MustParsePrefix("10.11.0.0/24"),
|
|
|
|
},
|
|
|
|
want: []netip.Prefix{
|
2024-10-02 07:06:09 +00:00
|
|
|
tsaddr.AllIPv4(),
|
2024-09-05 14:46:20 +00:00
|
|
|
netip.MustParsePrefix("10.10.0.0/16"),
|
2024-10-02 07:06:09 +00:00
|
|
|
netip.MustParsePrefix("10.11.0.0/24"),
|
|
|
|
tsaddr.AllIPv6(),
|
2024-09-05 14:46:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
adb, err := newTestDB()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
pol, err := policy.LoadACLPolicyFromBytes([]byte(tt.acl))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, pol)
|
|
|
|
|
|
|
|
user, err := adb.CreateUser("test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
pak, err := adb.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
machineKey := key.NewMachine()
|
|
|
|
|
|
|
|
v4 := netip.MustParseAddr("100.64.0.1")
|
|
|
|
node := types.Node{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: machineKey.Public(),
|
|
|
|
NodeKey: nodeKey.Public(),
|
|
|
|
Hostname: "test",
|
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
|
|
|
Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
RequestTags: []string{"tag:exit"},
|
|
|
|
RoutableIPs: tt.routes,
|
|
|
|
},
|
|
|
|
IPv4: &v4,
|
|
|
|
}
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
trx := adb.DB.Save(&node)
|
|
|
|
assert.NoError(t, trx.Error)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
sendUpdate, err := adb.SaveNodeRoutes(&node)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, sendUpdate)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
node0ByID, err := adb.GetNodeByID(0)
|
|
|
|
assert.NoError(t, err)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
// TODO(kradalby): Check state update
|
|
|
|
err = adb.EnableAutoApprovedRoutes(pol, node0ByID)
|
|
|
|
assert.NoError(t, err)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
enabledRoutes, err := adb.GetEnabledRoutes(node0ByID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, enabledRoutes, len(tt.want))
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
tsaddr.SortPrefixes(enabledRoutes)
|
2023-05-21 16:37:59 +00:00
|
|
|
|
2024-09-05 14:46:20 +00:00
|
|
|
if diff := cmp.Diff(tt.want, enabledRoutes, util.Comparers...); diff != "" {
|
|
|
|
t.Errorf("unexpected enabled routes (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
func TestEphemeralGarbageCollectorOrder(t *testing.T) {
|
|
|
|
want := []types.NodeID{1, 3}
|
|
|
|
got := []types.NodeID{}
|
2024-07-22 15:11:57 +00:00
|
|
|
var mu sync.Mutex
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
e := NewEphemeralGarbageCollector(func(ni types.NodeID) {
|
2024-07-22 15:11:57 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2024-07-18 08:01:59 +00:00
|
|
|
got = append(got, ni)
|
|
|
|
})
|
|
|
|
go e.Start()
|
|
|
|
|
2024-08-27 16:54:28 +00:00
|
|
|
go e.Schedule(1, 1*time.Second)
|
|
|
|
go e.Schedule(2, 2*time.Second)
|
|
|
|
go e.Schedule(3, 3*time.Second)
|
|
|
|
go e.Schedule(4, 4*time.Second)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
go e.Cancel(2)
|
|
|
|
go e.Cancel(4)
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
time.Sleep(6 * time.Second)
|
|
|
|
|
|
|
|
e.Close()
|
|
|
|
|
2024-07-22 15:11:57 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2024-07-18 08:01:59 +00:00
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong nodes deleted, unexpected result (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEphemeralGarbageCollectorLoads(t *testing.T) {
|
|
|
|
var got []types.NodeID
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
|
|
|
want := 1000
|
|
|
|
|
|
|
|
e := NewEphemeralGarbageCollector(func(ni types.NodeID) {
|
|
|
|
mu.Lock()
|
2024-07-22 15:11:57 +00:00
|
|
|
defer mu.Unlock()
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Duration(generateRandomNumber(t, 3)) * time.Millisecond)
|
|
|
|
got = append(got, ni)
|
|
|
|
})
|
|
|
|
go e.Start()
|
|
|
|
|
|
|
|
for i := 0; i < want; i++ {
|
|
|
|
go e.Schedule(types.NodeID(i), 1*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
|
|
|
e.Close()
|
2024-07-22 15:11:57 +00:00
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2024-07-18 08:01:59 +00:00
|
|
|
if len(got) != want {
|
|
|
|
t.Errorf("expected %d, got %d", want, len(got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRandomNumber(t *testing.T, max int64) int64 {
|
|
|
|
t.Helper()
|
|
|
|
maxB := big.NewInt(max)
|
|
|
|
n, err := rand.Int(rand.Reader, maxB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("getting random number: %s", err)
|
|
|
|
}
|
|
|
|
return n.Int64() + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEphemeralNodes(t *testing.T) {
|
|
|
|
db, err := newTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating db: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
pakEph, err := db.CreatePreAuthKey(user.Name, false, true, nil, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node := types.Node{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: key.NewMachine().Public(),
|
|
|
|
NodeKey: key.NewNode().Public(),
|
|
|
|
Hostname: "test",
|
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeEph := types.Node{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: key.NewMachine().Public(),
|
|
|
|
NodeKey: key.NewNode().Public(),
|
|
|
|
Hostname: "ephemeral",
|
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: ptr.To(pakEph.ID),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.DB.Save(&node).Error
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.DB.Save(&nodeEph).Error
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nodes, err := db.ListNodes()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
ephemeralNodes, err := db.ListEphemeralNodes()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, nodes, 2)
|
|
|
|
assert.Len(t, ephemeralNodes, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, nodeEph.ID, ephemeralNodes[0].ID)
|
|
|
|
assert.Equal(t, nodeEph.AuthKeyID, ephemeralNodes[0].AuthKeyID)
|
|
|
|
assert.Equal(t, nodeEph.UserID, ephemeralNodes[0].UserID)
|
|
|
|
assert.Equal(t, nodeEph.Hostname, ephemeralNodes[0].Hostname)
|
|
|
|
}
|
2024-09-11 16:27:49 +00:00
|
|
|
|
|
|
|
func TestRenameNode(t *testing.T) {
|
|
|
|
db, err := newTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating db: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := db.CreateUser("test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user2, err := db.CreateUser("test2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node := types.Node{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: key.NewMachine().Public(),
|
|
|
|
NodeKey: key.NewNode().Public(),
|
|
|
|
Hostname: "test",
|
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
node2 := types.Node{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: key.NewMachine().Public(),
|
|
|
|
NodeKey: key.NewNode().Public(),
|
|
|
|
Hostname: "test",
|
|
|
|
UserID: user2.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.DB.Save(&node).Error
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.DB.Save(&node2).Error
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
_, err := RegisterNode(tx, node, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = RegisterNode(tx, node2, nil, nil)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nodes, err := db.ListNodes()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, nodes, 2)
|
|
|
|
|
|
|
|
t.Logf("node1 %s %s", nodes[0].Hostname, nodes[0].GivenName)
|
|
|
|
t.Logf("node2 %s %s", nodes[1].Hostname, nodes[1].GivenName)
|
|
|
|
|
|
|
|
assert.Equal(t, nodes[0].Hostname, nodes[0].GivenName)
|
|
|
|
assert.NotEqual(t, nodes[1].Hostname, nodes[1].GivenName)
|
|
|
|
assert.Equal(t, nodes[0].Hostname, nodes[1].Hostname)
|
|
|
|
assert.NotEqual(t, nodes[0].Hostname, nodes[1].GivenName)
|
|
|
|
assert.Contains(t, nodes[1].GivenName, nodes[0].Hostname)
|
|
|
|
assert.Equal(t, nodes[0].GivenName, nodes[1].Hostname)
|
|
|
|
assert.Len(t, nodes[0].Hostname, 4)
|
|
|
|
assert.Len(t, nodes[1].Hostname, 4)
|
|
|
|
assert.Len(t, nodes[0].GivenName, 4)
|
|
|
|
assert.Len(t, nodes[1].GivenName, 13)
|
|
|
|
|
|
|
|
// Nodes can be renamed to a unique name
|
|
|
|
err = db.Write(func(tx *gorm.DB) error {
|
|
|
|
return RenameNode(tx, nodes[0].ID, "newname")
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nodes, err = db.ListNodes()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, nodes, 2)
|
|
|
|
assert.Equal(t, nodes[0].Hostname, "test")
|
|
|
|
assert.Equal(t, nodes[0].GivenName, "newname")
|
|
|
|
|
|
|
|
// Nodes can reuse name that is no longer used
|
|
|
|
err = db.Write(func(tx *gorm.DB) error {
|
|
|
|
return RenameNode(tx, nodes[1].ID, "test")
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nodes, err = db.ListNodes()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, nodes, 2)
|
|
|
|
assert.Equal(t, nodes[0].Hostname, "test")
|
|
|
|
assert.Equal(t, nodes[0].GivenName, "newname")
|
|
|
|
assert.Equal(t, nodes[1].GivenName, "test")
|
|
|
|
|
|
|
|
// Nodes cannot be renamed to used names
|
|
|
|
err = db.Write(func(tx *gorm.DB) error {
|
|
|
|
return RenameNode(tx, nodes[0].ID, "test")
|
|
|
|
})
|
|
|
|
assert.ErrorContains(t, err, "name is not unique")
|
|
|
|
}
|