api/v1: return 409 when a unique index refuses a write

Rename to a taken user name always returned 500, and so did every
concurrent create that passed the pre-check and lost the insert.
This commit is contained in:
Kristoffer Dalby committed 2026-11-20 21:22:18 +01:00
1 parent 1cf3b4796d
commit 5676e91629
3 files changed
+50 -1

No files matched your search

+1
View File
@@ -283,6 +283,7 @@ the `tailscale.com/cap/tailsql` app capability lets a user query it; see
- The v1 API stores an API key or pre-auth key created without `expiration` as one that never expires, and reports its expiration as `null`. It was stored as already expired and rejected on first use. The CLI always sends an expiration and was not affected. Keys created this way before stay expired; create new ones [#3254](https://github.com/juanfont/headscale/pull/3254)
- The v2 API accepts an auth key create that leaves out `tags`, as Tailscale does [#3254](https://github.com/juanfont/headscale/pull/3254)
- The v2 API refuses an auth key `expirySeconds` below 0 or above 90 days, as Tailscale does; a negative value used to mean 90 days [#3254](https://github.com/juanfont/headscale/pull/3254)
- The v1 API returns `409 Conflict`, not `500`, when a user create or rename asks for a name another user already has; a rename to a taken name always returned `500`
## 0.29.7 (2026-10-30)
+4 -1
View File
@@ -55,7 +55,10 @@ func mapError(msg string, err error) error {
errors.Is(err, state.ErrInvalidPolicy):
return huma.Error400BadRequest(msg, err)
case errors.Is(err, state.ErrNodeKeyInUse),
// A unique index refused the write, e.g. a racing create or a rename to a
// taken user name.
case errors.Is(err, gorm.ErrDuplicatedKey),
errors.Is(err, state.ErrNodeKeyInUse),
errors.Is(err, state.ErrAmbiguousNodeOwnership),
errors.Is(err, state.ErrAuthCacheEntryExists):
return huma.Error409Conflict(msg, err)
+45
View File
@@ -3,6 +3,7 @@ package hscontrol
import (
"encoding/json"
"net/http"
"sync"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
@@ -51,9 +52,53 @@ func TestAPIV1CreateUser(t *testing.T) {
[]byte(`{"name":"dup"}`))
assertStatus(t, res, http.StatusConflict)
})
// Concurrent creates all pass the handler's pre-check; the losers must
// still see a conflict when the unique index refuses their insert.
t.Run("concurrent duplicate name conflicts", func(t *testing.T) {
h := newAPIV1Harness(t)
const n = 16
var wg sync.WaitGroup
start := make(chan struct{})
statuses := make(chan int, n)
for range n {
wg.Go(func() {
<-start
statuses <- h.callHuma(http.MethodPost, "/api/v1/user", []byte(`{"name":"race"}`)).status
})
}
close(start)
wg.Wait()
close(statuses)
counts := map[int]int{}
for s := range statuses {
counts[s]++
}
assert.Equal(t, map[int]int{http.StatusOK: 1, http.StatusConflict: n - 1}, counts)
})
}
func TestAPIV1RenameUser(t *testing.T) {
t.Run("duplicate name conflicts", func(t *testing.T) {
h := newAPIV1Harness(t)
seedUsers("alice", "bob")(t, h.app)
res := h.callHuma(http.MethodPost, "/api/v1/user/2/rename/alice", nil)
assertStatus(t, res, http.StatusConflict)
got, err := h.app.state.GetUserByID(2)
require.NoError(t, err)
assert.Equal(t, "bob", got.Name)
})
t.Run("parity", func(t *testing.T) {
assertParityIsolated(t, seedUsers("alice"), http.MethodPost,
"/api/v1/user/1/rename/bob", nil)