ipn/*: make new WindowsUserID type to consolidate docs

The "userID is empty everywhere but Windows" docs on lots of places
but not everywhere while using just a string type was getting
confusing. This makes a new type to wrap up those rules, however
weird/historical they might be.

Change-Id: I142e85a8e38760988d6c0c91d0efecedade81b9b
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-11-27 09:04:00 -08:00
committed by Brad Fitzpatrick
parent 3b73727e39
commit 8049053f86
5 changed files with 42 additions and 24 deletions

View File

@@ -2076,12 +2076,12 @@ func (b *LocalBackend) CheckIPNConnectionAllowed(ci *ipnauth.ConnIdentity) error
if !b.pm.CurrentPrefs().ForceDaemon() {
return nil
}
uid := ci.UserID()
uid := ci.WindowsUserID()
if uid == "" {
return errors.New("empty user uid in connection identity")
}
if uid != serverModeUid {
return fmt.Errorf("Tailscale running in server mode (%q); connection from %q not allowed", b.tryLookupUserName(serverModeUid), b.tryLookupUserName(uid))
return fmt.Errorf("Tailscale running in server mode (%q); connection from %q not allowed", b.tryLookupUserName(string(serverModeUid)), b.tryLookupUserName(string(uid)))
}
return nil
}
@@ -2257,7 +2257,7 @@ func (b *LocalBackend) shouldUploadServices() bool {
// changed.
//
// On non-multi-user systems, the uid should be set to empty string.
func (b *LocalBackend) SetCurrentUserID(uid string) {
func (b *LocalBackend) SetCurrentUserID(uid ipn.WindowsUserID) {
b.mu.Lock()
if b.pm.CurrentUserID() == uid {
b.mu.Unlock()

View File

@@ -28,7 +28,7 @@ type profileManager struct {
store ipn.StateStore
logf logger.Logf
currentUserID string // only used on Windows
currentUserID ipn.WindowsUserID
knownProfiles map[ipn.ProfileID]*ipn.LoginProfile
currentProfile *ipn.LoginProfile // always non-nil
prefs ipn.PrefsView // always Valid.
@@ -42,13 +42,13 @@ type profileManager struct {
// CurrentUserID returns the current user ID. It is only non-empty on
// Windows where we have a multi-user system.
func (pm *profileManager) CurrentUserID() string {
func (pm *profileManager) CurrentUserID() ipn.WindowsUserID {
return pm.currentUserID
}
// SetCurrentUserID sets the current user ID. The uid is only non-empty
// on Windows where we have a multi-user system.
func (pm *profileManager) SetCurrentUserID(uid string) error {
func (pm *profileManager) SetCurrentUserID(uid ipn.WindowsUserID) error {
if pm.currentUserID == uid {
return nil
}
@@ -63,7 +63,7 @@ func (pm *profileManager) SetCurrentUserID(uid string) error {
// Read the CurrentProfileKey from the store which stores
// the selected profile for the current user.
b, err := pm.store.ReadState(ipn.CurrentProfileKey(uid))
b, err := pm.store.ReadState(ipn.CurrentProfileKey(string(uid)))
if err == ipn.ErrStateNotExist || len(b) == 0 {
pm.NewProfile()
return nil
@@ -310,7 +310,7 @@ func (pm *profileManager) SwitchProfile(id ipn.ProfileID) error {
}
func (pm *profileManager) setAsUserSelectedProfileLocked() error {
k := ipn.CurrentProfileKey(pm.currentUserID)
k := ipn.CurrentProfileKey(string(pm.currentUserID))
return pm.store.WriteState(k, []byte(pm.currentProfile.Key))
}
@@ -487,7 +487,7 @@ func newProfileManagerWithGOOS(store ipn.StateStore, logf logger.Logf, stateKey
}
if pm.currentProfile == nil {
if suf, ok := strs.CutPrefix(string(stateKey), "user-"); ok {
pm.currentUserID = suf
pm.currentUserID = ipn.WindowsUserID(suf)
}
pm.NewProfile()
} else {