control/controlclient,ipn/ipn: ensures that logout succeeds when no nodekey is present

Ensures that if a user runs the logout command when not logged in, the command
does not fail.
Ensures that if logout is ran whilst the backend is in a non-logged-in state
_and_ does not have a node key, the backend and the control client will be brought
to a logged out state
(for example, a continuous loop over a failed auth attempt will
now be cancelled when logout command is ran)

Updates #3833

Signed-off-by: Irbe Krumina <[email protected]>
This commit is contained in:
Irbe Krumina committed 2023-08-01 20:36:01 +01:00
1 parent ec9213a627
commit e43a6cbc29
3 files changed
+22 -3

No files matched your search

+3
View File
@@ -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 {
+11 -3
View File
@@ -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")
}
+8
View File
@@ -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)
}