From 2ebdb383f525c3e2b1f7c61bf97aaa1423ee3c38 Mon Sep 17 00:00:00 2001 From: Julien Barbot Date: Fri, 9 Dec 2022 10:44:54 +0100 Subject: [PATCH 1/4] Single quote database args in DSN https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING --- backend/pkg/database/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/pkg/database/database.go b/backend/pkg/database/database.go index 1083d9e3..e68913f5 100644 --- a/backend/pkg/database/database.go +++ b/backend/pkg/database/database.go @@ -145,7 +145,7 @@ func initDataBase(config *DBConfig) *gorm.DB { } func initPostgres(config *DBConfig, dbLogger logger.Interface) *gorm.DB { - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=UTC", + dsn := fmt.Sprintf("host='%s' user='%s' password='%s' dbname='%s' port='%s' sslmode='disable' TimeZone=UTC", config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.DBPort) db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ From 4883c676c851f653698a85d1c708333b1ec28bb5 Mon Sep 17 00:00:00 2001 From: Julien Barbot Date: Fri, 9 Dec 2022 12:42:10 +0100 Subject: [PATCH 2/4] Adds DB_DSN environment variable --- backend/pkg/backend/backend.go | 1 + backend/pkg/config/config.go | 3 +++ backend/pkg/database/database.go | 24 ++++++++++++++++++------ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/backend/pkg/backend/backend.go b/backend/pkg/backend/backend.go index 82f7dc14..8ab6698c 100644 --- a/backend/pkg/backend/backend.go +++ b/backend/pkg/backend/backend.go @@ -80,6 +80,7 @@ func CreateBackend(config *_config.Config, monitor *k8smonitor.Monitor, speculat func createDatabaseConfig(config *_config.Config) *_database.DBConfig { return &_database.DBConfig{ DriverType: config.DatabaseDriver, + DSN: config.DBDSN, EnableInfoLogs: config.EnableDBInfoLogs, DBPassword: config.DBPassword, DBUser: config.DBUser, diff --git a/backend/pkg/config/config.go b/backend/pkg/config/config.go index 3c13cbe4..948e0867 100644 --- a/backend/pkg/config/config.go +++ b/backend/pkg/config/config.go @@ -55,6 +55,7 @@ const ( DBHostEnvVar = "DB_HOST" DBPortEnvVar = "DB_PORT_NUMBER" DatabaseDriver = "DATABASE_DRIVER" + DBDSN = "DB_DSN" EnableDBInfoLogs = "ENABLE_DB_INFO_LOGS" ResponseHeadersToIgnore = "RESPONSE_HEADERS_TO_IGNORE" @@ -95,6 +96,7 @@ type Config struct { // database config DatabaseDriver string + DBDSN string DBName string DBUser string DBPassword string @@ -130,6 +132,7 @@ func LoadConfig() (*Config, error) { config.EnableK8s = viper.GetBool(EnableK8s) config.EnableTLS = viper.GetBool(EnableTLS) config.DatabaseDriver = viper.GetString(DatabaseDriver) + config.DBDSN = viper.GetString(DBDSN) config.DBPassword = viper.GetString(DBPasswordEnvVar) config.DBUser = viper.GetString(DBUserEnvVar) config.DBHost = viper.GetString(DBHostEnvVar) diff --git a/backend/pkg/database/database.go b/backend/pkg/database/database.go index e68913f5..df18d735 100644 --- a/backend/pkg/database/database.go +++ b/backend/pkg/database/database.go @@ -54,6 +54,7 @@ type Handler struct { type DBConfig struct { EnableInfoLogs bool DriverType string + DSN string DBPassword string DBUser string DBHost string @@ -126,7 +127,7 @@ func initDataBase(config *DBConfig) *gorm.DB { case DBDriverTypePostgres: db = initPostgres(config, dbLogger) case DBDriverTypeLocal: - db = initSqlite(dbLogger) + db = initSqlite(config, dbLogger) default: log.Fatalf("DB driver is not supported: %v", dbDriver) } @@ -145,8 +146,13 @@ func initDataBase(config *DBConfig) *gorm.DB { } func initPostgres(config *DBConfig, dbLogger logger.Interface) *gorm.DB { - dsn := fmt.Sprintf("host='%s' user='%s' password='%s' dbname='%s' port='%s' sslmode='disable' TimeZone=UTC", - config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.DBPort) + var dsn string + if config.DSN != "" { + dsn = config.DSN + } else { + dsn = fmt.Sprintf("host='%s' user='%s' password='%s' dbname='%s' port='%s' sslmode='disable' TimeZone=UTC", + config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.DBPort) + } db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ Logger: dbLogger, @@ -158,10 +164,16 @@ func initPostgres(config *DBConfig, dbLogger logger.Interface) *gorm.DB { return db } -func initSqlite(dbLogger logger.Interface) *gorm.DB { - cleanLocalDataBase(localDBPath) +func initSqlite(config *DBConfig, dbLogger logger.Interface) *gorm.DB { + var dsn string + if config.DSN != "" { + dsn = config.DSN + } else { + dsn = localDBPath + cleanLocalDataBase(localDBPath) + } - db, err := gorm.Open(sqlite.Open(localDBPath), &gorm.Config{ + db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{ Logger: dbLogger, }) if err != nil { From 0f27ccb87246a718f2ac1cc03b70ef38fd7835a7 Mon Sep 17 00:00:00 2001 From: Julien Barbot Date: Fri, 9 Dec 2022 12:42:39 +0100 Subject: [PATCH 3/4] Adds documentation for DB_DSN --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index bdf739f7..5d569da3 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,32 @@ Update [values.yaml](https://github.com/openclarity/apiclarity/blob/master/chart 4. Open APIClarity UI in the browser: +## Database + +SQLite, for local testing, and PostgreSQL databases are supported. + +To choose your database type, use the `DATABASE_DRIVER` environment variable: + +* LOCAL: Select SQLite, and create a local file called `db.db` +* POSTGRES: Select PostgreSQL. In that case you can specifiy other self-explanatory environment variables: + * DB_NAME + * DB_USER + * DB_PASS + * DB_HOST + * DB_PORT_NUMBER + +In any case, you can also specify the full DSN yourself, depending on your +database type, by setting the `DB_DSN` environment variable. +For example: + + DATABASE_DRIVER="POSTGRES" DB_DSN="postgresql:///apiclarity?host=/tmp/apiclaritydb" + + DATABASE_DRIVER="POSTGRES" DB_DSN="postgresql://user@localhost:5433/mydb?options=-c%20synchronous_commit%3Doff" + + DATABASE_DRIVER="LOCAL" DB_DSN="file::memory:?cache=shared" + + DATABASE_DRIVER="LOCAL" DB_DSN="file:/home/fred/data.db?vfs=unix-dotfile" + ## Contributing Pull requests and bug reports are welcome. From 268c73c56da7bd41d522f473c6e6519d49acffac Mon Sep 17 00:00:00 2001 From: Julien Barbot Date: Mon, 12 Dec 2022 09:37:46 +0100 Subject: [PATCH 4/4] Be more explicit in the README.md about databases --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d569da3..1506b836 100644 --- a/README.md +++ b/README.md @@ -192,8 +192,8 @@ SQLite, for local testing, and PostgreSQL databases are supported. To choose your database type, use the `DATABASE_DRIVER` environment variable: -* LOCAL: Select SQLite, and create a local file called `db.db` -* POSTGRES: Select PostgreSQL. In that case you can specifiy other self-explanatory environment variables: +* LOCAL: SQLite driver is used. By default, the the `db.db` SQlite file will be used. +* POSTGRES: PostgreSQL driver is used. In that case you can specifiy other self-explanatory environment variables: * DB_NAME * DB_USER * DB_PASS @@ -202,6 +202,10 @@ To choose your database type, use the `DATABASE_DRIVER` environment variable: In any case, you can also specify the full DSN yourself, depending on your database type, by setting the `DB_DSN` environment variable. + +https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING +https://www.sqlite.org/c3ref/open.html#urifilenameexamples + For example: DATABASE_DRIVER="POSTGRES" DB_DSN="postgresql:///apiclarity?host=/tmp/apiclaritydb"