-
Notifications
You must be signed in to change notification settings - Fork 157
/
quickstart.go
368 lines (273 loc) · 8.6 KB
/
quickstart.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package cli
import (
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
"github.com/hatchet-dev/hatchet/internal/config/database"
"github.com/hatchet-dev/hatchet/internal/config/loader"
"github.com/hatchet-dev/hatchet/internal/config/server"
"github.com/hatchet-dev/hatchet/internal/encryption"
)
var certDir string
var generatedConfigDir string
var skip []string
var overwrite bool
const (
StageCerts string = "certs"
StageKeys string = "keys"
StageSeed string = "seed"
)
var quickstartCmd = &cobra.Command{
Use: "quickstart",
Short: "Command used to setup a Hatchet instance",
Run: func(cmd *cobra.Command, args []string) {
err := runQuickstart()
if err != nil {
red := color.New(color.FgRed)
red.Printf("Error running [%s]:%s\n", cmd.Use, err.Error())
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(quickstartCmd)
quickstartCmd.PersistentFlags().StringVar(
&certDir,
"cert-dir",
"./certs",
"path to the directory where certificates should be stored",
)
quickstartCmd.PersistentFlags().StringVar(
&generatedConfigDir,
"generated-config-dir",
"./generated",
"path to the directory where the generated config should be written",
)
quickstartCmd.PersistentFlags().StringArrayVar(
&skip,
"skip",
[]string{},
"a list of steps to skip. possible values are \"certs\"",
)
quickstartCmd.PersistentFlags().BoolVar(
&overwrite,
"overwrite",
true,
"whether generated files should be overwritten, if they exist",
)
}
func runQuickstart() error {
generated, err := loadBaseConfigFiles()
if err != nil {
return fmt.Errorf("could not get base config files: %w", err)
}
if !shouldSkip(StageCerts) {
err := setupCerts(generated)
if err != nil {
return fmt.Errorf("could not setup certs: %w", err)
}
}
if !shouldSkip(StageKeys) {
err := generateKeys(generated)
if err != nil {
return fmt.Errorf("could not generate keys: %w", err)
}
}
err = writeGeneratedConfig(generated)
if err != nil {
return fmt.Errorf("could not write generated config files: %w", err)
}
if !shouldSkip(StageSeed) {
// reload config at this point
configLoader := loader.NewConfigLoader(configDirectory)
err = runSeed(configLoader)
if err != nil {
return fmt.Errorf("could not run seed: %w", err)
}
}
return nil
}
func shouldSkip(stage string) bool {
for _, skipStage := range skip {
if stage == skipStage {
return true
}
}
return false
}
//go:embed certs/cluster-cert.conf
var ClusterCertConf []byte
//go:embed certs/internal-admin-client-cert.conf
var InternalAdminClientCertConf []byte
//go:embed certs/worker-client-cert.conf
var WorkerClientCertConf []byte
//go:embed certs/generate-certs.sh
var GenerateCertsScript string
type generatedConfigFiles struct {
sc *server.ServerConfigFile
dc *database.ConfigFile
}
func setupCerts(generated *generatedConfigFiles) error {
color.New(color.FgGreen).Printf("Generating certificates in cert directory %s\n", certDir)
// verify that bash and openssl are installed on the system
if !commandExists("openssl") {
return fmt.Errorf("openssl must be installed and available in your $PATH")
}
if !commandExists("bash") {
return fmt.Errorf("bash must be installed and available in your $PATH")
}
// write certificate config files to system
fullPathCertDir, err := filepath.Abs(certDir)
if err != nil {
return err
}
err = os.MkdirAll(fullPathCertDir, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create cert directory: %w", err)
}
err = os.WriteFile(filepath.Join(fullPathCertDir, "./cluster-cert.conf"), ClusterCertConf, 0600)
if err != nil {
return fmt.Errorf("could not create cluster-cert.conf file: %w", err)
}
err = os.WriteFile(filepath.Join(fullPathCertDir, "./internal-admin-client-cert.conf"), InternalAdminClientCertConf, 0600)
if err != nil {
return fmt.Errorf("could not create internal-admin-client-cert.conf file: %w", err)
}
err = os.WriteFile(filepath.Join(fullPathCertDir, "./worker-client-cert.conf"), WorkerClientCertConf, 0600)
if err != nil {
return fmt.Errorf("could not create worker-client-cert.conf file: %w", err)
}
// if CA files don't exists, run the script to regenerate all certs
if overwrite || (!fileExists(filepath.Join(fullPathCertDir, "./ca.key")) || !fileExists(filepath.Join(fullPathCertDir, "./ca.cert"))) {
// run openssl commands
c := exec.Command("bash", "-s", "-", fullPathCertDir)
c.Stdin = strings.NewReader(GenerateCertsScript)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
return err
}
}
generated.sc.TLS.TLSRootCAFile = filepath.Join(fullPathCertDir, "ca.cert")
generated.sc.TLS.TLSCertFile = filepath.Join(fullPathCertDir, "cluster.pem")
generated.sc.TLS.TLSKeyFile = filepath.Join(fullPathCertDir, "cluster.key")
return nil
}
func generateKeys(generated *generatedConfigFiles) error {
color.New(color.FgGreen).Printf("Generating encryption keys for Hatchet server\n")
cookieHashKey, err := encryption.GenerateRandomBytes(8)
if err != nil {
return fmt.Errorf("could not generate hash key for instance: %w", err)
}
cookieBlockKey, err := encryption.GenerateRandomBytes(8)
if err != nil {
return fmt.Errorf("could not generate block key for instance: %w", err)
}
if overwrite || (generated.sc.Auth.Cookie.Secrets == "") {
generated.sc.Auth.Cookie.Secrets = fmt.Sprintf("%s %s", cookieHashKey, cookieBlockKey)
}
// if using local keys, generate master key
if !generated.sc.Encryption.CloudKMS.Enabled {
masterKeyBytes, privateEc256, publicEc256, err := encryption.GenerateLocalKeys()
if err != nil {
return err
}
if overwrite || (generated.sc.Encryption.MasterKeyset == "") {
generated.sc.Encryption.MasterKeyset = string(masterKeyBytes)
}
if overwrite || (generated.sc.Encryption.JWT.PublicJWTKeyset == "") || (generated.sc.Encryption.JWT.PrivateJWTKeyset == "") {
generated.sc.Encryption.JWT.PrivateJWTKeyset = string(privateEc256)
generated.sc.Encryption.JWT.PublicJWTKeyset = string(publicEc256)
}
}
// generate jwt keys
if generated.sc.Encryption.CloudKMS.Enabled && (overwrite || (generated.sc.Encryption.JWT.PublicJWTKeyset == "") || (generated.sc.Encryption.JWT.PrivateJWTKeyset == "")) {
privateEc256, publicEc256, err := encryption.GenerateJWTKeysetsFromCloudKMS(
generated.sc.Encryption.CloudKMS.KeyURI,
[]byte(generated.sc.Encryption.CloudKMS.CredentialsJSON),
)
if err != nil {
return err
}
generated.sc.Encryption.JWT.PrivateJWTKeyset = string(privateEc256)
generated.sc.Encryption.JWT.PublicJWTKeyset = string(publicEc256)
}
return nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func loadBaseConfigFiles() (*generatedConfigFiles, error) {
res := &generatedConfigFiles{}
var err error
res.dc, err = loader.LoadDatabaseConfigFile(getFiles("database.yaml")...)
if err != nil {
return nil, err
}
res.sc, err = loader.LoadServerConfigFile(getFiles("server.yaml")...)
if err != nil {
return nil, err
}
return res, nil
}
func getFiles(name string) [][]byte {
files := [][]byte{}
basePath := filepath.Join(configDirectory, name)
if fileExists(basePath) {
configFileBytes, err := os.ReadFile(basePath)
if err != nil {
panic(err)
}
files = append(files, configFileBytes)
}
generatedPath := filepath.Join(generatedConfigDir, name)
if fileExists(generatedPath) {
generatedFileBytes, err := os.ReadFile(filepath.Join(generatedConfigDir, name))
if err != nil {
panic(err)
}
files = append(files, generatedFileBytes)
}
return files
}
func writeGeneratedConfig(generated *generatedConfigFiles) error {
color.New(color.FgGreen).Printf("Generating config files %s\n", generatedConfigDir)
err := os.MkdirAll(generatedConfigDir, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create generated config directory: %w", err)
}
databasePath := filepath.Join(generatedConfigDir, "./database.yaml")
databaseConfigBytes, err := yaml.Marshal(generated.dc)
if err != nil {
return err
}
err = os.WriteFile(databasePath, databaseConfigBytes, 0600)
if err != nil {
return fmt.Errorf("could not write database.yaml file: %w", err)
}
serverPath := filepath.Join(generatedConfigDir, "./server.yaml")
serverConfigBytes, err := yaml.Marshal(generated.sc)
if err != nil {
return err
}
err = os.WriteFile(serverPath, serverConfigBytes, 0600)
if err != nil {
return fmt.Errorf("could not write server.yaml file: %w", err)
}
return nil
}