This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
mariadb.go
428 lines (365 loc) · 12.6 KB
/
mariadb.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
/* Copyright (c) Edgeless Systems GmbH
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
package db
//go:generate sh -c "./mariadb_gen_bootstrap.sh ../../3rdparty/edgeless-mariadb > mariadbbootstrap.go"
import (
"crypto"
"crypto/sha256"
"crypto/x509"
"database/sql"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/edgelesssys/edgelessdb/edb/rt"
_ "github.com/go-sql-driver/mysql" // import driver used via the database/sql package
)
const edbInternalAddr = "EDB_INTERNAL_ADDR" // must be kept sync with src/mysqld_edb.cc
const (
filenameCA = "ca.pem"
filenameCert = "cert.pem"
filenameKey = "key.pem"
filenameCnf = "my.cnf"
filenameInit = "init.sql"
filenameBootstrapLog = "mariadb-error.log"
filenameGeneralLog = "mariadb.log"
filenameSlowQueryLog = "mariadb-slow.log"
filenameBinaryLog = "mariadb-binary.log"
FilenameErrorLog = "mariadb.err" // this one is public as we try to parse it from elsewhere in case MariaDB directly tries to call exit() due to an error
)
// ErrPreviousInitFailed is thrown when a previous initialization attempt failed, but another init or start is attempted.
var ErrPreviousInitFailed = errors.New("a previous initialization attempt failed")
// ErrNotInitializedYet is thrown when the database has not been initialized yet
var ErrNotInitializedYet = errors.New("database has not been initialized yet")
// Mariadbd is used to control mariadbd.
type Mariadbd interface {
Main(cnfPath string) int
WaitUntilStarted()
WaitUntilListenInternalReady()
}
// Mariadb is a secure database based on MariaDB.
type Mariadb struct {
internalPath, externalPath string
internalAddress, externalAddress string
debug bool
debugLogDir string
mariadbd Mariadbd
cert []byte
key crypto.PrivateKey
manifestSig []byte
ca string
attemptedInit bool
}
// NewMariadb creates a new Mariadb object.
func NewMariadb(internalPath, externalPath, internalAddress, externalAddress, certificateDNSName, logDir string, debug bool, isMarble bool, mariadbd Mariadbd) (*Mariadb, error) {
if err := os.MkdirAll(externalPath, 0700); err != nil {
return nil, err
}
d := &Mariadb{
internalPath: internalPath,
externalPath: externalPath,
internalAddress: internalAddress,
externalAddress: externalAddress,
debug: debug,
debugLogDir: logDir,
mariadbd: mariadbd,
}
var cert []byte
var key crypto.PrivateKey
var err error
if isMarble {
// When running under Marblerun, expect that it passes edb's root certificate + private key
rt.Log.Println("parsing root certificate passed from Marblerun")
cert, key, err = setupCertificateFromMarblerun()
} else {
// Otherweise in standalone mode, we generate this here
cert, key, err = createCertificate(certificateDNSName)
}
if err != nil {
return nil, err
}
d.cert = cert
d.key = key
return d, nil
}
// GetCertificate gets the database certificate.
func (d *Mariadb) GetCertificate() ([]byte, crypto.PrivateKey) {
return d.cert, d.key
}
// Initialize sets up a database according to the jsonManifest.
func (d *Mariadb) Initialize(jsonManifest []byte) error {
if d.manifestSig != nil {
return errors.New("already initialized")
}
if d.attemptedInit {
rt.Log.Println("Cannot initialize the database, a previous attempt failed. The DB is in an inconsistent state. Please provide an empty data directory.")
return ErrPreviousInitFailed
}
var man manifest
if err := json.Unmarshal(jsonManifest, &man); err != nil {
return err
}
if d.debug && !man.Debug {
return fmt.Errorf("edb was started in debug mode but the manifest does not allow debug mode")
}
if err := d.configureBootstrap(man.SQL, jsonManifest); err != nil {
return err
}
rt.Log.Println("initializing ...")
// Remove already existing log file, as we do not want replayed logs
err := os.Remove(filepath.Join(d.internalPath, filenameBootstrapLog))
if err != nil && !os.IsNotExist(err) {
return err
}
d.attemptedInit = true
// Launch MariaDB
if err := d.mariadbd.Main(filepath.Join(d.internalPath, filenameCnf)); err != 0 {
d.printErrorLog(false)
rt.Log.Printf("FATAL: bootstrap failed, MariaDB exited with error code: %d\n", err)
panic("bootstrap failed")
}
return d.printErrorLog(true)
}
// Start starts the database.
func (d *Mariadb) Start() error {
_, err := os.Stat(filepath.Join(d.externalPath, "#rocksdb"))
if os.IsNotExist(err) {
rt.Log.Println("DB has not been initialized, waiting for manifest.")
return ErrNotInitializedYet
}
if err != nil {
return err
}
if err := d.configureStart(); err != nil {
return err
}
// Set internal addr env var so that mariadb will first listen on that addr. SSL and ACL will not be active at this point,
// so we can get the cert and key from the db, write it to the memfs, and then let mariadb complete its startup sequence.
normalizedInternalAddr := net.JoinHostPort(splitHostPort(d.internalAddress, "3305"))
if err := os.Setenv(edbInternalAddr, normalizedInternalAddr); err != nil {
return err
}
rt.Log.Println("starting up ...")
go func() {
ret := d.mariadbd.Main(filepath.Join(d.internalPath, filenameCnf))
panic(fmt.Errorf("mariadbd.Main returned unexpectedly with %v", ret))
}()
d.mariadbd.WaitUntilListenInternalReady()
// errors are unrecoverable from here
cert, key, jsonManifest, err := getConfigFromSQL(normalizedInternalAddr)
if err != nil {
rt.Log.Println("An initialization attempt failed. The DB is in an inconsistent state. Please provide an empty data directory.")
rt.Log.Fatalln(err)
}
var man manifest
if err := json.Unmarshal(jsonManifest, &man); err != nil {
panic(err)
}
if d.debug && !man.Debug {
panic(fmt.Errorf("edb was started in debug mode but the manifest does not allow debug mode"))
}
d.setManifestSignature(jsonManifest)
d.ca = man.CA
d.cert = cert
d.key = key
if err := d.writeCertificates(); err != nil {
panic(err)
}
// clear env var and connect once more to signal mariadb that we are ready to start
if err := os.Setenv(edbInternalAddr, ""); err != nil {
panic(err)
}
c, err := net.Dial("tcp", normalizedInternalAddr)
if err != nil {
panic(err)
}
c.Close()
d.mariadbd.WaitUntilStarted()
rt.Log.Println("DB is running.")
return nil
}
// GetManifestSignature returns the signature of the manifest that has been used to initialize the database.
func (d *Mariadb) GetManifestSignature() []byte {
return d.manifestSig
}
func (d *Mariadb) setManifestSignature(jsonManifest []byte) {
sig := sha256.Sum256(jsonManifest)
d.manifestSig = sig[:]
}
// configure MariaDB for bootstrap
func (d *Mariadb) configureBootstrap(sql []string, jsonManifest []byte) error {
var queries string
if len(sql) > 0 {
queries = strings.Join(sql, ";\n") + ";"
}
key, err := x509.MarshalPKCS8PrivateKey(d.key)
if err != nil {
return err
}
init := fmt.Sprintf(`
CREATE DATABASE mysql;
USE mysql;
%v
FLUSH PRIVILEGES;
%v
CREATE DATABASE $edgeless;
CREATE TABLE $edgeless.config (c BLOB, k BLOB, m BLOB);
INSERT INTO $edgeless.config VALUES (%#x, %#x, %#x);
`, mariadbBootstrap, queries, d.cert, key, jsonManifest)
cnf := `
[mysqld]
datadir=` + d.externalPath + `
default-storage-engine=ROCKSDB
enforce-storage-engine=ROCKSDB
log-error =` + filepath.Join(d.internalPath, filenameBootstrapLog) + `
bootstrap
init-file=` + filepath.Join(d.internalPath, filenameInit) + `
`
if len(d.debugLogDir) > 0 {
cnf += fmt.Sprintf("%v=%v\n", "rocksdb_db_log_dir", d.debugLogDir)
} else {
cnf += fmt.Sprintf("%v=%v\n", "rocksdb_db_log_dir", d.internalPath)
}
if err := d.writeFile(filenameCnf, []byte(cnf)); err != nil {
return err
}
if err := d.writeFile(filenameInit, []byte(init)); err != nil {
return err
}
return nil
}
// configure MariaDB for regular start
func (d *Mariadb) configureStart() error {
host, port := splitHostPort(d.externalAddress, "3306")
cnf := `
[mysqld]
datadir=` + d.externalPath + `
default-storage-engine=ROCKSDB
enforce-storage-engine=ROCKSDB
user=root
bind-address=` + host + `
port=` + port + `
skip-name-resolve
require-secure-transport=1
ssl-ca = "` + filepath.Join(d.internalPath, filenameCA) + `"
ssl-cert = "` + filepath.Join(d.internalPath, filenameCert) + `"
ssl-key = "` + filepath.Join(d.internalPath, filenameKey) + `"
`
if d.debug {
// If nothing is specified ONLY error-log is printed on stderr
// Setting any of the logs without a file logs them to default files
// log-basename only works for logging to `datadir`
// https://mariadb.com/kb/en/error-log/
// https://mariadb.com/kb/en/error-log/#writing-the-error-log-to-stderr-on-unix
// https://mariadb.com/kb/en/general-query-log/
// https://mariadb.com/kb/en/slow-query-log-overview/
// http://myrocks.io/docs/getting-started/
if len(d.debugLogDir) > 0 {
logFiles := map[string]string{"log_error": FilenameErrorLog, "general_log_file": filenameGeneralLog, "slow_query_log_file": filenameSlowQueryLog, "log_bin": filenameBinaryLog, "rocksdb_db_log_dir": ""}
for logName, logFile := range logFiles {
cnf += fmt.Sprintf("%v=%v\n", logName, filepath.Join(d.debugLogDir, logFile))
}
}
cnf += `
general_log
slow_query_log
log_warnings=9
binlog-format=ROW
`
} else {
// Redirect error-log to memfs
cnf += fmt.Sprintf("%v=%v\n", "log_error", filepath.Join(d.internalPath, FilenameErrorLog))
cnf += fmt.Sprintf("%v=%v\n", "rocksdb_db_log_dir", d.internalPath)
}
return d.writeFile(filenameCnf, []byte(cnf))
}
func (d *Mariadb) writeCertificates() error {
cert, key, err := toPEM(d.cert, d.key)
if err != nil {
return err
}
var ca []byte
if d.ca != "" {
ca = []byte(d.ca)
} else {
// The manifest didn't contain a CA certificate, but we set ssl-ca in configureStart.
// Thus, we must provide one or otherwise mariadb will not accept connections.
ca, _, err = createCertificate("dummy")
if err != nil {
return err
}
ca = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca})
}
if err := d.writeFile(filenameCA, ca); err != nil {
return err
}
if err := d.writeFile(filenameCert, cert); err != nil {
return err
}
if err := d.writeFile(filenameKey, key); err != nil {
return err
}
return nil
}
func (d *Mariadb) writeFile(filename string, data []byte) error {
return ioutil.WriteFile(filepath.Join(d.internalPath, filename), data, 0600)
}
func getConfigFromSQL(address string) (cert []byte, key crypto.PrivateKey, config []byte, err error) {
db, err := sqlOpen(address)
if err != nil {
return
}
defer db.Close()
var keyRaw []byte
if err := db.QueryRow("SELECT * from $edgeless.config").Scan(&cert, &keyRaw, &config); err != nil {
return nil, nil, nil, err
}
if key, err = x509.ParsePKCS8PrivateKey(keyRaw); err != nil {
return nil, nil, nil, err
}
return
}
func sqlOpen(address string) (*sql.DB, error) {
return sql.Open("mysql", "root@tcp("+address+")/")
}
func (d *Mariadb) printErrorLog(onlyPrintOnError bool) error {
// Restore original stdout & stderr from MariaDB's redirection
if err := rt.RestoreStdoutAndStderr(); err != nil {
panic(err)
}
// Read error log from internal memfs
// This file should always be created when everything is somewhat running okay
// Even when silent startup is set and nothing was printed to the error log
errorLogBytes, err := ioutil.ReadFile(filepath.Join(d.internalPath, filenameBootstrapLog))
if err != nil {
panic("cannot read MariaDB's error log: " + err.Error())
}
errorLog := string(errorLogBytes)
// Check if "ERROR" (case insensitive) occurs in MariaDB's error log
pattern := regexp.MustCompile(`(?mi)^ERROR.*$`)
foundErrors := pattern.FindAllString(errorLog, -1)
// Print error log if an error was found or we explicitly asked for the log
if foundErrors != nil || !onlyPrintOnError {
fmt.Print(errorLog)
}
// And if we found errors, return them to the caller
if foundErrors != nil {
return errors.New(strings.Join(foundErrors, ""))
}
return nil
}