Skip to content

Commit

Permalink
fix preauth key issue logging in as wrong user
Browse files Browse the repository at this point in the history
Fixes juanfont#1885

Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed May 1, 2024
1 parent 2582f7f commit d16a9a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions hscontrol/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &registerRequest.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
}
Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d16a9a0

Please sign in to comment.