Repository: Versent/saml2aws
Pull request #65 (2017-10-08 04:36:42)

fix(Config) Username is now loaded from config.


Patch:
diff --git a/cmd/saml2aws/commands/login.go b/cmd/saml2aws/commands/login.go
index 44db23a..978f8b4 100644
--- a/cmd/saml2aws/commands/login.go
+++ b/cmd/saml2aws/commands/login.go
@@ -40,9 +40,19 @@ func Login(loginFlags *LoginFlags) error {
 		return errors.Wrap(err, "error loading config file")
 	}
 
+	username, err := config.LoadUsername()
+	if err != nil {
+		return errors.Wrap(err, "error loading config file")
+	}
+
 	// fmt.Println("LookupCredentials", hostname)
 
-	loginDetails, err := resolveLoginDetails(hostname, loginFlags)
+	loginDetails := &saml2aws.LoginDetails{
+		Hostname: hostname,
+		Username: username,
+	}
+
+	err = resolveLoginDetails(loginDetails, loginFlags)
 	if err != nil {
 		fmt.Printf("%+v\n", err)
 		os.Exit(1)
@@ -150,16 +160,14 @@ func Login(loginFlags *LoginFlags) error {
 	return nil
 }
 
-func resolveLoginDetails(hostname string, loginFlags *LoginFlags) (*saml2aws.LoginDetails, error) {
-
-	loginDetails := new(saml2aws.LoginDetails)
+func resolveLoginDetails(loginDetails *saml2aws.LoginDetails, loginFlags *LoginFlags) error {
 
 	// fmt.Printf("loginFlags %+v\n", loginFlags)
 
-	savedUsername, savedPassword, err := credentials.LookupCredentials(hostname)
+	err := credentials.LookupCredentials(loginDetails)
 	if err != nil {
 		if !credentials.IsErrCredentialsNotFound(err) {
-			return nil, errors.Wrap(err, "error loading saved password")
+			return errors.Wrap(err, "error loading saved password")
 		}
 	}
 
@@ -168,29 +176,25 @@ func resolveLoginDetails(hostname string, loginFlags *LoginFlags) (*saml2aws.Log
 	// if you supply a username in a flag it takes precedence
 	if loginFlags.Username != "" {
 		loginDetails.Username = loginFlags.Username
-	} else if savedUsername != "" {
-		loginDetails.Username = savedUsername
 	}
 
 	// if you supply a password in a flag it takes precedence
 	if loginFlags.Password != "" {
 		loginDetails.Password = loginFlags.Password
-	} else if savedPassword != "" {
-		loginDetails.Password = savedPassword
 	}
 
 	// fmt.Printf("loginDetails %+v\n", loginDetails)
 
 	// if skip prompt was passed just pass back the flag values
 	if loginFlags.SkipPrompt {
-		return &saml2aws.LoginDetails{
-			Username: loginDetails.Username,
-			Password: loginDetails.Password,
-			Hostname: loginFlags.Hostname,
-		}, nil
+		return nil
 	}
 
-	return saml2aws.PromptForLoginDetails(savedUsername, hostname, savedPassword)
+	err = saml2aws.PromptForLoginDetails(loginDetails)
+	if err != nil {
+		return errors.Wrap(err, "Error occured accepting input")
+	}
+	return nil
 }
 
 func resolveRole(awsRoles []*saml2aws.AWSRole, samlAssertion string, loginFlags *LoginFlags) (*saml2aws.AWSRole, error) {
diff --git a/helper/credentials/credentials.go b/helper/credentials/credentials.go
index cb75b9f..b48e109 100644
--- a/helper/credentials/credentials.go
+++ b/helper/credentials/credentials.go
@@ -50,7 +50,7 @@ func (defaultHelper) Delete(serverURL string) error {
 }
 
 func (defaultHelper) Get(serverURL string) (string, string, error) {
-	return "", "", nil
+	return "", "", ErrCredentialsNotFound
 }
 
 func (defaultHelper) List() (map[string]string, error) {
diff --git a/helper/credentials/saml.go b/helper/credentials/saml.go
index f5e7bd0..10bb53e 100644
--- a/helper/credentials/saml.go
+++ b/helper/credentials/saml.go
@@ -1,13 +1,23 @@
 package credentials
 
-import "fmt"
+import (
+	"fmt"
+
+	"github.com/versent/saml2aws"
+)
 
 // LookupCredentials lookup an existing set of credentials and validate it.
-func LookupCredentials(hostname string) (string, string, error) {
+func LookupCredentials(loginDetails *saml2aws.LoginDetails) error {
+
+	username, password, err := CurrentHelper.Get(fmt.Sprintf("https://%s", loginDetails.Hostname))
+	if err != nil {
+		return err
+	}
 
-	username, password, err := CurrentHelper.Get(fmt.Sprintf("https://%s", hostname))
+	loginDetails.Username = username
+	loginDetails.Password = password
 
-	return username, password, err
+	return nil
 }
 
 // SaveCredentials save the user credentials.
diff --git a/input.go b/input.go
index 3df39b9..8d392e6 100644
--- a/input.go
+++ b/input.go
@@ -33,25 +33,21 @@ func (ld *LoginDetails) Validate() error {
 }
 
 // PromptForLoginDetails prompt the user to present their username, password and hostname
-func PromptForLoginDetails(username, hostname, password string) (*LoginDetails, error) {
+func PromptForLoginDetails(loginDetails *LoginDetails) error {
 
-	hostname = promptFor("Hostname [%s]", hostname)
+	loginDetails.Hostname = promptFor("Hostname [%s]", loginDetails.Hostname)
 
 	fmt.Println("To use saved username and password just hit enter.")
 
-	username = promptFor("Username [%s]", username)
+	loginDetails.Username = promptFor("Username [%s]", loginDetails.Username)
 
 	if enteredPassword := prompt.PasswordMasked("Password"); enteredPassword != "" {
-		password = enteredPassword
+		loginDetails.Password = enteredPassword
 	}
 
 	fmt.Println("")
 
-	return &LoginDetails{
-		Username: strings.TrimSpace(username),
-		Password: strings.TrimSpace(password),
-		Hostname: strings.TrimSpace(hostname),
-	}, nil
+	return nil
 }
 
 // PromptForAWSRoleSelection present a list of roles to the user for selection


Test patch:
diff --git a/cmd/saml2aws/commands/login_test.go b/cmd/saml2aws/commands/login_test.go
index 51ebc2e..7edf4f3 100644
--- a/cmd/saml2aws/commands/login_test.go
+++ b/cmd/saml2aws/commands/login_test.go
@@ -7,11 +7,13 @@ import (
 	"github.com/versent/saml2aws"
 )
 
-func TestResolveLoginDetails(t *testing.T) {
+func TestResolveLoginDetailsWithFlags(t *testing.T) {
 
 	loginFlags := &LoginFlags{Hostname: "id.example.com", Username: "wolfeidau", Password: "testtestlol", SkipPrompt: true}
 
-	loginDetails, err := resolveLoginDetails("id.example.com", loginFlags)
+	loginDetails := &saml2aws.LoginDetails{Hostname: "id.example.com", Username: ""}
+
+	err := resolveLoginDetails(loginDetails, loginFlags)
 
 	assert.Empty(t, err)
 	assert.Equal(t, loginDetails, &saml2aws.LoginDetails{Username: "wolfeidau", Password: "testtestlol", Hostname: "id.example.com"})