From 536d1c15fc7762a9ecb79aa7fb3e247a098777d9 Mon Sep 17 00:00:00 2001 From: Paramveersingh-S Date: Mon, 23 Nov 2026 16:59:19 +0530 Subject: [PATCH] access: apply CLI -u/-k/-s overrides after -p profile load and isolate supplied credentials Fixes #188 --- cli/completer.go | 2 +- cli/exec.go | 2 +- cmd/network.go | 8 +++++++- cmd/network_test.go | 23 +++++++++++++++++++++++ cmk.go | 22 +++++++++++----------- config/config.go | 26 +++++++++++++++----------- 6 files changed, 58 insertions(+), 25 deletions(-) diff --git a/cli/completer.go b/cli/completer.go index 9547f5a..ff6f9b7 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -474,7 +474,7 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) } spinner := t.Config.StartSpinner("fetching options, please wait...") - request := cmd.NewRequest(nil, completer.Config, nil, false) + request := cmd.NewRequest(nil, completer.Config, nil, completer.Config.CredentialsSupplied) response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false) t.Config.StopSpinner(spinner) diff --git a/cli/exec.go b/cli/exec.go index 1b4a0d4..53c3f16 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -48,7 +48,7 @@ func ExecLine(line string) error { } } - return ExecCmd(args, false) + return ExecCmd(args, cfg != nil && cfg.CredentialsSupplied) } // ExecCmd executes a single provided command diff --git a/cmd/network.go b/cmd/network.go index 4ef6ce3..d9d2e72 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -492,7 +492,13 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str return nil, err } return processAPIResponse(r, response, isAsync) - } else if len(r.Config.ActiveProfile.Username) > 0 && len(r.Config.ActiveProfile.Password) > 0 { + } else if len(r.Config.ActiveProfile.APIKey) > 0 || len(r.Config.ActiveProfile.SecretKey) > 0 { + if r.CredentialsSupplied { + return nil, errors.New("both apikey and secretkey must be provided when authenticating with keys") + } + } + + if len(r.Config.ActiveProfile.Username) > 0 && len(r.Config.ActiveProfile.Password) > 0 && !r.CredentialsSupplied { usingSessionAuth = true sessionKey, err := Login(r) if err != nil { diff --git a/cmd/network_test.go b/cmd/network_test.go index 6333dab..104b31d 100644 --- a/cmd/network_test.go +++ b/cmd/network_test.go @@ -253,3 +253,26 @@ func TestAutoDoesNotRetryUserCommandDirectly(t *testing.T) { t.Fatalf("deployVirtualMachine attempt count = %d, want 1", userCommandAttempts) } } + +func TestPartialCLICredentialsFailWithoutFallingBackToProfileLogin(t *testing.T) { + server := newSignatureTestServer(t, map[string]bool{ + config.SignatureAlgorithmHmacSHA512: true, + }) + request := newTestRequest(t, server.server.URL, config.SignatureAlgorithmAuto) + request.Config.ActiveProfile.APIKey = testAPIKey + request.Config.ActiveProfile.SecretKey = "" + request.Config.ActiveProfile.Username = "admin" + request.Config.ActiveProfile.Password = "password" + request.CredentialsSupplied = true + + _, err := NewAPIRequest(request, "listZones", nil, false) + if err == nil { + t.Fatal("expected partial CLI credentials (apikey without secretkey) to fail") + } + if !strings.Contains(err.Error(), "both apikey and secretkey must be provided") { + t.Fatalf("unexpected error: %v", err) + } + if len(server.snapshotAttempts()) != 0 { + t.Fatalf("expected 0 server requests, got %+v", server.snapshotAttempts()) + } +} diff --git a/cmk.go b/cmk.go index 2869d89..9e8d66c 100644 --- a/cmk.go +++ b/cmk.go @@ -98,27 +98,27 @@ func main() { cfg.UpdateConfig("output", *outputFormat, false) } - if *acsURL != config.DefaultACSAPIEndpoint { - cfg.UpdateConfig("url", *acsURL, false) - } - - if *apiKey != "" { - cfg.UpdateConfig("apikey", *apiKey, false) + if *profile != "" { + cfg.LoadProfile(*profile, true) } - if *secretKey != "" { - cfg.UpdateConfig("secretkey", *secretKey, false) + if *acsURL != config.DefaultACSAPIEndpoint { + cfg.UpdateConfig("url", *acsURL, false) } - if *profile != "" { - cfg.LoadProfile(*profile, true) + if *apiKey != "" || *secretKey != "" { + cfg.CredentialsSupplied = true + cfg.ActiveProfile.Username = "" + cfg.ActiveProfile.Password = "" + cfg.ActiveProfile.APIKey = *apiKey + cfg.ActiveProfile.SecretKey = *secretKey } config.LoadCache(cfg) cli.SetConfig(cfg) config.Debug("cmdline args:", strings.Join(os.Args, ", ")) if len(args) > 0 { - if err := cli.ExecCmd(args, (*apiKey != "" || *secretKey != "")); err != nil { + if err := cli.ExecCmd(args, cfg.CredentialsSupplied); err != nil { fmt.Println("🙈 Error:", err) os.Exit(1) } diff --git a/config/config.go b/config/config.go index 1ec319b..9904955 100644 --- a/config/config.go +++ b/config/config.go @@ -110,17 +110,18 @@ type Core struct { // Config describes CLI config file and default options type Config struct { - Dir string - ConfigFile string - HistoryFile string - LogFile string - HasShell bool - Core *Core - ActiveProfile *ServerProfile - Context *context.Context - Cancel context.CancelFunc - C chan bool - activeSpinners []*spinner.Spinner + Dir string + ConfigFile string + HistoryFile string + LogFile string + HasShell bool + CredentialsSupplied bool + Core *Core + ActiveProfile *ServerProfile + Context *context.Context + Cancel context.CancelFunc + C chan bool + activeSpinners []*spinner.Spinner } // GetOutputFormats returns the supported output formats. @@ -457,12 +458,15 @@ func (c *Config) UpdateConfig(key string, value string, update bool) { case "profile": c.Core.ProfileName = value c.ActiveProfile = nil + c.CredentialsSupplied = false case "url": c.ActiveProfile.URL = value case "username": c.ActiveProfile.Username = value + c.CredentialsSupplied = false case "password": c.ActiveProfile.Password = value + c.CredentialsSupplied = false case "domain": c.ActiveProfile.Domain = value case "apikey":