Skip to content

Commit

Permalink
feat: add TSL support when connecting to database (#94)
Browse files Browse the repository at this point in the history
<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺
v                               ✰  Thanks for creating a PR! ✰    
v    Before smashing the submit button please review the checkboxes.
v If a checkbox is n/a - please still include it but + a little note why
☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >  -->

## Description
<!-- Small description -->

[BDU-494](https://forbole.atlassian.net/jira/software/c/projects/BDU/issues/BDU-494)

## Checklist
- [x] Targeted PR against correct branch.
- [x] Linked to Github issue with discussion and accepted design OR link
to spec that describes this work.
- [x] Wrote unit tests.  
- [x] Re-reviewed `Files changed` in the Github PR explorer.


[BDU-494]:
https://forbole.atlassian.net/browse/BDU-494?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

---------

Co-authored-by: Riccardo <[email protected]>
  • Loading branch information
MonikaCat and RiccardoM authored Apr 18, 2023
1 parent 97a15cf commit 8db7b77
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
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 = "JUNO_DATABASE_SSL_MODE_ENABLED"
DatabaseSSLRootCert = "JUNO_DATABASE_SSL_ROOT_CERT"
DatabaseSSLCert = "JUNO_DATABASE_SSL_CERT"
DatabaseSSLKey = "JUNO_DATABASE_SSL_KEY"
)

0 comments on commit 8db7b77

Please sign in to comment.