forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
180 lines (130 loc) · 3.9 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package saml2aws
import (
"bufio"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/segmentio/go-prompt"
"github.com/versent/saml2aws/pkg/cfg"
"github.com/versent/saml2aws/pkg/creds"
)
// PromptForConfigurationDetails prompt the user to present their hostname, username and mfa
func PromptForConfigurationDetails(idpAccount *cfg.IDPAccount) error {
providers := MFAsByProvider.Names()
var err error
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")
}
mfas := MFAsByProvider.Mfas(idpAccount.Provider)
// 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", idpAccount.MFA, mfas)
if err != nil {
return errors.Wrap(err, "error selecting provider file")
}
} else {
idpAccount.MFA = mfas[0]
}
fmt.Println("")
idpAccount.URL = promptForURL("URL [%s]", idpAccount.URL)
idpAccount.Username = promptFor("Username [%s]", idpAccount.Username)
fmt.Println("")
return nil
}
// PromptForLoginDetails prompt the user to present their username, password and hostname
func PromptForLoginDetails(loginDetails *creds.LoginDetails) error {
// loginDetails.Hostname = promptFor("Hostname [%s]", loginDetails.Hostname)
fmt.Println("To use saved password just hit enter.")
loginDetails.Username = promptFor("Username [%s]", loginDetails.Username)
if enteredPassword := prompt.PasswordMasked("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) {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Please choose the role you would like to assume: ")
roles := []*AWSRole{}
for _, account := range accounts {
fmt.Println(account.Name)
for _, role := range account.Roles {
fmt.Println("[", len(roles), "]: ", role.Name)
fmt.Println()
roles = append(roles, role)
}
}
fmt.Print("Selection: ")
selectedRoleIndex, _ := reader.ReadString('\n')
v, err := strconv.Atoi(strings.TrimSpace(selectedRoleIndex))
if err != nil {
return nil, fmt.Errorf("Unrecognised role index")
}
if v > len(roles) {
return nil, fmt.Errorf("You selected an invalid role index")
}
return roles[v], nil
}
func promptForSelection(prompt string, defaultValue string, options []string) (string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Println(prompt)
for i, val := range options {
fmt.Println("[", i, "]: ", val)
fmt.Println()
}
var v int
var err error
for {
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
}
if v >= 0 && v < len(options) {
break
}
fmt.Println("Invalid selection")
}
return options[v], nil
}
func promptFor(promptString, defaultValue string) string {
var val string
// do while
for ok := true; ok; ok = strings.TrimSpace(defaultValue) == "" && strings.TrimSpace(val) == "" {
val = prompt.String(promptString, defaultValue)
}
if val == "" {
val = defaultValue
}
return val
}
func promptForURL(promptString, defaultValue string) string {
var rawURL string
// do while
for {
rawURL = prompt.String(promptString, defaultValue)
if rawURL == "" {
rawURL = defaultValue
}
_, err := url.ParseRequestURI(rawURL)
if err != nil {
fmt.Println("please enter a valid url eg https://id.example.com")
} else {
break
}
}
return rawURL
}