-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.go
740 lines (609 loc) · 19.6 KB
/
storage.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
Copyright © 2021, 2022 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// Generated documentation is available at:
// https://pkg.go.dev/github.com/RedHatInsights/insights-results-aggregator-exporter
//
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/insights-results-aggregator-exporter/packages/storage.html
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"os"
"strings"
"database/sql"
_ "github.com/lib/pq" // PostgreSQL database driver
_ "github.com/mattn/go-sqlite3" // SQLite database driver
"github.com/rs/zerolog/log"
"github.com/minio/minio-go/v7"
)
// Driver types
const (
// DBDriverSQLite3 shows that db driver is sqlite
DBDriverSQLite3 DBDriver = iota
// DBDriverPostgres shows that db driver is postgres
DBDriverPostgres
)
// Error messages for all database-relevant errors
const (
unableToCloseDBRowsHandle = "Unable to close the DB rows handle"
sqlStatementExecutionError = "SQL statement execution error"
unableToRetrieveColumnTypes = "Unable to retrieve column types"
readTableContentFailed = "Read table content failed"
readListOfRecordsFailed = "Unable to read list of records"
writeOneRowToCSV = "Write one row to CSV"
sqlStatementExecuted = "SQL statement"
)
// SQL statements
const (
// Select all public tables from open database
selectListOfTablesInPostgres = `
SELECT tablename
FROM pg_catalog.pg_tables
WHERE schemaname != 'information_schema'
AND schemaname != 'pg_catalog';
`
selectListOfTablesInSQLite = `
SELECT name FROM sqlite_master
WHERE type IN ('table','view')
AND name NOT LIKE 'sqlite_%'
ORDER BY 1;
`
selectDisabledRules = `
SELECT rule_id, count(rule_id) AS rule_count
FROM rule_disable
GROUP BY rule_id
HAVING count(rule_id)>1
ORDER BY rule_count DESC;
`
)
// CSVFileExtension is common extension used for files with comma-separated records
const CSVFileExtension = ".csv"
var (
selectiveExportAllowedTables = []TableName{
"report",
"recommendation",
"rule_hit",
"rule_disable",
"rule_toggle",
"cluster_rule_user_feedback",
"cluster_user_rule_disable_feedback",
"advisor_ratings",
}
whereOrgIDFilter = " WHERE org_id IN ('%v')"
)
// Storage represents an interface to almost any database or storage system
type Storage interface {
Close() error
ReadListOfTables() ([]TableName, error)
ReadTable(tableName string, limit int) error
}
// DBStorage is an implementation of Storage interface that use selected SQL like database
// like SQLite, PostgreSQL, MariaDB, RDS etc. That implementation is based on the standard
// sql package. It is possible to configure connection via Configuration structure.
// SQLQueriesLog is log for sql queries, default is nil which means nothing is logged
type DBStorage struct {
connection *sql.DB
dbDriverType DBDriver
config *StorageConfiguration
}
// NewStorage function creates and initializes a new instance of Storage interface
func NewStorage(configuration *StorageConfiguration) (*DBStorage, error) {
log.Info().Msg("Initializing connection to storage")
// initialize database driver
driverType, driverName, dataSource, err := initAndGetDriver(configuration)
if err != nil {
log.Error().Err(err).Msg("Unsupported driver")
return nil, err
}
// print info about initialized driver
log.Info().
Str("driver", driverName).
Str("datasource", dataSource).
Msg("Making connection to data storage")
// prepare connection to database
connection, err := sql.Open(driverName, dataSource)
if err != nil {
log.Error().Err(err).Msg("Can not connect to data storage")
return nil, err
}
log.Info().Msg("Connection to storage established")
return NewFromConnection(connection, driverType, configuration), nil
}
// NewFromConnection function creates and initializes a new instance of Storage interface from prepared connection
func NewFromConnection(connection *sql.DB, dbDriverType DBDriver, config *StorageConfiguration) *DBStorage {
return &DBStorage{
connection: connection,
dbDriverType: dbDriverType,
config: config,
}
}
// initAndGetDriver initializes driver(with logs if logSQLQueries is true),
// checks if it's supported and returns driver type, driver name, dataSource and error
func initAndGetDriver(configuration *StorageConfiguration) (driverType DBDriver, driverName, dataSource string, err error) {
driverName = configuration.Driver
switch driverName {
case "sqlite3":
driverType = DBDriverSQLite3
dataSource = configuration.SQLiteDataSource
case "postgres":
driverType = DBDriverPostgres
dataSource = fmt.Sprintf(
"postgresql://%v:%v@%v:%v/%v?%v",
configuration.PGUsername,
configuration.PGPassword,
configuration.PGHost,
configuration.PGPort,
configuration.PGDBName,
configuration.PGParams,
)
default:
err = fmt.Errorf("driver %v is not supported", driverName)
return
}
return
}
// Close method closes the connection to database. Needs to be called at the
// end of application lifecycle.
func (storage DBStorage) Close() error {
log.Info().Msg("Closing connection to data storage")
// try to close the connection
if storage.connection != nil {
err := storage.connection.Close()
if err != nil {
log.Error().Err(err).Msg("Can not close connection to data storage")
return err
}
}
return nil
}
// ReadListOfTables method reads names of all public tables stored in opened
// database.
func (storage DBStorage) ReadListOfTables() ([]TableName, error) {
// slice to make list of tables
var tableList = make([]TableName, 0)
var selectListOfTables string
switch storage.dbDriverType {
case DBDriverSQLite3:
selectListOfTables = selectListOfTablesInSQLite
case DBDriverPostgres:
selectListOfTables = selectListOfTablesInPostgres
default:
return tableList, fmt.Errorf("Invalid DB driver")
}
rows, err := storage.connection.Query(selectListOfTables)
if err != nil {
return tableList, err
}
defer func() {
err := rows.Close()
if err != nil {
log.Error().Err(err).Msg(unableToCloseDBRowsHandle)
}
}()
// read all table names
for rows.Next() {
var tableName TableName
err := rows.Scan(&tableName)
if err != nil {
if closeErr := rows.Close(); closeErr != nil {
log.Error().Err(closeErr).Msg(unableToCloseDBRowsHandle)
}
return tableList, err
}
tableList = append(tableList, tableName)
}
return tableList, nil
}
// logColumnTypes is helper function to print column names and types for
// selected table.
func logColumnTypes(tableName TableName, columnTypes []*sql.ColumnType) {
log.Info().
Str("table columns", string(tableName)).
Int("columns", len(columnTypes)).
Msg("table metadata")
for i, columnType := range columnTypes {
log.Info().
Str("name", columnType.Name()).
Str("type", columnType.DatabaseTypeName()).
Int("column", i+1).Msg("column type")
}
}
// logRecordCount is helper function to print number of records stored in
// given database table.
func logRecordCount(tableName TableName, count int) {
log.Info().
Str("table name", string(tableName)).
Int("record count", count).
Msg("records in table")
}
// fillInScanArgs prepares arguments for the Scan method to retrieve row from
// selected table.
//
// Based on:
// https://stackoverflow.com/questions/42774467/how-to-convert-sql-rows-to-typed-json-in-golang#60386531
func fillInScanArgs(columnTypes []*sql.ColumnType) []interface{} {
count := len(columnTypes)
// data structure to scan one row
scanArgs := make([]interface{}, count)
for i, v := range columnTypes {
switch v.DatabaseTypeName() {
case "VARCHAR", "TEXT", "UUID", "TIMESTAMP":
scanArgs[i] = new(sql.NullString)
case "BOOL":
scanArgs[i] = new(sql.NullBool)
case "INT4":
scanArgs[i] = new(sql.NullInt64)
default:
scanArgs[i] = new(sql.NullString)
}
}
return scanArgs
}
// fillInMasterData fills the structure by row data read from database from
// selected table.
//
// Based on:
// https://stackoverflow.com/questions/42774467/how-to-convert-sql-rows-to-typed-json-in-golang#60386531
func fillInMasterData(columnTypes []*sql.ColumnType, scanArgs []interface{}) map[string]interface{} {
masterData := map[string]interface{}{}
// fill-in the data structure by row data
for i, v := range columnTypes {
if z, ok := (scanArgs[i]).(*sql.NullBool); ok {
masterData[v.Name()] = z.Bool
continue
}
if z, ok := (scanArgs[i]).(*sql.NullString); ok {
masterData[v.Name()] = z.String
continue
}
if z, ok := (scanArgs[i]).(*sql.NullInt64); ok {
masterData[v.Name()] = z.Int64
continue
}
if z, ok := (scanArgs[i]).(*sql.NullFloat64); ok {
masterData[v.Name()] = z.Float64
continue
}
if z, ok := (scanArgs[i]).(*sql.NullInt32); ok {
masterData[v.Name()] = z.Int32
continue
}
masterData[v.Name()] = scanArgs[i]
}
return masterData
}
// select1FromTable is helper function to construct query to database - read
// one record from given table.
func select1FromTable(tableName TableName) string {
// it is not possible to use parameter for table name or a key
// disable "G201 (CWE-89): SQL string formatting (Confidence: HIGH, Severity: MEDIUM)"
// #nosec G201
return fmt.Sprintf("SELECT * FROM %s LIMIT 1", string(tableName))
}
// selectCountFromTable is helper function to construct query to database -
// read number of records in table.
func selectCountFromTable(tableName TableName) string {
// it is not possible to use parameter for table name or a key
// disable "G201 (CWE-89): SQL string formatting (Confidence: HIGH, Severity: MEDIUM)"
// #nosec G201
return fmt.Sprintf("SELECT count(*) FROM %s", string(tableName))
}
func selectAllFromTable(tableName TableName) string {
// it is not possible to use parameter for table name or a key
// disable "G201 (CWE-89): SQL string formatting (Confidence: HIGH, Severity: MEDIUM)"
// #nosec G201
return fmt.Sprintf("SELECT * FROM %s", string(tableName))
}
// ReadTable method reads the whole content of selected table.
func (storage DBStorage) ReadTable(tableName TableName, limit int) ([]M, error) {
sqlStatement := selectAllFromTable(tableName)
storage.applySelectiveExport(&sqlStatement, tableName)
if limit > 0 {
sqlStatement += fmt.Sprintf(" LIMIT %d", limit)
}
log.Info().Str(sqlStatementExecuted, sqlStatement).Msg("Performing")
rows, err := storage.connection.Query(sqlStatement)
if err != nil {
log.Error().Err(err).Str(sqlStatementExecuted, sqlStatement).Msg(sqlStatementExecutionError)
return nil, err
}
defer func() {
err := rows.Close()
if err != nil {
log.Error().Err(err).Msg(unableToCloseDBRowsHandle)
}
}()
// try to retrieve column types
columnTypes, err := rows.ColumnTypes()
if err != nil {
log.Error().Err(err).Msg(unableToRetrieveColumnTypes)
return nil, err
}
logColumnTypes(tableName, columnTypes)
// prepare data structure to hold raw values
var finalRows []M
// read table row by row
for rows.Next() {
// prepare arguments for the Scan method to retrieve row from
// selected table.
scanArgs := fillInScanArgs(columnTypes)
// do the actual scan of row read from database
err := rows.Scan(scanArgs...)
if err != nil {
log.Error().Err(err).Msg("Unable to scan row")
return nil, err
}
// it is now needed to check each element of values for nil
// then to use type introspection and type assertion to be
// able to fetch the column into a typed variable if needed
masterData := fillInMasterData(columnTypes, scanArgs)
// TODO: make the export part there
// println(masterData)
finalRows = append(finalRows, masterData)
}
return finalRows, nil
}
// StoreTable function stores specified table into S3/Minio
func (storage DBStorage) StoreTable(ctx context.Context,
minioClient *minio.Client, bucketName, prefix string, tableName TableName,
limit int) error {
columnTypes, err := storage.RetrieveColumnTypes(tableName)
if err != nil {
return err
}
colNames := getColumnNames(columnTypes)
buffer := new(bytes.Buffer)
// initialize CSV writer
writer := csv.NewWriter(buffer)
err = writeColumnNames(writer, colNames)
if err != nil {
return err
}
err = storage.WriteTableContent(writer, tableName, colNames, limit)
if err != nil {
return err
}
writer.Flush()
reader := io.Reader(buffer)
// Compute exact object size instead of using default value -1
//
// Warning: possible problems with large tables and 32bit architecture
// Warning: passing -1 will allocate a large amount of memory
//
// Previous warning taken from:
// https://docs.min.io/docs/golang-client-api-reference#PutObject
size := buffer.Len()
options := minio.PutObjectOptions{ContentType: "text/csv"}
objectName := setObjectPrefix(prefix, string(tableName)) + CSVFileExtension
_, err = minioClient.PutObject(ctx, bucketName, objectName, reader, int64(size), options)
if err != nil {
return err
}
// reset buffer before it will be garbage collected
buffer.Reset()
return nil
}
// StoreTableIntoFile function stores specified table into selected file
func (storage DBStorage) StoreTableIntoFile(tableName TableName,
limit int) error {
columnTypes, err := storage.RetrieveColumnTypes(tableName)
if err != nil {
return err
}
colNames := getColumnNames(columnTypes)
fileName := string(tableName) + CSVFileExtension
// open new CSV file to be filled in
// disable "G304 (CWE-22): Potential file inclusion via variable"
fout, err := os.Create(fileName) // #nosec G304
if err != nil {
return err
}
// initialize CSV writer
writer := csv.NewWriter(fout)
err = writeColumnNames(writer, colNames)
if err != nil {
return err
}
err = storage.WriteTableContent(writer, tableName, colNames, limit)
if err != nil {
return err
}
writer.Flush()
// check for any error during export to CSV
err = writer.Error()
if err != nil {
return err
}
// close the file and check if close operation was ok
err = fout.Close()
if err != nil {
return err
}
return nil
}
// ReadRecordsCount method reads number of records stored in given database
// table.
func (storage DBStorage) ReadRecordsCount(tableName TableName) (int, error) {
sqlStatement := selectCountFromTable(tableName)
storage.applySelectiveExport(&sqlStatement, tableName)
// try to query DB
row := storage.connection.QueryRow(sqlStatement)
var count int
err := row.Scan(&count)
if err != nil {
return -1, err
}
// everything seems to be ok
logRecordCount(tableName, count)
return count, nil
}
// RetrieveColumnTypes read column types from given table
func (storage DBStorage) RetrieveColumnTypes(tableName TableName) ([]*sql.ColumnType, error) {
sqlStatement := select1FromTable(tableName)
// try to query DB
rows, err := storage.connection.Query(sqlStatement)
if err != nil {
log.Error().Err(err).Str(sqlStatementExecuted, sqlStatement).Msg(sqlStatementExecutionError)
return nil, err
}
// try to retrieve column types
columnTypes, err := rows.ColumnTypes()
if err != nil {
log.Error().Err(err).Msg(unableToRetrieveColumnTypes)
return nil, err
}
// close query
err = rows.Close()
if err != nil {
log.Error().Err(err).Msg(unableToCloseDBRowsHandle)
return nil, err
}
// everything seems to be ok
logColumnTypes(tableName, columnTypes)
return columnTypes, nil
}
// WriteTableContent method writes content of whole table into given CSV
// writera (may be file or S3 bucke)
func (storage DBStorage) WriteTableContent(writer *csv.Writer,
tableName TableName, colNames []string, limit int) error {
// now we know column types, time to perform export
finalRows, err := storage.ReadTable(tableName, limit)
if err != nil {
log.Error().Err(err).Msg(readTableContentFailed)
return err
}
for _, finalRow := range finalRows {
var columns []string
for _, colName := range colNames {
value := finalRow[colName]
str := fmt.Sprintf("%v", value)
columns = append(columns, str)
}
err = writer.Write(columns)
if err != nil {
log.Error().Err(err).Msg(writeOneRowToCSV)
return err
}
}
return nil
}
// StoreTableMetadataIntoFile method stores metadata about given tables into
// file.
func (storage DBStorage) StoreTableMetadataIntoFile(fileName string, tableNames []TableName) error {
// open new CSV file to be filled in
// disable "G304 (CWE-22): Potential file inclusion via variable"
fout, err := os.Create(fileName) // #nosec G304
if err != nil {
return err
}
err = TableMetadataToCSV(fout, tableNames, storage)
if err != nil {
// logging has been performed already
return err
}
// initialize CSV writer
writer := csv.NewWriter(fout)
// check for any error during export to CSV
err = writer.Error()
if err != nil {
return err
}
// close the file and check if close operation was ok
err = fout.Close()
if err != nil {
return err
}
return nil
}
// StoreTableMetadataIntoS3 method stores metadata about given tables into
// S3 or Minio.
func (storage DBStorage) StoreTableMetadataIntoS3(ctx context.Context,
minioClient *minio.Client, bucketName string, objectName string,
tableNames []TableName) error {
buffer := new(bytes.Buffer)
err := TableMetadataToCSV(buffer, tableNames, storage)
if err != nil {
// logging has been performed already
return err
}
// write CSV data into S3 bucket or Minio bucket
reader := io.Reader(buffer)
options := minio.PutObjectOptions{ContentType: "text/csv"}
_, err = minioClient.PutObject(ctx, bucketName, objectName, reader, -1, options)
if err != nil {
return err
}
// everything look ok
return nil
}
func getColumnNames(columnTypes []*sql.ColumnType) []string {
var colNames []string
for _, columnType := range columnTypes {
colNames = append(colNames, columnType.Name())
}
return colNames
}
func writeColumnNames(writer *csv.Writer, colNames []string) error {
err := writer.Write(colNames)
if err != nil {
log.Error().Err(err).Msg("Write column names to CSV")
return err
}
return nil
}
// ReadDisabledRules method reads rules disabled by more than one user
func (storage DBStorage) ReadDisabledRules() ([]DisabledRuleInfo, error) {
// slice to make list of disabled rule
var disabledRulesInfo = make([]DisabledRuleInfo, 0)
rows, err := storage.connection.Query(selectDisabledRules)
if err != nil {
return disabledRulesInfo, err
}
defer func() {
err := rows.Close()
if err != nil {
log.Error().Err(err).Msg(unableToCloseDBRowsHandle)
}
}()
// read all records
for rows.Next() {
var disabledRuleInfo DisabledRuleInfo
err := rows.Scan(&disabledRuleInfo.Rule, &disabledRuleInfo.Count)
if err != nil {
if closeErr := rows.Close(); closeErr != nil {
log.Error().Err(closeErr).Msg(unableToCloseDBRowsHandle)
}
return disabledRulesInfo, err
}
disabledRulesInfo = append(disabledRulesInfo, disabledRuleInfo)
}
return disabledRulesInfo, nil
}
// check whether table is allowed to be exported selectively by org_id
func selectiveExportAllowed(tablename TableName) bool {
for i := range selectiveExportAllowedTables {
if tablename == selectiveExportAllowedTables[i] {
return true
}
}
return false
}
func (storage DBStorage) applySelectiveExport(sqlStatement *string, tablename TableName) {
if storage.config.EnableOrgIDFiltering && selectiveExportAllowed(tablename) {
*sqlStatement += fmt.Sprintf(whereOrgIDFilter, strings.Join(storage.config.OrganizationsToExport, "','"))
}
}