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

add option for ssl mode #85

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
5 changes: 3 additions & 2 deletions config_loader_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down
8 changes: 4 additions & 4 deletions db/db_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
18 changes: 16 additions & 2 deletions db/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Loading