Skip to content

Commit d07eace

Browse files
author
Jon Day
committed
fixes promptForRole to handle friendly labels with wildcards
1 parent 310a52e commit d07eace

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

internal/webssoauth/webssoauth.go

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -480,56 +480,52 @@ func (w *WebSSOAuthentication) choiceFriendlyLabelRole(arn string, roles map[str
480480

481481
// promptForRole prompt operator for the AWS Role ARN given a slice of Role ARNs
482482
func (w *WebSSOAuthentication) promptForRole(idp string, roleARNs []string, configRoles map[string]string) (roleARN string, err error) {
483-
if len(roleARNs) == 1 || w.config.AWSIAMRole() != "" {
484-
roleARN = w.config.AWSIAMRole()
485-
if len(roleARNs) == 1 {
486-
roleARN = roleARNs[0]
487-
}
488-
roleLabel := w.choiceFriendlyLabelRole(roleARN, configRoles)
489-
roleData := roleTemplateData{
490-
Role: roleLabel,
491-
}
483+
// roleLabels are the friendly names if configured or the ARNs themselves
484+
roleLabels := make([]string, len(roleARNs))
485+
roleArnByLabel := map[string]string{}
486+
for _, arn := range roleARNs {
487+
roleLabel := w.choiceFriendlyLabelRole(arn, configRoles)
488+
roleLabels = append(roleLabels, roleLabel)
489+
roleArnByLabel[roleLabel] = arn
490+
}
492491

493-
// reverse case when friendly role name alias is given as the input value
494-
// --aws-iam-role "OK S3 Read"
495-
if roleLabel == roleARN {
496-
for rARN, rLbl := range configRoles {
497-
if roleARN == rLbl {
498-
roleARN = rARN
499-
break
500-
}
501-
}
502-
}
492+
var roleLabelChoice string
503493

504-
if !w.config.IsProcessCredentialsFormat() {
505-
rich, _, err := core.RunTemplate(roleSelectedTemplate, roleData)
506-
if err != nil {
507-
return "", err
508-
}
509-
fmt.Fprintln(os.Stderr, rich)
510-
}
511-
return roleARN, nil
494+
// There is only a single choice so go ahead and use its label
495+
if len(roleARNs) == 1 {
496+
rArn := roleARNs[0]
497+
roleLabelChoice = w.choiceFriendlyLabelRole(rArn, configRoles)
512498
}
513499

514-
promptRoles := []string{}
515-
labelsARNs := map[string]string{}
516-
for _, arn := range roleARNs {
517-
roleLabel := w.choiceFriendlyLabelRole(arn, configRoles)
518-
promptRoles = append(promptRoles, roleLabel)
519-
labelsARNs[roleLabel] = arn
500+
// The user already provided their choice via config
501+
if roleLabelChoice == "" && w.config.AWSIAMRole() != "" {
502+
rArg := w.config.AWSIAMRole()
503+
roleLabelChoice = w.choiceFriendlyLabelRole(rArg, configRoles)
520504
}
521505

522-
prompt := &survey.Select{
523-
Message: chooseRole,
524-
Options: promptRoles,
525-
}
526-
var selected string
527-
err = survey.AskOne(prompt, &selected, survey.WithValidator(survey.Required), stderrIsOutAskOpt)
528-
if err != nil {
529-
return "", fmt.Errorf(askRoleError, err)
506+
// Prompt the user to choose
507+
if roleLabelChoice == "" {
508+
prompt := &survey.Select{
509+
Message: chooseRole,
510+
Options: roleLabels,
511+
}
512+
err = survey.AskOne(prompt, &roleLabelChoice, survey.WithValidator(survey.Required), stderrIsOutAskOpt)
513+
if err != nil {
514+
return "", fmt.Errorf(askRoleError, err)
515+
}
516+
} else if !w.config.IsProcessCredentialsFormat() {
517+
// The choice was determined without prompting the user so pretty print the role
518+
// todo: explain why we check IsProcessCredentialsFormat?
519+
rich, _, err := core.RunTemplate(roleSelectedTemplate, roleTemplateData{
520+
Role: roleLabelChoice,
521+
})
522+
if err != nil {
523+
return "", err
524+
}
525+
fmt.Fprintln(os.Stderr, rich)
530526
}
531527

532-
roleARN = labelsARNs[selected]
528+
roleARN = roleArnByLabel[roleLabelChoice]
533529
if roleARN == "" {
534530
return "", fmt.Errorf(noRolesError, idp)
535531
}

0 commit comments

Comments
 (0)