ipn/ipnlocal: on fresh lazy-connecting install, start in state NeedsLogin

Fixes #1759

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-04-21 12:57:48 -07:00 committed by Brad Fitzpatrick
parent a29b0cf55f
commit 09891b9868
3 changed files with 55 additions and 0 deletions

View File

@ -597,6 +597,9 @@ func (c *Client) mapRoutine() {
} }
func (c *Client) AuthCantContinue() bool { func (c *Client) AuthCantContinue() bool {
if c == nil {
return true
}
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()

View File

@ -603,6 +603,7 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
return errors.New("no state key or prefs provided") return errors.New("no state key or prefs provided")
} }
defer b.stateMachine()
if opts.Prefs != nil { if opts.Prefs != nil {
b.logf("Start: %v", opts.Prefs.Pretty()) b.logf("Start: %v", opts.Prefs.Pretty())
} else { } else {

View File

@ -5,14 +5,20 @@
package ipnlocal package ipnlocal
import ( import (
"bytes"
"fmt"
"net/http"
"reflect" "reflect"
"sync"
"testing" "testing"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/ipn"
"tailscale.com/net/interfaces" "tailscale.com/net/interfaces"
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/netmap" "tailscale.com/types/netmap"
"tailscale.com/wgengine"
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
) )
@ -419,3 +425,48 @@ func TestPeerAPIBase(t *testing.T) {
}) })
} }
} }
type panicOnUseTransport struct{}
func (panicOnUseTransport) RoundTrip(*http.Request) (*http.Response, error) {
panic("unexpected HTTP request")
}
var nl = []byte("\n")
func TestStartsInNeedsLoginState(t *testing.T) {
var (
mu sync.Mutex
logBuf bytes.Buffer
)
logf := func(format string, a ...interface{}) {
mu.Lock()
defer mu.Unlock()
fmt.Fprintf(&logBuf, format, a...)
if !bytes.HasSuffix(logBuf.Bytes(), nl) {
logBuf.Write(nl)
}
}
store := new(ipn.MemoryStore)
eng, err := wgengine.NewFakeUserspaceEngine(logf, 0)
if err != nil {
t.Fatalf("NewFakeUserspaceEngine: %v", err)
}
lb, err := NewLocalBackend(logf, "logid", store, eng)
if err != nil {
t.Fatalf("NewLocalBackend: %v", err)
}
lb.SetHTTPTestClient(&http.Client{
Transport: panicOnUseTransport{}, // validate we don't send HTTP requests
})
if err := lb.Start(ipn.Options{
StateKey: ipn.GlobalDaemonStateKey,
}); err != nil {
t.Fatalf("Start: %v", err)
}
if st := lb.State(); st != ipn.NeedsLogin {
t.Errorf("State = %v; want NeedsLogin", st)
}
}