diff --git a/config_loader.go b/config_loader.go index 13a9e2a..176f58c 100644 --- a/config_loader.go +++ b/config_loader.go @@ -2,14 +2,15 @@ package shared import ( "fmt" - "github.com/DIMO-Network/yaml" - "github.com/ethereum/go-ethereum/common" "net/url" "os" "reflect" "strconv" "strings" + "github.com/DIMO-Network/yaml" + "github.com/ethereum/go-ethereum/common" + "github.com/pkg/errors" ) diff --git a/config_loader_test.go b/config_loader_test.go index b141c96..c3dd3bd 100644 --- a/config_loader_test.go +++ b/config_loader_test.go @@ -1,12 +1,13 @@ package shared import ( - "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" "net/url" "reflect" "testing" + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" ) diff --git a/db/db_store.go b/db/db_store.go index ad0cab9..510fb6c 100644 --- a/db/db_store.go +++ b/db/db_store.go @@ -36,7 +36,7 @@ func NewDbConnectionFromSettings(ctx context.Context, settings *Settings, withSe Retries: 5, RetryDelay: time.Second * 10, ConnectTimeout: time.Minute * 5, - DSN: settings.BuildConnectionString(withSearchPath), + DSN: settings.BuildConnectionString(withSearchPath, SSLModePrefer), MaxOpenConnections: settings.MaxOpenConnections, MaxIdleConnections: settings.MaxIdleConnections, ConnMaxLifetime: time.Minute * 5, @@ -46,7 +46,7 @@ func NewDbConnectionFromSettings(ctx context.Context, settings *Settings, withSe Retries: 5, RetryDelay: time.Second * 10, ConnectTimeout: time.Minute * 5, - DSN: settings.BuildConnectionString(true), + DSN: settings.BuildConnectionString(true, SSLModePrefer), MaxOpenConnections: settings.MaxOpenConnections, MaxIdleConnections: settings.MaxIdleConnections, ConnMaxLifetime: time.Minute * 5, @@ -78,7 +78,7 @@ func NewDbConnectionForTest(ctx context.Context, settings *Settings, withSearchP Retries: 5, RetryDelay: time.Second * 10, ConnectTimeout: time.Minute * 5, - DSN: settings.BuildConnectionString(withSearchPath), + DSN: settings.BuildConnectionString(withSearchPath, SSLModePrefer), MaxOpenConnections: settings.MaxOpenConnections, MaxIdleConnections: settings.MaxIdleConnections, ConnMaxLifetime: time.Minute * 5, @@ -88,7 +88,7 @@ func NewDbConnectionForTest(ctx context.Context, settings *Settings, withSearchP Retries: 5, RetryDelay: time.Second * 10, ConnectTimeout: time.Minute * 5, - DSN: settings.BuildConnectionString(true), + DSN: settings.BuildConnectionString(true, SSLModePrefer), MaxOpenConnections: settings.MaxOpenConnections, MaxIdleConnections: settings.MaxIdleConnections, ConnMaxLifetime: time.Minute * 5, diff --git a/db/settings.go b/db/settings.go index 27d86dd..2e8f014 100644 --- a/db/settings.go +++ b/db/settings.go @@ -14,16 +14,30 @@ type Settings struct { } // BuildConnectionString builds the connection string to the database - for now same as reader -func (app *Settings) BuildConnectionString(withSearchPath bool) string { - cs := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable", +func (app *Settings) BuildConnectionString(withSearchPath bool, sslMode SSLMode) string { + cs := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=%s", app.User, app.Password, app.Name, app.Host, app.Port, + sslMode, ) if withSearchPath { cs = fmt.Sprintf("%s search_path=%s", cs, app.Name) // assumption is schema has same name as dbname } return cs } + +// SSLMode represents the different PostgreSQL SSL modes +type SSLMode string + +const ( + SSLModeDisable SSLMode = "disable" + SSLModeAllow SSLMode = "allow" + // SSLModePrefer falls back to no SSL if can't connect with SSL + SSLModePrefer SSLMode = "prefer" + SSLModeRequire SSLMode = "require" + SSLModeVerifyCA SSLMode = "verify-ca" + SSLModeVerifyFull SSLMode = "verify-full" +)