forked from 99designs/aws-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (96 loc) · 3.04 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
package main
import (
"io/ioutil"
"log"
"os"
"os/signal"
"github.com/99designs/aws-vault/keyring"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
Version string
)
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
}
func main() {
var (
debug = kingpin.Flag("debug", "Show debugging output").Bool()
add = kingpin.Command("add", "Adds credentials, prompts if none provided")
addProfile = add.Arg("profile", "Name of the profile").Required().String()
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()
execSessDuration = exec.Flag("session-ttl", "Expiration time for aws session").Default("4h").OverrideDefaultFromEnvar("AWS_SESSION_TTL").Short('t').Duration()
execWriteEnv = exec.Flag("write-env", "Write AWS env vars").Short('e').Bool()
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")
rmProfile = rm.Arg("profile", "Name of the profile").Required().String()
login = kingpin.Command("login", "Generate a login link for the AWS Console")
loginProfile = login.Arg("profile", "Name of the profile").Required().String()
)
kingpin.Version(Version)
kingpin.CommandLine.Help =
`A vault for securely storing and accessing AWS credentials in development environments.`
ui := Ui{
Logger: log.New(os.Stdout, "", 0),
Error: log.New(os.Stderr, "", 0),
Debug: log.New(ioutil.Discard, "", 0),
Exit: os.Exit,
}
keyring, err := keyring.Open("aws-vault")
if err != nil {
ui.Error.Fatal(err)
}
cmd := kingpin.Parse()
if *debug {
ui.Debug = log.New(os.Stderr, "DEBUG ", log.LstdFlags)
log.SetFlags(0)
log.SetOutput(&logWriter{ui.Debug})
} else {
log.SetOutput(ioutil.Discard)
}
switch cmd {
case ls.FullCommand():
LsCommand(ui, LsCommandInput{
Keyring: keyring,
})
case rm.FullCommand():
RemoveCommand(ui, RemoveCommandInput{
Profile: *rmProfile,
Keyring: keyring,
})
case add.FullCommand():
AddCommand(ui, AddCommandInput{
Profile: *addProfile,
Keyring: keyring,
FromEnv: *addFromEnv,
})
case exec.FullCommand():
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt, os.Kill)
ExecCommand(ui, ExecCommandInput{
Profile: *execProfile,
Command: *execCmd,
Args: *execCmdArgs,
Keyring: keyring,
Duration: *execSessDuration,
WriteEnv: *execWriteEnv,
Signals: signals,
})
case login.FullCommand():
LoginCommand(ui, LoginCommandInput{
Profile: *loginProfile,
Keyring: keyring,
})
}
}