diff --git a/capten/common-pkg/postgres/db-init/db_init.go b/capten/common-pkg/postgres/db-init/db_init.go index 9bfc77a3..22897863 100644 --- a/capten/common-pkg/postgres/db-init/db_init.go +++ b/capten/common-pkg/postgres/db-init/db_init.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "fmt" "math/big" + "net/url" "github.com/intelops/go-common/credentials" "github.com/intelops/go-common/logging" @@ -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 } diff --git a/capten/common-pkg/postgres/db-init/db_migrate.go b/capten/common-pkg/postgres/db-init/db_migrate.go index c06029af..b2c9760f 100644 --- a/capten/common-pkg/postgres/db-init/db_migrate.go +++ b/capten/common-pkg/postgres/db-init/db_migrate.go @@ -3,6 +3,7 @@ package dbinit import ( "context" "fmt" + "net/url" "os" "time" @@ -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"` @@ -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) { diff --git a/capten/common-pkg/postgres/db_client.go b/capten/common-pkg/postgres/db_client.go index ec943cc5..fb21e42b 100644 --- a/capten/common-pkg/postgres/db_client.go +++ b/capten/common-pkg/postgres/db_client.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "net/url" "github.com/intelops/go-common/logging" "github.com/kelseyhightower/envconfig" @@ -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) }