Skip to content

Commit 6b84727

Browse files
authored
Revert "Added ability to specify multiple services top be opened with the --c…" (#872)
This reverts commit 479cc4a.
1 parent 4c48a32 commit 6b84727

File tree

4 files changed

+161
-203
lines changed

4 files changed

+161
-203
lines changed

pkg/assume/assume.go

Lines changed: 74 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func AssumeCommand(c *cli.Context) error {
307307

308308
// if getConsoleURL is true, we'll use the AWS federated login to retrieve a URL to access the console.
309309
// depending on how Granted is configured, this is then printed to the terminal or a browser is launched at the URL automatically.
310-
getConsoleURL := !assumeFlags.Bool("env") && ((assumeFlags.Bool("console") || assumeFlags.String("console-destination") != "") || assumeFlags.Bool("active-role") || assumeFlags.StringSlice("service") != nil || assumeFlags.Bool("url") || assumeFlags.String("browser-profile") != "")
310+
getConsoleURL := !assumeFlags.Bool("env") && ((assumeFlags.Bool("console") || assumeFlags.String("console-destination") != "") || assumeFlags.Bool("active-role") || assumeFlags.String("service") != "" || assumeFlags.Bool("url") || assumeFlags.String("browser-profile") != "")
311311

312312
// this makes it easy for users to copy the actual command and avoid needing to lookup profiles
313313
if !cfg.DisableUsageTips && showRerunCommand {
@@ -317,15 +317,11 @@ func AssumeCommand(c *cli.Context) error {
317317
if getConsoleURL {
318318
con := console.AWS{
319319
Profile: profile.Name,
320+
Service: assumeFlags.String("service"),
321+
Region: region,
320322
Destination: assumeFlags.String("console-destination"),
321323
}
322324

323-
services := assumeFlags.StringSlice("service")
324-
con.Service = append(con.Service, services...)
325-
326-
region := assumeFlags.String("region")
327-
con.Region = region
328-
329325
creds, err := profile.AssumeConsole(c.Context, configOpts)
330326
if err != nil && strings.HasPrefix(err.Error(), "no access") {
331327
clio.Debugw("received a No Access error", "error", err)
@@ -342,98 +338,92 @@ func AssumeCommand(c *cli.Context) error {
342338
containerProfile = assumeFlags.String("browser-profile")
343339
}
344340

345-
consoleURLs, err := con.URLs(creds)
341+
consoleURL, err := con.URL(creds)
346342
if err != nil {
347343
return err
348344
}
349345

350-
clio.Debugf("number of console urls created", "amount", len(consoleURLs))
351-
for _, consoleURL := range consoleURLs {
346+
if cfg.DefaultBrowser == browser.FirefoxKey || cfg.DefaultBrowser == browser.WaterfoxKey || cfg.DefaultBrowser == browser.FirefoxStdoutKey || cfg.DefaultBrowser == browser.FirefoxDevEditionKey || cfg.DefaultBrowser == browser.FirefoxNightlyKey {
347+
// transform the URL into the Firefox Tab Container format.
348+
consoleURL = fmt.Sprintf("ext+granted-containers:name=%s&url=%s&color=%s&icon=%s", containerProfile, url.QueryEscape(consoleURL), profile.CustomGrantedProperty("color"), profile.CustomGrantedProperty("icon"))
349+
}
352350

353-
if cfg.DefaultBrowser == browser.FirefoxKey || cfg.DefaultBrowser == browser.WaterfoxKey || cfg.DefaultBrowser == browser.FirefoxStdoutKey || cfg.DefaultBrowser == browser.FirefoxDevEditionKey || cfg.DefaultBrowser == browser.FirefoxNightlyKey {
354-
// transform the URL into the Firefox Tab Container format.
355-
consoleURL = fmt.Sprintf("ext+granted-containers:name=%s&url=%s&color=%s&icon=%s", containerProfile, url.QueryEscape(consoleURL), profile.CustomGrantedProperty("color"), profile.CustomGrantedProperty("icon"))
356-
}
351+
justPrintURL := assumeFlags.Bool("url") || cfg.DefaultBrowser == browser.StdoutKey || cfg.DefaultBrowser == browser.FirefoxStdoutKey
352+
if justPrintURL {
353+
// return the url via stdout through the CLI wrapper script and return early.
354+
fmt.Print(assumeprint.SafeOutput(consoleURL))
355+
return nil
356+
}
357357

358-
justPrintURL := assumeFlags.Bool("url") || cfg.DefaultBrowser == browser.StdoutKey || cfg.DefaultBrowser == browser.FirefoxStdoutKey
359-
if justPrintURL {
360-
// return the url via stdout through the CLI wrapper script and return early.
361-
fmt.Print(assumeprint.SafeOutput(consoleURL))
362-
return nil
363-
}
358+
browserPath := cfg.CustomBrowserPath
359+
if browserPath == "" && cfg.AWSConsoleBrowserLaunchTemplate == nil {
360+
return errors.New("default browser not configured. run `granted browser set` to configure")
361+
}
364362

365-
browserPath := cfg.CustomBrowserPath
366-
if browserPath == "" && cfg.AWSConsoleBrowserLaunchTemplate == nil {
367-
return errors.New("default browser not configured. run `granted browser set` to configure")
363+
var l Launcher
364+
switch cfg.DefaultBrowser {
365+
case browser.ChromeKey, browser.BraveKey, browser.EdgeKey, browser.ChromiumKey, browser.VivaldiKey:
366+
l = launcher.ChromeProfile{
367+
BrowserType: cfg.DefaultBrowser,
368+
ExecutablePath: browserPath,
368369
}
369-
370-
var l Launcher
371-
switch cfg.DefaultBrowser {
372-
case browser.ChromeKey, browser.BraveKey, browser.EdgeKey, browser.ChromiumKey, browser.VivaldiKey:
373-
l = launcher.ChromeProfile{
374-
BrowserType: cfg.DefaultBrowser,
375-
ExecutablePath: browserPath,
376-
}
377-
case browser.FirefoxKey, browser.WaterfoxKey:
378-
l = launcher.Firefox{
379-
ExecutablePath: browserPath,
380-
}
381-
case browser.SafariKey:
382-
l = launcher.Safari{}
383-
case browser.ArcKey:
384-
l = launcher.Arc{}
385-
case browser.FirefoxDevEditionKey:
386-
l = launcher.FirefoxDevEdition{
387-
ExecutablePath: browserPath,
388-
}
389-
case browser.FirefoxNightlyKey:
390-
l = launcher.FirefoxNightly{
391-
ExecutablePath: browserPath,
392-
}
393-
case browser.CustomKey:
394-
l, err = launcher.CustomFromLaunchTemplate(cfg.AWSConsoleBrowserLaunchTemplate, c.StringSlice("browser-launch-template-arg"))
395-
if err == launcher.ErrLaunchTemplateNotConfigured {
396-
return errors.New("error configuring custom browser, ensure that [AWSConsoleBrowserLaunchTemplate] is specified in your Granted config file")
397-
}
398-
if err != nil {
399-
return err
400-
}
401-
default:
402-
l = launcher.Open{}
370+
case browser.FirefoxKey, browser.WaterfoxKey:
371+
l = launcher.Firefox{
372+
ExecutablePath: browserPath,
373+
}
374+
case browser.SafariKey:
375+
l = launcher.Safari{}
376+
case browser.ArcKey:
377+
l = launcher.Arc{}
378+
case browser.FirefoxDevEditionKey:
379+
l = launcher.FirefoxDevEdition{
380+
ExecutablePath: browserPath,
403381
}
382+
case browser.FirefoxNightlyKey:
383+
l = launcher.FirefoxNightly{
384+
ExecutablePath: browserPath,
385+
}
386+
case browser.CustomKey:
387+
l, err = launcher.CustomFromLaunchTemplate(cfg.AWSConsoleBrowserLaunchTemplate, c.StringSlice("browser-launch-template-arg"))
388+
if err == launcher.ErrLaunchTemplateNotConfigured {
389+
return errors.New("error configuring custom browser, ensure that [AWSConsoleBrowserLaunchTemplate] is specified in your Granted config file")
390+
}
391+
if err != nil {
392+
return err
393+
}
394+
default:
395+
l = launcher.Open{}
396+
}
404397

405-
servicesString := strings.Join(services, " ")
406-
printFlagUsage(con.Region, servicesString)
407-
clio.Debugf("Opening a console for %s in your browser...", profile.Name)
398+
printFlagUsage(con.Region, con.Service)
399+
clio.Infof("Opening a console for %s in your browser...", profile.Name)
408400

409-
// now build the actual command to run - e.g. 'firefox --new-tab <URL>'
401+
// now build the actual command to run - e.g. 'firefox --new-tab <URL>'
402+
args, err := l.LaunchCommand(consoleURL, containerProfile)
403+
if err != nil {
404+
return fmt.Errorf("error building browser launch command: %w", err)
405+
}
410406

411-
args, err := l.LaunchCommand(consoleURL, containerProfile)
407+
var startErr error
408+
if l.UseForkProcess() {
409+
clio.Debugf("running command using forkprocess: %s", args)
410+
cmd, err := forkprocess.New(args...)
412411
if err != nil {
413-
return fmt.Errorf("error building browser launch command: %w", err)
414-
}
415-
416-
var startErr error
417-
if l.UseForkProcess() {
418-
clio.Debugf("running command using forkprocess: %s", args)
419-
cmd, err := forkprocess.New(args...)
420-
if err != nil {
421-
return err
422-
}
423-
startErr = cmd.Start()
424-
} else {
425-
clio.Debugf("running command without forkprocess: %s", args)
426-
cmd := exec.Command(args[0], args[1:]...)
427-
startErr = cmd.Start()
412+
return err
428413
}
414+
startErr = cmd.Start()
415+
} else {
416+
clio.Debugf("running command without forkprocess: %s", args)
417+
cmd := exec.Command(args[0], args[1:]...)
418+
startErr = cmd.Start()
419+
}
429420

430-
if startErr != nil {
431-
return clierr.New(fmt.Sprintf("Granted was unable to open a browser session automatically due to the following error: %s", startErr.Error()),
432-
// allow them to try open the url manually
433-
clierr.Info("You can open the browser session manually using the following url:"),
434-
clierr.Info(consoleURL),
435-
)
436-
}
421+
if startErr != nil {
422+
return clierr.New(fmt.Sprintf("Granted was unable to open a browser session automatically due to the following error: %s", startErr.Error()),
423+
// allow them to try open the url manually
424+
clierr.Info("You can open the browser session manually using the following url:"),
425+
clierr.Info(consoleURL),
426+
)
437427
}
438428
}
439429

pkg/assume/entrypoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func GlobalFlags() []cli.Flag {
3030
&cli.BoolFlag{Name: "export-sso-token", Aliases: []string{"es"}, Usage: "Export sso token to ~/.aws/sso/cache"},
3131
&cli.BoolFlag{Name: "unset", Aliases: []string{"un"}, Usage: "Unset all environment variables configured by Assume"},
3232
&cli.BoolFlag{Name: "url", Aliases: []string{"u"}, Usage: "Get an active console session url"},
33-
&cli.StringSliceFlag{Name: "service", Aliases: []string{"s"}, Usage: "Like --c, but opens to a specified service"},
33+
&cli.StringFlag{Name: "service", Aliases: []string{"s"}, Usage: "Like --c, but opens to a specified service"},
3434
&cli.StringFlag{Name: "region", Aliases: []string{"r"}, Usage: "region to launch the console or export to the terminal"},
3535
&cli.StringFlag{Name: "console-destination", Aliases: []string{"cd"}, Usage: "Open a web console at this destination"},
3636
&cli.StringSliceFlag{Name: "pass-through", Aliases: []string{"pt"}, Usage: "Pass args to proxy assumer"},

pkg/console/aws.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type AWS struct {
1414
Profile string
1515
Region string
16-
Service []string
16+
Service string
1717
Destination string
1818
}
1919

@@ -28,39 +28,11 @@ type awsSession struct {
2828
SessionToken string `json:"sessionToken"`
2929
}
3030

31-
func (a AWS) URLs(creds aws.Credentials) ([]string, error) {
32-
33-
urls := []string{}
34-
35-
//if region and service were not specified create a single default url and return
36-
if a.Region == "" && len(a.Service) == 0 {
37-
url, err := a.URL(creds, "", "")
38-
if err != nil {
39-
return nil, err
40-
}
41-
urls = append(urls, url)
42-
return urls, nil
43-
}
44-
45-
//if one or more services were specified in the assume command then create multiple urls to be opened up in the browser
46-
if len(a.Service) > 0 {
47-
for _, service := range a.Service {
48-
url, err := a.URL(creds, a.Region, service)
49-
if err != nil {
50-
return nil, err
51-
}
52-
urls = append(urls, url)
53-
}
54-
}
55-
56-
return urls, nil
57-
}
58-
5931
// URL retrieves an authorised access URL for the AWS console. The URL includes a security token which is retrieved
6032
// by exchanging AWS session credentials using the AWS federation endpoint.
6133
//
6234
// see: https://docs.aws.amazon.com/IAM/latest/UserGuide/example_sts_Scenario_ConstructFederatedUrl_section.html
63-
func (a AWS) URL(creds aws.Credentials, region string, service string) (string, error) {
35+
func (a AWS) URL(creds aws.Credentials) (string, error) {
6436
sess := awsSession{
6537
SessionID: creds.AccessKeyID,
6638
SessionKey: creds.SecretAccessKey,
@@ -71,12 +43,12 @@ func (a AWS) URL(creds aws.Credentials, region string, service string) (string,
7143
return "", err
7244
}
7345

74-
partition := GetPartitionFromRegion(region)
46+
partition := GetPartitionFromRegion(a.Region)
7547
clio.Debugf("Partition is detected as %s for region %s...\n", partition, a.Region)
7648

7749
u := url.URL{
7850
Scheme: "https",
79-
Host: partition.RegionalHostString(region),
51+
Host: partition.RegionalHostString(a.Region),
8052
Path: "/federation",
8153
}
8254
q := u.Query()
@@ -103,11 +75,11 @@ func (a AWS) URL(creds aws.Credentials, region string, service string) (string,
10375

10476
u = url.URL{
10577
Scheme: "https",
106-
Host: partition.RegionalHostString(region),
78+
Host: partition.RegionalHostString(a.Region),
10779
Path: "/federation",
10880
}
10981

110-
dest, err := makeDestinationURL(service, region, a.Destination)
82+
dest, err := makeDestinationURL(a.Service, a.Region, a.Destination)
11183

11284
if err != nil {
11385
return "", err

0 commit comments

Comments
 (0)