-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
274 lines (252 loc) · 7.17 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
func nameAndEnvFromFilename(path string) (name string, environment string, err error) {
filename := filepath.Base(path)
if !strings.HasPrefix(filename, "secret-") {
err = fmt.Errorf("filename missing expected 'secret-' prefix: %s", filename)
return
}
if !strings.HasSuffix(filename, ".yaml") {
err = fmt.Errorf("filename is missing the expected .yaml extension: %s", filename)
return
}
trimmed := strings.TrimPrefix(filename, "templates/")
trimmed = strings.TrimPrefix(trimmed, "secret-")
trimmed = strings.TrimSuffix(trimmed, ".yaml")
split := strings.Split(trimmed, ".")
if len(split) != 2 {
err = fmt.Errorf("filename not in expected format (incorrect number of period characters): %s", filename)
return
}
name = split[0] + "-secret"
environment = split[1]
return
}
func main() {
command := ""
if len(os.Args) >= 2 {
command = os.Args[1]
}
switch command {
case "new":
if len(os.Args) != 3 || len(os.Args[2]) == 0 {
fmt.Printf("Usage:\n\tkubesealplus new (secret-example.environment.yaml)\n")
os.Exit(1)
}
new(os.Args[2])
case "rotate":
if len(os.Args) != 3 || len(os.Args[2]) == 0 {
fmt.Printf("Usage:\n\tkubesealplus rotate (secret-example.environment.yaml)\n")
os.Exit(1)
}
rotate(os.Args[2])
case "config":
if len(os.Args) != 5 || len(os.Args[2]) == 0 || os.Args[3] != "cert" {
fmt.Printf("Usage:\n\tkubesealplus config (environment) cert (file path or URL)\n")
os.Exit(1)
}
configure(os.Args[2], os.Args[3], os.Args[4])
default:
fmt.Println("Usage: kubesealplus COMMAND")
fmt.Println("")
fmt.Println("Commands:")
fmt.Println("\tnew (secret-example.environment.yaml)")
fmt.Println("\trotate (secret-example.environment.yaml)")
fmt.Println("\tconfig (environment) cert (file path or URL)")
}
}
var isValidEnv = regexp.MustCompile(`^[a-z0-9-]+$`).MatchString
func configure(environment string, configKey string, configValue string) {
if !isValidEnv(environment) {
fmt.Printf("Invalid environment value: %s\n", environment)
os.Exit(1)
}
if configKey != "cert" {
fmt.Printf("cert is the only config key supported currently")
os.Exit(1)
}
_, err := CertLoad(configValue)
if err != nil {
fmt.Printf("Unable to load cert '%s':\n%s\n", configValue, err)
os.Exit(1)
}
configFile, err := ConfigFileDefaultPath("")
if err != nil {
fmt.Printf("Unable to determine default config file path: %s", err)
os.Exit(1)
}
configDoc := ConfigDoc{}
if configDoc.Exists(configFile) {
err := configDoc.Load(configFile)
if err != nil {
fmt.Printf("Unable to load config file: %s", err)
os.Exit(1)
}
}
configDoc.SetEnvironment(environment, "cert", configValue)
err = configDoc.Save(configFile)
if err != nil {
fmt.Printf("Unable to save config file: %s", err)
os.Exit(1)
}
fmt.Printf("Cert value '%s'\nfor environment '%s'\nsuccessfully saved to config file '%s'\n", configValue, environment, configFile)
}
func loadConfig(environment string) (certFilename string, err error) {
configFile, err := ConfigFileDefaultPath("")
if err != nil {
panic(err)
}
configDoc := ConfigDoc{}
if !configDoc.Exists(configFile) {
err = fmt.Errorf("Config for environment '%s' not found. Run this:\n"+
"kubesealplus config %s cert (your-cert-file)", environment, environment)
return
}
err = configDoc.Load(configFile)
if err != nil {
err = fmt.Errorf("Error loading config file %s: %s\n", configFile, err)
return
}
certConfigValue := configDoc.Environments[environment]["cert"]
// TODO: implement caching of cert load.
// we probably only need to download it at most once per hour (or day?)
cert, err := CertLoad(certConfigValue)
if err != nil {
err = fmt.Errorf("unable to load cert '%s':\n%s\n", certConfigValue, err)
return
}
certFilename, err = ConfigWriteCert(environment, cert)
if err != nil {
err = fmt.Errorf("Unable to write latest cert to disk:\n%s\n", err)
return
}
return
}
func new(filename string) {
fileInfo, err := os.Stat(filename)
if err == nil && fileInfo != nil {
fmt.Printf("Error: cannot create new file as file already exists\n\t%s\n", filename)
os.Exit(1)
}
secretName, environment, err := nameAndEnvFromFilename(filename)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
secrets := PromptSecrets{}
namespace, err := secrets.Namespace(os.Stdin, os.Stdout)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
sealedSecret := SealedSecret{Environment: environment}
sealedSecret.Init(secretName, namespace)
rotateAndNew(&sealedSecret, secrets)
file, err := os.Create(filename)
if err != nil {
fmt.Printf("Error creating new file: %s\n", err)
os.Exit(1)
}
defer file.Close()
out, err := sealedSecret.ToTemplate(file, environment)
if err != nil {
fmt.Printf("error writing SealedSecret file %s: %s\n", filename, err)
os.Exit(1)
}
fmt.Printf("Created SealedSecret file '%s' with content:\n%s", filename, out.String())
}
func rotate(filename string) {
file, err := os.OpenFile(filename, os.O_RDWR, 0644)
if err != nil {
fmt.Printf("Cannot open file: %s\n", filename)
os.Exit(1)
}
defer file.Close()
template, err := io.ReadAll(file)
if err != nil {
fmt.Printf("Cannot read file: %s\n", filename)
os.Exit(1)
}
_, environment, err := nameAndEnvFromFilename(filename)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
sealedSecret, err := sealedSecretFromTemplate(filename, environment, string(template))
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
secrets := PromptSecrets{}
for k := range sealedSecret.Spec.EncryptedData {
secrets.InitKey(k)
}
rotateAndNew(&sealedSecret, secrets)
out, err := sealedSecret.ToTemplate(file, environment)
if err != nil {
fmt.Printf("error writing SealedSecret file %s: %s\n", filename, err)
os.Exit(1)
}
fmt.Printf("Updated SealedSecret file '%s' with content:\n%s", filename, out.String())
}
func rotateAndNew(sealedSecret *SealedSecret, secrets PromptSecrets) {
certFilename, err := loadConfig(sealedSecret.Environment)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
redo := 0
for {
var err error
if len(secrets.secrets) > 0 {
err = secrets.Update(redo, os.Stdin, os.Stdout)
} else {
err = secrets.Enter(os.Stdin, os.Stdout)
}
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
redo, err = secrets.Confirm(os.Stdin, os.Stdout)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
if redo == 0 {
break
}
}
PromptClear(os.Stdout)
newSecrets := secrets.ToValues()
secretYAML, err := createSecretYAML(
sealedSecret.Spec.Template.Metadata,
newSecrets,
)
if err != nil {
fmt.Printf("error creating Secret:\n%s\n", err)
os.Exit(1)
}
newSealedSecrets, err := createSealedSecrets(secretYAML, certFilename)
if err != nil {
fmt.Printf("error creating SealedSecret via kubeseal:\n%s\n", err)
os.Exit(1)
}
if len(newSealedSecrets) != len(newSecrets) {
fmt.Printf("error creating SealedSecret via kubeseal:\n%s\n",
"number of secrets returned do not match number given")
os.Exit(1)
}
if sealedSecret.Spec.EncryptedData == nil {
sealedSecret.Spec.EncryptedData = map[string]string{}
}
for k, v := range newSealedSecrets {
sealedSecret.Spec.EncryptedData[k] = v
}
}