Skip to content

Commit

Permalink
Merge pull request #819 from lsowen/add-no-browser
Browse files Browse the repository at this point in the history
Add `--stdout` option to `aws-vault exec`
  • Loading branch information
mtibben committed Feb 5, 2022
2 parents 280d143 + b3a9a23 commit 33b7337
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ExecCommandInput struct {
Config vault.Config
SessionDuration time.Duration
NoSession bool
UseStdout bool
}

func (input ExecCommandInput) validate() error {
Expand Down Expand Up @@ -88,6 +89,9 @@ func ConfigureExecCommand(app *kingpin.Application, a *AwsVault) {
cmd.Flag("ecs-server", "Run a ECS credential server in the background for credentials (the SDK or app must support AWS_CONTAINER_CREDENTIALS_FULL_URI)").
BoolVar(&input.StartEcsServer)

cmd.Flag("stdout", "Print the SSO link to the terminal without automatically opening the browser").
BoolVar(&input.UseStdout)

cmd.Arg("profile", "Name of the profile").
Required().
HintAction(a.MustGetProfileNames).
Expand All @@ -104,6 +108,7 @@ func ConfigureExecCommand(app *kingpin.Application, a *AwsVault) {
input.Config.MfaPromptMethod = a.PromptDriver
input.Config.NonChainedGetSessionTokenDuration = input.SessionDuration
input.Config.AssumeRoleDuration = input.SessionDuration
input.Config.SSOUseStdout = input.UseStdout

f, err := a.AwsConfigFile()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions vault/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ type Config struct {
// SSORoleName specifies the AWS SSO Role name to target.
SSORoleName string

// SSOUseStdout specifies that the system browser should not be automatically opened
SSOUseStdout bool

// SessionTags specifies assumed role Session Tags
SessionTags map[string]string

Expand Down
13 changes: 9 additions & 4 deletions vault/ssorolecredentialsprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type SSORoleCredentialsProvider struct {
SSOClient *sso.Client
AccountID string
RoleName string
UseStdout bool
}

func millisecondsTimeValue(v int64) time.Time {
Expand Down Expand Up @@ -130,10 +131,14 @@ func (p *SSORoleCredentialsProvider) newOIDCToken() (*ssooidc.CreateTokenOutput,
}
log.Printf("Created OIDC device code for %s (expires in: %ds)", p.StartURL, deviceCreds.ExpiresIn)

log.Println("Opening SSO authorization page in browser")
fmt.Fprintf(os.Stderr, "Opening the SSO authorization page in your default browser (use Ctrl-C to abort)\n%s\n", aws.ToString(deviceCreds.VerificationUriComplete))
if err := open.Run(aws.ToString(deviceCreds.VerificationUriComplete)); err != nil {
log.Printf("Failed to open browser: %s", err)
if p.UseStdout {
fmt.Fprintf(os.Stderr, "Open the SSO authorization page in a browser (use Ctrl-C to abort)\n%s\n", aws.ToString(deviceCreds.VerificationUriComplete))
} else {
log.Println("Opening SSO authorization page in browser")
fmt.Fprintf(os.Stderr, "Opening the SSO authorization page in your default browser (use Ctrl-C to abort)\n%s\n", aws.ToString(deviceCreds.VerificationUriComplete))
if err := open.Run(aws.ToString(deviceCreds.VerificationUriComplete)); err != nil {
log.Printf("Failed to open browser: %s", err)
}
}

// These are the default values defined in the following RFC:
Expand Down
1 change: 1 addition & 0 deletions vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func NewSSORoleCredentialsProvider(k keyring.Keyring, config *Config) (aws.Crede
SSOClient: sso.NewFromConfig(cfg),
AccountID: config.SSOAccountID,
RoleName: config.SSORoleName,
UseStdout: config.SSOUseStdout,
}

if UseSessionCache {
Expand Down

0 comments on commit 33b7337

Please sign in to comment.