Skip to content

Commit

Permalink
Merge pull request #497 from kube-tarian/fix-dbconnect
Browse files Browse the repository at this point in the history
fix db connect issue when password has special chars
  • Loading branch information
vramk23 authored May 29, 2024
2 parents 2c987ad + 4e84a2a commit 7bea17d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion capten/common-pkg/postgres/db-init/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"fmt"
"math/big"
"net/url"

"github.com/intelops/go-common/credentials"
"github.com/intelops/go-common/logging"
Expand Down Expand Up @@ -58,8 +59,9 @@ func CreatedDatabaseWithConfig(log logging.Logger, conf *Config) (err error) {
}
}

password := url.QueryEscape(adminCredential.Password)
dbAddress := conf.DBHost + ":" + conf.DBPort
adminClient, err := NewPostgresAdmin(log, dbAddress, adminCredential.UserName, adminCredential.Password)
adminClient, err := NewPostgresAdmin(log, dbAddress, adminCredential.UserName, password)
if err != nil {
return
}
Expand Down
16 changes: 7 additions & 9 deletions capten/common-pkg/postgres/db-init/db_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dbinit
import (
"context"
"fmt"
"net/url"
"os"
"time"

Expand All @@ -28,7 +29,7 @@ const (
var log = logging.NewLogger()

type DBConfig struct {
DBAddr string `envconfig:"PG_DB_HOST" required:"true"`
DBHost string `envconfig:"PG_DB_HOST" required:"true"`
DBPort string `envconfig:"PG_DB_PORT" default:"5432"`
DBName string `envconfig:"PG_DB_NAME" required:"true"`
EntityName string `envconfig:"PG_DB_ENTITY_NAME" default:"postgres"`
Expand All @@ -55,20 +56,17 @@ func RunMigrations(mode Mode) error {
}

func RunMigrationsWithConfig(conf *DBConfig, mode Mode) error {
dbConnectionString, err := getDbConnectionURLFromDbType(conf, conf.Password)
if err != nil {
return errors.WithMessage(err, "DB connection Url create failed")
}

password := url.QueryEscape(conf.Password)
dbConnectionString := getDbConnectionURLFromDbType(conf, password)
if err := runMigrations(conf.SourceURI, dbConnectionString, conf.DBName, mode); err != nil {
return err
}
return nil
}

func getDbConnectionURLFromDbType(conf *DBConfig, password string) (string, error) {
return fmt.Sprintf("postgres://%s:%s@%s:%v/%s?sslmode=disable",
conf.Username, password, conf.DBAddr, conf.DBPort, conf.DBName), nil
func getDbConnectionURLFromDbType(conf *DBConfig, password string) string {
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
conf.Username, password, conf.DBHost, conf.DBPort, conf.DBName)
}

func runMigrations(sourceURL, databaseURL, dbName string, mode Mode) (err error) {
Expand Down
4 changes: 3 additions & 1 deletion capten/common-pkg/postgres/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/url"

"github.com/intelops/go-common/logging"
"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -62,7 +63,8 @@ func NewDBFromENV(logger logging.Logger) (*gorm.DB, error) {
}

func NewDB(conf *Config, logger logging.Logger) (*gorm.DB, error) {
db, err := gorm.Open(postgres.Open(DataSourceName(conf.Username, conf.Password, fmt.Sprintf("%s:%s", conf.DBHost, conf.DBPort), conf.DatabaseName, conf.IsTLSEnabled)), &gorm.Config{})
password := url.QueryEscape(conf.Password)
db, err := gorm.Open(postgres.Open(DataSourceName(conf.Username, password, fmt.Sprintf("%s:%s", conf.DBHost, conf.DBPort), conf.DatabaseName, conf.IsTLSEnabled)), &gorm.Config{})
if err != nil {
return nil, gerrors.NewFromError(DBConnectError, err)
}
Expand Down

0 comments on commit 7bea17d

Please sign in to comment.