Skip to content

Commit 412c4c5

Browse files
soypeteSoypete
authored andcommitted
cmd/tailscale: make up respect explicitly empty --operator= value
Fixes tailscale#3808 Signed-off-by: soypete <miriah@tailscale.com>
1 parent c434e47 commit 412c4c5

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

cmd/tailscale/cli/cli_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,16 @@ func TestCheckForAccidentalSettingReverts(t *testing.T) {
493493
if err != nil {
494494
t.Fatal(err)
495495
}
496-
applyImplicitPrefs(newPrefs, tt.curPrefs, tt.curUser)
497-
var got string
498-
if err := checkForAccidentalSettingReverts(newPrefs, tt.curPrefs, upCheckEnv{
496+
upEnv := upCheckEnv{
499497
goos: goos,
500498
flagSet: flagSet,
501499
curExitNodeIP: tt.curExitNodeIP,
502500
distro: tt.distro,
503-
}); err != nil {
501+
user: tt.curUser,
502+
}
503+
applyImplicitPrefs(newPrefs, tt.curPrefs, upEnv)
504+
var got string
505+
if err := checkForAccidentalSettingReverts(newPrefs, tt.curPrefs, upEnv); err != nil {
504506
got = err.Error()
505507
}
506508
if strings.TrimSpace(got) != tt.want {
@@ -784,6 +786,10 @@ func TestUpdatePrefs(t *testing.T) {
784786
curPrefs *ipn.Prefs
785787
env upCheckEnv // empty goos means "linux"
786788

789+
// checkUpdatePrefsMutations, if non-nil, is run with the new prefs after
790+
// updatePrefs might've mutated them (from applyImplicitPrefs).
791+
checkUpdatePrefsMutations func(t *testing.T, newPrefs *ipn.Prefs)
792+
787793
wantSimpleUp bool
788794
wantJustEditMP *ipn.MaskedPrefs
789795
wantErrSubtr string
@@ -885,6 +891,28 @@ func TestUpdatePrefs(t *testing.T) {
885891
},
886892
env: upCheckEnv{backendState: "Running"},
887893
},
894+
{
895+
// Issue 3808: explicitly empty --operator= should clear value.
896+
name: "explicit_empty_operator",
897+
flags: []string{"--operator="},
898+
curPrefs: &ipn.Prefs{
899+
ControlURL: "https://login.tailscale.com",
900+
CorpDNS: true,
901+
AllowSingleHosts: true,
902+
NetfilterMode: preftype.NetfilterOn,
903+
OperatorUser: "somebody",
904+
},
905+
env: upCheckEnv{user: "somebody", backendState: "Running"},
906+
wantJustEditMP: &ipn.MaskedPrefs{
907+
OperatorUserSet: true,
908+
WantRunningSet: true,
909+
},
910+
checkUpdatePrefsMutations: func(t *testing.T, prefs *ipn.Prefs) {
911+
if prefs.OperatorUser != "" {
912+
t.Errorf("operator sent to backend should be empty; got %q", prefs.OperatorUser)
913+
}
914+
},
915+
},
888916
}
889917
for _, tt := range tests {
890918
t.Run(tt.name, func(t *testing.T) {
@@ -909,6 +937,9 @@ func TestUpdatePrefs(t *testing.T) {
909937
}
910938
t.Fatal(err)
911939
}
940+
if tt.checkUpdatePrefsMutations != nil {
941+
tt.checkUpdatePrefsMutations(t, newPrefs)
942+
}
912943
if simpleUp != tt.wantSimpleUp {
913944
t.Fatalf("simpleUp=%v, want %v", simpleUp, tt.wantSimpleUp)
914945
}

cmd/tailscale/cli/up.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func prefsFromUpArgs(upArgs upArgsT, warnf logger.Logf, st *ipnstate.Status, goo
362362
// without changing any settings.
363363
func updatePrefs(prefs, curPrefs *ipn.Prefs, env upCheckEnv) (simpleUp bool, justEditMP *ipn.MaskedPrefs, err error) {
364364
if !env.upArgs.reset {
365-
applyImplicitPrefs(prefs, curPrefs, env.user)
365+
applyImplicitPrefs(prefs, curPrefs, env)
366366

367367
if err := checkForAccidentalSettingReverts(prefs, curPrefs, env); err != nil {
368368
return false, nil, err
@@ -881,13 +881,20 @@ func checkForAccidentalSettingReverts(newPrefs, curPrefs *ipn.Prefs, env upCheck
881881
return errors.New(sb.String())
882882
}
883883

884-
// applyImplicitPrefs mutates prefs to add implicit preferences. Currently
885-
// this is just the operator user, which only needs to be set if it doesn't
884+
// applyImplicitPrefs mutates prefs to add implicit preferences for the user operator.
885+
// If the operator flag is passed no action is taken, otherwise this only needs to be set if it doesn't
886886
// match the current user.
887887
//
888888
// curUser is os.Getenv("USER"). It's pulled out for testability.
889-
func applyImplicitPrefs(prefs, oldPrefs *ipn.Prefs, curUser string) {
890-
if prefs.OperatorUser == "" && oldPrefs.OperatorUser == curUser {
889+
func applyImplicitPrefs(prefs, oldPrefs *ipn.Prefs, env upCheckEnv) {
890+
explicitOperator := false
891+
env.flagSet.Visit(func(f *flag.Flag) {
892+
if f.Name == "operator" {
893+
explicitOperator = true
894+
}
895+
})
896+
897+
if prefs.OperatorUser == "" && oldPrefs.OperatorUser == env.user && !explicitOperator {
891898
prefs.OperatorUser = oldPrefs.OperatorUser
892899
}
893900
}

0 commit comments

Comments
 (0)