diff --git a/control/controlclient/auto.go b/control/controlclient/auto.go index a27e9e2fa..8fef4fff0 100644 --- a/control/controlclient/auto.go +++ b/control/controlclient/auto.go @@ -237,6 +237,9 @@ func (c *Auto) sendNewMapRequest() { }() } +// cancelAuth ensures that the auth routine, that might be currently sleeping, +// wakes up and checks for a new login/logout request that needs to be +// fulfilled. func (c *Auto) cancelAuth() { c.mu.Lock() if c.authCancel != nil { diff --git a/control/controlclient/direct.go b/control/controlclient/direct.go index ffc0467f7..2b716ebc3 100644 --- a/control/controlclient/direct.go +++ b/control/controlclient/direct.go @@ -446,6 +446,17 @@ func (c *Direct) doLogin(ctx context.Context, opt loginOpt) (mustRegen bool, new regen := opt.Regen if opt.Logout { c.logf("logging out...") + // check if logout from control server is a no-op early so that we don't + // call control server unnecessarily + if persist.PrivateNodeKey.IsZero() { + // This must is an attempt to log out a node that is not currently + // logged in. We are not erroring out here to ensure that a user who + // hits logout twice will not be faced with errors as well as so + // that logout command can be used to cancel failed login attempts + // (that would otherwise be retried in a loop) + c.logf("no nodeky found, skipping logging out from control server") + return false, "", nil, nil + } } else { if expired { c.logf("Old key expired -> regen=true") @@ -504,9 +515,6 @@ func (c *Direct) doLogin(ctx context.Context, opt loginOpt) (mustRegen bool, new nlPub := persist.NetworkLockKey.Public() if tryingNewKey.IsZero() { - if opt.Logout { - return false, "", nil, errors.New("no nodekey to log out") - } log.Fatalf("tryingNewKey is empty, give up") } diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index bc67e1ccb..b27f26158 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -3881,6 +3881,14 @@ func (b *LocalBackend) Logout() { b.logout(context.Background(), false) } +// Logout tells the controlclient that we want to log out, and transitions the +// local engine to the logged-out state. It also waits for one auth routine +// attempt to log out from control plane. +// After local engine has been transitioned and control client successfully logged out (auth routine): +// - local backend state should be NeedsLogin +// - auth routine should be sleeping and waiting for a login/logout attempt +// - client login goal should be set to nil +// - node key should be removed from control server and local state func (b *LocalBackend) LogoutSync(ctx context.Context) error { return b.logout(ctx, true) }