Skip to content

[infrastructure] Add unified admin CLI for self-hosted helpers#6507

Open
jnfrati wants to merge 7 commits into
mainfrom
feat/admin-cli
Open

[infrastructure] Add unified admin CLI for self-hosted helpers#6507
jnfrati wants to merge 7 commits into
mainfrom
feat/admin-cli

Conversation

@jnfrati

@jnfrati jnfrati commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a unified admin CLI for self-hosted instance administrators in both the management and combined binaries.

User Management

admin user change-password

  • Changes a local embedded IdP user's password.
  • Selects the user with --email or --user-id.
  • Reads the new password from --password or --password-file.
  • Clears the user's local authentication session so the new password is required on the next login.
  • Alias: admin user set-password.

admin user reset-mfa

  • Resets a local embedded IdP user's MFA enrollment.
  • Selects the user with --email or --user-id.
  • Clears TOTP/WebAuthn enrollment data and removes the local authentication session.
  • The user will re-enroll MFA on the next login.

MFA Management

admin mfa status

  • Shows whether local MFA is enabled in the account settings.
  • Checks the embedded IdP client configuration and reports whether MFA is enabled there.

admin mfa enable

  • Enables local MFA for embedded IdP users.
  • Updates embedded IdP clients and saves the account MFA setting.
  • Records an audit event on a best-effort basis.

admin mfa disable

  • Disables local MFA for embedded IdP users.
  • Updates embedded IdP clients and saves the account MFA setting.
  • Records an audit event on a best-effort basis.

Reverse Proxy Tokens

admin token create --name <name> [--expires-in <duration>]

  • Creates a reverse proxy access token.
  • Prints the plaintext token once, along with the token ID.
  • --expires-in supports values such as 24h, 30d, or 365d. If omitted, the token never expires.

admin token list

  • Lists reverse proxy access tokens.
  • Shows the token ID, name, creation date, expiration, last-used time, and revocation status.
  • Alias: admin token ls.

admin token revoke <token-id>

  • Revokes a reverse proxy access token.
  • Revoked tokens can no longer authenticate reverse proxy instances.

Reverse Proxy Management

admin proxy disconnect-all

  • Lists registered reverse proxy instances and force-marks all connected instances as disconnected.
  • Useful for repairing stale proxy state after an unclean management server shutdown.
  • Prompts for confirmation by default.
  • --dry-run previews the changes without applying them.
  • --force skips the confirmation prompt.
  • Live proxies may appear again after their next heartbeat, reconnect, or re-registration.

Compatibility Commands

token ...

  • Deprecated top-level compatibility path.
  • Behaves the same as admin token ....
  • Retained so existing scripts using token create, token list, or token revoke continue to work.

Changes

  • Adds reusable management/cmd/admin command package.
  • Wires admin into netbird-mgmt and combined.
  • Adds local user password reset with existing password strength validation.
  • Adds local MFA enrollment reset by clearing Dex TOTP/WebAuthn credentials and local auth sessions.
  • Adds local MFA enable/disable/status helpers for embedded IdP deployments.
  • Moves proxy access token commands under admin token for a single admin-focused CLI entry point.
  • Exports server.ValidatePassword for reuse by CLI helpers.

Tests

go test ./management/cmd/...
go test ./management/cmd/admin ./management/cmd ./combined/cmd
go test ./management/server -run TestValidatePassword

Pre-push lint also passed.

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • This change does not modify the public API, gRPC protocols, functionality behavior, CLI / service flags, or introduce a new feature — OR I have discussed it with the NetBird team beforehand (link the issue / Slack thread in the description). See CONTRIBUTING.md.

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

netbirdio/docs#832

Summary by CodeRabbit

Release Notes

  • New Features

    • Added self-hosted admin CLI commands for changing passwords, resetting MFA (including WebAuthn), and managing embedded IdP client MFA (enable/disable/status).
    • Introduced a unified admin command entry point and improved data-directory handling for embedded IdP storage.
  • Refactor

    • Centralized password strength validation into a shared exported validator.
  • Tests

    • Added a comprehensive admin command test suite covering password input, selectors, MFA reset, and client MFA state handling.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds admin command trees for management and combined CLIs, enabling embedded IdP password changes, MFA reset, and MFA enable/disable/status operations. It also exports password validation for reuse and replaces token command registration with admin command registration.

Changes

Admin CLI for Embedded IdP

Layer / File(s) Summary
Export ValidatePassword for reuse
management/server/user.go
validatePassword delegates to exported ValidatePassword, which now holds the password-strength checks.
Admin command types, Cobra tree, and handlers
management/cmd/admin/admin.go
Defines the admin command tree, resource opener types, embedded IDP storage opening, password input handling, password change, MFA reset, MFA enable/disable/status, user lookup, session deletion, and MFA client helpers.
Admin command tests
management/cmd/admin/admin_test.go
Adds in-memory storage setup and tests covering password change, MFA reset, MFA toggle/status, selector validation, lookup failures, and password input resolution.
Management cmd: admin wiring replaces token wiring
management/cmd/admin.go, management/cmd/root.go, management/cmd/token.go
Adds management admin wiring and store/bootstrap helpers, switches root registration to admin commands, and removes token command wiring.
Combined cmd: admin wiring replaces token wiring
combined/cmd/admin.go, combined/cmd/root.go, combined/cmd/token.go
Adds combined admin wiring and store/bootstrap helpers, switches root registration to admin commands, and removes token command wiring.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI as Cobra CLI
  participant Opener as Opener callback
  participant IDPStorage as Dex storage
  participant MgmtStore as management store

  CLI->>Opener: open Resources{Store, IDPStorage}
  Opener-->>CLI: Resources
  CLI->>IDPStorage: update password / reset MFA
  CLI->>MgmtStore: update account settings / MFA status
  CLI-->>CLI: print command result
Loading

Suggested reviewers: pascal-fischer

Poem

🐇 I hop through commands, both shiny and new,
With MFA toggles and passwords too.
The admin tree blossoms, the token paths rest,
And embedded IdP flows pass every test.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.78% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: a unified admin CLI for self-hosted helpers.
Description check ✅ Passed The description covers scope, commands, tests, and docs; only the issue ticket and stack sections are missing.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/admin-cli

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown

Release artifacts

Built for PR head f25011f in workflow run #16466.

Artifact Link
All release artifacts Download
Linux packages Download
Windows packages Download
macOS packages Download
UI artifacts Download
UI macOS artifacts Download

GHCR images (amd64)

This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy.

@jnfrati
jnfrati marked this pull request as ready for review June 22, 2026 15:52

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@combined/cmd/admin.go`:
- Around line 68-78: The `withAdminStore` function sets environment variables
for store configuration (NB_STORE_ENGINE_POSTGRES_DSN,
NB_STORE_ENGINE_MYSQL_DSN, NB_STORE_ENGINE_SQLITE_FILE) without restoring their
original values, causing them to leak and persist across subsequent command
invocations in the same process. Save the original values of these environment
variables before setting them, then use a defer statement to restore them when
the function returns. Apply the same fix to the identical code block in
combined/cmd/root.go.

In `@management/cmd/admin/admin.go`:
- Around line 297-307: The MFA toggle operation is not rollback-safe because
SaveAccountSettings commits the new settings to the store before
setIDPClientsMFA updates the Dex clients; if setIDPClientsMFA fails, the account
settings and Dex clients become inconsistent. Capture the previous settings
state before calling SaveAccountSettings, and if setIDPClientsMFA subsequently
fails, restore the previous settings by calling SaveAccountSettings again with
the original settings to rollback the change. This ensures that either both
operations succeed or neither does, maintaining consistency between the account
store and the Dex IDP clients.
- Around line 75-95: Remove the ability to pass the password via the
`--password` command-line flag in the passwordCmd command definition. Delete the
`password` variable declaration, remove the `passwordCmd.Flags().StringVar` line
that registers the password flag, and update the command's Use string to remove
the `--password password` option. Modify the `resolvePasswordInput` function
call to only accept the `passwordFile` parameter instead of both password and
passwordFile. Apply the same changes to the other password-related command
mentioned in the comment (around lines 179-198).
- Around line 362-365: The code block handling rawUserID assignment calls
DecodeDexUserID which returns the decodedUserID, connectorID, and error, but
currently ignores the connector ID by using a blank underscore. Capture the
connectorID return value instead of discarding it, and add an additional
condition to the if statement to check that connectorID equals "local" before
assigning decodedUserID to rawUserID, ensuring that decoded user IDs from
non-local connectors are rejected.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f4b82d21-509b-4c0f-95da-6aff1c995f78

📥 Commits

Reviewing files that changed from the base of the PR and between af3b7e4 and 520370a.

📒 Files selected for processing (9)
  • combined/cmd/admin.go
  • combined/cmd/root.go
  • combined/cmd/token.go
  • management/cmd/admin.go
  • management/cmd/admin/admin.go
  • management/cmd/admin/admin_test.go
  • management/cmd/root.go
  • management/cmd/token.go
  • management/server/user.go
💤 Files with no reviewable changes (2)
  • combined/cmd/token.go
  • management/cmd/token.go

Comment thread combined/cmd/admin.go Outdated
Comment thread management/cmd/admin/admin.go
Comment thread management/cmd/admin/admin.go Outdated
Comment thread management/cmd/admin/admin.go
@sonarqubecloud

sonarqubecloud Bot commented Jul 3, 2026

Copy link
Copy Markdown

@jnfrati jnfrati changed the title Add unified admin CLI for self-hosted helpers [infrastructure] Add unified admin CLI for self-hosted helpers Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants