Skip to content

[ISSUE #94] Support Bearer token authentication via --token - #95

Merged
KomachiSion merged 2 commits into
nacos-group:mainfrom
saynoword:codex/add-bearer-token-auth
Jul 15, 2026
Merged

KomachiSion merged 2 commits into
nacos-group:mainfrom
saynoword:codex/add-bearer-token-auth

Conversation

@saynoword

@saynoword saynoword commented Jul 15, 2026 •

Copy link
Copy Markdown
Collaborator

Please do not create a Pull Request without creating an issue first.

Fixes #94

What is the purpose of the change

Allow users who already have a Nacos access token to authenticate CLI requests directly with the standard Authorization: Bearer <token> header, without requiring another username/password login. Token authentication can be supplied for one command or stored securely in a reusable profile.

Brief changelog

  • Add a global --token option to the root command.
  • Add a token authentication mode to the shared Nacos client.
  • Apply the Bearer header to both Resty and standard HTTP request paths.
  • Support auth-type=token with an encrypted token field in profiles.
  • Mask profile Token values and preserve command-line-over-profile precedence.
  • Add regression tests for CLI forwarding, Authorization header injection, profile encryption, masking, and precedence.

Verifying this change

  • GOCACHE=/private/tmp/nacos-cli-go-build go vet ./...
  • GOCACHE=/private/tmp/nacos-cli-go-build go test ./...
  • Log in to a local Nacos server, then run go run . --host 127.0.0.1 --port 8848 --token <token> skill-list --size 1 --output json.
  • Create a temporary profile with go run . profile set dev host=127.0.0.1 port=8848 auth-type=token token=<token>, confirm profile get dev token prints ******, and run go run . --profile dev skill-list --size 1 --output json.

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check spotbugs:check -DskipTests to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@saynoword
saynoword marked this pull request as ready for review July 15, 2026 03:14
@KomachiSion

Copy link
Copy Markdown
Contributor

🚨 Critical: Token flag not checked in config loading logic

The hasCommandLineConfig check on line 62 determines whether the CLI should load from a profile or use command-line parameters. However, token is missing from this condition, which breaks the feature entirely.

Current code:

hasCommandLineConfig := host != "" || port > 0 || serverAddr != "" || 
    username != "" || password != "" || accessKey != "" || 
    secretKey != "" || securityToken != "" || 
    isCommandLineStsAuthType(authType) || scheme != ""

Problem:
When users run:

nacos-cli --host 127.0.0.1 --port 8848 --token my-bearer-token skill-list

The CLI treats this as "no command-line config" because token is not in the check. This causes:

  1. The profile loading logic to trigger unexpectedly
  2. Command-line token to lose precedence over profile tokens
  3. Potential interactive prompts when the user expected direct execution

Fix:
Add || token != "" to the condition:

hasCommandLineConfig := host != "" || port > 0 || serverAddr != "" || 
    username != "" || password != "" || accessKey != "" || 
    secretKey != "" || securityToken != "" || token != "" ||
    isCommandLineStsAuthType(authType) || scheme != ""

This aligns token with other credential flags and preserves the documented precedence: command line > profile > env vars > default.

@KomachiSion
KomachiSion merged commit cafbe73 into nacos-group:main Jul 15, 2026
10 checks passed
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.

Support Bearer token authentication via --token

2 participants