Return better web errors to the user (#2398)

* add dedicated http error to propagate to user

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

* classify user errors in http handlers

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

* move validation of pre auth key out of db

This move separates the logic a bit and allow us to
write specific errors for the caller, in this case the web
layer so we can present the user with the correct error
codes without bleeding web stuff into a generic validate.

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

* update changelog

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

---------

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2025-02-01 15:25:18 +01:00 committed by GitHub
parent 1c7f3bc440
commit 45752db0f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 268 additions and 229 deletions

View file

@ -39,19 +39,19 @@ func (h *Headscale) ApplePlatformConfig(
vars := mux.Vars(req)
platform, ok := vars["platform"]
if !ok {
httpError(writer, nil, "No platform specified", http.StatusBadRequest)
httpError(writer, NewHTTPError(http.StatusBadRequest, "no platform specified", nil))
return
}
id, err := uuid.NewV4()
if err != nil {
httpError(writer, nil, "Failed to create UUID", http.StatusInternalServerError)
httpError(writer, err)
return
}
contentID, err := uuid.NewV4()
if err != nil {
httpError(writer, nil, "Failed to create UUID", http.StatusInternalServerError)
httpError(writer, err)
return
}
@ -65,21 +65,21 @@ func (h *Headscale) ApplePlatformConfig(
switch platform {
case "macos-standalone":
if err := macosStandaloneTemplate.Execute(&payload, platformConfig); err != nil {
httpError(writer, err, "Could not render Apple macOS template", http.StatusInternalServerError)
httpError(writer, err)
return
}
case "macos-app-store":
if err := macosAppStoreTemplate.Execute(&payload, platformConfig); err != nil {
httpError(writer, err, "Could not render Apple macOS template", http.StatusInternalServerError)
httpError(writer, err)
return
}
case "ios":
if err := iosTemplate.Execute(&payload, platformConfig); err != nil {
httpError(writer, err, "Could not render Apple iOS template", http.StatusInternalServerError)
httpError(writer, err)
return
}
default:
httpError(writer, err, "Invalid platform. Only ios, macos-app-store and macos-standalone are supported", http.StatusInternalServerError)
httpError(writer, NewHTTPError(http.StatusBadRequest, "platform must be ios, macos-app-store or macos-standalone", nil))
return
}
@ -91,7 +91,7 @@ func (h *Headscale) ApplePlatformConfig(
var content bytes.Buffer
if err := commonTemplate.Execute(&content, config); err != nil {
httpError(writer, err, "Could not render platform iOS template", http.StatusInternalServerError)
httpError(writer, err)
return
}