forked from 99designs/aws-vault
/
exec.go
155 lines (132 loc) · 3.5 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"log"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/99designs/aws-vault/keyring"
"github.com/99designs/aws-vault/prompt"
"github.com/aws/aws-sdk-go/aws/awserr"
"gopkg.in/alecthomas/kingpin.v2"
)
type ExecCommandInput struct {
Profile string
Command string
Args []string
Keyring keyring.Keyring
Duration time.Duration
RoleDuration time.Duration
MfaToken string
MfaPrompt prompt.PromptFunc
StartServer bool
Signals chan os.Signal
NoSession bool
}
func ExecCommand(app *kingpin.Application, input ExecCommandInput) {
if os.Getenv("AWS_VAULT") != "" {
app.Fatalf("aws-vault sessions should be nested with care, unset $AWS_VAULT to force")
return
}
var writeEnv = true
if input.NoSession && input.StartServer {
app.Fatalf("Can't start a credential server without a session")
return
}
profiles, err := awsConfigFile.Parse()
if err != nil {
app.Fatalf("Error parsing config: %v", err)
return
}
creds, err := NewVaultCredentials(input.Keyring, input.Profile, VaultOptions{
SessionDuration: input.Duration,
AssumeRoleDuration: input.RoleDuration,
MfaToken: input.MfaToken,
MfaPrompt: input.MfaPrompt,
NoSession: input.NoSession,
Profiles: profiles,
})
if err != nil {
app.Fatalf("%v", err)
}
val, err := creds.Get()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
app.Fatalf("No credentials found for profile %q", input.Profile)
} else {
app.Fatalf("Failed to get credentials: %v", err)
}
}
if input.StartServer {
if err := startCredentialsServer(creds); err != nil {
app.Fatalf("Failed to start credential server: %v", err)
} else {
writeEnv = false
}
}
profs, err := awsConfigFile.Parse()
if err != nil {
app.Fatalf("%v", err)
return
}
env := environ(os.Environ())
env.Set("AWS_VAULT", input.Profile)
env.Unset("AWS_ACCESS_KEY_ID")
env.Unset("AWS_SECRET_ACCESS_KEY")
env.Unset("AWS_CREDENTIAL_FILE")
env.Unset("AWS_DEFAULT_PROFILE")
env.Unset("AWS_PROFILE")
if region, ok := profs[input.Profile]["region"]; ok {
env.Set("AWS_DEFAULT_REGION", region)
env.Set("AWS_REGION", region)
}
if writeEnv {
log.Println("Writing temporary credentials to ENV")
env.Set("AWS_ACCESS_KEY_ID", val.AccessKeyID)
env.Set("AWS_SECRET_ACCESS_KEY", val.SecretAccessKey)
if val.SessionToken != "" {
env.Set("AWS_SESSION_TOKEN", val.SessionToken)
env.Set("AWS_SECURITY_TOKEN", val.SessionToken)
}
}
cmd := exec.Command(input.Command, input.Args...)
cmd.Env = env
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
sig := <-input.Signals
if cmd.Process != nil {
cmd.Process.Signal(sig)
}
}()
var waitStatus syscall.WaitStatus
if err := cmd.Run(); err != nil {
if err != nil {
app.Errorf("%v", err)
return
}
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
os.Exit(waitStatus.ExitStatus())
}
}
}
// environ is a slice of strings representing the environment, in the form "key=value".
type environ []string
// Unset an environment variable by key
func (e *environ) Unset(key string) {
for i := range *e {
if strings.HasPrefix((*e)[i], key+"=") {
(*e)[i] = (*e)[len(*e)-1]
*e = (*e)[:len(*e)-1]
break
}
}
}
// Set adds an environment variable, replacing any existing ones of the same key
func (e *environ) Set(key, val string) {
e.Unset(key)
*e = append(*e, key+"="+val)
}