2021-05-02 20:47:36 +02:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2021-05-05 23:00:04 +02:00
|
|
|
"time"
|
2021-05-02 20:47:36 +02:00
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*Suite) TestCreatePreAuthKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
_, err := app.CreatePreAuthKey("bogus", true, false, nil)
|
2021-05-05 23:00:04 +02:00
|
|
|
|
2021-05-02 20:47:36 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-02 20:47:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.CreatePreAuthKey(namespace.Name, true, false, nil)
|
2021-05-02 20:47:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// Did we get a valid key?
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key.Key, check.NotNil)
|
|
|
|
c.Assert(len(key.Key), check.Equals, 48)
|
2021-05-02 20:47:36 +02:00
|
|
|
|
|
|
|
// Make sure the Namespace association is populated
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key.Namespace.Name, check.Equals, namespace.Name)
|
2021-05-02 20:47:36 +02:00
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
_, err = app.ListPreAuthKeys("bogus")
|
2021-05-02 20:47:36 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
keys, err := app.ListPreAuthKeys(namespace.Name)
|
2021-05-02 20:47:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-04 23:14:39 +01:00
|
|
|
c.Assert(len(keys), check.Equals, 1)
|
2021-05-02 20:47:36 +02:00
|
|
|
|
|
|
|
// Make sure the Namespace association is populated
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert((keys)[0].Namespace.Name, check.Equals, namespace.Name)
|
2021-05-02 20:47:36 +02:00
|
|
|
}
|
2021-05-05 23:00:04 +02:00
|
|
|
|
|
|
|
func (*Suite) TestExpiredPreAuthKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test2")
|
2021-05-05 23:00:04 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
now := time.Now()
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, true, false, &now)
|
2021-05-05 23:00:04 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity(pak.Key)
|
2021-11-15 17:33:16 +01:00
|
|
|
c.Assert(err, check.Equals, errPreAuthKeyExpired)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 23:00:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity("potatoKey")
|
2021-11-15 17:33:16 +01:00
|
|
|
c.Assert(err, check.Equals, errPreAuthKeyNotFound)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-05 23:00:04 +02:00
|
|
|
}
|
2021-05-06 00:08:36 +02:00
|
|
|
|
|
|
|
func (*Suite) TestValidateKeyOk(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test3")
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity(pak.Key)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-06 00:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestAlreadyUsedKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test4")
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
machine := Machine{
|
2021-05-06 00:08:36 +02:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testest",
|
2021-11-15 17:16:04 +01:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 09:49:55 +01:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-06 00:08:36 +02:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 17:16:04 +01:00
|
|
|
app.db.Save(&machine)
|
2021-05-06 00:08:36 +02:00
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity(pak.Key)
|
2021-10-13 23:23:07 +02:00
|
|
|
c.Assert(err, check.Equals, errSingleUseAuthKeyHasBeenUsed)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-05-06 00:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestReusableBeingUsedKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test5")
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
machine := Machine{
|
2021-05-06 00:08:36 +02:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testest",
|
2021-11-15 17:16:04 +01:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 09:49:55 +01:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-06 00:08:36 +02:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 17:16:04 +01:00
|
|
|
app.db.Save(&machine)
|
2021-05-06 00:08:36 +02:00
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity(pak.Key)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-06 00:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestNotReusableNotBeingUsedKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test6")
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity(pak.Key)
|
2021-05-06 00:08:36 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key.ID, check.Equals, pak.ID)
|
2021-05-06 00:08:36 +02:00
|
|
|
}
|
2021-05-23 02:15:29 +02:00
|
|
|
|
|
|
|
func (*Suite) TestEphemeralKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test7")
|
2021-05-23 02:15:29 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, true, nil)
|
2021-05-23 02:15:29 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
now := time.Now()
|
2021-11-15 17:16:04 +01:00
|
|
|
machine := Machine{
|
2021-05-23 02:15:29 +02:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Name: "testest",
|
2021-11-15 17:16:04 +01:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 09:49:55 +01:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-23 02:15:29 +02:00
|
|
|
LastSeen: &now,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 17:16:04 +01:00
|
|
|
app.db.Save(&machine)
|
2021-05-23 02:15:29 +02:00
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
_, err = app.checkKeyValidity(pak.Key)
|
2021-05-23 02:15:29 +02:00
|
|
|
// Ephemeral keys are by definition reusable
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
_, err = app.GetMachine("test7", "testest")
|
2021-05-23 02:15:29 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
app.expireEphemeralNodesWorker()
|
2021-05-23 02:15:29 +02:00
|
|
|
|
|
|
|
// The machine record should have been deleted
|
2021-11-15 17:16:04 +01:00
|
|
|
_, err = app.GetMachine("test7", "testest")
|
2021-05-23 02:15:29 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
2021-08-07 23:57:52 +02:00
|
|
|
|
|
|
|
func (*Suite) TestExpirePreauthKey(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test3")
|
2021-08-07 23:57:52 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil)
|
2021-08-07 23:57:52 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(pak.Expiration, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01: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 17:16:04 +01:00
|
|
|
key, err := app.checkKeyValidity(pak.Key)
|
2021-11-15 17:33:16 +01:00
|
|
|
c.Assert(err, check.Equals, errPreAuthKeyExpired)
|
2021-11-15 17:16:04 +01:00
|
|
|
c.Assert(key, check.IsNil)
|
2021-08-07 23:57:52 +02:00
|
|
|
}
|
2021-10-13 18:13:26 +02:00
|
|
|
|
2021-10-13 22:51:55 +02:00
|
|
|
func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) {
|
2021-11-15 17:16:04 +01:00
|
|
|
namespace, err := app.CreateNamespace("test6")
|
2021-10-13 18:13:26 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
|
2021-10-13 18:13:26 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-10-13 22:51:55 +02:00
|
|
|
pak.Used = true
|
2021-11-15 17:16:04 +01:00
|
|
|
app.db.Save(&pak)
|
2021-10-13 18:13:26 +02:00
|
|
|
|
2021-11-15 17:16:04 +01:00
|
|
|
_, err = app.checkKeyValidity(pak.Key)
|
2021-10-13 23:28:47 +02:00
|
|
|
c.Assert(err, check.Equals, errSingleUseAuthKeyHasBeenUsed)
|
2021-10-13 18:13:26 +02:00
|
|
|
}
|