Skip to content

Commit 486faa9

Browse files
committed
WIP Working on authkeys + tests
1 parent 03bb320 commit 486faa9

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

preauth_keys.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"time"
88
)
99

10+
const errorAuthKeyNotFound = Error("AuthKey not found")
11+
const errorAuthKeyExpired = Error("AuthKey expired")
12+
1013
// PreAuthKey describes a pre-authorization key usable in a particular namespace
1114
type PreAuthKey struct {
1215
ID uint64 `gorm:"primary_key"`
@@ -72,6 +75,28 @@ func (h *Headscale) GetPreAuthKeys(namespaceName string) (*[]PreAuthKey, error)
7275
return &keys, nil
7376
}
7477

78+
// checkKeyValidity does the heavy lifting for validation of the PreAuthKey coming from a node
79+
// If returns no error and a PreAuthKey, it can be used
80+
func (h *Headscale) checkKeyValidity(k string) (*PreAuthKey, error) {
81+
db, err := h.db()
82+
if err != nil {
83+
return nil, err
84+
}
85+
defer db.Close()
86+
87+
pak := PreAuthKey{}
88+
if db.First(&pak, "key = ?", k).RecordNotFound() {
89+
return nil, errorAuthKeyNotFound
90+
}
91+
92+
if pak.Expiration != nil && pak.Expiration.Before(time.Now()) {
93+
return nil, errorAuthKeyExpired
94+
}
95+
96+
// missing here validation on current usage
97+
return &pak, nil
98+
}
99+
75100
func (h *Headscale) generateKey() (string, error) {
76101
size := 24
77102
bytes := make([]byte, size)

preauth_keys_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"testing"
8+
"time"
89

910
_ "github.com/jinzhu/gorm/dialects/sqlite" // sql driver
1011

@@ -48,6 +49,7 @@ func (s *Suite) TearDownSuite(c *check.C) {
4849

4950
func (*Suite) TestCreatePreAuthKey(c *check.C) {
5051
_, err := h.CreatePreAuthKey("bogus", true, nil)
52+
5153
c.Assert(err, check.NotNil)
5254

5355
n, err := h.CreateNamespace("test")
@@ -73,3 +75,22 @@ func (*Suite) TestCreatePreAuthKey(c *check.C) {
7375
// Make sure the Namespace association is populated
7476
c.Assert((*keys)[0].Namespace.Name, check.Equals, n.Name)
7577
}
78+
79+
func (*Suite) TestExpiredPreAuthKey(c *check.C) {
80+
n, err := h.CreateNamespace("test2")
81+
c.Assert(err, check.IsNil)
82+
83+
now := time.Now()
84+
pak, err := h.CreatePreAuthKey(n.Name, true, &now)
85+
c.Assert(err, check.IsNil)
86+
87+
p, err := h.checkKeyValidity(pak.Key)
88+
c.Assert(err, check.Equals, errorAuthKeyExpired)
89+
c.Assert(p, check.IsNil)
90+
}
91+
92+
func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
93+
p, err := h.checkKeyValidity("potatoKey")
94+
c.Assert(err, check.Equals, errorAuthKeyNotFound)
95+
c.Assert(p, check.IsNil)
96+
}

utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"tailscale.com/wgengine/wgcfg"
2222
)
2323

24+
type Error string
25+
26+
func (e Error) Error() string { return string(e) }
27+
2428
func decode(msg []byte, v interface{}, pubKey *wgcfg.Key, privKey *wgcfg.PrivateKey) error {
2529
return decodeMsg(msg, v, pubKey, privKey)
2630
}

0 commit comments

Comments
 (0)