Skip to content

Commit

Permalink
Improve login handling.
Browse files Browse the repository at this point in the history
See https://issues.apache.org/jira/browse/BROOKLYN-463.

Prompt for user name if not supplied.
Check for 401 Unauthorized and give tidier output.
  • Loading branch information
geomacy committed Jul 4, 2017
1 parent 052d3bd commit 4d6b4fd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
9 changes: 5 additions & 4 deletions cli/api/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import (
"github.com/apache/brooklyn-client/cli/net"
)

func Version(network *net.Network) (models.VersionSummary, error) {
func Version(network *net.Network) (models.VersionSummary, int, error) {
url := "/v1/server/version"
var versionSummary models.VersionSummary
body, err := network.SendGetRequest(url)
req := network.NewGetRequest(url)
body, code, err := network.SendRequestGetStatusCode(req)
if err != nil {
return versionSummary, err
return versionSummary, code, err
}
err = json.Unmarshal(body, &versionSummary)
return versionSummary, err
return versionSummary, code, err
}
24 changes: 22 additions & 2 deletions cli/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ package commands
import (
"fmt"
"syscall"
"bufio"
"os"
"strings"
"net/http"
"errors"

"github.com/apache/brooklyn-client/cli/api/version"
"github.com/apache/brooklyn-client/cli/command_metadata"
Expand Down Expand Up @@ -76,6 +81,17 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
cmd.network.BrooklynUrl = cmd.network.BrooklynUrl[0 : len(cmd.network.BrooklynUrl)-1]
}

// Prompt for username if not supplied
if cmd.network.BrooklynUser == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Username: ")
user, err := reader.ReadString('\n')
if err != nil {
error_handler.ErrorExit(err)
}
cmd.network.BrooklynUser = strings.TrimSpace(user)
}

// Prompt for password if not supplied (password is not echoed to screen
if cmd.network.BrooklynUser != "" && cmd.network.BrooklynPass == "" {
fmt.Print("Enter Password: ")
Expand All @@ -84,7 +100,8 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
error_handler.ErrorExit(err)
}
fmt.Printf("\n")
cmd.network.BrooklynPass = string(bytePassword)
password := string(bytePassword)
cmd.network.BrooklynPass = strings.TrimSpace(password)
}

if cmd.config.Map == nil {
Expand All @@ -106,8 +123,11 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
cmd.config.Map["skipSslChecks"] = cmd.network.SkipSslChecks
cmd.config.Write()

loginVersion, err := version.Version(cmd.network)
loginVersion, code, err := version.Version(cmd.network)
if nil != err {
if code == http.StatusUnauthorized {
err = errors.New("Unauthorized")
}
error_handler.ErrorExit(err)
}
fmt.Printf("Connected to Brooklyn version %s at %s\n", loginVersion.Version, cmd.network.BrooklynUrl)
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cmd *Version) Run(scope scope.Scope, c *cli.Context) {
if err := net.VerifyLoginURL(cmd.network); err != nil {
error_handler.ErrorExit(err)
}
version, err := version.Version(cmd.network)
version, _, err := version.Version(cmd.network)
if nil != err {
error_handler.ErrorExit(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ func (net *Network) SendRequestGetStatusCode(req *http.Request) ([]byte, int, er
client := net.makeClient()
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
return nil, resp.StatusCode, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
body = nil
}
if failed := unsuccessful(resp.StatusCode); failed {
return nil, 0, makeError(resp, resp.StatusCode, body)
return nil, resp.StatusCode, makeError(resp, resp.StatusCode, body)
}
return body, resp.StatusCode, err
}
Expand Down

0 comments on commit 4d6b4fd

Please sign in to comment.