Skip to content

Commit

Permalink
feat(Configure) Add skip prompt for configure command.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfeidau committed Nov 14, 2017
1 parent e55bf99 commit 0c7a822
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ You can also add named accounts, below is an example where I am setting up an ac
saml2aws configure -a wolfeidau
```

You can also configure the account alias without prompts.

```
saml2aws configure -a wolfeidau --idp-provider KeyCloak --username mark@wolfe.id.au --url https://keycloak.wolfe.id.au/auth/realms/master/protocol/saml/clients/amazon-aws --skip-prompt
```

# Install

## OSX
Expand Down
9 changes: 6 additions & 3 deletions cmd/saml2aws/commands/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ func Configure(loginFlags *LoginFlags, cmdline []string) error {
// update username and hostname if supplied
applyFlagOverrides(loginFlags, account)

err = saml2aws.PromptForConfigurationDetails(account)
if err != nil {
return errors.Wrap(err, "failed to input configuration")
// do we need to prompt for values now?
if !loginFlags.SkipPrompt {
err = saml2aws.PromptForConfigurationDetails(account)
if err != nil {
return errors.Wrap(err, "failed to input configuration")
}
}

err = cfgm.SaveIDPAccount(idpAccountName, account)
Expand Down
30 changes: 19 additions & 11 deletions cmd/saml2aws/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ const MaxDurationSeconds = 3600

// LoginFlags login specific command flags
type LoginFlags struct {
IdpAccount string
//Provider string
Profile string
//Hostname string
URL string
Username string
Password string
RoleArn string
SkipVerify bool
Timeout int
SkipPrompt bool
IdpAccount string
IdpProvider string
MFA string
Profile string
URL string
Username string
Password string
RoleArn string
SkipVerify bool
Timeout int
SkipPrompt bool
}

// RoleSupplied role arn has been passed as a flag
Expand Down Expand Up @@ -274,6 +274,14 @@ func applyFlagOverrides(loginFlags *LoginFlags, account *cfg.IDPAccount) {
account.SkipVerify = loginFlags.SkipVerify
}

if loginFlags.IdpProvider != "" {
account.Provider = loginFlags.IdpProvider
}

if loginFlags.MFA != "" {
account.MFA = loginFlags.MFA
}

if loginFlags.Timeout > 0 {
account.Timeout = loginFlags.Timeout
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/saml2aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func configureLoginFlags(app *kingpin.Application) *commands.LoginFlags {
c := &commands.LoginFlags{}

app.Flag("idp-account", "The name of the configured IDP account").Short('a').Default("default").StringVar(&c.IdpAccount)
app.Flag("idp-provider", "The configured IDP provider").EnumVar(&c.IdpProvider, "ADFS", "ADFS2", "Ping", "JumpCloud", "Okta", "KeyCloak")
app.Flag("mfa", "The name of the mfa").Default("Auto").StringVar(&c.MFA)
app.Flag("profile", "The AWS profile to save the temporary credentials").Short('p').Default("saml").StringVar(&c.Profile)
app.Flag("skip-verify", "Skip verification of server certificate.").Short('s').BoolVar(&c.SkipVerify)
// app.Flag("timeout", "Override the default HTTP client timeout in seconds.").Short('t').IntVar(&c.Timeout)
// app.Flag("provider", "The type of SAML IDP provider.").Short('i').Default("ADFS").EnumVar(&c.Provider, "ADFS", "ADFS2", "Ping", "JumpCloud", "Okta", "KeyCloak")
app.Flag("URL", "The URL of the SAML IDP server used to login.").StringVar(&c.URL)
app.Flag("url", "The URL of the SAML IDP server used to login.").StringVar(&c.URL)
app.Flag("username", "The username used to login.").StringVar(&c.Username)
app.Flag("password", "The password used to login.").Envar("SAML2AWS_PASSWORD").StringVar(&c.Password)
app.Flag("role", "The ARN of the role to assume.").StringVar(&c.RoleArn)
Expand Down
16 changes: 12 additions & 4 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func PromptForConfigurationDetails(idpAccount *cfg.IDPAccount) error {

var err error

idpAccount.Provider, err = promptForSelection("\nPlease choose the provider you would like to use:\n", providers)
idpAccount.Provider, err = promptForSelection("\nPlease choose the provider you would like to use:\n", idpAccount.Provider, providers)
if err != nil {
return errors.Wrap(err, "error selecting provider file")
}
Expand All @@ -30,7 +30,7 @@ func PromptForConfigurationDetails(idpAccount *cfg.IDPAccount) error {

// only prompt for MFA if there is more than one option
if len(mfas) > 1 {
idpAccount.MFA, err = promptForSelection("\nPlease choose an MFA you would like to use:\n", mfas)
idpAccount.MFA, err = promptForSelection("\nPlease choose an MFA you would like to use:\n", idpAccount.MFA, mfas)
if err != nil {
return errors.Wrap(err, "error selecting provider file")
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func PromptForAWSRoleSelection(accounts []*AWSAccount) (*AWSRole, error) {
return roles[v], nil
}

func promptForSelection(prompt string, options []string) (string, error) {
func promptForSelection(prompt string, defaultValue string, options []string) (string, error) {

reader := bufio.NewReader(os.Stdin)

Expand All @@ -116,9 +116,17 @@ func promptForSelection(prompt string, options []string) (string, error) {
var err error

for {
fmt.Print("Selection: ")
if defaultValue != "" {
fmt.Print("Selection [" + defaultValue + "]: ")
} else {
fmt.Print("Selection: ")
}
selectedRoleIndex, _ := reader.ReadString('\n')

if strings.TrimSpace(selectedRoleIndex) == "" && defaultValue != "" {
return defaultValue, nil
}

v, err = strconv.Atoi(strings.TrimSpace(selectedRoleIndex))
if err != nil {
continue
Expand Down
4 changes: 4 additions & 0 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func NewConfigManager(configFile string) (*ConfigManager, error) {
// SaveIDPAccount save idp account
func (cm *ConfigManager) SaveIDPAccount(idpAccountName string, account *IDPAccount) error {

if err := account.Validate(); err != nil {
return errors.Wrap(err, "Account validation failed")
}

cfg, err := ini.LoadSources(ini.LoadOptions{Loose: true}, cm.configPath)
if err != nil {
return errors.Wrap(err, "Unable to load configuration file")
Expand Down
6 changes: 3 additions & 3 deletions saml2aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type ProviderList map[string][]string

// MFAsByProvider a list of providers with their respective supported MFAs
var MFAsByProvider = ProviderList{
"ADFS": []string{"None", "VIP"},
"ADFS2": []string{"None"},
"ADFS": []string{"Auto", "VIP"},
"ADFS2": []string{"Auto"},
"Ping": []string{"Auto"}, // automatically detects PingID
"JumpCloud": []string{"None"},
"JumpCloud": []string{"Auto"},
"Okta": []string{"Auto"}, // automatically detects DUO, SMS and ToTP
"KeyCloak": []string{"Auto"}, // automatically detects ToTP
}
Expand Down

0 comments on commit 0c7a822

Please sign in to comment.