Skip to content

access: apply CLI -u/-k/-s overrides after -p profile load and isolate supplied credentials - #232

Open
Paramveersingh-S wants to merge 1 commit into
apache:mainfrom
Paramveersingh-S:fix/cli-profile-credential-isolation
Open

Paramveersingh-S wants to merge 1 commit into
apache:mainfrom
Paramveersingh-S:fix/cli-profile-credential-isolation

Conversation

@Paramveersingh-S

Copy link
Copy Markdown

Fixes #188

Problem

Following PR #174 (which addressed #168), three credential-handling bugs remained when combining -p <profile> with -k / -s / -u or when launching the interactive cmk shell with CLI credentials (as noted in #174 (comment) and #174 (comment)):

  1. -p <profile> overwrote -u, -k, and -s CLI flags (cmk.go:101-115):
    In main(), cfg.LoadProfile(*profile, true) ran after cfg.UpdateConfig("url", ...), cfg.UpdateConfig("apikey", ...), and cfg.UpdateConfig("secretkey", ...). Because LoadProfile replaces cfg.ActiveProfile with the profile section from .cmk/config, running cmk -p randy -k <key> -s <secret> discarded the CLI -k, -s, and -u flags and silently authenticated with randy's profile credentials.
  2. Interactive shell (ExecLine / completer) dropped credentialsSupplied:
    When launching an interactive shell with cmk -k <key> -s <secret>, ExecLine hardcoded ExecCmd(args, false) (cli/exec.go:51). Additionally, cmk.go left cfg.ActiveProfile.Username and Password intact from the loaded profile. If only -k was supplied or if the key failed in the interactive shell, cmk fell back to the profile's username/password.
  3. Partial CLI key (only -k or only -s) fell back to profile credentials:
    If a user passed only -k <key> (without -s), len(APIKey) > 0 && len(SecretKey) > 0 evaluated to false and NewAPIRequest fell through to Login(r) with the profile's username/password.

Solution

  1. cmk.go:
    • Load -p <profile> before applying -u, -k, and -s overrides so CLI flags and environment variables take precedence over the loaded profile.
    • When -k or -s is supplied on the command line (or via CMK_API_KEY / CMK_SECRET_KEY), set cfg.CredentialsSupplied = true, clear ActiveProfile.Username and ActiveProfile.Password in memory, and set both ActiveProfile.APIKey = *apiKey and ActiveProfile.SecretKey = *secretKey so stored profile credentials cannot leak into the session.
  2. config/config.go & cli/exec.go & cli/completer.go:
    • Store CredentialsSupplied on config.Config and propagate cfg.CredentialsSupplied in ExecLine and autoCompleter.Do. Reset CredentialsSupplied = false if the user explicitly switches profiles (set profile) or sets username/password in the shell.
  3. cmd/network.go & cmd/network_test.go:
    • Reject partial CLI key authentication (CredentialsSupplied && (len(APIKey) == 0 || len(SecretKey) == 0)) with a clear error ("both apikey and secretkey must be provided when authenticating with keys") instead of falling back to profile username/password login.
    • Add TestPartialCLICredentialsFailWithoutFallingBackToProfileLogin in cmd/network_test.go.

Verification / Solution Proof

$ go test -mod=vendor -v ./cli ./cmd ./config
...
=== RUN   TestPartialCLICredentialsFailWithoutFallingBackToProfileLogin
--- PASS: TestPartialCLICredentialsFailWithoutFallingBackToProfileLogin (0.00s)
PASS
ok  	github.com/apache/cloudstack-cloudmonkey/cmd

Copilot AI balanced review requested due to automatic review settings November 23, 2026 11:29

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Paramveersingh-S

Copy link
Copy Markdown
Author

cc @shwstppr @DaanHoogland @weizhouapache for review whenever you get a chance — fixes -p profile ordering with CLI credential flags and isolates supplied credentials per your comments in #188.

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.

Better handling of credentials change

2 participants