{"record":{"id":"d9b158a3892f03b4","repo":"juanfont/headscale","slug":"creating-pre-auth-key-w","errorCode":null,"errorMessage":"creating pre-auth key: %w","messagePattern":"creating pre-auth key: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/dev/main.go","lineNumber":184,"sourceCode":"\t\treturn fmt.Errorf(\"creating user: %w\", err)\n\t}\n\n\tuserID, err := extractUserID(userJSON)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"parsing user: %w\", err)\n\t}\n\n\t// Create pre-auth key.\n\tkeyJSON, err := runHS(\n\t\tctx, hsBin, configPath,\n\t\t\"preauthkeys\", \"create\",\n\t\t\"-u\", strconv.FormatUint(userID, 10),\n\t\t\"--reusable\",\n\t\t\"-e\", \"24h\",\n\t\t\"-o\", \"json\",\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"creating pre-auth key: %w\", err)\n\t}\n\n\tauthKey, err := extractAuthKey(keyJSON)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"parsing pre-auth key: %w\", err)\n\t}\n\n\t// Print banner.\n\tfmt.Printf(\n\t\t`\n=== Headscale Dev Environment ===\n  Server:  http://127.0.0.1:%d\n  Metrics: http://127.0.0.1:%d\n  Debug:   http://127.0.0.1:%d/debug/ping\n  Config:  %s\n  State:   %s\n\nPre-auth key: %s","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/juanfont/headscale/blob/565fd254d06c4c7f9a8cad1714a43445c79ba420/cmd/dev/main.go#L166-L202","documentation":"\"creating pre-auth key: %w\" at cmd/dev/main.go:184 wraps runHS(ctx, hsBin, configPath, \"preauthkeys\", \"create\", \"-u\", <userID>, \"--reusable\", \"-e\", \"24h\", \"-o\", \"json\") in cmd/dev. After creating the 'dev' user, the tool provisions a reusable pre-auth key valid for 24h. The wrapped error is a non-zero exit of that CLI invocation — the CLI's own stderr names the cause (bad user id, expiration rejected, DB failure).","triggerScenarios":"The userID extracted in the previous step not matching a DB row (e.g. user created in a different DB file because the state dir changed between calls); the CLI rejecting '-e 24h' or '--reusable' due to flag changes; ctx cancelled mid-run.","commonSituations":"Version skew between the compiled dev tool and the current CLI flags; state directory mutated between the users-create and preauthkeys-create steps.","solutions":["Read the CLI error text above the wrapped message and fix the named flag/argument","Ensure the whole run uses one scratch dir (don't reuse state across runs with -keep)","Re-run `go run ./cmd/dev` from the current source so tool and CLI stay in sync"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"keyJSON, err := runHS(ctx, hsBin, configPath, \"preauthkeys\", \"create\", ...)\nif err != nil {\n\t// CLI stderr above names the refused flag/arg; ctx.Err() means interrupted\n\tif ctx.Err() != nil {\n\t\treturn nil\n\t}\n\treturn fmt.Errorf(\"creating pre-auth key: %w\", err)\n}","preventionTips":["Keep tool and CLI in one source tree to avoid flag drift","Use a single scratch state dir per run"],"tags":["dev-tooling","cli","preauthkey","provisioning"],"backgroundTag":null,"analyzedSha":"565fd254d06c4c7f9a8cad1714a43445c79ba420","analyzedAt":"2026-08-15T13:12:30.133Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}