Skip to content

Commit

Permalink
Add --no-cache flag
Browse files Browse the repository at this point in the history
Force refreshing the cache and query AWS on a per-command basis for
config-profiles, console, exec and list.

Fixes: #574
  • Loading branch information
synfinatic committed Sep 27, 2023
1 parent 636bc4d commit 9817763
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/aws-sso/config_profiles_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (cc *ConfigProfilesCmd) Run(ctx *RunContext) error {

urlAction, _ := url.NewAction(string(action))

// refresh our cache
// always refresh our cache
c := &CacheCmd{}
if err = c.Run(ctx); err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion cmd/aws-sso/console_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ const AWS_FEDERATED_URL = "https://signin.aws.amazon.com/federation"

type ConsoleCmd struct {
// Console actually should honor the --region flag
Region string `kong:"help='AWS Region',env='AWS_DEFAULT_REGION',predictor='region'"`
Duration int32 `kong:"short='d',help='AWS Session duration in minutes (default 60)'"` // default stored in DEFAULT_CONFIG
Prompt bool `kong:"short='P',help='Force interactive prompt to select role'"`
NoCache bool `kong:"help='Do not use cache'"`
Region string `kong:"help='AWS Region',env='AWS_DEFAULT_REGION',predictor='region'"`

Arn string `kong:"short='a',help='ARN of role to assume',env='AWS_SSO_ROLE_ARN',predictor='arn'"`
AccountId int64 `kong:"name='account',short='A',help='AWS AccountID of role to assume',env='AWS_SSO_ACCOUNT_ID',predictor='accountId'"`
Expand All @@ -68,6 +69,13 @@ func (cc *ConsoleCmd) Run(ctx *RunContext) error {
return fmt.Errorf("Invalid --duration %d. Must be between 15 and 720", ctx.Settings.ConsoleDuration)
}

if ctx.Cli.Console.NoCache {
c := &CacheCmd{}
if err := c.Run(ctx); err != nil {
return err
}
}

// do we force interactive prompt?
if ctx.Cli.Console.Prompt {
return ctx.PromptExec(openConsole)
Expand Down
8 changes: 8 additions & 0 deletions cmd/aws-sso/exec_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ExecCmd struct {
AccountId int64 `kong:"name='account',short='A',help='AWS AccountID of role to assume',env='AWS_SSO_ACCOUNT_ID',predictor='accountId'"`
Role string `kong:"short='R',help='Name of AWS Role to assume',env='AWS_SSO_ROLE_NAME',predictor='role'"`
Profile string `kong:"short='p',help='Name of AWS Profile to assume',predictor='profile'"`
NoCache bool `kong:"help='Do not use cache'"`
NoRegion bool `kong:"short='n',help='Do not set AWS_DEFAULT_REGION from config.yaml'"`

// Exec Params
Expand All @@ -58,6 +59,13 @@ func (cc *ExecCmd) Run(ctx *RunContext) error {
return execCmd(ctx, awssso, sci.AccountId, sci.RoleName)
}

if ctx.Cli.Exec.NoCache {
c := &CacheCmd{}
if err = c.Run(ctx); err != nil {
return err
}
}

return ctx.PromptExec(execCmd)
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/aws-sso/list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ListCmd struct {
CSV bool `kong:"help='Generate CSV instead of a table',xor='listfields'"`
Prefix string `kong:"short='P',help='Filter based on the <FieldName>=<Prefix>'"`
Fields []string `kong:"optional,arg,help='Fields to display',env='AWS_SSO_FIELDS',predictor='fieldList',xor='listfields'"`
NoCache bool `kong:"help='Do not use cache'"`
Sort string `kong:"short='s',help='Sort results by the <FieldName>',default='AccountId',env='AWS_SSO_FIELD_SORT',predictor='fieldList'"`
Reverse bool `kong:"help='Reverse sort results',env='AWS_SSO_FIELD_SORT_REVERSE'"`
}
Expand All @@ -54,6 +55,13 @@ func (cc *ListCmd) Run(ctx *RunContext) error {
return nil
}

if ctx.Cli.List.NoCache {
c := &CacheCmd{}
if err = c.Run(ctx); err != nil {
return err
}
}

if ctx.Cli.List.Prefix != "" {
if !strings.Contains(ctx.Cli.List.Prefix, "=") {
return fmt.Errorf("--prefix must be in the format of <FieldName>=<Prefix>")
Expand Down
2 changes: 1 addition & 1 deletion cmd/aws-sso/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type CLI struct {
List ListCmd `kong:"cmd,help='List all accounts / roles (default command)'"`
Logout LogoutCmd `kong:"cmd,help='Logout in browser and invalidate all credentials'"`
Process ProcessCmd `kong:"cmd,help='Generate JSON for credential_process in ~/.aws/config'"`
Static StaticCmd `kong:"cmd,help='Manage static AWS API credentials',hidden"`
Static StaticCmd `kong:"cmd,hidden,help='Manage static AWS API credentials'"`
Tags TagsCmd `kong:"cmd,help='List tags'"`
Time TimeCmd `kong:"cmd,help='Print how much time before current STS Token expires'"`
Completions CompleteCmd `kong:"cmd,help='Manage shell completions'"`
Expand Down

0 comments on commit 9817763

Please sign in to comment.