1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-06-05 01:20:21 +02:00

types/pak: make user/tag optional

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2025-05-26 10:07:26 +02:00
parent 4d3483ed3a
commit bdcd5496fc
No known key found for this signature in database
2 changed files with 43 additions and 5 deletions

View File

@ -734,6 +734,20 @@ AND auth_key_id NOT IN (
},
Rollback: func(db *gorm.DB) error { return nil },
},
// Migrate preauthkey table to make users and tags optional.
// Use prefix+hash for keys.
{
ID: "202505231615-preauthkey-user-optional-tags-user",
Migrate: func(tx *gorm.DB) error {
err = tx.AutoMigrate(&types.PreAuthKey{})
if err != nil {
return fmt.Errorf("automigrating types.PreAuthKey: %w", err)
}
return nil
},
Rollback: func(db *gorm.DB) error { return nil },
},
},
)

View File

@ -9,19 +9,30 @@ import (
// PreAuthKey describes a pre-authorization key usable in a particular user.
type PreAuthKey struct {
ID uint64 `gorm:"primary_key"`
Key string
UserID uint
User User `gorm:"constraint:OnDelete:SET NULL;"`
ID uint64 `gorm:"primary_key"`
// Old Key, for backwards compatibility
Key string
// Encrypted key
Prefix string
Hash []byte
Reusable bool
Ephemeral bool `gorm:"default:false"`
Used bool `gorm:"default:false"`
// UserID if set, is the owner of the key.
// If a node is authenticated with this key, the node
// is assigned to this user.
UserID *uint `sql:"DEFAULT:NULL"`
User *User
// Tags are always applied to the node and is one of
// the sources of tags a node might have. They are copied
// from the PreAuthKey when the node logs in the first time,
// and ignored after.
Tags []string `gorm:"serializer:json"`
Tags []string `gorm:"column:tags;serializer:json"`
CreatedAt *time.Time
Expiration *time.Time
@ -48,3 +59,16 @@ func (key *PreAuthKey) Proto() *v1.PreAuthKey {
return &protoKey
}
// IsTagged reports if a key is tagged.
func (key *PreAuthKey) IsTagged() bool {
if key.Tags == nil {
return false
}
if len(key.Tags) > 0 {
return true
}
return false
}