Skip to content

Commit

Permalink
Fix auto-complete when DefaultSSO != Default
Browse files Browse the repository at this point in the history
Users who chose something other than to set `DefaultSSO: Default`
would find that auto-complete would present the wrong values or
in the case of no `Default` instance, no options at all.

We now load the config file and find the user specificed DefaultSSO
and load those values from the cache.

Fixes: #249
  • Loading branch information
synfinatic committed Jan 16, 2022
1 parent 89b1f59 commit 4c9dffe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Bug Fixes

* `AWS_SSO` env var is now set with the `eval` and `exec` command #251
* `AWS_SSO` env var is now set with the `eval` and `exec` command #251
* Fix broken auto-complete for non-Default AWS SSO instances #249

### Changes

Expand Down
20 changes: 19 additions & 1 deletion cmd/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package main
*/

import (
"fmt"
"io/ioutil"
"os"
"strings"

// "github.com/davecgh/go-spew/spew"
"github.com/goccy/go-yaml"
"github.com/posener/complete"
"github.com/synfinatic/aws-sso-cli/sso"
Expand Down Expand Up @@ -66,17 +69,32 @@ var AvailableAwsRegions []string = []string{

// NewPredictor loads our cache file (if exists) and loads the values
func NewPredictor(cacheFile, configFile string) *Predictor {
defaults := map[string]interface{}{}
override := sso.OverrideSettings{}
p := Predictor{
configFile: configFile,
}
c, err := sso.OpenCache(cacheFile, &sso.Settings{})
ssoName := os.Getenv("AWS_SSO")
if ssoName != "" {
override.DefaultSSO = ssoName
}

settings, err := sso.LoadSettings(configFile, cacheFile, defaults, override)
if err != nil {
fmt.Printf("Unable to open config. Auto-complete is disabled: %s", err.Error())
return &p
}

c, err := sso.OpenCache(cacheFile, settings)
if err != nil {
fmt.Printf("Unable to open cache. Auto-complete is disabled: %s", err.Error())
return &p
}

uniqueRoles := map[string]bool{}

cache := c.GetSSO()
// fmt.Printf("cache: %s", spew.Sdump(c))
for i, a := range cache.Roles.Accounts {
id, _ := utils.AccountIdToString(i)
p.accountids = append(p.accountids, id)
Expand Down
23 changes: 23 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [Example of multiple AWS SSO instances](#example-of-multiple-aws-sso-instances)
* [What are the purpose of the Tags?](#what-are-the-purpose-of-the-tags)
* [Which SecureStore should I use?](#which-securestore-should-i-use)
* [Using non-default AWS SSO instances with auto-complete](#using-non-default-aws-sso-instances-with-auto-complete)

### How do I delete all secrets from the macOS keychain?

Expand Down Expand Up @@ -211,3 +212,25 @@ Is there another secure storage backend you would like to see AWS SSO CLI
support? If so, please [open a feature request](
https://github.com/synfinatic/aws-sso-cli/issues/new?assignees=&labels=enhancement&template=feature_request.md)
and let me know!


### Using non-default AWS SSO instances with auto-complete

The handling of the auto-completion of the `-A`, `-R`, and `-a` flags happens
before processing of the command line arguments so you can not use the `--sso` / `-S`
flag to specify a non-default AWS SSO instance. The result is it will always
present your [DefaultSSO](config.md#defaultsso) list of accounts and roles.

If you wish to use auto-complete with a different AWS SSO instance, you must
first set the `AWS_SSO` environment variable in your shell:

```bash
$ export AWS_SSO=OtherInstance
$ aws eval ...
```

Note, the following shorter version of specifying it as a single command does not work:

```bash
$ AWS_SSO=OtherInstance aws-sso eval ...
```
2 changes: 1 addition & 1 deletion sso/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func OpenCache(f string, s *Settings) (*Cache, error) {
ConfigCreatedAt: 0,
Version: 1, // use an invalid default version for cache files without a version
SSO: map[string]*SSOCache{},
ssoName: s.DefaultSSO,
ssoName: s.DefaultSSO, // default to the config file default
}

var err error
Expand Down

0 comments on commit 4c9dffe

Please sign in to comment.