headscale/preauth_keys_test.go

210 lines
5.7 KiB
Go
Raw Normal View History

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