Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions cmd/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
22 changes: 11 additions & 11 deletions cmk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 15 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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":
Expand Down