Skip to content

feat(auth): loopback first-run setup for keyring credentials - #1434

Open
nbmaiti wants to merge 1 commit into
pr/standalone-password-changefrom
pr/standalone-first-run
Open

nbmaiti wants to merge 1 commit into
pr/standalone-password-changefrom
pr/standalone-first-run

Conversation

@nbmaiti

@nbmaiti nbmaiti commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces the terminal keyring prompt with a loopback first-run setup page. Stacked on #1433, so review only the top commit.

Scope

  • Setup token and listener: when AUTH_SECRET_BACKEND=keyring and both keyring entries are missing, Console creates a single-use setup token valid for 10 minutes. It then starts a separate HTTPS listener bound to 127.0.0.1 (random port) that serves:
    • GET /api/v1/auth/setup/status
    • POST /api/v1/auth/setup (token, username, password, confirmPassword)
    • GET /setup/redirect, which redirects to /setup#<token>
  • Local-only checks: a peer or Origin that is not loopback gets 403 SETUP_LOCAL_ONLY.
  • Responses: a bad or expired token gets 401, a weak password or mismatch 400, and an already configured keyring 409 SETUP_ALREADY_COMPLETED.
  • Token handling: the token lives only in process memory. It is never written to a file, log, process arguments or the tray lock file. cmd/app creates one authsetup.Manager and the loopback listener; app.Run serves them. Restarting Console issues a new token.
  • Credential writes (authsetup.Manager.Bootstrap):
    • the token check is constant-time;
    • setup runs under a lock and re-checks that the keyring is still empty before writing;
    • it writes the username and a PBKDF2 hash, and removes the username if the hash write fails.
  • Browser and tray: Console opens the token-free /setup/redirect URL, and the page clears the fragment with history.replaceState. Setup does not create a login session.
  • Headless (noui):
    • On a terminal: Console prints Headless setup URL (...): https://localhost:<port>/setup#<token> for manual copy and paste.
    • Output redirected (log file, pipe or journal): Console prints a token-free hint instead. An operator on the same host can then get the token with:
      curl -sk -o /dev/null -w '%{redirect_url}\n' https://localhost:<setup-port>/setup/redirect
    • Tray mode: Console stays in the foreground while setup is pending, so the token is never written to the background log.
  • Failure handling: a partially populated keyring still fails closed at startup.
  • Dependencies: golang.org/x/term is removed.

Unchanged

  • Config backend, AUTH_DISABLED, OIDC: no setup listener and no token.
  • Existing keyring installs with valid entries start normally without setup.

Testing notes

Prerequisites: a noui build of this branch, AUTH_SECRET_BACKEND=keyring, and an unlocked keyring with no device-management-toolkit entries:

secret-tool clear service device-management-toolkit username admin-username
secret-tool clear service device-management-toolkit username admin-password-hash

1. Get the setup URL.

  • Terminal: start Console in a terminal; it prints Headless setup URL ...: https://localhost:<port>/setup#<token>.
  • Redirected output: start it with output redirected (./console > console.log 2>&1). The log shows only the hint, with no token. Run the curl ... %{redirect_url} command from the hint to get the same URL.

2. Set up the shell. SETUP is the URL's origin and TOKEN is the part after #:

read -r -p 'Setup base (https://localhost:<port>): ' SETUP
read -r -s -p 'Token: ' TOKEN; printf '\n'
read -r -p 'Admin username: ' U; read -r -s -p 'Password: ' P; printf '\n'

post() { jq -Rn '[inputs]|{token:.[0],username:.[1],password:.[2],confirmPassword:.[3]}' |
  curl -sk -w '\n%{http_code}\n' -X POST "$SETUP/api/v1/auth/setup" -H 'Content-Type: application/json' --data-binary @-; }

3. Run the checks:

# Step Expected
1 curl -sk "$SETUP/api/v1/auth/setup/status" {"setupRequired":true}
2 printf '%s\n' nope "$U" "$P" "$P" | post 401 INVALID_SETUP_TOKEN
3 printf '%s\n' "$TOKEN" "$U" weak weak | post 400 INVALID_CREDENTIALS
4 printf '%s\n' "$TOKEN" "$U" "$P" "$P" | post 201 {"status":"configured"}
5 Repeat step 4 409 SETUP_ALREADY_COMPLETED (single use)
6 Step 1 again {"setupRequired":false}
7 Log in on the main port (POST /api/v1/authorize) with the new credentials 200
8 secret-tool lookup service device-management-toolkit username admin-password-hash starts with $pbkdf2-sha256$
9 Restart Console no setup message and no setup listener; login works

Afterwards: unset TOKEN P.

4. Check that the token is not stored. In redirected mode, after stopping Console:

grep -rlF "<token>" console.log ~/.config/device-management-toolkit/ /tmp   # no matches

5. Check the edge cases:

  • Non-loopback: a request to the setup port from another host can't connect (bound to 127.0.0.1). A loopback request with Origin: https://evil.example gets 403 SETUP_LOCAL_ONLY.
  • Partial keyring: delete only admin-password-hash and restart; Console exits with the partial-credentials error.
  • Recovery: stop Console, delete both entries, restart, and complete setup again.
  • Other modes: with AUTH_SECRET_BACKEND=config, there is no setup listener and no setup log line.
  • With the UI (non-noui build): the browser opens /setup/redirect, lands on /setup, and the token disappears from the address bar.

Unit tests:

go test -race -count=1 ./internal/authsetup ./internal/app ./cmd/app ./internal/controller/httpapi/... ./pkg/httpserver

Validation

  • gofumpt -extra, go vet ./..., go test -race -count=1 ./... and golangci-lint with the CI config all pass. The tray and tray,noui builds compile.
  • E2E against a real gnome-keyring passed for three cases: terminal output, redirected output using the curl redirect_url hint, and the config backend. The keyring runs continued through password change (feat(auth): keyring standalone password change #1433) and a restart.

Known limitations

  • Any local OS user can reach /setup/redirect during the 10-minute window; access is limited to loopback.
  • The browser and tray launch paths are covered by unit tests but were not exercised end to end.

Related: #1356

@nbmaiti
nbmaiti requested a review from a team as a code owner November 25, 2026 04:18
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.99526% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.65%. Comparing base (e149975) to head (a217ff4).

Files with missing lines Patch % Lines
cmd/app/main.go 75.75% 8 Missing ⚠️
internal/app/app.go 63.15% 7 Missing ⚠️
internal/authsetup/manager.go 95.34% 2 Missing ⚠️
internal/authsetup/handler.go 98.73% 1 Missing ⚠️
pkg/httpserver/server.go 88.88% 1 Missing ⚠️
Additional details and impacted files
@@                        Coverage Diff                        @@
##           pr/standalone-password-change    #1434      +/-   ##
=================================================================
+ Coverage                          64.16%   64.65%   +0.48%     
=================================================================
  Files                                164      166       +2     
  Lines                              13812    13924     +112     
=================================================================
+ Hits                                8863     9003     +140     
+ Misses                              4949     4921      -28     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nbmaiti
nbmaiti marked this pull request as draft November 25, 2026 04:25
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from e0c4b08 to d003178 Compare November 25, 2026 04:49
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from a2c42f1 to e4c70bd Compare November 25, 2026 04:55
@nbmaiti nbmaiti changed the title feat(auth): standalone first-run setup listener feat(auth): standalone first-run setup infrastructure Nov 25, 2026
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from e4c70bd to 686306c Compare November 25, 2026 06:36
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from c740e74 to f7f1d05 Compare November 25, 2026 07:02
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from 686306c to 127e52a Compare November 25, 2026 07:02
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from f7f1d05 to 6696226 Compare November 25, 2026 07:09
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from 127e52a to 50380a6 Compare November 25, 2026 07:09
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from 6696226 to 5a0df73 Compare November 25, 2026 07:12
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch 4 times, most recently from 433f0c1 to 2fcc7aa Compare November 25, 2026 12:53
@nbmaiti nbmaiti changed the title feat(auth): standalone first-run setup infrastructure feat(auth): standalone first-run setup API Nov 25, 2026
@nbmaiti
nbmaiti requested a balanced review from Copilot November 25, 2026 13:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unsafe file replacement can expose the administrator-bootstrap token, while a bootstrap race can overwrite concurrent password changes and leave login credentials inconsistent with the keyring.

6 open issues
What changed in this PR

Replaces terminal-based administrator setup with a loopback-only API for standalone keyring deployments, building on the authenticated password-change work.

Changes:

  • Adds setup status and provisioning endpoints with a one-time token, credential validation, and keyring write verification.
  • Integrates setup into startup and shares credential locking with login and password changes.
  • Adds setup tests and OpenAPI declarations, and removes the terminal-input dependency.
File Description
internal/​entity/​dto/​v1/​auth_setup.go Defines setup request and response types.
internal/​controller/​openapi/​tenant_test.go Exempts public setup routes from tenant headers.
internal/​controller/​openapi/​devices.go Declares setup endpoints in OpenAPI.
internal/​controller/​openapi/​devices_test.go Checks setup endpoint registration.
internal/​controller/​httpapi/​v1/​login.go Reuses shared credential locking and keyring constants.
internal/​controller/​httpapi/​v1/​login_test.go Updates password-change tests for shared types.
internal/​authsetup/​manager.go Implements token lifecycle and credential provisioning.
internal/​authsetup/​manager_test.go Tests provisioning, validation, rollback, and HTTP behavior.
internal/​authsetup/​handler.go Exposes loopback-only setup handlers.
internal/​app/​app.go Registers setup routes on the normal listener.
go.sum Removes terminal dependency checksums.
go.mod Removes the direct terminal dependency.
config/​config.go Derives the setup-token file path.
cmd/​app/​secret_store.go Starts API-based setup instead of terminal prompts.
cmd/​app/​main_test.go Replaces prompt tests with setup-service coverage.

Review effort: Balanced

Comment thread internal/authsetup/manager.go Resolved
Comment thread internal/authsetup/manager.go Resolved
Comment thread cmd/app/secret_store.go Resolved
Comment thread internal/app/app.go Resolved
Comment thread internal/controller/openapi/devices.go Resolved
Comment thread internal/controller/openapi/devices.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmd/app/main.go Resolved
Comment thread cmd/app/secret_store.go Resolved
Comment thread internal/authsetup/manager.go Resolved
Comment thread internal/authsetup/manager.go Resolved
Comment thread internal/authsetup/manager_test.go Resolved
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from 1e409b8 to 03c0a73 Compare November 26, 2026 13:21
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from e2acd49 to 321afb7 Compare November 26, 2026 13:21
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from 03c0a73 to 30689a7 Compare November 26, 2026 13:29
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch 2 times, most recently from db50ffc to 9669e35 Compare November 27, 2026 09:17
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from 30689a7 to c03b687 Compare November 27, 2026 09:17
@nbmaiti nbmaiti changed the title feat(auth): standalone first-run setup API feat(auth): loopback first-run setup for keyring credentials Nov 27, 2026
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from c03b687 to 0dcbdc4 Compare November 27, 2026 10:04
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from 9669e35 to e4b442e Compare November 27, 2026 10:04
@nbmaiti
nbmaiti force-pushed the pr/standalone-password-change branch from 0dcbdc4 to e149975 Compare November 27, 2026 11:44
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from e4b442e to b2be741 Compare November 27, 2026 11:44
Replace the terminal keyring prompt with a first-run setup page:

- With AUTH_SECRET_BACKEND=keyring and no keyring entries, Console
  issues a one-time setup token (10-minute lifetime) and serves
  GET /api/v1/auth/setup/status and POST /api/v1/auth/setup on a
  separate HTTPS listener bound to 127.0.0.1; non-loopback peers or
  origins get 403 SETUP_LOCAL_ONLY. Setup stores the username and a
  PBKDF2 hash in the keyring and is single-use.
- The token lives only in process memory and is never written to any
  file or log.
- The browser and the tray "Open" action open the token-free
  /setup/redirect URL; that loopback-only endpoint redirects to
  /setup#<token>, so the token never appears in process arguments.
- Headless (no embedded UI) startup on a terminal prints the setup URL
  with the token for copy/paste. When output is redirected it prints a
  token-free curl hint for /setup/redirect instead. Tray mode stays in
  the foreground while setup is pending.
- Config backend, AUTH_DISABLED, and OIDC are unchanged.
- Partial keyring state still fails closed. golang.org/x/term removed.

Related: #1356

Signed-off-by: Nabendu Maiti <nabendu.bikash.maiti@intel.com>
@nbmaiti
nbmaiti force-pushed the pr/standalone-first-run branch from b2be741 to a217ff4 Compare November 27, 2026 11:56
@nbmaiti
nbmaiti requested a review from sudhir-intc November 27, 2026 12:45
@nbmaiti
nbmaiti marked this pull request as ready for review November 27, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants