Skip to content

Commit

Permalink
TER-186 resolve panic when db password is not set (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanak Singhal authored Sep 8, 2023
1 parent b83f039 commit b4362a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api/internal/config/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
db:
host: "localhost" # db host name
user: "postgres" # db username
# password: # db password no default, panic if not set.
password: "" # db password no default, panic if not set.
name: "cc_terrarium" # database name
port: 5432 # db port
ssl_mode: false # enable SSL mode
Expand Down
2 changes: 1 addition & 1 deletion src/cli/internal/config/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
db:
host: "localhost" # db host name
user: "postgres" # db username
# password: # db password no default, panic if not set.
password: "" # db password no default, panic if not set.
name: "cc_terrarium" # database name
port: 5432 # db port
ssl_mode: false # enable SSL mode
Expand Down
15 changes: 11 additions & 4 deletions src/pkg/db/dbhelper/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,26 @@ func Connect(dialector gorm.Dialector, options ...ConnOption) (*gorm.DB, error)
return db, err
}

// createDSN creates the DSN (Data Source Name) string for the database connection.
func createDSN(host, user, password, dbName string, port int, sslMode bool) string {
// createPostgresDSN creates the DSN (Data Source Name) string for the postgres database connection.
func createPostgresDSN(host, user, password, dbName string, port int, sslMode bool) string {
sslModeStr := "disable"
if sslMode {
sslModeStr = "enable"
}

return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s", host, user, password, dbName, port, sslModeStr)
passwordStr := ""
if password != "" {
// omitting the password block when not set, allows the library to look at
// other standard sources like `~/.pgpass`
passwordStr = "password=" + password
}

return fmt.Sprintf("host=%s user=%s %s dbname=%s port=%d sslmode=%s", host, user, passwordStr, dbName, port, sslModeStr)
}

// ConnectPG establishes a connection to a postgres database using the provided connection parameters.
func ConnectPG(host, user, password, dbName string, port int, sslMode bool, options ...ConnOption) (*gorm.DB, error) {
dsn := createDSN(host, user, password, dbName, port, sslMode)
dsn := createPostgresDSN(host, user, password, dbName, port, sslMode)
return Connect(postgres.Open(dsn), options...)
}

Expand Down
12 changes: 11 additions & 1 deletion src/pkg/db/dbhelper/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,21 @@ func TestCreateDSN(t *testing.T) {
sslMode: true,
expected: "host=localhost user=test password=test dbname=test port=5432 sslmode=enable",
},
{
name: "no password",
host: "localhost",
user: "test",
password: "",
dbName: "test",
port: 5432,
sslMode: true,
expected: "host=localhost user=test dbname=test port=5432 sslmode=enable",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dsn := createDSN(tc.host, tc.user, tc.password, tc.dbName, tc.port, tc.sslMode)
dsn := createPostgresDSN(tc.host, tc.user, tc.password, tc.dbName, tc.port, tc.sslMode)
assert.Equal(t, tc.expected, dsn)
})
}
Expand Down

0 comments on commit b4362a5

Please sign in to comment.