Paramveersingh-S opened a new pull request, #232:
URL: https://github.com/apache/cloudstack-cloudmonkey/pull/232

   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 
https://github.com/apache/cloudstack-cloudmonkey/pull/174#issuecomment-3178553661
 and 
https://github.com/apache/cloudstack-cloudmonkey/pull/174#issuecomment-3206143040):
   
   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
   
   ```text
   $ go test -mod=vendor -v ./cli ./cmd ./config
   ...
   === RUN   TestPartialCLICredentialsFailWithoutFallingBackToProfileLogin
   --- PASS: TestPartialCLICredentialsFailWithoutFallingBackToProfileLogin 
(0.00s)
   PASS
   ok   github.com/apache/cloudstack-cloudmonkey/cmd
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to