Skip to content

Commit 413b5ae

Browse files
committed
allow for role specification on the command line
1 parent b73f799 commit 413b5ae

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

cartogram/pack.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212
)
1313

1414
const (
15-
accountRegexString = `^(\d+)(/(\w+))?$`
15+
// accountRegexString matches an account number with an optional /$role_name
16+
// Per https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-limits.html .
17+
// role names can contain alphanumeric characters, and these symbols: +=,.@_-
18+
accountRegexString = `^(\d+)(?:/([a-zA-Z0-9+=,.@_-]+))?$`
1619
)
1720

1821
// AccountRegex matches an account number with an optional role name

travel/main.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Travel(i Itinerary) (creds.Creds, error) {
5454
if err := v.loadAccount(i.Args, i.Prompt); err != nil {
5555
return c, err
5656
}
57-
if err := v.loadRole(i.RoleName, i.Prompt); err != nil {
57+
if err := v.loadRole(i.RoleName, i.Args, i.Prompt); err != nil {
5858
return c, err
5959
}
6060
if err := v.loadHops(); err != nil {
@@ -77,8 +77,14 @@ func (v *voyage) loadAccount(args []string, pf prompt.Func) error {
7777
return err
7878
}
7979

80-
func (v *voyage) loadRole(roleName string, pf prompt.Func) error {
80+
func (v *voyage) loadRole(roleName string, args []string, pf prompt.Func) error {
8181
var err error
82+
if roleName == "" && len(args) == 1 {
83+
accountMatch := cartogram.AccountRegex.FindStringSubmatch(args[0])
84+
if len(accountMatch) > 2 {
85+
roleName = accountMatch[2]
86+
}
87+
}
8288
v.role, err = v.account.PickRoleWithPrompt(roleName, pf)
8389
return err
8490
}
@@ -163,12 +169,12 @@ func parseHops(stack *[]hop, cp cartogram.Pack, a cartogram.Account, r string) e
163169
},
164170
)
165171
accountMatch := cartogram.AccountRegex.FindStringSubmatch(a.Source)
166-
if len(accountMatch) != 4 {
172+
if len(accountMatch) != 3 {
167173
*stack = append(*stack, hop{Profile: a.Source})
168174
return nil
169175
}
170176
sAccountID := accountMatch[1]
171-
sRole := accountMatch[3]
177+
sRole := accountMatch[2]
172178
found, sAccount := cp.Lookup(sAccountID)
173179
if !found {
174180
return fmt.Errorf("Failed to resolve hop for %s", sAccountID)

0 commit comments

Comments
 (0)