Skip to content

Commit a6b815c

Browse files
committed
prevent fallback only when command-line credentials given
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent cb1ecfd commit a6b815c

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

cli/completer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
421421
}
422422

423423
spinner := t.Config.StartSpinner("fetching options, please wait...")
424-
request := cmd.NewRequest(nil, completer.Config, nil)
424+
request := cmd.NewRequest(nil, completer.Config, nil, false)
425425
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
426426
t.Config.StopSpinner(spinner)
427427

cli/exec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,22 @@ func ExecLine(line string) error {
4848
}
4949
}
5050

51-
return ExecCmd(args)
51+
return ExecCmd(args, false)
5252
}
5353

5454
// ExecCmd executes a single provided command
55-
func ExecCmd(args []string) error {
55+
func ExecCmd(args []string, credentialsSupplied bool) error {
5656
config.Debug("ExecCmd args: ", strings.Join(args, ", "))
5757
if len(args) < 1 {
5858
return nil
5959
}
6060

6161
command := cmd.FindCommand(args[0])
6262
if command != nil && !(args[0] == "sync" && len(args) > 1) {
63-
return command.Handle(cmd.NewRequest(command, cfg, args[1:]))
63+
r := cmd.NewRequest(command, cfg, args[1:], credentialsSupplied)
64+
return command.Handle(r)
6465
}
6566

6667
catchAllHandler := cmd.GetAPIHandler()
67-
return catchAllHandler.Handle(cmd.NewRequest(catchAllHandler, cfg, args))
68+
return catchAllHandler.Handle(cmd.NewRequest(catchAllHandler, cfg, args, credentialsSupplied))
6869
}

cmd/network.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
308308

309309
var encodedParams string
310310
var err error
311-
usingAPIKeySecretKey := false
312311

313312
if len(r.Config.ActiveProfile.APIKey) > 0 && len(r.Config.ActiveProfile.SecretKey) > 0 {
314313
apiKey := r.Config.ActiveProfile.APIKey
@@ -328,7 +327,6 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
328327
encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature))
329328
params = nil
330329
}
331-
usingAPIKeySecretKey = true
332330
} else if len(r.Config.ActiveProfile.Username) > 0 && len(r.Config.ActiveProfile.Password) > 0 {
333331
sessionKey, err := Login(r)
334332
if err != nil {
@@ -351,7 +349,11 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
351349
}
352350
config.Debug("NewAPIRequest response status code:", response.StatusCode)
353351

354-
if response.StatusCode == http.StatusUnauthorized && !usingAPIKeySecretKey {
352+
if r.CredentialsSupplied {
353+
config.Debug("Credentials supplied on command-line, not falling back to login")
354+
}
355+
356+
if response.StatusCode == http.StatusUnauthorized && !r.CredentialsSupplied {
355357
r.Client().Jar, _ = cookiejar.New(nil)
356358
sessionKey, err := Login(r)
357359
if err != nil {

cmd/request.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
package cmd
1919

2020
import (
21-
"github.com/apache/cloudstack-cloudmonkey/config"
2221
"net/http"
22+
23+
"github.com/apache/cloudstack-cloudmonkey/config"
2324
)
2425

2526
// Request describes a command request
2627
type Request struct {
27-
Command *Command
28-
Config *config.Config
29-
Args []string
28+
Command *Command
29+
Config *config.Config
30+
Args []string
31+
CredentialsSupplied bool
3032
}
3133

3234
// Client method returns the http Client for the current server profile
@@ -35,10 +37,11 @@ func (r *Request) Client() *http.Client {
3537
}
3638

3739
// NewRequest creates a new request from a command
38-
func NewRequest(cmd *Command, cfg *config.Config, args []string) *Request {
40+
func NewRequest(cmd *Command, cfg *config.Config, args []string, credentialsSupplied bool) *Request {
3941
return &Request{
40-
Command: cmd,
41-
Config: cfg,
42-
Args: args,
42+
Command: cmd,
43+
Config: cfg,
44+
Args: args,
45+
CredentialsSupplied: credentialsSupplied,
4346
}
4447
}

cmk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func main() {
9292

9393
config.Debug("cmdline args:", strings.Join(os.Args, ", "))
9494
if len(args) > 0 {
95-
if err := cli.ExecCmd(args); err != nil {
95+
if err := cli.ExecCmd(args, (*apiKey != "" || *secretKey != "")); err != nil {
9696
fmt.Println("🙈 Error:", err)
9797
os.Exit(1)
9898
}

0 commit comments

Comments
 (0)