Skip to content

Commit b13929a

Browse files
committed
Support DSN Empty Password
Signed-off-by: RuliXu <[email protected]>
1 parent 4359773 commit b13929a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

pkg/storage/internalstorage/config.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
)
1919

2020
const (
21-
defaultMaxIdleConns = 5
22-
defaultMaxOpenConns = 40
23-
defaultConnMaxLifetime = time.Hour
21+
defaultMaxIdleConns = 5
22+
defaultMaxOpenConns = 40
23+
defaultConnMaxLifetime = time.Hour
24+
databasePasswordEnvName = "DB_PASSWORD"
2425
)
2526

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

146147
func (cfg *Config) genMySQLConfig() (*mysql.Config, error) {
147148
if cfg.DSN != "" {
148-
return mysql.ParseDSN(cfg.DSN)
149+
mysqlConfig, err := mysql.ParseDSN(cfg.DSN)
150+
if err != nil {
151+
return nil, err
152+
}
153+
if mysqlConfig.Passwd == "" {
154+
mysqlConfig.Passwd = os.Getenv(databasePasswordEnvName)
155+
}
156+
mysqlConfig.ParseTime = true
157+
return mysqlConfig, nil
149158
}
150159

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

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

0 commit comments

Comments
 (0)