{"record":{"id":"f96a3a7994170421","repo":"juanfont/headscale","slug":"creating-user-w-f96a3a","errorCode":null,"errorMessage":"creating user: %w","messagePattern":"creating user: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"integration/scenario.go","lineNumber":527,"sourceCode":"\tuser := &User{\n\t\tClients: make(map[string]TailscaleClient),\n\t}\n\ts.users[userStr] = user\n\n\treturn user\n}\n\n// CreatePreAuthKey creates a \"pre authentorised key\" to be created in the\n// Headscale instance on behalf of the [Scenario].\nfunc (s *Scenario) CreatePreAuthKey(\n\tuser uint64,\n\treusable bool,\n\tephemeral bool,\n) (*clientv1.PreAuthKey, error) {\n\tif headscale, err := s.Headscale(); err == nil { //nolint:noinlineerr\n\t\tkey, err := headscale.CreateAuthKey(user, reusable, ephemeral)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"creating user: %w\", err)\n\t\t}\n\n\t\treturn key, nil\n\t}\n\n\treturn nil, fmt.Errorf(\"creating user: %w\", errNoHeadscaleAvailable)\n}\n\n// CreatePreAuthKeyWithOptions creates a \"pre authorised key\" with the specified options\n// to be created in the Headscale instance on behalf of the [Scenario].\nfunc (s *Scenario) CreatePreAuthKeyWithOptions(opts hsic.AuthKeyOptions) (*clientv1.PreAuthKey, error) {\n\theadscale, err := s.Headscale()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"creating preauth key with options: %w\", errNoHeadscaleAvailable)\n\t}\n\n\tkey, err := headscale.CreateAuthKeyWithOptions(opts)\n\tif err != nil {","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/juanfont/headscale/blob/565fd254d06c4c7f9a8cad1714a43445c79ba420/integration/scenario.go#L509-L545","documentation":"Returned by Scenario.CreatePreAuthKey when the headscale control server's gRPC/API call to mint a pre-auth key fails, despite the server handle being available. The message (\"creating user\") is misleading — the failing operation is headscale.CreateAuthKey for the given user ID.","triggerScenarios":"Calling s.CreatePreAuthKey(user, reusable, ephemeral) when the gRPC CreatePreAuthKey call inside the container returns an error, most commonly because the user ID does not exist in headscale.","commonSituations":"Test passes a user ID before calling s.CreateUser(user), or uses an ID from a different scenario/run; the user was deleted by an earlier test step; API key/machine auth to the local gRPC socket is broken.","solutions":["Create the user first: s.CreateUser(\"...\"), then use the returned user ID for CreatePreAuthKey","Print the wrapped error — a 'user not found' style message from the gRPC layer confirms the ID problem","Verify the user still exists via headscale.ListUsers if earlier steps may have removed it"],"exampleFix":"// before\nu, _ := scenario.CreateUser(\"alice\")\nkey, err := scenario.CreatePreAuthKey(999, false, false)\n\n// after\nu, err := scenario.CreateUser(\"alice\")\nif err != nil {\n    t.Fatal(err)\n}\nkey, err := scenario.CreatePreAuthKey(u.Id, false, false)","handlingStrategy":"validation","validationCode":"u, err := scenario.CreateUser(name)\nif err != nil {\n    t.Fatal(err)\n}\n// only now is u.Id valid for key creation\n_, err = scenario.CreatePreAuthKey(u.Id, reusable, ephemeral)","typeGuard":null,"tryCatchPattern":"key, err := scenario.CreatePreAuthKey(u.Id, reusable, ephemeral)\nif err != nil {\n    t.Fatalf(\"preauth key creation failed (user %d exists?): %v\", u.Id, err)\n}","preventionTips":["Always create the user first and use the returned ID","Never hardcode user IDs across scenario restarts"],"tags":["preauth-key","grpc","integration-test","headscale"],"backgroundTag":null,"analyzedSha":"565fd254d06c4c7f9a8cad1714a43445c79ba420","analyzedAt":"2026-08-15T13:12:30.133Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}