2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2021-05-02 18:47:36 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-03 10:01:48 +00:00
|
|
|
"sort"
|
2021-05-05 21:00:04 +00:00
|
|
|
"time"
|
2021-05-02 18:47:36 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2021-05-02 18:47:36 +00:00
|
|
|
"gopkg.in/check.v1"
|
2024-07-18 08:01:59 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2021-05-02 18:47:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (*Suite) TestCreatePreAuthKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err := db.CreatePreAuthKey("bogus", true, false, nil, nil)
|
2021-05-05 21:00:04 +00:00
|
|
|
|
2021-05-02 18:47:36 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2021-05-02 18:47:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-05-02 18:47:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// Did we get a valid key?
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key.Key, check.NotNil)
|
|
|
|
c.Assert(len(key.Key), check.Equals, 48)
|
2021-05-02 18:47:36 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
// Make sure the User association is populated
|
|
|
|
c.Assert(key.User.Name, check.Equals, user.Name)
|
2021-05-02 18:47:36 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err = db.ListPreAuthKeys("bogus")
|
2021-05-02 18:47:36 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
keys, err := db.ListPreAuthKeys(user.Name)
|
2021-05-02 18:47:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-04 22:14:39 +00:00
|
|
|
c.Assert(len(keys), check.Equals, 1)
|
2021-05-02 18:47:36 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
// Make sure the User association is populated
|
|
|
|
c.Assert((keys)[0].User.Name, check.Equals, user.Name)
|
2021-05-02 18:47:36 +00:00
|
|
|
}
|
2021-05-05 21:00:04 +00:00
|
|
|
|
|
|
|
func (*Suite) TestExpiredPreAuthKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test2")
|
2021-05-05 21:00:04 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
now := time.Now().Add(-5 * time.Second)
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, true, false, &now, nil)
|
2021-05-05 21:00:04 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey(pak.Key)
|
2022-07-29 15:35:21 +00:00
|
|
|
c.Assert(err, check.Equals, ErrPreAuthKeyExpired)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey("potatoKey")
|
2022-07-29 15:35:21 +00:00
|
|
|
c.Assert(err, check.Equals, ErrPreAuthKeyNotFound)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 21:00:04 +00:00
|
|
|
}
|
2021-05-05 22:08:36 +00:00
|
|
|
|
|
|
|
func (*Suite) TestValidateKeyOk(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test3")
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey(pak.Key)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-05 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestAlreadyUsedKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test4")
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2021-05-05 22:08:36 +00:00
|
|
|
ID: 0,
|
2022-04-24 19:54:38 +00:00
|
|
|
Hostname: "testest",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2021-05-05 22:08:36 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2021-05-05 22:08:36 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey(pak.Key)
|
2022-07-29 15:35:21 +00:00
|
|
|
c.Assert(err, check.Equals, ErrSingleUseAuthKeyHasBeenUsed)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestReusableBeingUsedKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test5")
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2021-05-05 22:08:36 +00:00
|
|
|
ID: 1,
|
2022-04-24 19:54:38 +00:00
|
|
|
Hostname: "testest",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2021-05-05 22:08:36 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2021-05-05 22:08:36 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey(pak.Key)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-05 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestNotReusableNotBeingUsedKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test6")
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey(pak.Key)
|
2021-05-05 22:08:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-05 22:08:36 +00:00
|
|
|
}
|
2021-05-23 00:15:29 +00:00
|
|
|
|
2021-08-07 21:57:52 +00:00
|
|
|
func (*Suite) TestExpirePreauthKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test3")
|
2021-08-07 21:57:52 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-08-07 21:57:52 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(pak.Expiration, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.ExpirePreAuthKey(pak)
|
2021-08-07 21:57:52 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(pak.Expiration, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.ValidatePreAuthKey(pak.Key)
|
2022-07-29 15:35:21 +00:00
|
|
|
c.Assert(err, check.Equals, ErrPreAuthKeyExpired)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-08-07 21:57:52 +00:00
|
|
|
}
|
2021-10-13 16:13:26 +00:00
|
|
|
|
2021-10-13 20:51:55 +00:00
|
|
|
func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test6")
|
2021-10-13 16:13:26 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-10-13 16:13:26 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 20:51:55 +00:00
|
|
|
pak.Used = true
|
2024-02-08 16:28:19 +00:00
|
|
|
db.DB.Save(&pak)
|
2021-10-13 16:13:26 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err = db.ValidatePreAuthKey(pak.Key)
|
2022-07-29 15:35:21 +00:00
|
|
|
c.Assert(err, check.Equals, ErrSingleUseAuthKeyHasBeenUsed)
|
2021-10-13 16:13:26 +00:00
|
|
|
}
|
2022-08-25 10:43:15 +00:00
|
|
|
|
2022-09-07 12:12:29 +00:00
|
|
|
func (*Suite) TestPreAuthKeyACLTags(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test8")
|
2022-08-25 10:43:15 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err = db.CreatePreAuthKey(user.Name, false, false, nil, []string{"badtag"})
|
2022-08-25 10:43:15 +00:00
|
|
|
c.Assert(err, check.NotNil) // Confirm that malformed tags are rejected
|
|
|
|
|
|
|
|
tags := []string{"tag:test1", "tag:test2"}
|
|
|
|
tagsWithDuplicate := []string{"tag:test1", "tag:test2", "tag:test2"}
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err = db.CreatePreAuthKey(user.Name, false, false, nil, tagsWithDuplicate)
|
2022-08-25 10:43:15 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
listedPaks, err := db.ListPreAuthKeys("test8")
|
2022-08-25 10:43:15 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2024-10-03 10:01:48 +00:00
|
|
|
gotTags := listedPaks[0].Proto().GetAclTags()
|
|
|
|
sort.Sort(sort.StringSlice(gotTags))
|
|
|
|
c.Assert(gotTags, check.DeepEquals, tags)
|
2022-08-25 10:43:15 +00:00
|
|
|
}
|