1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-08-14 13:51:01 +02:00

Show if a authkey has been used already (fixes #154)

This commit is contained in:
Juan Font Alonso 2021-10-13 00:46:19 +02:00
parent dd1e425d02
commit 999c7ee287
2 changed files with 14 additions and 1 deletions

View File

@ -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"),
})

View File

@ -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
}