-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdb.go
475 lines (409 loc) · 13.6 KB
/
db.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
// Package e2edb is a reference, instrumented runnables that are running various popular databases one could run in their
// tests or benchmarks.
package e2edb
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/e2e"
e2emon "github.com/efficientgo/e2e/monitoring"
e2eprof "github.com/efficientgo/e2e/profiling"
)
const (
MinioAccessKey = "Cheescake"
MinioSecretKey = "supersecret"
)
type Option func(*options)
type options struct {
image string
flagOverride map[string]string
minioOptions minioOptions
azuriteOptions azuriteOptions
}
type azuriteOptions struct {
enableTLS bool
customStorageAccounts string
connectionString string
}
type minioOptions struct {
enableSSE bool
enableTLS bool
}
func WithImage(image string) Option {
return func(o *options) {
o.image = image
}
}
func WithFlagOverride(ov map[string]string) Option {
return func(o *options) {
o.flagOverride = ov
}
}
func WithMinioSSE() Option {
return func(o *options) {
o.minioOptions.enableSSE = true
}
}
func WithMinioTLS() Option {
return func(o *options) {
o.minioOptions.enableTLS = true
}
}
func WithAzuriteStorageAccounts(acc string) Option {
return func(o *options) {
o.azuriteOptions.customStorageAccounts = acc
}
}
func WithAzuriteConnectionStringOverride(s string) Option {
return func(o *options) {
o.azuriteOptions.connectionString = s
}
}
const AccessPortName = "http"
func NewPrometheus(env e2e.Environment, name string, opts ...Option) *e2emon.Prometheus {
o := options{}
for _, opt := range opts {
opt(&o)
}
return e2emon.NewPrometheus(env, name, o.image, o.flagOverride)
}
func NewParca(env e2e.Environment, name string, opts ...Option) *e2eprof.Parca {
o := options{}
for _, opt := range opts {
opt(&o)
}
return e2eprof.NewParca(env, name, o.image, o.flagOverride)
}
// NewMinio returns minio server, used as a local replacement for S3.
func NewMinio(env e2e.Environment, name, bktName string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "minio/minio:RELEASE.2022-03-14T18-25-24Z"}
for _, opt := range opts {
opt(&o)
}
userID := strconv.Itoa(os.Getuid())
ports := map[string]int{AccessPortName: 8090}
envVars := []string{
"MINIO_ROOT_USER=" + MinioAccessKey,
"MINIO_ROOT_PASSWORD=" + MinioSecretKey,
"MINIO_BROWSER=" + "off",
}
f := env.Runnable(name).WithPorts(ports).Future()
// Hacky: Create user that matches ID with host ID to be able to remove .minio.sys details on the start.
// Proper solution would be to contribute/create our own minio image which is non root.
command := fmt.Sprintf("useradd -G root -u %v me && mkdir -p %s && chown -R me %s &&", userID, f.Dir(), f.Dir())
if o.minioOptions.enableSSE {
envVars = append(envVars, []string{
// https://docs.min.io/docs/minio-kms-quickstart-guide.html
"MINIO_KMS_KES_ENDPOINT=" + "https://play.min.io:7373",
"MINIO_KMS_KES_KEY_FILE=" + "root.key",
"MINIO_KMS_KES_CERT_FILE=" + "root.cert",
"MINIO_KMS_KES_KEY_NAME=" + "my-minio-key",
}...)
command += "curl -sSL --tlsv1.3 -O 'https://raw.githubusercontent.com/minio/kes/master/root.key' -O 'https://raw.githubusercontent.com/minio/kes/master/root.cert' && cp root.* /home/me/ && "
}
var readiness e2e.ReadinessProbe
if o.minioOptions.enableTLS {
if err := os.MkdirAll(filepath.Join(f.Dir(), "certs", "CAs"), 0750); err != nil {
return &e2emon.InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(name, errors.Wrap(err, "create certs dir"))}
}
if err := genCerts(
filepath.Join(f.Dir(), "certs", "public.crt"),
filepath.Join(f.Dir(), "certs", "private.key"),
filepath.Join(f.Dir(), "certs", "CAs", "ca.crt"),
fmt.Sprintf("%s-%s", env.Name(), name),
); err != nil {
return &e2emon.InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(name, errors.Wrap(err, "fail to generate certs"))}
}
envVars = append(envVars, "ENABLE_HTTPS="+"1")
command = command + fmt.Sprintf(
"su - me -s /bin/sh -c 'mkdir -p %s && %s /opt/bin/minio server --certs-dir %s/certs --address :%v --quiet %v'",
filepath.Join(f.Dir(), bktName),
strings.Join(envVars, " "),
f.Dir(),
ports[AccessPortName],
f.Dir(),
)
readiness = e2e.NewHTTPSReadinessProbe(
AccessPortName,
"/minio/health/cluster",
200,
200,
)
} else {
envVars = append(envVars, "ENABLE_HTTPS="+"0")
command = command + fmt.Sprintf(
"su - me -s /bin/sh -c 'mkdir -p %s && %s /opt/bin/minio server --address :%v --quiet %v'",
filepath.Join(f.Dir(), bktName),
strings.Join(envVars, " "),
ports[AccessPortName],
f.Dir(),
)
readiness = e2e.NewHTTPReadinessProbe(
AccessPortName,
"/minio/health/cluster",
200,
200,
)
}
return e2emon.AsInstrumented(f.Init(
e2e.StartOptions{
Image: o.image,
// Create the required bucket before starting minio.
Command: e2e.NewCommandWithoutEntrypoint(
"sh",
"-c",
command,
),
Readiness: readiness,
},
), AccessPortName)
}
// NewAzuriteBlobStorage returns azurite server, used as a local replacement for Azure Blob Storage.
func NewAzuriteBlobStorage(env e2e.Environment, name string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "mcr.microsoft.com/azure-storage/azurite:latest"}
for _, opt := range opts {
opt(&o)
}
ports := map[string]int{"blob": 10000}
f := env.Runnable(name).WithPorts(ports).Future()
envVars := []string{}
if o.azuriteOptions.customStorageAccounts != "" {
envVars = append(envVars, "AZURITE_ACCOUNTS="+o.azuriteOptions.customStorageAccounts)
}
command := fmt.Sprintf(
"'%s azurite-blob --blobHost 0.0.0.0 --blobPort %v -l %v",
strings.Join(envVars, " "),
ports["blob"],
f.Dir(),
)
if o.azuriteOptions.enableTLS {
if err := os.MkdirAll(filepath.Join(f.Dir(), "certs", "CAs"), 0750); err != nil {
return &e2emon.InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(name, errors.Wrap(err, "create certs dir"))}
}
if err := genCerts(
filepath.Join(f.Dir(), "certs", "cert.pem"),
filepath.Join(f.Dir(), "certs", "key.pem"),
filepath.Join(f.Dir(), "certs", "CAs", "ca.crt"),
fmt.Sprintf("%s-%s", env.Name(), name),
); err != nil {
return &e2emon.InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(name, errors.Wrap(err, "fail to generate certs"))}
}
// As per https://github.com/Azure/Azurite/tree/main#start-azurite-with-https-and-pem-1
command += fmt.Sprintf(
" --cert %s/certs/cert.pem --key %s/certs/key.pem --oauth basic",
f.Dir(),
f.Dir(),
)
}
command += "'"
return e2emon.AsInstrumented(f.Init(e2e.StartOptions{
Image: o.image,
Command: e2e.NewCommandWithoutEntrypoint(
"sh",
"-c",
command,
),
}), "blob")
}
// NewAzuriteBlobStorageWriter starts a container with azure CLI, to write data into azurite blob storage.
// This is needed, since Azurite doesn't support copying over dirs, but encodes into https://github.com/techfort/LokiJS db.
func NewAzuriteBlobStorageWriter(env e2e.Environment, name, containerName, tempDataDir string, opts ...Option) e2e.Runnable {
o := options{image: "mcr.microsoft.com/azure-cli:latest", azuriteOptions: azuriteOptions{
// As per https://github.com/Azure/Azurite/tree/main#default-storage-account
connectionString: "\"DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://0.0.0.0:10000/devstoreaccount1;\"",
}}
for _, opt := range opts {
opt(&o)
}
f := env.Runnable(name).Future()
command := fmt.Sprintf(
"'az storage container create -n %s --connection-string=%s",
containerName,
o.azuriteOptions.connectionString,
)
command += fmt.Sprintf(
"&& az storage blob upload-batch -d %s -s /shared/ --connection-string=%s'",
containerName,
o.azuriteOptions.connectionString,
)
return f.Init(e2e.StartOptions{
Image: o.image,
Command: e2e.NewCommandWithoutEntrypoint(
"sh",
"-c",
command,
),
Volumes: []string{tempDataDir + ":/shared"},
})
}
// genCerts generates certificates and writes those to the provided paths.
func genCerts(certPath, privkeyPath, caPath, serverName string) error {
var caRoot = &x509.Certificate{
SerialNumber: big.NewInt(2019),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
var cert = &x509.Certificate{
SerialNumber: big.NewInt(1658),
DNSNames: []string{serverName},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
NotAfter: time.Now().AddDate(10, 0, 0),
SubjectKeyId: []byte{1, 2, 3},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
caPrivKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
certPrivKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
// Generate CA cert.
caBytes, err := x509.CreateCertificate(rand.Reader, caRoot, caRoot, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
return err
}
caPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
err = os.WriteFile(caPath, caPEM, 0644)
if err != nil {
return err
}
// Sign the cert with the CA private key.
certBytes, err := x509.CreateCertificate(rand.Reader, cert, caRoot, &certPrivKey.PublicKey, caPrivKey)
if err != nil {
return err
}
certPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
err = os.WriteFile(certPath, certPEM, 0644)
if err != nil {
return err
}
certPrivKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
})
err = os.WriteFile(privkeyPath, certPrivKeyPEM, 0644)
if err != nil {
return err
}
return nil
}
func NewConsul(env e2e.Environment, name string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "consul:1.8.4"}
for _, opt := range opts {
opt(&o)
}
e2e.MergeFlags()
return e2emon.AsInstrumented(env.Runnable(name).WithPorts(map[string]int{AccessPortName: 8500}).Init(
e2e.StartOptions{
Image: o.image,
// Run consul in "dev" mode so that the initial leader election is immediate.
Command: e2e.NewCommand("agent", "-server", "-client=0.0.0.0", "-dev", "-log-level=err"),
Readiness: e2e.NewHTTPReadinessProbe(AccessPortName, "/v1/operator/autopilot/health", 200, 200, `"Healthy": true`),
},
), AccessPortName)
}
func NewDynamoDB(env e2e.Environment, name string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "amazon/dynamodb-local:1.11.477"}
for _, opt := range opts {
opt(&o)
}
return e2emon.AsInstrumented(env.Runnable(name).WithPorts(map[string]int{AccessPortName: 8000}).Init(
e2e.StartOptions{
Image: o.image,
Command: e2e.NewCommand("-jar", "DynamoDBLocal.jar", "-inMemory", "-sharedDb"),
// DynamoDB doesn't have a readiness probe, so we check if the / works even if returns 400
Readiness: e2e.NewHTTPReadinessProbe("http", "/", 400, 400),
},
), AccessPortName)
}
func NewBigtable(env e2e.Environment, name string, opts ...Option) e2e.Runnable {
o := options{image: "shopify/bigtable-emulator:0.1.0"}
for _, opt := range opts {
opt(&o)
}
return env.Runnable(name).Init(
e2e.StartOptions{
Image: o.image,
},
)
}
func NewCassandra(env e2e.Environment, name string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "rinscy/cassandra:3.11.0"}
for _, opt := range opts {
opt(&o)
}
return e2emon.AsInstrumented(env.Runnable(name).WithPorts(map[string]int{AccessPortName: 9042}).Init(
e2e.StartOptions{
Image: o.image,
// Readiness probe inspired from https://github.com/kubernetes/examples/blob/b86c9d50be45eaf5ce74dee7159ce38b0e149d38/cassandra/image/files/ready-probe.sh
Readiness: e2e.NewCmdReadinessProbe(e2e.NewCommand("bash", "-c", "nodetool status | grep UN")),
},
), AccessPortName)
}
func NewSwiftStorage(env e2e.Environment, name string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "bouncestorage/swift-aio:55ba4331"}
for _, opt := range opts {
opt(&o)
}
return e2emon.AsInstrumented(env.Runnable(name).WithPorts(map[string]int{AccessPortName: 8080}).Init(
e2e.StartOptions{
Image: o.image,
Readiness: e2e.NewHTTPReadinessProbe(AccessPortName, "/", 404, 404),
},
), AccessPortName)
}
func NewMemcached(env e2e.Environment, name string, opts ...Option) e2e.Runnable {
o := options{image: "memcached:1.6.1"}
for _, opt := range opts {
opt(&o)
}
return env.Runnable(name).WithPorts(map[string]int{AccessPortName: 11211}).Init(
e2e.StartOptions{
Image: o.image,
Readiness: e2e.NewTCPReadinessProbe(AccessPortName),
},
)
}
func NewETCD(env e2e.Environment, name string, opts ...Option) *e2emon.InstrumentedRunnable {
o := options{image: "gcr.io/etcd-development/etcd:v3.4.7"}
for _, opt := range opts {
opt(&o)
}
return e2emon.AsInstrumented(env.Runnable(name).WithPorts(map[string]int{AccessPortName: 2379, "metrics": 9000}).Init(
e2e.StartOptions{
Image: o.image,
Command: e2e.NewCommand(
"/usr/local/bin/etcd",
"--listen-client-urls=http://0.0.0.0:2379",
"--advertise-client-urls=http://0.0.0.0:2379",
"--listen-metrics-urls=http://0.0.0.0:9000",
"--log-level=error",
),
Readiness: e2e.NewHTTPReadinessProbe("metrics", "/health", 200, 204),
},
), "metrics")
}