Skip to content

Commit 2504f0d

Browse files
committed
return regresp or err after waiting for registration
Signed-off-by: Kristoffer Dalby <[email protected]>
1 parent b4e4b79 commit 2504f0d

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

hscontrol/auth.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ func (h *Headscale) handleRegister(
4343
}
4444

4545
if regReq.Followup != "" {
46-
// TODO(kradalby): Does this need to return an error of some sort?
47-
// Maybe if the registration fails down the line it can be sent
48-
// on the channel and returned here?
49-
h.waitForFollowup(ctx, regReq)
46+
return h.waitForFollowup(ctx, regReq)
5047
}
5148

5249
if regReq.Auth != nil && regReq.Auth.AuthKey != "" {
@@ -111,42 +108,51 @@ func (h *Headscale) handleExistingNode(
111108
h.nodeNotifier.NotifyWithIgnore(ctx, types.UpdateExpire(node.ID, requestExpiry), node.ID)
112109
}
113110

111+
return nodeToRegisterResponse(node), nil
112+
}
113+
114+
func nodeToRegisterResponse(node *types.Node) *tailcfg.RegisterResponse {
114115
return &tailcfg.RegisterResponse{
115116
// TODO(kradalby): Only send for user-owned nodes
116117
// and not tagged nodes when tags is working.
117118
User: *node.User.TailscaleUser(),
118119
Login: *node.User.TailscaleLogin(),
119-
NodeKeyExpired: expired,
120+
NodeKeyExpired: node.IsExpired(),
120121

121122
// Headscale does not implement the concept of machine authorization
122123
// so we always return true here.
123124
// Revisit this if #2176 gets implemented.
124125
MachineAuthorized: true,
125-
}, nil
126+
}
126127
}
127128

128129
func (h *Headscale) waitForFollowup(
129130
ctx context.Context,
130131
regReq tailcfg.RegisterRequest,
131-
) {
132+
) (*tailcfg.RegisterResponse, error) {
132133
fu, err := url.Parse(regReq.Followup)
133134
if err != nil {
134-
return
135+
return nil, NewHTTPError(http.StatusUnauthorized, "invalid followup URL", err)
135136
}
136137

137138
followupReg, err := types.RegistrationIDFromString(strings.ReplaceAll(fu.Path, "/register/", ""))
138139
if err != nil {
139-
return
140+
return nil, NewHTTPError(http.StatusUnauthorized, "invalid registration ID", err)
140141
}
141142

142143
if reg, ok := h.registrationCache.Get(followupReg); ok {
143144
select {
144145
case <-ctx.Done():
145-
return
146-
case <-reg.Registered:
147-
return
146+
return nil, NewHTTPError(http.StatusUnauthorized, "registration timed out", err)
147+
case node := <-reg.Registered:
148+
if node == nil {
149+
return nil, NewHTTPError(http.StatusUnauthorized, "node not found", nil)
150+
}
151+
return nodeToRegisterResponse(node), nil
148152
}
149153
}
154+
155+
return nil, NewHTTPError(http.StatusNotFound, "followup registration not found", nil)
150156
}
151157

152158
// canUsePreAuthKey checks if a pre auth key can be used.
@@ -271,7 +277,7 @@ func (h *Headscale) handleRegisterInteractive(
271277
Hostinfo: regReq.Hostinfo,
272278
LastSeen: ptr.To(time.Now()),
273279
},
274-
Registered: make(chan struct{}),
280+
Registered: make(chan *types.Node),
275281
}
276282

277283
if !regReq.Expiry.IsZero() {

hscontrol/db/node.go

+5
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,12 @@ func (hsdb *HSDatabase) HandleNodeFromAuthPath(
372372
}
373373

374374
// Signal to waiting clients that the machine has been registered.
375+
select {
376+
case reg.Registered <- node:
377+
default:
378+
}
375379
close(reg.Registered)
380+
376381
newNode = true
377382
return node, err
378383
} else {

hscontrol/grpcv1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ func (api headscaleV1APIServer) DebugCreateNode(
838838

839839
Hostinfo: &hostinfo,
840840
},
841-
Registered: make(chan struct{}),
841+
Registered: make(chan *types.Node),
842842
}
843843

844844
log.Debug().

hscontrol/types/common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,5 @@ func (r RegistrationID) String() string {
194194

195195
type RegisterNode struct {
196196
Node Node
197-
Registered chan struct{}
197+
Registered chan *Node
198198
}

0 commit comments

Comments
 (0)