Add serviceclientprefs, an optional feature that stores and loads the desktop clients' saved service launch preferences, one file per login profile. - Add GET|POST /localapi/v0/prefs/service-clients to load and save the current profile's service client prefs. - Add local client GetServiceClientPrefs and SetServiceClientPref that call the new local api endpoint. - Store the prefs with the ipn/store FileStore at TailscaleVarRoot()/profile-data/<profileID>/service-client-prefs/<hex-encoded-key>, so DeleteProfile cleans them up for free. Fall back to an in-memory store when there's no var root. - Register the feature and its local api route from build tagged files so the whole thing drops out under ts_omit_serviceclientprefs. - Add the serviceclient package holding Pref and Prefs (saved client, username, database name, and last used time), so the local api client and desktop apps can import the types without the feature machinery. Change-Id: I340a99c1b332d181fb1556fbf3e8003bb3b95a08 Updates: https://github.com/tailscale/tailscale/issues/20429 Signed-off-by: Rollie Ma <rollie@tailscale.com>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package serviceclientprefs
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"tailscale.com/client/tailscale/apitype"
|
|
"tailscale.com/ipn"
|
|
"tailscale.com/tsd"
|
|
"tailscale.com/tstest"
|
|
"tailscale.com/tstime"
|
|
"tailscale.com/types/logger"
|
|
)
|
|
|
|
// fakeBackend is a minimal [ipnext.SafeBackend] for tests. Only Clock and TailscaleVarRoot are
|
|
// used by the extension; Sys is never called.
|
|
type fakeBackend struct {
|
|
clock tstime.Clock
|
|
varRoot string
|
|
}
|
|
|
|
func (f *fakeBackend) Sys() *tsd.System { return nil }
|
|
func (f *fakeBackend) Clock() tstime.Clock { return f.clock }
|
|
func (f *fakeBackend) TailscaleVarRoot() string { return f.varRoot }
|
|
|
|
// newTestExtension returns an extension backed by a temp var root and a fixed test clock, with its
|
|
// current profile set to pid.
|
|
func newTestExtension(t *testing.T, pid ipn.ProfileID) *extension {
|
|
t.Helper()
|
|
e := &extension{
|
|
logf: logger.Discard,
|
|
sb: &fakeBackend{
|
|
clock: tstest.NewClock(tstest.ClockOpts{Start: time.Now()}),
|
|
varRoot: t.TempDir(),
|
|
},
|
|
}
|
|
e.onChangeProfile((&ipn.LoginProfile{ID: pid}).View(), ipn.PrefsView{}, false)
|
|
return e
|
|
}
|
|
|
|
func TestNoVarRootUsesMemory(t *testing.T) {
|
|
e := &extension{
|
|
logf: logger.Discard,
|
|
sb: &fakeBackend{
|
|
clock: tstest.NewClock(tstest.ClockOpts{Start: time.Now()}),
|
|
varRoot: "", // force the in-memory fallback
|
|
},
|
|
}
|
|
e.onChangeProfile((&ipn.LoginProfile{ID: "pid"}).View(), ipn.PrefsView{}, false)
|
|
|
|
if _, err := e.setServiceClientPref(apitype.ServiceClientPrefRequest{Key: "ssh:22", Client: "terminal"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := e.serviceClientPrefs()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got["ssh:22"].Client != "terminal" {
|
|
t.Errorf("in-memory store: got %v, want Client=terminal", got["ssh:22"])
|
|
}
|
|
}
|