mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
ipn/ipnlocal: add shared login bootstrap orchestration (#21857)
Apple and Android both implement their own login orchestration, coordinating preference updates, backend startup, and interative login. This requires the clients to understand details about the backend lifecycle and has led to duplicated logic and ordering issues. Android needs to avoid setting WantRunning=true via EditPrefs before calling Start, because doing so triggers Login on the existing control client, which Start then tears down, potentially canceling an in-flight registration before an authentication URL is returned (see https://github.com/tailscale/tailscale-android/commit/83057865e13b8f116a0871a3c6d6adf46f5c3d32). Apple has similar orchestration, including separate handling for auth keys, custom control servers, and reauth. This adds a LocalBackend helper that accepts optional masked prefs and an auth key, prepares the desired startup prefs, and initiates or resumes auth. This avoids unnecessary control client resets, preserves the existing control client and auth url when the login configuration hasn't changed, and ensures WantRunning is applied as part of Start when restarting the backend. This can be used for: -first time login with a custom control url or auth key -resuming an existing interactive login without losing auth url -retrying login without unnecessarily resetting the control client -reauth without restarting the backend A followup will expose this through localapi. Updates tailscale/tailscale#18304 Signed-off-by: kari-ts <kari@tailscale.com>
This commit is contained in:
1 parent
c38365bcff
commit
211f737bb4
3 files changed
+185
-1
No files matched your search
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user