Skip to content

Commit

Permalink
enforce uniqness with username and provider id
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
  • Loading branch information
kradalby committed Oct 21, 2024
1 parent efa697c commit a06fd49
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 17 additions & 0 deletions hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ func NewHeadscaleDatabase(
Rollback: func(db *gorm.DB) error { return nil },
},
{
// Pick up new user fields used for OIDC and to
// populate the user with more interesting information.
ID: "202407191627",
Migrate: func(tx *gorm.DB) error {
err := tx.AutoMigrate(&types.User{})
Expand All @@ -485,6 +487,21 @@ func NewHeadscaleDatabase(
},
Rollback: func(db *gorm.DB) error { return nil },
},
{
// The unique constraint of Name has been dropped
// in favour of a unique together of name and
// provider identity.
ID: "202408181235",
Migrate: func(tx *gorm.DB) error {
err := tx.AutoMigrate(&types.User{})
if err != nil {
return err
}

return nil
},
Rollback: func(db *gorm.DB) error { return nil },
},
},
)

Expand Down
9 changes: 6 additions & 3 deletions hscontrol/types/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ type UserID uint64
// that contain our machines.
type User struct {
gorm.Model
// The index `idx_name_provider_identifier` is to enforce uniqueness
// between Name and ProviderIdentifier. This ensures that
// you can have multiple usersnames of the same name in OIDC,
// but not if you only run with CLI users.

// Username for the user, is used if email is empty
// Should not be used, please use Username().
// TODO(kradalby): Figure out how do deal with uniqueness.
Name string `gorm:"index"`
Name string `gorm:"index,uniqueIndex:idx_name_provider_identifier"`

// Typically the full name of the user
DisplayName string
Expand All @@ -35,7 +38,7 @@ type User struct {
// Unique identifier of the user from OIDC,
// comes from `sub` claim in the OIDC token
// and is used to lookup the user.
ProviderIdentifier string `gorm:"index"`
ProviderIdentifier string `gorm:"index,uniqueIndex:idx_name_provider_identifier"`

// Provider is the origin of the user account,
// same as RegistrationMethod, without authkey.
Expand Down

0 comments on commit a06fd49

Please sign in to comment.