diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 5069c5958..f97b6d2d5 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -316,7 +316,8 @@ type LocalBackend struct { // for testing and graceful shutdown purposes. goTracker goroutines.Tracker - startOnce sync.Once // protects the one‑time initialization in [LocalBackend.Start] + startOnce sync.Once // protects the one‑time initialization in [LocalBackend.Start] + beginLoginMu sync.Mutex // serializes beginLoginAs calls; acquire before b.mu // extHost is the bridge between [LocalBackend] and the registered [ipnext.Extension]s. // It may be nil in tests that use direct composite literal initialization of [LocalBackend] diff --git a/ipn/ipnlocal/login.go b/ipn/ipnlocal/login.go new file mode 100644 index 000000000..557c914e8 --- /dev/null +++ b/ipn/ipnlocal/login.go @@ -0,0 +1,64 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package ipnlocal + +import ( + "context" + + "tailscale.com/ipn" + "tailscale.com/ipn/ipnauth" +) + +// beginLoginOpts describes the optional configuration for an interactive login. +// prefs contains edits to the current profile. +type beginLoginOpts struct { + prefs *ipn.MaskedPrefs + authKey string + controlURL string +} + +// beginLoginAs initiates interactive login, reusing the existing control client +// when its configuration is already suitable. In particular, it does not set +// WantRunning through EditPrefs before calling Start, since doing this could start a +// login on a control client that Start would immediately replace. +// +// This method blocks until login has been initiated, but does not wait for authentication +// to complete. The Caller must not hold b.mu or b.beginLoginMu. +// +// Profile creation and selection are the caller's responsibility. The OS VPN +// tunnel must also be started separately by platform clients. +func (b *LocalBackend) beginLoginAs(ctx context.Context, actor ipnauth.Actor, opts beginLoginOpts) error { + b.beginLoginMu.Lock() + defer b.beginLoginMu.Unlock() + + if err := ctx.Err(); err != nil { + return err + } + + b.mu.Lock() + current := b.pm.CurrentPrefs() + prefs := current.AsStruct() + if opts.prefs != nil { + prefs.ApplyEdits(opts.prefs) + } + if opts.controlURL != "" { + prefs.ControlURL = opts.controlURL + } + prefs.WantRunning = true + if opts.authKey != "" { + prefs.LoggedOut = false + } + // Start recreates the control client and clears any pending auth URL. + // Keep the current client (and pending auth flow) when no changes are needed., + // unless an explicit auth key or control URL was provided. + needsStart := b.cc == nil || opts.authKey != "" || opts.controlURL != "" || !prefs.View().Equals(current) + b.mu.Unlock() + + if needsStart { + if err := b.Start(ipn.Options{UpdatePrefs: prefs, AuthKey: opts.authKey}); err != nil { + return err + } + } + return b.StartLoginInteractiveAs(ctx, actor) +} diff --git a/ipn/ipnlocal/login_test.go b/ipn/ipnlocal/login_test.go new file mode 100644 index 000000000..b22ca9afe --- /dev/null +++ b/ipn/ipnlocal/login_test.go @@ -0,0 +1,119 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package ipnlocal + +import ( + "context" + "testing" + + "tailscale.com/control/controlclient" + "tailscale.com/ipn" + "tailscale.com/ipn/ipnauth" +) + +// TestBeginLoginReusesAuthURL verifies that a repeated login request does not +// replace an existing control client or discard an available auth URL. +func TestBeginLoginReusesAuthURL(t *testing.T) { + lb := newLocalBackendWithTestControl(t, false, func(tb testing.TB, opts controlclient.Options) controlclient.Client { + return newClient(tb, opts) + }) + if err := lb.Start(ipn.Options{UpdatePrefs: &ipn.Prefs{WantRunning: true}}); err != nil { + t.Fatal(err) + } + const loginURL = "https://login.tailscale.com/a/test-reuse" + lb.mu.Lock() + originalClient := lb.cc + lb.setAuthURLLocked(loginURL) + lb.mu.Unlock() + + watcher := newNotificationWatcher(t, lb, nil) + watcher.watch(0, []wantedNotification{{ + name: "reused authentication URL", + cond: func(_ testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool { + return n.BrowseToURL != nil && *n.BrowseToURL == loginURL + }, + }}) + if err := lb.beginLoginAs(context.Background(), nil, beginLoginOpts{}); err != nil { + t.Fatal(err) + } + watcher.check() + lb.mu.Lock() + defer lb.mu.Unlock() + if lb.cc != originalClient { + t.Error("login replaced the existing control client") + } + if lb.authURL != loginURL { + t.Errorf("authURL = %q, want %q", lb.authURL, loginURL) + } +} + +// TestBeginLoginPendingWithoutURL verifies that a second login request doesn't +// reset the control client merely because the auth URL hasn't arrived yet. +func TestBeginLoginPendingWithoutURL(t *testing.T) { + lb := newLocalBackendWithTestControl(t, false, func(tb testing.TB, opts controlclient.Options) controlclient.Client { + return newClient(tb, opts) + }) + if err := lb.beginLoginAs(context.Background(), nil, beginLoginOpts{}); err != nil { + t.Fatal(err) + } + lb.mu.Lock() + originalClient := lb.cc + lb.mu.Unlock() + if originalClient == nil { + t.Fatal("beginLoginAs did not start a control client") + } + if err := lb.beginLoginAs(context.Background(), nil, beginLoginOpts{}); err != nil { + t.Fatal(err) + } + lb.mu.Lock() + defer lb.mu.Unlock() + if lb.cc != originalClient { + t.Error("second login replaced the in-flight control client") + } +} + +// TestBeginLoginWithControlURL verifies that masked configuration is passed to +// Start instead of triggering an EditPrefs WantRunning transition first. +func TestBeginLoginWithControlURL(t *testing.T) { + lb := newLocalBackendWithTestControl(t, false, func(tb testing.TB, opts controlclient.Options) controlclient.Client { + return newClient(tb, opts) + }) + const controlURL = "https://control.example.test" + if err := lb.beginLoginAs(context.Background(), nil, beginLoginOpts{ + prefs: &ipn.MaskedPrefs{ + Prefs: ipn.Prefs{ControlURL: controlURL}, + ControlURLSet: true, + }, + }); err != nil { + t.Fatal(err) + } + prefs := lb.Prefs() + if !prefs.WantRunning() { + t.Error("WantRunning = false, want true") + } + if got := prefs.ControlURL(); got != controlURL { + t.Errorf("ControlURL = %q, want %q", got, controlURL) + } +} + +// TestBeginLoginAuthKey verifies that an auth-key login clears LoggedOut and +// enables WantRunning as part of the Start preferences. +func TestBeginLoginAuthKey(t *testing.T) { + lb := newLocalBackendWithTestControl(t, false, func(tb testing.TB, opts controlclient.Options) controlclient.Client { + return newClient(tb, opts) + }) + lb.ForTest().SetPrefs(&ipn.Prefs{LoggedOut: true}) + if err := lb.beginLoginAs(context.Background(), nil, beginLoginOpts{ + authKey: "tskey-auth-test", + }); err != nil { + t.Fatal(err) + } + prefs := lb.Prefs() + if !prefs.WantRunning() { + t.Error("WantRunning = false, want true") + } + if prefs.LoggedOut() { + t.Error("LoggedOut = true, want false") + } +}