Factor wgkey to types/key

This commit converts all the uses of wgkey to the new key interfaces.

It now has specific  machine, node and discovery keys and we now should
use them correctly.

Please note the new logic which strips a key prefix (in utils.go) that
is now standard inside tailscale.

In theory we could put it in the database, but to preserve backwards
compatibility and not spend a lot of resources on accounting for both,
we just strip them.
This commit is contained in:
Kristoffer Dalby 2021-11-26 23:30:42 +00:00
parent 07418140a2
commit cfd53bc4aa
7 changed files with 184 additions and 143 deletions

16
oidc.go
View file

@ -15,8 +15,10 @@ import (
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
"github.com/rs/zerolog/log"
"go4.org/mem"
"golang.org/x/oauth2"
"gorm.io/gorm"
"tailscale.com/types/key"
)
const (
@ -187,7 +189,17 @@ func (h *Headscale) OIDCCallback(ctx *gin.Context) {
return
}
machineKey, machineKeyOK := machineKeyIf.(string)
machineKeyStr, machineKeyOK := machineKeyIf.(string)
machineKey, err := key.ParseMachinePublicUntyped(mem.S(machineKeyStr))
if err != nil {
log.Error().
Msg("could not parse machine public key")
ctx.String(http.StatusBadRequest, "could not parse public key")
return
}
if !machineKeyOK {
log.Error().Msg("could not get machine key from cache")
@ -201,7 +213,7 @@ func (h *Headscale) OIDCCallback(ctx *gin.Context) {
// TODO(kradalby): Currently, if it fails to find a requested expiry, non will be set
requestedTime := time.Time{}
if requestedTimeIf, found := h.requestedExpiryCache.Get(machineKey); found {
if requestedTimeIf, found := h.requestedExpiryCache.Get(machineKey.String()); found {
if reqTime, ok := requestedTimeIf.(time.Time); ok {
requestedTime = reqTime
}