Skip to content

Commit

Permalink
Merge pull request #532 from RuliXu/support-dsn-empty-password
Browse files Browse the repository at this point in the history
Support DSN Empty Password
  • Loading branch information
Iceber authored Jun 27, 2023
2 parents 4359773 + b13929a commit a3cdc94
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/storage/internalstorage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
)

const (
defaultMaxIdleConns = 5
defaultMaxOpenConns = 40
defaultConnMaxLifetime = time.Hour
defaultMaxIdleConns = 5
defaultMaxOpenConns = 40
defaultConnMaxLifetime = time.Hour
databasePasswordEnvName = "DB_PASSWORD"
)

type Config struct {
Expand Down Expand Up @@ -145,7 +146,15 @@ func (cfg *Config) getConnPoolConfig() (ConnPoolConfig, error) {

func (cfg *Config) genMySQLConfig() (*mysql.Config, error) {
if cfg.DSN != "" {
return mysql.ParseDSN(cfg.DSN)
mysqlConfig, err := mysql.ParseDSN(cfg.DSN)
if err != nil {
return nil, err
}
if mysqlConfig.Passwd == "" {
mysqlConfig.Passwd = os.Getenv(databasePasswordEnvName)
}
mysqlConfig.ParseTime = true
return mysqlConfig, nil
}

if cfg.Database == "" {
Expand Down Expand Up @@ -229,6 +238,9 @@ func (cfg *Config) genMySQLConfig() (*mysql.Config, error) {

func (cfg *Config) genPostgresConfig() (*pgx.ConnConfig, error) {
if cfg.DSN != "" {
if !strings.Contains(cfg.DSN, "password") && os.Getenv(databasePasswordEnvName) != "" {
cfg.DSN = cfg.DSN + fmt.Sprintf(" password=%s", os.Getenv(databasePasswordEnvName))
}
return pgx.ParseConfig(cfg.DSN)
}

Expand Down

0 comments on commit a3cdc94

Please sign in to comment.