Skip to content
Permalink
Browse files
Allow login without user/password if target already in config
  • Loading branch information
geomacy committed Jul 4, 2017
1 parent 61df6a2 commit 92de0b6d1189fadf63c5e6f5a34bae4a6b3e0e51
Showing 5 changed files with 97 additions and 15 deletions.
@@ -82,6 +82,18 @@ func (cmd *Login) promptAndReadPassword() (password string) {
return string(bytePassword)
}

func (cmd *Login) getCredentialsFromCommandLineIfNeeded() {
// Prompt for username if not supplied
if cmd.network.BrooklynUser == "" {
cmd.network.BrooklynUser = cmd.promptAndReadUsername()
}

// Prompt for password if not supplied (password is not echoed to screen
if cmd.network.BrooklynUser != "" && cmd.network.BrooklynPass == "" {
cmd.network.BrooklynPass = cmd.promptAndReadPassword()
}
}

func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
if !c.Args().Present() {
error_handler.ErrorExit("A URL must be provided as the first argument", error_handler.CLIUsageErrorExitCode)
@@ -93,8 +105,7 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
cmd.network.BrooklynPass = c.Args().Get(2)
cmd.network.SkipSslChecks = c.Bool("skipSslChecks")

// invalidate current credentials record
io.GetConfig().Delete()
config := io.GetConfig()

if err := net.VerifyLoginURL(cmd.network); err != nil {
error_handler.ErrorExit(err)
@@ -108,15 +119,14 @@ 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 == "" {
cmd.network.BrooklynUser = cmd.promptAndReadUsername()
}

// Prompt for password if not supplied (password is not echoed to screen
if cmd.network.BrooklynUser != "" && cmd.network.BrooklynPass == "" {
cmd.network.BrooklynPass = cmd.promptAndReadPassword()
if cmd.network.BrooklynUrl != "" && cmd.network.BrooklynUser == "" {
// if only target supplied at command line see if it already exists in the config file
if username, password, err := config.GetNetworkCredentialsForTarget(cmd.network.BrooklynUrl); err == nil {
cmd.network.BrooklynUser = username
cmd.network.BrooklynPass = password
}
}
cmd.getCredentialsFromCommandLineIfNeeded()

// now persist these credentials to the yaml file
cmd.config.SetNetworkCredentials(cmd.network.BrooklynUrl, cmd.network.BrooklynUser, cmd.network.BrooklynPass)
@@ -128,7 +138,6 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
if code == http.StatusUnauthorized {
err = errors.New("Unauthorized")
}
cmd.config.Delete()
error_handler.ErrorExit(err)
}
fmt.Printf("Connected to Brooklyn version %s at %s\n", loginVersion.Version, cmd.network.BrooklynUrl)
@@ -104,7 +104,8 @@ func (config *Config) read() {
// Note that the password is base64 encoded to avoid json formatting problems
//{
// "credentials": {
// "http://geoffs-macbook-pro.local:8081": "Z2VvZmY6cGFzc3dvcmQ="
// "http://geoffs-macbook-pro.local:8081": "Z2VvZmY6cGFzc3dvcmQ=",
// "http://localhost:8081": "Z2VvZmY6cGFzc3dvcmQ="
// },
// "skipSslChecks": false,
// "target": "http://geoffs-macbook-pro.local:8081"
@@ -220,12 +221,17 @@ func (config *Config) adaptLegacyCredentialFormat() {
}
}

func (config *Config) GetNetworkCredentialsForTarget(target string) (username string, password string, err error) {
if username, password, err = config.getCredentials(target); err != nil {
username, password, err = config.getCredentialsOldStyle(target)
}
return
}

func (config *Config) GetNetworkCredentials() (target string, username string, password string, err error) {
target, found := config.Map[targetKey].(string)
if found {
if username, password, err = config.getCredentials(target); nil != err {
username, password, err = config.getCredentialsOldStyle(target)
}
username, password, err = config.GetNetworkCredentialsForTarget(target)
} else {
err = errors.New("Not logged in")
}
@@ -0,0 +1,45 @@
package io

import (
"testing"
"path/filepath"
)

func TestConfig(t *testing.T) {

testFileFormat(t, "testConfig.json")
testFileFormat(t, "legacyConfig.json")
}

func testFileFormat(t *testing.T, testFile string) {

config := new(Config)
expectedTarget := "http://some.site:8081"

path, err := filepath.Abs(testFile)
if err != nil {
t.Error(err)
}
config.FilePath = path
config.read()
if config.Map["target"] != expectedTarget {
t.Errorf("target != %s: %s", expectedTarget, config.Map["target"])
}
_, username, password, err := config.GetNetworkCredentials()
assertUserPassword(err, t, username, "user1", password, "password1")

username, password, err = config.GetNetworkCredentialsForTarget("http://another.one:8081")
assertUserPassword(err, t, username, "user2", password, "password2")
}

func assertUserPassword(err error, t *testing.T, username string, expectedUser string, password string, expectedPassword string) {
if err != nil {
t.Error(err)
}
if username != expectedUser {
t.Errorf("username != %s: %s", expectedUser, username)
}
if password != expectedPassword {
t.Errorf("password != %s: %s", expectedPassword, username)
}
}
@@ -0,0 +1,14 @@
{
"auth": {
"http://some.site:8081": {
"password": "password1",
"username": "user1"
},
"http://another.one:8081": {
"password": "password2",
"username": "user2"
}
},
"skipSslChecks": false,
"target": "http://some.site:8081"
}
@@ -0,0 +1,8 @@
{
"credentials": {
"http://some.site:8081": "dXNlcjE6cGFzc3dvcmQx",
"http://another.one:8081": "dXNlcjI6cGFzc3dvcmQy"
},
"skipSslChecks": false,
"target": "http://some.site:8081"
}

0 comments on commit 92de0b6

Please sign in to comment.