Fix: POST /api/user-mappings/bulk returns 404 — Holistic Root Cause & Fix
Context
The CLI import-s3 --dry-run command successfully authenticates and downloads from S3, but POST /api/user-mappings/bulk returns 404. The previous workaround added a CLI-side fallback (already implemented), but the real root cause is that the route is never registered by Micronaut — the backend log shows zero trace of the request (no security filter, no controller log), meaning the router rejects it before it enters the pipeline.
Root Cause: Stale KSP Incremental Processing Cache
Micronaut registers routes at compile time via KSP (Kotlin Symbol Processing), not at runtime. When @Post("/bulk") was added to UserMappingController.kt, the incremental KSP processor did not regenerate the route definitions:
build.gradle.kts has incremental(true) enabled for KSP
The method exists in compiled .class bytecode, but the KSP-generated $UserMappingController$Definition.class (which Micronaut reads for route registration) is stale and doesn't include /bulk
Result: Micronaut's router has no entry for POST /api/user-mappings/bulk → returns 404 at the router level, before any filter or controller code executes → no log trace
This explains why the backend shows GET /api/user-mappings working (that route was in the original KSP output) but POST /api/user-mappings/bulk is invisible.
Fix (3 parts)
1. Backend: Force KSP regeneration (fixes the root cause)
bash
./gradlew clean build
This deletes build/generated/ksp/ and build/kspCaches/, forcing full reprocessing. The $UserMappingController$Definition.class will be regenerated with the /bulk route included.
No code changes needed — the controller, DTO, service, and repository code is already correct.
2. Backend: Add route debug logging for future diagnosis
Add temporary startup route logging in application.yml to verify all routes are registered:
Expected after fix: Backend log should show Bulk create user mappings: entries=..., dryRun=true (the controller's INFO log at line 162). The CLI should NOT show the "Bulk endpoint not available (404), falling back" warning.
If you need specific details from before exiting plan mode (like exact code snippets, error messages, or content you generated), read the full transcript at: /Users/flake/.claude/projects/-Users-flake-sources-misc-secman/b56afea6-b61b-474b-8949-f757f1d20088.jsonl
Assistant
Let me read the application.yml to find the logger levels section, and start the clean build in parallel.
Assistant
Build succeeded. Now let me add the router debug logging.
Assistant
★ Insight ─────────────────────────────────────Why clean build fixes this: Micronaut uses compile-time annotation processing (KSP for Kotlin) to generate route metadata classes like $UserMappingController$Definition.class. With incremental KSP enabled, Gradle may skip reprocessing a file if it thinks nothing changed — but KSP's incremental tracking can miss newly added @Post methods. clean deletes build/generated/ksp/ and build/kspCaches/, forcing full reprocessing.
Why the router debug log helps: io.micronaut.web.router: DEBUG causes Micronaut to log every registered route at startup. This makes it trivial to grep for /bulk in the output and confirm the route was actually registered — turning an invisible 404 into a diagnosable issue.
─────────────────────────────────────────────────
Done. Here's what was applied:
./gradlew clean build — completed successfully (1m 49s). This deleted and regenerated all KSP output, so the $UserMappingController$Definition.class now includes the /bulk route.
application.yml — added io.micronaut.web.router: DEBUG at line 331. On next startup, all registered routes will be logged, letting you verify /bulk is present.
CLI fallback — no changes needed; the existing fallback in UserMappingCliService.kt (lines 577-689) stays as backward-compatibility insurance.
To verify, start the backend and look for a log line like:
Let me look at an existing test script to match the project's conventions.
Assistant
Let me check the bulk endpoint's request/response format to get the test payloads right.
Assistant
Now I have everything I need. Let me create the test script following the project's conventions.
Assistant
★ Insight ─────────────────────────────────────Response parsing pattern: The script uses curl -w "\n%{http_code}" to append the HTTP status code as the last line of output, then splits body/code with tail -1 and sed '$d'. This avoids needing -o temp files and keeps everything in-memory — a common pattern in the project's existing test scripts.
Dry-run as a diagnostic: The dry-run test (Step 2) specifically checks for HTTP 404 and prints a targeted error message pointing at the KSP cache issue. This makes the script useful both as a regression test and as a quick diagnostic if the route goes missing again after an incremental build.
Cleanup via trap: trap cleanup EXIT ensures test mappings are deleted even if the script fails mid-run, preventing test data from accumulating in the target environment.
─────────────────────────────────────────────────
Created tests/bulk-user-mapping-test.sh. It follows the project's existing test conventions (1Password credential resolution, colored logging, DEBUG=1 mode, trap-based cleanup) and runs 5 tests:
Auth — login to get JWT
Dry-run — POST /api/user-mappings/bulk with dryRun: true (checks for 404 specifically)
Actual create — persists 2 test mappings (domain + AWS account)
Verify — confirms mappings exist via GET
Validation — sends empty email to test error handling
Cleanup — deletes test mappings on exit
Run it with:
bash
# Against production (default)
./tests/bulk-user-mapping-test.sh
# Against localhost
SECMAN_BASE_URL=http://localhost:8080 ./tests/bulk-user-mapping-test.sh
# Verbose
DEBUG=1 ./tests/bulk-user-mapping-test.sh
User
something is wrong with the testscript as it not even reaches the web server logfile
User
[Image: image/png]
User
[Image: source: /var/folders/k7/jz4z13354rlbg2yhm0vw9g2w0000gn/T/TemporaryItems/NSIRD_screencaptureui_PEZl6o/Screenshot 2026-03-16 at 20.45.34.png]
Assistant
The problem is set -euo pipefail — when curl fails (likely an SSL certificate error against https://secman.covestro.net), the script exits immediately before it can print the error. The request never reaches the server because curl rejects the certificate locally.
Let me fix the authenticate and api_call functions to handle this properly.
Assistant
The issue is clear: set -euo pipefail + curl failing with an SSL certificate error = silent death. When curl returns non-zero (e.g., exit code 60 for untrusted cert), set -e kills the script before your error-handling code runs. The trap fires cleanup, which prints "No auth token" — and that's all you see.
Two fixes needed: catch curl failures, and pass -k for self-signed/internal certs (the CLI itself uses --insecure for the same server).
Assistant
Now update the usage comment to document the new SECMAN_INSECURE variable:
Assistant
★ Insight ─────────────────────────────────────Why it failed silently: set -e (errexit) terminates the shell the moment any command returns non-zero — including curl. Since curl's SSL failure (exit code 60) happened inside a $() subshell assignment, the script died before reaching the if [[ "$http_code" != "200" ]] check. The fix wraps curl with || curl_exit=$?, which captures the exit code without triggering set -e, so the error-handling code actually runs.
-k is safe here: The -k flag skips certificate verification, which is appropriate for internal/self-signed certs. The default is INSECURE=true since secman.covestro.net uses an internal cert (matching the CLI's --insecure flag).
─────────────────────────────────────────────────
Fixed two things:
Curl error capture — all curl calls now use || curl_exit=$? so failures don't trigger set -e silently. The actual error (SSL, connection refused, etc.) is now printed.
SSL -k flag — SECMAN_INSECURE=true (default) adds -k to all curl calls, matching the CLI's --insecure behavior for the internal cert.
Try again:
bash
DEBUG=1 ./tests/bulk-user-mapping-test.sh
User
propose a fix for this
User
[Image: image/png]
User
[Image: source: /var/folders/k7/jz4z13354rlbg2yhm0vw9g2w0000gn/T/TemporaryItems/NSIRD_screencaptureui_1L2W8I/Screenshot 2026-03-16 at 20.49.26.png]
Assistant
This is a different task — the test script's token extraction is wrong. Let me explore how the auth endpoint actually returns the JWT.
Assistant
Now I understand the issue. The JWT is in a Set-Cookie: secman_auth=<token> header, not in the JSON body. The script tries to parse .token from JSON — which doesn't exist.
Assistant
Clear picture. Let me write the plan.
Assistant
★ Insight ─────────────────────────────────────Cookie-based vs token-based auth: This project uses HttpOnly cookies for JWT transport — the browser never sees the token in JavaScript (XSS protection). The test script assumed a token-in-body pattern (common in SPAs), but secman follows a more secure cookie-based approach where AuthCookieService wraps the JWT in Set-Cookie: secman_auth=<jwt>.
Two ways to send the token back: The backend accepts both Authorization: Bearer <token> headers and secman_auth cookies. The script can use either — but it first needs to capture the cookie from the login response headers.
─────────────────────────────────────────────────
Assistant
The fix is straightforward — change authenticate() to capture the Set-Cookie header instead of parsing the JSON body for a token that doesn't exist there.