From 999c7ee287502e3d7be037d7bc9ccc338146140f Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Wed, 13 Oct 2021 00:46:19 +0200 Subject: [PATCH] Show if a authkey has been used already (fixes #154) --- cmd/headscale/cli/preauthkeys.go | 3 ++- preauth_keys.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index 1340267e..28bbb7e7 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -57,7 +57,7 @@ var listPreAuthKeys = &cobra.Command{ return } - d := pterm.TableData{{"ID", "Key", "Reusable", "Ephemeral", "Expiration", "Created"}} + d := pterm.TableData{{"ID", "Key", "Reusable", "Ephemeral", "AlreadyUsed", "Expiration", "Created"}} for _, k := range *keys { expiration := "-" if k.Expiration != nil { @@ -76,6 +76,7 @@ var listPreAuthKeys = &cobra.Command{ k.Key, reusable, strconv.FormatBool(k.Ephemeral), + fmt.Sprintf("%v", k.AlreadyUsed), expiration, k.CreatedAt.Format("2006-01-02 15:04:05"), }) diff --git a/preauth_keys.go b/preauth_keys.go index cc849fc0..5adaee8a 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -22,6 +22,8 @@ type PreAuthKey struct { Reusable bool Ephemeral bool `gorm:"default:false"` + AlreadyUsed bool `gorm:"-"` // this field is not stored in the DB, has to be manually filled + CreatedAt *time.Time Expiration *time.Time } @@ -64,6 +66,16 @@ func (h *Headscale) GetPreAuthKeys(namespaceName string) (*[]PreAuthKey, error) if err := h.db.Preload("Namespace").Where(&PreAuthKey{NamespaceID: n.ID}).Find(&keys).Error; err != nil { return nil, err } + + for i, k := range keys { + machines := []Machine{} + if err := h.db.Preload("AuthKey").Where(&Machine{AuthKeyID: uint(k.ID)}).Find(&machines).Error; err != nil { + return nil, err + } + if len(machines) > 0 { + keys[i].AlreadyUsed = true + } + } return &keys, nil }