From d16a9a0c2600625a4ba94b49c9607f31e42d7a8b Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 1 May 2024 14:37:49 +0200 Subject: [PATCH] fix preauth key issue logging in as wrong user Fixes #1885 Signed-off-by: Kristoffer Dalby --- hscontrol/auth.go | 9 ++++++--- integration/cli_test.go | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/hscontrol/auth.go b/hscontrol/auth.go index 0679d72ee3..dab9ff4271 100644 --- a/hscontrol/auth.go +++ b/hscontrol/auth.go @@ -315,13 +315,16 @@ func (h *Headscale) handleAuthKey( node.NodeKey = nodeKey node.AuthKeyID = uint(pak.ID) - err := h.db.NodeSetExpiry(node.ID, registerRequest.Expiry) + node.Expiry = ®isterRequest.Expiry + node.User = pak.User + node.UserID = pak.UserID + err := h.db.DB.Save(node).Error if err != nil { log.Error(). Caller(). Str("node", node.Hostname). Err(err). - Msg("Failed to refresh node") + Msg("failed to save node after logging in with auth key") return } @@ -344,7 +347,7 @@ func (h *Headscale) handleAuthKey( } ctx := types.NotifyCtx(context.Background(), "handle-authkey", "na") - h.nodeNotifier.NotifyWithIgnore(ctx, types.StateUpdateExpire(node.ID, registerRequest.Expiry), node.ID) + h.nodeNotifier.NotifyAll(ctx, types.StateUpdate{Type: types.StatePeerChanged, ChangeNodes: []types.NodeID{node.ID}}) } else { now := time.Now().UTC() diff --git a/integration/cli_test.go b/integration/cli_test.go index 9fa4544f8e..57edf58ef0 100644 --- a/integration/cli_test.go +++ b/integration/cli_test.go @@ -461,8 +461,8 @@ func TestPreAuthKeyCorrectUserLoggedInCommand(t *testing.T) { t.Fatalf("expected node to be logged in, backend state: %s", status.BackendState) } - if status.Self.UserID.String() != "2" { - t.Fatalf("expected node to be logged in as user2, got: %s", status.Self.UserID.String()) + if status.Self.UserID.String() != "userid:2" { + t.Fatalf("expected node to be logged in as userid:2, got: %s", status.Self.UserID.String()) } var listNodes []v1.Node