Skip to content

Commit 068275e

Browse files
authored
access: prevent login with username-password when command-line credentials given (#174)
Fixes #168 Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 03342d0 commit 068275e

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

cli/completer.go

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

445445
spinner := t.Config.StartSpinner("fetching options, please wait...")
446-
request := cmd.NewRequest(nil, completer.Config, nil)
446+
request := cmd.NewRequest(nil, completer.Config, nil, false)
447447
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
448448
t.Config.StopSpinner(spinner)
449449

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
341341
encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature))
342342
params = nil
343343
}
344-
345344
} else if len(r.Config.ActiveProfile.Username) > 0 && len(r.Config.ActiveProfile.Password) > 0 {
346345
sessionKey, err := Login(r)
347346
if err != nil {
@@ -364,7 +363,11 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
364363
}
365364
config.Debug("NewAPIRequest response status code:", response.StatusCode)
366365

367-
if response.StatusCode == http.StatusUnauthorized {
366+
if r.CredentialsSupplied {
367+
config.Debug("Credentials supplied on command-line, not falling back to login")
368+
}
369+
370+
if response.StatusCode == http.StatusUnauthorized && !r.CredentialsSupplied {
368371
r.Client().Jar, _ = cookiejar.New(nil)
369372
sessionKey, err := Login(r)
370373
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)