Skip to content

Commit

Permalink
verify that user IDs are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoyt committed Jun 18, 2024
1 parent 79088f5 commit cc22e97
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions internals/overlord/state/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"strings"
)

// TODO: disallow multiple users with same "local: user-id" value?

// Identity holds the configuration of a single identity.
type Identity struct {
Name string
Expand Down Expand Up @@ -130,6 +128,10 @@ func (s *State) AddIdentities(identities map[string]*Identity) error {
sort.Strings(existing)
return fmt.Errorf("identities already exist: %s", strings.Join(existing, ", "))
}
err := verifyUniqueUserIDs(s.identities, identities)
if err != nil {
return nil
}

for name, identity := range identities {
identity.Name = name
Expand Down Expand Up @@ -158,6 +160,10 @@ func (s *State) UpdateIdentities(identities map[string]*Identity) error {
sort.Strings(missing)
return fmt.Errorf("identities missing: %s", strings.Join(missing, ", "))
}
err := verifyUniqueUserIDs(s.identities, identities)
if err != nil {
return nil
}

for name, identity := range identities {
identity.Name = name
Expand All @@ -180,6 +186,10 @@ func (s *State) ReplaceIdentities(identities map[string]*Identity) error {
}
}
}
err := verifyUniqueUserIDs(s.identities, identities)
if err != nil {
return nil
}

for name, identity := range identities {
if identity == nil {
Expand Down Expand Up @@ -226,3 +236,28 @@ func (s *State) Identities() map[string]*Identity {
}
return result
}

func verifyUniqueUserIDs(existing map[string]*Identity, new map[string]*Identity) error {
existingNamesByUserID := make(map[uint32]string)
for name, identity := range existing {
switch {
case identity.Local != nil:
existingNamesByUserID[identity.Local.UserID] = name
}
}
for name, identity := range new {
if identity == nil {
continue // removing identity (for ReplaceIdentities only)
}
switch {
case identity.Local != nil:
existingName, ok := existingNamesByUserID[identity.Local.UserID]
if ok && name != existingName {
return fmt.Errorf("identity %q and %q cannot both have user ID %d",
name, existingName, identity.Local.UserID)
}
existingNamesByUserID[identity.Local.UserID] = name
}
}
return nil
}

0 comments on commit cc22e97

Please sign in to comment.