Skip to content
Permalink
Browse files
Improve login handling.
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 4d6b4fdf57088150b33bf14f642a2cce203ff82f
Showing 4 changed files with 30 additions and 9 deletions.
@@ -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
}
@@ -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"
@@ -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: ")
@@ -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 {
@@ -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)
@@ -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)
}
@@ -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
}

0 comments on commit 4d6b4fd

Please sign in to comment.