-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmscli.go
224 lines (190 loc) · 4.97 KB
/
kmscli.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
package main
import (
"os"
"github.com/urfave/cli"
"fmt"
"encoding/json"
"bytes"
)
func CheckApp(app string) {
if app == "" {
//TODO: Try to get app name from containing folder
panic("No App Provided")
}
}
func CheckEnv(env string) {
if env == "" {
panic("No Env Provided")
}
}
func CheckName(name string) {
if(name == "") {
panic("No name provided for secret!")
}
}
func addSecret(app, env, name string) {
CheckApp(app)
CheckEnv(env)
CheckName(name)
CheckAndAddKey(app, env)
fmt.Println("Adding secret called:", name)
//prompt for secret value
secret := GetInput("Enter secret value: ")
if(secret == "") {
panic("No Secret Added!")
}
secretsExist, secretPath := CheckForSecretsFile(env, true)
if(!secretsExist) {
//Create
secrets := map[string]string{}
secrets[name] = secret
ParseEncryptWrite(secrets, app, env, secretPath)
} else {
secrets := ReadDecryptParse(secretPath, app, env)
secrets[name] = secret
ParseEncryptWrite(secrets, app, env, secretPath)
}
}
func RemoveSecrets(name, app, env string) {
CheckName(name)
CheckEnv(env)
CheckApp(app)
fmt.Println("env", env)
//Check for secrets file
secretsExist, secretPath := CheckForSecretsFile(env, false)
if(secretsExist) {
//Decrypt and parse secrets
secrets := ReadDecryptParse(secretPath, app, env)
delete(secrets, name)
ParseEncryptWrite(secrets, app, env, secretPath)
} else {
fmt.Println("No secrets file for env")
}
}
func ListSecrets(app, env string) {
CheckEnv(env)
CheckApp(app)
secretsExist, secretPath := CheckForSecretsFile(env, false)
if(secretsExist) {
encryptedSecrets := ReadFile(secretPath)
result := Decrypt(GetKMSSession(), app, env, encryptedSecrets)
fmt.Println(string(result))
} else {
fmt.Println("No secrets found for app / env")
os.Exit(1)
}
}
func ReadDecryptParse(path, app, env string) map[string]interface{} {
session := GetKMSSession()
encryptedSecrets := ReadFile(path)
decryptedSecrets := Decrypt(session, app, env, encryptedSecrets)
secrets := UnmarshalSecrets(decryptedSecrets)
return secrets
}
func ParseEncryptWrite(input interface{}, app, env, path string) {
session := GetKMSSession()
newJson := MarshalSecrets(input)
encryptedPayload := Encrypt(session, app, env, newJson)
WriteFile(path,encryptedPayload)
}
func CheckAndAddKey(app, env string) {
session := GetKMSSession()
aliases := ListAliases(session)
aliasExists := AliasExists(GetAliasName(app, env), aliases)
if(!aliasExists) {
createKey := BoolQuestion("Master Key doesn't exist, create?")
if(createKey) {
CreateKeyWithAlias(session, app, env)
fmt.Println("Key Created")
} else {
fmt.Println("Could not save secret without key")
os.Exit(1)
}
}
}
func JSONMarshal(v interface{}, unescape bool) ([]byte, error) {
b, err := json.Marshal(v)
if unescape {
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
}
return b, err
}
func UnmarshalSecrets(input []byte) map[string]interface{} {
var dat map[string]interface{}
err := json.Unmarshal(input, &dat)
if(err != nil) {
panic(err)
}
return dat
}
func MarshalSecrets(input interface{}) []byte {
data, err := JSONMarshal(input, true)
if(err != nil) {
panic(err)
}
return data
}
func main() {
var env string
var appName string
app := cli.NewApp()
app.Name = "AWS KMS Secrets Wrapper"
app.Usage = "Add or remove application secrets"
app.Authors = []cli.Author{
cli.Author{
Name: "kyle.white",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "env",
Usage: "The application environment to target",
Destination: &env,
},
cli.StringFlag{
Name: "app",
Usage: "The Application to target",
Destination: &appName,
},
}
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add to encrypted file for environment",
ArgsUsage: "[name]",
Action: func(c *cli.Context) error {
fmt.Println("Add to encrypted file")
fmt.Println("Env", env)
addSecret(appName, env, c.Args().Get(0))
fmt.Println("File written")
return nil
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove from encrypted file for Environment",
ArgsUsage: "[name]",
Action: func(c *cli.Context) error {
fmt.Println("Remove secret")
RemoveSecrets(c.Args().Get(0), appName, env)
fmt.Println("Secret Removed")
return nil
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List secrets for a given environment",
Action: func(c *cli.Context) error {
ListSecrets(appName, env)
return nil
},
},
}
app.Run(os.Args)
}