2023-05-10 03:24:05 -04:00
|
|
|
package hscontrol
|
2021-05-02 14:47:36 -04:00
|
|
|
|
|
|
|
import (
|
2021-05-05 17:00:04 -04:00
|
|
|
"time"
|
2021-05-02 14:47:36 -04:00
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*Suite) TestCreatePreAuthKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err := app.db.CreatePreAuthKey("bogus", true, false, nil, nil)
|
2021-05-05 17:00:04 -04:00
|
|
|
|
2021-05-02 14:47:36 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test")
|
2021-05-02 14:47:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-05-02 14:47:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// Did we get a valid key?
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key.Key, check.NotNil)
|
|
|
|
c.Assert(len(key.Key), check.Equals, 48)
|
2021-05-02 14:47:36 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Make sure the User association is populated
|
|
|
|
c.Assert(key.User.Name, check.Equals, user.Name)
|
2021-05-02 14:47:36 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err = app.db.ListPreAuthKeys("bogus")
|
2021-05-02 14:47:36 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
keys, err := app.db.ListPreAuthKeys(user.Name)
|
2021-05-02 14:47:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-04 18:14:39 -04:00
|
|
|
c.Assert(len(keys), check.Equals, 1)
|
2021-05-02 14:47:36 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Make sure the User association is populated
|
|
|
|
c.Assert((keys)[0].User.Name, check.Equals, user.Name)
|
2021-05-02 14:47:36 -04:00
|
|
|
}
|
2021-05-05 17:00:04 -04:00
|
|
|
|
|
|
|
func (*Suite) TestExpiredPreAuthKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test2")
|
2021-05-05 17:00:04 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
now := time.Now()
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, true, false, &now, nil)
|
2021-05-05 17:00:04 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity(pak.Key)
|
2022-07-29 11:35:21 -04:00
|
|
|
c.Assert(err, check.Equals, ErrPreAuthKeyExpired)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 17:00:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity("potatoKey")
|
2022-07-29 11:35:21 -04:00
|
|
|
c.Assert(err, check.Equals, ErrPreAuthKeyNotFound)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 17:00:04 -04:00
|
|
|
}
|
2021-05-05 18:08:36 -04:00
|
|
|
|
|
|
|
func (*Suite) TestValidateKeyOk(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test3")
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity(pak.Key)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-05 18:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestAlreadyUsedKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test4")
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-05-05 18:08:36 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 15:54:38 -04:00
|
|
|
Hostname: "testest",
|
2023-01-17 14:36:46 -05:00
|
|
|
UserID: user.ID,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-05 18:08:36 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-05-11 03:09:18 -04:00
|
|
|
app.db.db.Save(&machine)
|
2021-05-05 18:08:36 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity(pak.Key)
|
2022-07-29 11:35:21 -04:00
|
|
|
c.Assert(err, check.Equals, ErrSingleUseAuthKeyHasBeenUsed)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 18:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestReusableBeingUsedKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test5")
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-05-05 18:08:36 -04:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 15:54:38 -04:00
|
|
|
Hostname: "testest",
|
2023-01-17 14:36:46 -05:00
|
|
|
UserID: user.ID,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-05 18:08:36 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-05-11 03:09:18 -04:00
|
|
|
app.db.db.Save(&machine)
|
2021-05-05 18:08:36 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity(pak.Key)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-05 18:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestNotReusableNotBeingUsedKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test6")
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity(pak.Key)
|
2021-05-05 18:08:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-05 18:08:36 -04:00
|
|
|
}
|
2021-05-22 20:15:29 -04:00
|
|
|
|
|
|
|
func (*Suite) TestEphemeralKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test7")
|
2021-05-22 20:15:29 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, false, true, nil, nil)
|
2021-05-22 20:15:29 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
now := time.Now()
|
2021-11-15 11:16:04 -05:00
|
|
|
machine := Machine{
|
2021-05-22 20:15:29 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-04-24 15:54:38 -04:00
|
|
|
Hostname: "testest",
|
2023-01-17 14:36:46 -05:00
|
|
|
UserID: user.ID,
|
2021-11-18 03:49:55 -05:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-22 20:15:29 -04:00
|
|
|
LastSeen: &now,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2023-05-11 03:09:18 -04:00
|
|
|
app.db.db.Save(&machine)
|
2021-05-22 20:15:29 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err = app.db.checkKeyValidity(pak.Key)
|
2021-05-22 20:15:29 -04:00
|
|
|
// Ephemeral keys are by definition reusable
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err = app.db.GetMachine("test7", "testest")
|
2021-05-22 20:15:29 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 11:16:04 -05:00
|
|
|
app.expireEphemeralNodesWorker()
|
2021-05-22 20:15:29 -04:00
|
|
|
|
|
|
|
// The machine record should have been deleted
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err = app.db.GetMachine("test7", "testest")
|
2021-05-22 20:15:29 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
2021-08-07 17:57:52 -04:00
|
|
|
|
|
|
|
func (*Suite) TestExpirePreauthKey(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test3")
|
2021-08-07 17:57:52 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, true, false, nil, nil)
|
2021-08-07 17:57:52 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(pak.Expiration, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
err = app.db.ExpirePreAuthKey(pak)
|
2021-08-07 17:57:52 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(pak.Expiration, check.NotNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
key, err := app.db.checkKeyValidity(pak.Key)
|
2022-07-29 11:35:21 -04:00
|
|
|
c.Assert(err, check.Equals, ErrPreAuthKeyExpired)
|
2021-11-15 11:16:04 -05:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-08-07 17:57:52 -04:00
|
|
|
}
|
2021-10-13 12:13:26 -04:00
|
|
|
|
2021-10-13 16:51:55 -04:00
|
|
|
func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test6")
|
2021-10-13 12:13:26 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
pak, err := app.db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-10-13 12:13:26 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 16:51:55 -04:00
|
|
|
pak.Used = true
|
2023-05-11 03:09:18 -04:00
|
|
|
app.db.db.Save(&pak)
|
2021-10-13 12:13:26 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err = app.db.checkKeyValidity(pak.Key)
|
2022-07-29 11:35:21 -04:00
|
|
|
c.Assert(err, check.Equals, ErrSingleUseAuthKeyHasBeenUsed)
|
2021-10-13 12:13:26 -04:00
|
|
|
}
|
2022-08-25 06:43:15 -04:00
|
|
|
|
2022-09-07 08:12:29 -04:00
|
|
|
func (*Suite) TestPreAuthKeyACLTags(c *check.C) {
|
2023-05-11 03:09:18 -04:00
|
|
|
user, err := app.db.CreateUser("test8")
|
2022-08-25 06:43:15 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err = app.db.CreatePreAuthKey(user.Name, false, false, nil, []string{"badtag"})
|
2022-08-25 06:43:15 -04: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-11 03:09:18 -04:00
|
|
|
_, err = app.db.CreatePreAuthKey(user.Name, false, false, nil, tagsWithDuplicate)
|
2022-08-25 06:43:15 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
listedPaks, err := app.db.ListPreAuthKeys("test8")
|
2022-08-25 06:43:15 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(listedPaks[0].toProto().AclTags, check.DeepEquals, tags)
|
|
|
|
}
|