From 167925b2645c3a93350abdee00c030dedc6991fa Mon Sep 17 00:00:00 2001 From: Josef Citrine Date: Fri, 26 Nov 2021 15:13:20 +0000 Subject: [PATCH] Fixed auth key expired error --- grpcv1.go | 12 +++++++----- preauth_keys.go | 26 +++++++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/grpcv1.go b/grpcv1.go index 1850ce7a..24e7c274 100644 --- a/grpcv1.go +++ b/grpcv1.go @@ -6,7 +6,7 @@ import ( "encoding/json" "time" - "github.com/juanfont/headscale/gen/go/headscale/v1" + v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/rs/zerolog/log" "gorm.io/datatypes" "tailscale.com/tailcfg" @@ -99,16 +99,18 @@ func (api headscaleV1APIServer) CreatePreAuthKey( ctx context.Context, request *v1.CreatePreAuthKeyRequest, ) (*v1.CreatePreAuthKeyResponse, error) { - var expiration time.Time + var expiration *time.Time if request.GetExpiration() != nil { - expiration = request.GetExpiration().AsTime() + expirationTime := request.GetExpiration().AsTime() + expiration = &expirationTime } - preAuthKey, err := api.h.CreatePreAuthKey( + preAuthKey, err := api.h.CreatePreAuthKeyWithSubnet( request.GetNamespace(), request.GetReusable(), request.GetEphemeral(), - &expiration, + expiration, + request.GetSubnet(), ) if err != nil { return nil, err diff --git a/preauth_keys.go b/preauth_keys.go index 1a99504f..05553b1f 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -26,21 +26,32 @@ type PreAuthKey struct { NamespaceID uint Namespace Namespace Reusable bool - Ephemeral bool `gorm:"default:false"` - Subnet string `gorm:"default:''"` - Used bool `gorm:"default:false"` + Ephemeral bool `gorm:"default:false"` + Used bool `gorm:"default:false"` + Subnet string CreatedAt *time.Time Expiration *time.Time } -// CreatePreAuthKey creates a new PreAuthKey in a namespace for the default subnet, and returns it -func (h *Headscale) CreatePreAuthKey(namespaceName string, reusable bool, ephemeral bool, expiration *time.Time) (*PreAuthKey, error) { +// CreatePreAuthKey creates a new PreAuthKey in a namespace for the default subnet, and returns it. +func (h *Headscale) CreatePreAuthKey( + namespaceName string, + reusable bool, + ephemeral bool, + expiration *time.Time, +) (*PreAuthKey, error) { return h.CreatePreAuthKeyWithSubnet(namespaceName, reusable, ephemeral, expiration, "") } -// CreatePreAuthKey creates a new PreAuthKey in a namespace with a subnet, and returns it -func (h *Headscale) CreatePreAuthKeyWithSubnet(namespaceName string, reusable bool, ephemeral bool, expiration *time.Time, subnet string) (*PreAuthKey, error) { +// CreatePreAuthKeyWithSubnet creates a new PreAuthKey in a namespace with a subnet, and returns it +func (h *Headscale) CreatePreAuthKeyWithSubnet( + namespaceName string, + reusable bool, + ephemeral bool, + expiration *time.Time, + subnet string, +) (*PreAuthKey, error) { namespace, err := h.GetNamespace(namespaceName) if err != nil { return nil, err @@ -164,6 +175,7 @@ func (key *PreAuthKey) toProto() *v1.PreAuthKey { Ephemeral: key.Ephemeral, Reusable: key.Reusable, Used: key.Used, + Subnet: key.Subnet, } if key.Expiration != nil {