Skip to content

Commit

Permalink
Merge pull request #55 from 99designs/no-session-flag
Browse files Browse the repository at this point in the history
Add a --no-session flag to exec
  • Loading branch information
lox committed Dec 3, 2015
2 parents 13ff58e + aebb312 commit dfb9246
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
65 changes: 43 additions & 22 deletions exec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"os"
"os/exec"
"strings"
Expand All @@ -9,6 +10,7 @@ import (

"github.com/99designs/aws-vault/keyring"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
)

type ExecCommandInput struct {
Expand All @@ -20,28 +22,57 @@ type ExecCommandInput struct {
MfaToken string
StartServer bool
Signals chan os.Signal
NoSession bool
}

func ExecCommand(ui Ui, input ExecCommandInput) {
if os.Getenv("AWS_VAULT") != "" {
ui.Fatal("aws-vault sessions should be nested with care, unset $AWS_VAULT to force")
}

creds, err := NewVaultCredentials(input.Keyring, input.Profile, VaultOptions{
SessionDuration: input.Duration,
MfaToken: input.MfaToken,
})
if err != nil {
ui.Error.Fatal(err)
}
var (
err error
val credentials.Value
writeEnv bool = true
)

val, err := creds.Get()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
ui.Error.Fatalf("No credentials found for profile %q", input.Profile)
} else {
if input.NoSession {
if input.StartServer {
ui.Error.Fatal("Can't start a credential server without a session")
}

log.Println("No session requested, be careful!")
provider := &KeyringProvider{input.Keyring, input.Profile}
val, err = provider.Retrieve()
if err != nil {
log.Fatal(err)
}
} else {
creds, err := NewVaultCredentials(input.Keyring, input.Profile, VaultOptions{
SessionDuration: input.Duration,
MfaToken: input.MfaToken,
})
if err != nil {
ui.Error.Fatal(err)
}

val, err = creds.Get()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
ui.Error.Fatalf("No credentials found for profile %q", input.Profile)
} else {
ui.Error.Fatal(err)
}
}

if input.StartServer {
if err := startCredentialsServer(ui, creds); err != nil {
ui.Error.Fatal(err)
} else {
writeEnv = false
}
}

}

profs, err := parseProfiles()
Expand All @@ -64,16 +95,6 @@ func ExecCommand(ui Ui, input ExecCommandInput) {
env.Set("AWS_REGION", region)
}

writeEnv := true

if input.StartServer {
if err := startCredentialsServer(ui, creds); err != nil {
ui.Error.Fatal(err)
} else {
writeEnv = false
}
}

if writeEnv {
ui.Debug.Println("Writing temporary credentials to ENV")

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ func main() {
addFromEnv = add.Flag("env", "Read the credentials from the environment").Bool()
ls = kingpin.Command("ls", "List profiles")
exec = kingpin.Command("exec", "Executes a command with AWS credentials in the environment")
execProfile = exec.Arg("profile", "Name of the profile").Required().String()
execNoSession = exec.Flag("no-session", "Use root credentials, no session created").Short('n').Bool()
execSessDuration = exec.Flag("session-ttl", "Expiration time for aws session").Default("4h").OverrideDefaultFromEnvar("AWS_SESSION_TTL").Short('t').Duration()
execMfaToken = exec.Flag("mfa-token", "The mfa token to use").Short('m').String()
execServer = exec.Flag("server", "Run the server in the background for credentials").Short('s').Bool()
execProfile = exec.Arg("profile", "Name of the profile").Required().String()
execCmd = exec.Arg("cmd", "Command to execute").Default(os.Getenv("SHELL")).String()
execCmdArgs = exec.Arg("args", "Command arguments").Strings()
rm = kingpin.Command("rm", "Removes credentials, including sessions")
Expand Down Expand Up @@ -109,6 +110,7 @@ func main() {
Signals: signals,
MfaToken: *execMfaToken,
StartServer: *execServer,
NoSession: *execNoSession,
})

case login.FullCommand():
Expand Down

0 comments on commit dfb9246

Please sign in to comment.