forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 6
/
input.go
91 lines (64 loc) · 2.32 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package saml2aws
import (
"fmt"
"github.com/pkg/errors"
"github.com/versent/saml2aws/pkg/cfg"
"github.com/versent/saml2aws/pkg/creds"
"github.com/versent/saml2aws/pkg/prompter"
)
// PromptForConfigurationDetails prompt the user to present their hostname, username and mfa
func PromptForConfigurationDetails(idpAccount *cfg.IDPAccount) error {
var prompt = prompter.NewCli()
providers := MFAsByProvider.Names()
var err error
idpAccount.Provider, err = prompt.ChooseWithDefault("Please choose a provider:", idpAccount.Provider, providers)
if err != nil {
return errors.Wrap(err, "error selecting provider file")
}
mfas := MFAsByProvider.Mfas(idpAccount.Provider)
// only prompt for MFA if there is more than one option
if len(mfas) > 1 {
idpAccount.MFA, err = prompt.ChooseWithDefault("Please choose an MFA", idpAccount.MFA, mfas)
if err != nil {
return errors.Wrap(err, "error selecting provider file")
}
} else {
idpAccount.MFA = mfas[0]
}
fmt.Println("")
idpAccount.URL = prompt.String("URL", idpAccount.URL)
idpAccount.Username = prompt.String("Username", idpAccount.Username)
fmt.Println("")
return nil
}
// PromptForLoginDetails prompt the user to present their username, password
func PromptForLoginDetails(loginDetails *creds.LoginDetails) error {
var prompt = prompter.NewCli()
fmt.Println("To use saved password just hit enter.")
loginDetails.Username = prompt.String("Username", loginDetails.Username)
if enteredPassword := prompt.Password("Password"); enteredPassword != "" {
loginDetails.Password = enteredPassword
}
fmt.Println("")
return nil
}
// PromptForAWSRoleSelection present a list of roles to the user for selection
func PromptForAWSRoleSelection(accounts []*AWSAccount) (*AWSRole, error) {
roles := map[string]*AWSRole{}
for _, account := range accounts {
for _, role := range account.Roles {
// name := fmt.Sprintf("%s / %s", role.Name, strings.TrimPrefix(account.Name, "Account: "))
name := fmt.Sprintf("%s / %s", role.Name, account.Name)
roles[name] = role
}
}
roleOptions := []string{}
for k := range roles {
roleOptions = append(roleOptions, k)
}
selectedRole, err := prompter.NewCli().ChooseWithDefault("Please choose the role", "", roleOptions)
if err != nil {
return nil, errors.Wrap(err, "Role selection failed")
}
return roles[selectedRole], nil
}