forked from Versent/unicreds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
233 lines (192 loc) · 7.57 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/apex/log/handlers/json"
"github.com/alecthomas/kingpin"
"github.com/versent/unicreds"
)
var (
app = kingpin.New("unicreds", "A credential/secret storage command line tool.")
csv = app.Flag("csv", "Enable csv output for table data.").Short('c').Bool()
debug = app.Flag("debug", "Enable debug mode.").Short('d').Bool()
logJSON = app.Flag("json", "Output results in JSON").Short('j').Bool()
region = app.Flag("region", "Configure the AWS region").Short('r').String()
profile = app.Flag("profile", "Configure the AWS profile").Short('p').String()
role = app.Flag("role", "Specify an AWS role ARN to assume").Short('R').String()
dynamoTable = app.Flag("table", "DynamoDB table.").Default("credential-store").OverrideDefaultFromEnvar("UNICREDS_TABLE").Short('t').String()
alias = app.Flag("alias", "KMS key alias.").Default("alias/credstash").OverrideDefaultFromEnvar("UNICREDS_ALIAS").Short('k').String()
encContext = encryptionContext(app.Flag("enc-context", "Add a key value pair to the encryption context.").Short('E'))
// commands
cmdSetup = app.Command("setup", "Setup the dynamodb table used to store credentials.")
cmdSetupRead = cmdSetup.Flag("read", "Dynamo read capacity.").Default("4").Int64()
cmdSetupWrite = cmdSetup.Flag("write", "Dynamo write capacity.").Default("4").Int64()
cmdGet = app.Command("get", "Get a credential from the store.")
cmdGetName = cmdGet.Arg("credential", "The name of the credential to get.").Required().String()
cmdGetNoLine = cmdGet.Flag("noline", "Leave off the newline when emitting secret").Short('n').Bool()
cmdGetVersion = cmdGet.Arg("version", "The version of the credential to get.").Int()
cmdGetAll = app.Command("getall", "Get latest credentials from the store.")
cmdGetAllVersions = cmdGetAll.Flag("all", "List all versions").Bool()
cmdList = app.Command("list", "List latest credentials with names and version.")
cmdListAllVersions = cmdList.Flag("all", "List all versions").Bool()
cmdPut = app.Command("put", "Put a credential into the store.")
cmdPutName = cmdPut.Arg("credential", "The name of the credential to store.").Required().String()
cmdPutSecret = cmdPut.Arg("value", "The value of the credential to store.").Required().String()
cmdPutVersion = cmdPut.Arg("version", "Version to store with the credential.").Int()
cmdPutFile = app.Command("put-file", "Put a credential from a file into the store.")
cmdPutFileName = cmdPutFile.Arg("credential", "The name of the credential to store.").Required().String()
cmdPutFileSecretPath = cmdPutFile.Arg("value", "Path to file containing the credential to store.").Required().String()
cmdPutFileVersion = cmdPutFile.Arg("version", "Version to store with the credential.").Int()
cmdDelete = app.Command("delete", "Delete a credential from the store.")
cmdDeleteName = cmdDelete.Arg("credential", "The name of the credential to delete.").Required().String()
cmdExecute = app.Command("exec", "Execute a command with all secrets loaded as environment variables.")
cmdExecuteCommand = cmdExecute.Arg("command", "The command to execute.").Required().Strings()
// Version app version
Version = "1.0.0"
)
func main() {
app.Version(Version)
command := kingpin.MustParse(app.Parse(os.Args[1:]))
if *logJSON {
log.SetHandler(json.New(os.Stderr))
} else {
log.SetHandler(cli.Default)
}
if *debug {
log.SetLevel(log.DebugLevel)
}
unicreds.SetAwsConfig(region, profile, role)
switch command {
case cmdSetup.FullCommand():
err := unicreds.Setup(dynamoTable, cmdSetupRead, cmdSetupWrite)
if err != nil {
printFatalError(err)
}
log.WithFields(log.Fields{"status": "success"}).Info("Created table")
case cmdGet.FullCommand():
var cred *unicreds.DecryptedCredential
var err error
if *cmdGetVersion == 0 {
cred, err = unicreds.GetHighestVersionSecret(dynamoTable, *cmdGetName, encContext)
} else {
cred, err = unicreds.GetSecret(dynamoTable, *cmdGetName, unicreds.PaddedInt(*cmdGetVersion), encContext)
}
if err != nil {
printFatalError(err)
}
printEncryptionContext(encContext)
if *logJSON {
log.WithFields(log.Fields{"name": *cmdGetName, "secret": cred.Secret, "status": "success"}).Info(cred.Secret)
} else {
// Or just print, out of backwards compatibility
printSecret(cred.Secret, *cmdGetNoLine)
}
case cmdPut.FullCommand():
version, err := unicreds.ResolveVersion(dynamoTable, *cmdPutName, *cmdPutVersion)
if err != nil {
printFatalError(err)
}
printEncryptionContext(encContext)
err = unicreds.PutSecret(dynamoTable, *alias, *cmdPutName, *cmdPutSecret, version, encContext)
if err != nil {
printFatalError(err)
}
log.WithFields(log.Fields{"name": *cmdPutName, "version": version}).Info("stored")
case cmdPutFile.FullCommand():
version, err := unicreds.ResolveVersion(dynamoTable, *cmdPutFileName, *cmdPutFileVersion)
if err != nil {
printFatalError(err)
}
printEncryptionContext(encContext)
data, err := ioutil.ReadFile(*cmdPutFileSecretPath)
if err != nil {
printFatalError(err)
}
err = unicreds.PutSecret(dynamoTable, *alias, *cmdPutFileName, string(data), version, encContext)
if err != nil {
printFatalError(err)
}
log.WithFields(log.Fields{"name": *cmdPutFileName, "version": version}).Info("stored")
case cmdList.FullCommand():
creds, err := unicreds.ListSecrets(dynamoTable, *cmdListAllVersions)
if err != nil {
printFatalError(err)
}
table := unicreds.NewTable(os.Stdout)
table.SetHeaders([]string{"Name", "Version", "Created-At"})
if *csv {
table.SetFormat(unicreds.TableFormatCSV)
}
for _, cred := range creds {
table.Write([]string{cred.Name, cred.Version, cred.CreatedAtDate()})
}
if err = table.Render(); err != nil {
printFatalError(err)
}
case cmdGetAll.FullCommand():
creds, err := unicreds.GetAllSecrets(dynamoTable, *cmdGetAllVersions, encContext)
if err != nil {
printFatalError(err)
}
table := unicreds.NewTable(os.Stdout)
table.SetHeaders([]string{"Name", "Secret"})
if *csv {
table.SetFormat(unicreds.TableFormatCSV)
}
for _, cred := range creds {
table.Write([]string{cred.Name, cred.Secret})
}
if err = table.Render(); err != nil {
printFatalError(err)
}
case cmdDelete.FullCommand():
err := unicreds.DeleteSecret(dynamoTable, *cmdDeleteName)
if err != nil {
printFatalError(err)
}
case cmdExecute.FullCommand():
args := []string(*cmdExecuteCommand)
commandPath, err := exec.LookPath(args[0])
if err != nil {
printFatalError(err)
}
creds, err := unicreds.GetAllSecrets(dynamoTable, *cmdGetAllVersions, encContext)
for _, cred := range creds {
os.Setenv(cred.Name, cred.Secret)
}
err = syscall.Exec(commandPath, args, os.Environ())
if err != nil {
printFatalError(err)
}
}
}
func printFatalError(err error) {
log.WithError(err).Error("failed")
os.Exit(1)
}
func printSecret(secret string, noline bool) {
log.WithField("noline", noline).Debug("print secret")
if noline {
fmt.Print(secret)
} else {
fmt.Println(secret)
}
}
func printEncryptionContext(encContext *unicreds.EncryptionContextValue) {
if encContext == nil || len(*encContext) == 0 {
return
}
for key, value := range *encContext {
log.WithFields(log.Fields{"Key": key, "Value": *value}).Debug("Encryption Context")
}
}
func encryptionContext(s kingpin.Settings) (target *unicreds.EncryptionContextValue) {
target = unicreds.NewEncryptionContextValue()
s.SetValue((*unicreds.EncryptionContextValue)(target))
return
}