forked from 99designs/aws-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
246 lines (195 loc) · 6.12 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"github.com/99designs/aws-vault/keyring"
"github.com/99designs/aws-vault/prompt"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
KeyringName = "aws-vault"
)
var (
Version string
keyringImpl keyring.Keyring
promptsAvailable = prompt.Available()
backendsAvailable = keyring.SupportedBackends()
)
type Ui struct {
*log.Logger
Error, Debug *log.Logger
Exit func(code int)
}
type logWriter struct{ *log.Logger }
func (w logWriter) Write(b []byte) (int, error) {
w.Printf("%s", b)
return len(b), nil
}
type globalFlags struct {
Debug bool
Backend string
PromptDriver string
}
func configureAddCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := AddCommandInput{}
cmd := app.Command("add", "Adds credentials, prompts if none provided")
cmd.Arg("profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Flag("env", "Read the credentials from the environment").
BoolVar(&input.FromEnv)
cmd.Action(func(c *kingpin.ParseContext) error {
input.Keyring = keyringImpl
AddCommand(ui, input)
return nil
})
}
func configureListCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := LsCommandInput{}
cmd := app.Command("ls", "List profiles")
cmd.Action(func(c *kingpin.ParseContext) error {
input.Keyring = keyringImpl
LsCommand(ui, input)
return nil
})
}
func configureRotateCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := RotateCommandInput{}
cmd := app.Command("rotate", "Rotates credentials")
cmd.Arg("profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Flag("mfa-token", "The mfa token to use").
Short('t').
StringVar(&input.MfaToken)
cmd.Action(func(c *kingpin.ParseContext) error {
input.MfaPrompt = prompt.Method(g.PromptDriver)
input.Keyring = keyringImpl
RotateCommand(ui, input)
return nil
})
}
func configureExecCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := ExecCommandInput{}
cmd := app.Command("exec", "Executes a command with AWS credentials in the environment")
cmd.Flag("no-session", "Use root credentials, no session created").
Short('n').
BoolVar(&input.NoSession)
cmd.Flag("session-ttl", "Expiration time for aws session").
Default("4h").
OverrideDefaultFromEnvar("AWS_SESSION_TTL").
Short('t').
DurationVar(&input.Duration)
cmd.Flag("assume-role-ttl", "Expiration time for aws assumed role").
Default("15m").
DurationVar(&input.RoleDuration)
cmd.Flag("mfa-token", "The mfa token to use").
Short('m').
StringVar(&input.MfaToken)
cmd.Flag("server", "Run the server in the background for credentials").
Short('s').
BoolVar(&input.StartServer)
cmd.Arg("profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Arg("cmd", "Command to execute").
Default(os.Getenv("SHELL")).
StringVar(&input.Command)
cmd.Arg("args", "Command arguments").
StringsVar(&input.Args)
cmd.Action(func(c *kingpin.ParseContext) error {
input.Keyring = keyringImpl
input.MfaPrompt = prompt.Method(g.PromptDriver)
input.Signals = make(chan os.Signal)
signal.Notify(input.Signals, os.Interrupt, os.Kill)
ExecCommand(ui, input)
return nil
})
}
func configureRemoveCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := RemoveCommandInput{}
cmd := app.Command("rm", "Removes credentials, including sessions")
cmd.Arg("profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Flag("sessions-only", "Only remove sessions, leave credentials intact").
Short('s').
BoolVar(&input.SessionsOnly)
cmd.Action(func(c *kingpin.ParseContext) error {
input.Keyring = keyringImpl
RemoveCommand(ui, input)
return nil
})
}
func configureLoginCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := LoginCommandInput{}
cmd := app.Command("login", "Generate a login link for the AWS Console")
cmd.Arg("profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Flag("mfa-token", "The mfa token to use").
Short('t').
StringVar(&input.MfaToken)
cmd.Flag("stdout", "Print login URL to stdout instead of opening in default browser").
Short('s').
BoolVar(&input.UseStdout)
cmd.Action(func(c *kingpin.ParseContext) error {
input.MfaPrompt = prompt.Method(g.PromptDriver)
input.Keyring = keyringImpl
LoginCommand(ui, input)
return nil
})
}
func configureServerCommand(app *kingpin.Application, ui Ui, g *globalFlags) {
input := ServerCommandInput{}
cmd := app.Command("server", "Run an ec2 instance role server locally")
cmd.Action(func(c *kingpin.ParseContext) error {
ServerCommand(ui, input)
return nil
})
}
func main() {
ui := Ui{
Logger: log.New(os.Stdout, "", 0),
Error: log.New(os.Stderr, "", 0),
Debug: log.New(ioutil.Discard, "", 0),
Exit: os.Exit,
}
app := kingpin.New("aws-vault",
`A vault for securely storing and accessing AWS credentials in development environments.`)
globals := &globalFlags{}
app.Version(Version)
app.Writer(os.Stdout)
app.Flag("debug", "Show debugging output").
BoolVar(&globals.Debug)
app.Flag("backend", fmt.Sprintf("Secret backend to use %v", backendsAvailable)).
Default(keyring.DefaultBackend).
OverrideDefaultFromEnvar("AWS_VAULT_BACKEND").
EnumVar(&globals.Backend, backendsAvailable...)
app.Flag("prompt", fmt.Sprintf("Prompt driver to use %v", promptsAvailable)).
Default("terminal").
OverrideDefaultFromEnvar("AWS_VAULT_PROMPT").
EnumVar(&globals.PromptDriver, promptsAvailable...)
app.PreAction(func(c *kingpin.ParseContext) (err error) {
keyringImpl, err = keyring.Open(KeyringName, globals.Backend)
return err
})
configureAddCommand(app, ui, globals)
configureListCommand(app, ui, globals)
configureRotateCommand(app, ui, globals)
configureExecCommand(app, ui, globals)
configureRemoveCommand(app, ui, globals)
configureLoginCommand(app, ui, globals)
configureServerCommand(app, ui, globals)
kingpin.MustParse(app.Parse(os.Args[1:]))
if globals.Debug {
ui.Debug = log.New(os.Stderr, "DEBUG ", log.LstdFlags)
log.SetFlags(0)
log.SetOutput(&logWriter{ui.Debug})
} else {
log.SetOutput(ioutil.Discard)
}
}