2023-05-21 12:37:59 -04:00
|
|
|
package db
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
import (
|
2022-09-01 18:05:18 -04:00
|
|
|
"net/netip"
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-09-01 18:05:18 -04:00
|
|
|
"go4.org/netipx"
|
2021-08-02 16:57:45 -04:00
|
|
|
"gopkg.in/check.v1"
|
2024-02-08 11:28:19 -05:00
|
|
|
"gorm.io/gorm"
|
2021-08-02 16:57:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetAvailableIp(c *check.C) {
|
2024-02-08 11:28:19 -05:00
|
|
|
tx := db.DB.Begin()
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
ips, err := getAvailableIPs(tx, []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("10.27.0.0/23"),
|
|
|
|
})
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-01 18:05:18 -04:00
|
|
|
expected := netip.MustParseAddr("10.27.0.1")
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
c.Assert(len(ips), check.Equals, 1)
|
|
|
|
c.Assert(ips[0].String(), check.Equals, expected.String())
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetUsedIps(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
ips, err := db.getAvailableIPs()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test-ip")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
_, err = db.getNode("test", "testnode")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2021-08-02 16:57:45 -04:00
|
|
|
ID: 0,
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-01-17 14:36:46 -05:00
|
|
|
UserID: user.ID,
|
2023-05-21 12:37:59 -04:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-08-02 16:57:45 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: ips,
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
2024-02-08 11:28:19 -05:00
|
|
|
db.Write(func(tx *gorm.DB) error {
|
|
|
|
return tx.Save(&node).Error
|
|
|
|
})
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
usedIps, err := Read(db.DB, func(rx *gorm.DB) (*netipx.IPSet, error) {
|
|
|
|
return getUsedIPs(rx)
|
|
|
|
})
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-01 18:05:18 -04:00
|
|
|
expected := netip.MustParseAddr("10.27.0.1")
|
|
|
|
expectedIPSetBuilder := netipx.IPSetBuilder{}
|
2022-02-25 03:28:22 -05:00
|
|
|
expectedIPSetBuilder.Add(expected)
|
|
|
|
expectedIPSet, _ := expectedIPSetBuilder.IPSet()
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2022-02-25 03:28:22 -05:00
|
|
|
c.Assert(usedIps.Equal(expectedIPSet), check.Equals, true)
|
|
|
|
c.Assert(usedIps.Contains(expected), check.Equals, true)
|
2021-08-02 17:39:18 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node1, err := db.GetNodeByID(0)
|
2021-08-02 17:39:18 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(len(node1.IPAddresses), check.Equals, 1)
|
|
|
|
c.Assert(node1.IPAddresses[0], check.Equals, expected)
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetMultiIp(c *check.C) {
|
2024-02-08 11:28:19 -05:00
|
|
|
user, err := db.CreateUser("test-ip")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
ipPrefixes := []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("10.27.0.0/23"),
|
|
|
|
}
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
for index := 1; index <= 350; index++ {
|
2024-02-08 11:28:19 -05:00
|
|
|
tx := db.DB.Begin()
|
2022-02-25 03:28:22 -05:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
ips, err := getAvailableIPs(tx, ipPrefixes)
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
pak, err := CreatePreAuthKey(tx, user.Name, false, false, nil, nil)
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
_, err = getNode(tx, "test", "testnode")
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2021-11-15 11:16:04 -05:00
|
|
|
ID: uint64(index),
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-01-17 14:36:46 -05:00
|
|
|
UserID: user.ID,
|
2023-05-21 12:37:59 -04:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-08-02 16:57:45 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-01-16 08:16:59 -05:00
|
|
|
IPAddresses: ips,
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
2024-02-08 11:28:19 -05:00
|
|
|
tx.Save(&node)
|
|
|
|
c.Assert(tx.Commit().Error, check.IsNil)
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
usedIps, err := Read(db.DB, func(rx *gorm.DB) (*netipx.IPSet, error) {
|
|
|
|
return getUsedIPs(rx)
|
|
|
|
})
|
2022-02-25 03:28:22 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-01 18:05:18 -04:00
|
|
|
expected0 := netip.MustParseAddr("10.27.0.1")
|
|
|
|
expected9 := netip.MustParseAddr("10.27.0.10")
|
|
|
|
expected300 := netip.MustParseAddr("10.27.0.45")
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2022-09-01 18:05:18 -04:00
|
|
|
notExpectedIPSetBuilder := netipx.IPSetBuilder{}
|
2022-02-25 03:28:22 -05:00
|
|
|
notExpectedIPSetBuilder.Add(expected0)
|
|
|
|
notExpectedIPSetBuilder.Add(expected9)
|
|
|
|
notExpectedIPSetBuilder.Add(expected300)
|
|
|
|
notExpectedIPSet, err := notExpectedIPSetBuilder.IPSet()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-02-25 03:28:22 -05:00
|
|
|
// We actually expect it to be a lot larger
|
|
|
|
c.Assert(usedIps.Equal(notExpectedIPSet), check.Equals, false)
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2022-02-25 03:28:22 -05:00
|
|
|
c.Assert(usedIps.Contains(expected0), check.Equals, true)
|
|
|
|
c.Assert(usedIps.Contains(expected9), check.Equals, true)
|
|
|
|
c.Assert(usedIps.Contains(expected300), check.Equals, true)
|
2021-08-02 16:57:45 -04:00
|
|
|
|
2021-08-02 17:39:18 -04:00
|
|
|
// Check that we can read back the IPs
|
2023-09-24 07:42:05 -04:00
|
|
|
node1, err := db.GetNodeByID(1)
|
2021-08-02 17:39:18 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(len(node1.IPAddresses), check.Equals, 1)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(
|
2023-09-24 07:42:05 -04:00
|
|
|
node1.IPAddresses[0],
|
2021-11-15 11:16:04 -05:00
|
|
|
check.Equals,
|
2022-09-01 18:05:18 -04:00
|
|
|
netip.MustParseAddr("10.27.0.1"),
|
2021-11-15 11:16:04 -05:00
|
|
|
)
|
2021-08-02 17:39:18 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node50, err := db.GetNodeByID(50)
|
2021-08-02 17:39:18 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 07:42:05 -04:00
|
|
|
c.Assert(len(node50.IPAddresses), check.Equals, 1)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(
|
2023-09-24 07:42:05 -04:00
|
|
|
node50.IPAddresses[0],
|
2021-11-15 11:16:04 -05:00
|
|
|
check.Equals,
|
2022-09-01 18:05:18 -04:00
|
|
|
netip.MustParseAddr("10.27.0.50"),
|
2021-11-15 11:16:04 -05:00
|
|
|
)
|
2021-08-02 17:39:18 -04:00
|
|
|
|
2022-09-01 18:05:18 -04:00
|
|
|
expectedNextIP := netip.MustParseAddr("10.27.1.95")
|
2023-05-21 12:37:59 -04:00
|
|
|
nextIP, err := db.getAvailableIPs()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
c.Assert(len(nextIP), check.Equals, 1)
|
|
|
|
c.Assert(nextIP[0].String(), check.Equals, expectedNextIP.String())
|
2021-08-02 16:57:45 -04:00
|
|
|
|
|
|
|
// If we call get Available again, we should receive
|
|
|
|
// the same IP, as it has not been reserved.
|
2023-05-21 12:37:59 -04:00
|
|
|
nextIP2, err := db.getAvailableIPs()
|
2021-08-02 16:57:45 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
c.Assert(len(nextIP2), check.Equals, 1)
|
|
|
|
c.Assert(nextIP2[0].String(), check.Equals, expectedNextIP.String())
|
2021-08-02 16:57:45 -04:00
|
|
|
}
|
2021-08-03 04:26:28 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (s *Suite) TestGetAvailableIpNodeWithoutIP(c *check.C) {
|
2023-05-21 12:37:59 -04:00
|
|
|
ips, err := db.getAvailableIPs()
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-03 17:46:14 -04:00
|
|
|
expected := netip.MustParseAddr("10.27.0.1")
|
2021-08-03 04:26:28 -04:00
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
c.Assert(len(ips), check.Equals, 1)
|
|
|
|
c.Assert(ips[0].String(), check.Equals, expected.String())
|
2021-08-03 04:26:28 -04:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
user, err := db.CreateUser("test-ip")
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
_, err = db.getNode("test", "testnode")
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := types.Node{
|
2021-08-03 04:26:28 -04:00
|
|
|
ID: 0,
|
2023-09-24 07:42:05 -04:00
|
|
|
Hostname: "testnode",
|
2023-01-17 14:36:46 -05:00
|
|
|
UserID: user.ID,
|
2023-05-21 12:37:59 -04:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-08-03 04:26:28 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2024-02-08 11:28:19 -05:00
|
|
|
db.DB.Save(&node)
|
2021-08-03 04:26:28 -04:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
ips2, err := db.getAvailableIPs()
|
2021-08-03 04:26:28 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
c.Assert(len(ips2), check.Equals, 1)
|
|
|
|
c.Assert(ips2[0].String(), check.Equals, expected.String())
|
2021-08-03 04:26:28 -04:00
|
|
|
}
|