Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TSL support when connecting to database #94

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Unreleased
### Changes
- ([\#93](https://github.com/forbole/juno/pull/93)) Decode IBC transfer data to JSON for `/ibc.core.channel.v1.MsgRecvPacket` message

- ([\#94](https://github.com/forbole/juno/pull/94)) Added TSL support when connecting to database

## v4.1.0
### Changes
Expand Down
14 changes: 13 additions & 1 deletion database/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type Config struct {
MaxIdleConnections int `yaml:"max_idle_connections"`
PartitionSize int64 `yaml:"partition_size"`
PartitionBatchSize int64 `yaml:"partition_batch"`
SSLModeEnable string `yaml:"ssl_mode_enable"`
SSLRootCert string `yaml:"ssl_root_cert"`
SSLCert string `yaml:"ssl_cert"`
SSLKey string `yaml:"ssl_key"`
}

func (c *Config) getURL() *url.URL {
Expand Down Expand Up @@ -44,7 +48,7 @@ func (c *Config) GetSSLMode() string {
}

func NewDatabaseConfig(
url string,
url, sslModeEnable, sslRootCert, sslCert, sslKey string,
maxOpenConnections int, maxIdleConnections int,
partitionSize int64, batchSize int64,
) Config {
Expand All @@ -54,13 +58,21 @@ func NewDatabaseConfig(
MaxIdleConnections: maxIdleConnections,
PartitionSize: partitionSize,
PartitionBatchSize: batchSize,
SSLModeEnable: sslModeEnable,
SSLRootCert: sslRootCert,
SSLCert: sslCert,
SSLKey: sslKey,
}
}

// DefaultDatabaseConfig returns the default instance of Config
func DefaultDatabaseConfig() Config {
return NewDatabaseConfig(
"postgresql://user:password@localhost:5432/database-name?sslmode=disable&search_path=public",
"false",
"",
"",
"",
1,
1,
100000,
Expand Down
14 changes: 13 additions & 1 deletion database/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ import (
// from config. It returns a database connection handle or an error if the
// connection fails.
func Builder(ctx *database.Context) (database.Database, error) {
postgresDb, err := sqlx.Open("postgres", utils.GetEnvOr(env.DatabaseURI, ctx.Cfg.URL))
dbURI := utils.GetEnvOr(env.DatabaseURI, ctx.Cfg.URL)
dbEnableSSL := utils.GetEnvOr(env.DatabaseSSLModeEnable, ctx.Cfg.SSLModeEnable)

// Configure SSL certificates (optional)
if dbEnableSSL == "true" {
dbRootCert := utils.GetEnvOr(env.DatabaseSSLRootCert, ctx.Cfg.SSLRootCert)
dbCert := utils.GetEnvOr(env.DatabaseSSLCert, ctx.Cfg.SSLCert)
dbKey := utils.GetEnvOr(env.DatabaseSSLKey, ctx.Cfg.SSLKey)
dbURI += fmt.Sprintf(" sslmode=require sslrootcert=%s sslcert=%s sslkey=%s",
dbRootCert, dbCert, dbKey)
}

postgresDb, err := sqlx.Open("postgres", dbURI)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions database/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (suite *DbTestSuite) SetupTest() {
// Build the database
dbCfg := databaseconfig.NewDatabaseConfig(
"postgres://bdjuno:password@localhost:6433/bdjuno?sslmode=disable&search_path=public",
"false",
"",
"",
"",
-1,
-1,
100000,
Expand Down
6 changes: 5 additions & 1 deletion types/env/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package env

const (
DatabaseURI = "JUNO_DATABASE_URL"
DatabaseURI = "JUNO_DATABASE_URL"
DatabaseSSLModeEnable = "false"
DatabaseSSLRootCert = ""
DatabaseSSLCert = ""
DatabaseSSLKey = ""
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
)