-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
315 lines (265 loc) · 9.08 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/aidan-/aws-cli-federator/federator"
"github.com/go-ini/ini"
"github.com/howeyc/gopass"
)
type configuration struct {
version *bool
verbose *bool
path string
cfg *ini.File
account string
profile string
}
var Version = "1.0.0"
var c configuration //arguments
var l *log.Logger
func init() {
c.version = flag.Bool("version", false, "prints cli version information")
c.verbose = flag.Bool("v", false, "print debug messages to STDOUT")
flag.StringVar(&c.path, "path", "", "set path to aws-federator configuration")
flag.StringVar(&c.account, "account", "", "set which AWS account configuration should be used")
flag.StringVar(&c.account, "acct", "", "set which AWS account configuration should be used (shorthand)")
flag.StringVar(&c.profile, "profile", "", "set which AWS credential profile the temporary credentials should be written to. Defaults to 'default'")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(1)
}
}
func (c *configuration) loadConfigurationFile() error {
if c.path == "" {
usr, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to get current user information: %s\n", err)
os.Exit(1)
}
l.Printf("Found user's homedirectory: %s\n", usr.HomeDir)
c.path = filepath.Join(usr.HomeDir, ".aws/federatedcli")
}
l.Printf("Loading configuration from file: %s\n", c.path)
cfg, err := ini.Load(c.path)
if err != nil {
return err
}
cfg.BlockMode = false
c.cfg = cfg
return nil
}
// findAccount looks through the loaded configuration file to locate a
// matching account declaration with the account name loaded from the CLI.
// It returns the configuration block if there is a match and false if there
// is not.
func (c configuration) matchAccount() (*ini.Section, bool) {
for _, acct := range c.cfg.Sections() {
if acct.Name() == c.account {
return acct, true
}
}
return &ini.Section{}, false
}
func main() {
flag.Parse()
if *c.version {
fmt.Fprintf(os.Stderr, "%s version %s\n", filepath.Base(os.Args[0]), Version)
os.Exit(0)
}
l = log.New(ioutil.Discard, "", log.LstdFlags)
if *c.verbose {
l.SetOutput(os.Stderr)
}
if c.account == "" {
c.account = "default"
}
if err := c.loadConfigurationFile(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to parse configuration file: %s\n", err)
os.Exit(1)
}
acct, found := c.matchAccount()
if !found {
fmt.Fprintf(os.Stderr, "ERROR: Could not find configuration matching provided account name '%s'\n", c.account)
os.Exit(1)
}
if !acct.HasKey("sp_identity_url") {
fmt.Fprintf(os.Stderr, "ERROR: Account configuration '%s' does not have an 'sp_identity_url' defined\n", c.account)
os.Exit(1)
}
spIdentityURL := acct.Key("sp_identity_url").String()
//get username
user := ""
if acct.HasKey("username") {
user = acct.Key("username").String()
} else {
reader := bufio.NewReader(os.Stdin)
fmt.Fprint(os.Stderr, "Enter Username: ")
u, _ := reader.ReadString('\n')
user = strings.TrimSpace(u)
}
//get password
pass := ""
if acct.HasKey("password") {
pass = acct.Key("password").String()
} else {
fmt.Fprint(os.Stderr, "Enter Password: ")
var err error
p, err := gopass.GetPasswd()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not get password: %s\n", err)
os.Exit(1)
}
pass = string(p)
//pass = strings.TrimSpace(p)
}
aws, err := federator.New(user, pass, spIdentityURL)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to initialize federator: %s\n", err)
os.Exit(1)
}
if err = aws.Login(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Authentication failure: %s\n", err)
os.Exit(1)
}
roles, err := aws.GetRoles()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not retrieve roles: %s\n", err)
}
var roleToAssume federator.Role
if acct.HasKey("assume_role") {
for _, r := range roles {
if acct.Key("assume_role").String() == string(r) {
roleToAssume = r
break
}
}
if roleToAssume == "" {
//couldn't find the role
fmt.Fprintf(os.Stderr, "ERROR: Unable to find role '%s'. Perhaps your federator configuration is incorrect?\n", acct.Key("assume_role").String())
os.Exit(1)
}
} else {
if len(roles) == 1 {
roleToAssume = roles[0]
} else {
roleMap := make(map[string]federator.Role) // mapping of pretty names to roles
var printableRoles []string // slice for sorting matched pretty names
var unmatchedRoles []string // slice for sorting unmatched pretty names
// iterate over available roles to build up a map of 'printable role name' -> role arn
// capture the key names in string arrays for order sorting
accountMap, err := c.cfg.GetSection("account_map") // mapping of accountId's to pretty names
if err == nil {
for _, role := range roles {
if accountMap.HasKey(role.AccountId()) {
an := accountMap.Key(role.AccountId()).String()
roleMap[fmt.Sprintf("%s:role/%s", an, role.RoleName())] = role
printableRoles = append(printableRoles, fmt.Sprintf("%s:role/%s", an, role.RoleName()))
} else {
roleMap[fmt.Sprintf("%s", role.RoleArn())] = role
unmatchedRoles = append(unmatchedRoles, role.RoleArn())
}
}
} else {
for _, role := range roles {
roleMap[fmt.Sprintf("%s", role.RoleArn())] = role
}
}
// sort the role keys alphabetically and append the unmatchedRoles to the printableRoles array to ensure they appear last
sort.Strings(printableRoles)
sort.Strings(unmatchedRoles)
for _, k := range unmatchedRoles {
printableRoles = append(printableRoles, k)
}
for n, r := range printableRoles {
fmt.Fprintf(os.Stderr, "%d) %s\n", n+1, r)
}
var i int // user selection
fmt.Fprintf(os.Stderr, "Enter the ID# of the role you want to assume: ")
_, err = fmt.Scanf("%d", &i)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Invalid selection made.\n")
os.Exit(1)
}
if i > len(roles)+1 {
fmt.Fprintf(os.Stderr, "ERROR: Invalid ID selection, but in range from %d to %d.\n", 1, len(roles)+1)
os.Exit(1)
}
roleToAssume = roleMap[printableRoles[i-1]]
}
}
l.Printf("User has selected ARN: %s\n", roleToAssume)
l.Printf("Attempting to AssumeRoleWithSAML\n")
creds, err := aws.AssumeRole(roleToAssume)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to assume role: %s", err)
os.Exit(1)
}
fmt.Fprintln(os.Stderr, "-------------------------------------------------------")
// output temporary credentials to stdout instead of writing to credentials file
if c.profile == "" {
fmt.Fprintf(os.Stderr, "Temporary credentials successfully generated. Set the following environment variables to being using them:\n\n")
if runtime.GOOS == "windows" {
fmt.Printf("set AWS_ACCESS_KEY_ID=%s\n", creds.AccessKeyId)
fmt.Printf("set AWS_SECRET_ACCESS_KEY=%s\n", creds.SecretAccessKey)
fmt.Printf("set AWS_SESSION_TOKEN=%s\n", creds.SessionToken)
} else {
fmt.Printf("export AWS_ACCESS_KEY_ID=%s\n", creds.AccessKeyId)
fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s\n", creds.SecretAccessKey)
fmt.Printf("export AWS_SESSION_TOKEN=%s\n", creds.SessionToken)
}
} else {
if err := WriteAWSCredentials(creds, c.profile); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to write credentials: %s", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Temporary credentials successfully saved to credential profile '%s'.\nYou can use these credentials with the AWS CLI by including the '--profile %s' flag.\n", c.profile, c.profile)
}
fmt.Fprintf(os.Stderr, "\nThese credentials will remain valid until %s\n", creds.Expiration.String())
}
func WriteAWSCredentials(c federator.Credentials, p string) error {
usr, err := user.Current()
if err != nil {
return fmt.Errorf("Unable to get current user information: %s\n", err)
}
cpath := filepath.Join(usr.HomeDir, ".aws/credentials")
l.Printf("Writing to AWS credentials file: %s\n", cpath)
cfg, err := ini.Load(cpath)
if err != nil {
return err
}
if _, err := cfg.GetSection(p); err != nil {
if _, err := cfg.NewSection(p); err != nil {
return fmt.Errorf("Unable to create credential profile: %s", err)
}
}
prof, err := cfg.GetSection(p)
if err != nil {
return fmt.Errorf("Unable to retrieve recently created profile: %s", err)
}
//aws_access_key_id
if _, err := prof.NewKey("aws_access_key_id", c.AccessKeyId); err != nil {
return fmt.Errorf("Unable to write aws_access_key_id to credential file: %s", err)
}
//aws_secret_access_key
if _, err := prof.NewKey("aws_secret_access_key", c.SecretAccessKey); err != nil {
return fmt.Errorf("Unable to write aws_secret_access_key to credential file: %s", err)
}
//aws_session_token
if _, err := prof.NewKey("aws_session_token", c.SessionToken); err != nil {
return fmt.Errorf("Unable to write aws_session_token to credential file: %s", err)
}
if err := cfg.SaveTo(filepath.Join(usr.HomeDir, ".aws/credentials")); err != nil {
return fmt.Errorf("Unable to save configuration to disk: %s", err)
}
return nil
}