Skip to content

Commit

Permalink
Handle password and URL files during preprocessConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
msakrejda committed Nov 6, 2024
1 parent c819dfc commit 0a6902b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ type ServerConfig struct {
EnableLogExplain bool `ini:"enable_log_explain"`

DbURL string `ini:"db_url"`
DbURLFile string `ini:"db_url_file"`
DbName string `ini:"db_name"`
DbUsername string `ini:"db_username"`
DbPassword string `ini:"db_password"`
DbPasswordFile string `ini:"db_password_file"`
DbHost string `ini:"db_host"`
DbPort int `ini:"db_port"`
DbSslMode string `ini:"db_sslmode"`
Expand Down
36 changes: 22 additions & 14 deletions config/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,7 @@ func getDefaultConfig() *ServerConfig {
config.DbURL = dbURL
}
if dbURLFile := os.Getenv("DB_URL_FILE"); dbURLFile != "" {
dbURL, err := os.ReadFile(dbURLFile)

if err != nil {
panic(err)
}

config.DbURL = strings.TrimSpace(string(dbURL))
config.DbURLFile = dbURLFile
}
if dbName := os.Getenv("DB_NAME"); dbName != "" {
config.DbName = dbName
Expand All @@ -131,13 +125,7 @@ func getDefaultConfig() *ServerConfig {
config.DbPassword = dbPassword
}
if dbPasswordFile := os.Getenv("DB_PASSWORD_FILE"); dbPasswordFile != "" {
dbPassword, err := os.ReadFile(dbPasswordFile)

if err != nil {
panic(err)
}

config.DbPassword = strings.TrimSpace(string(dbPassword))
config.DbPasswordFile = dbPasswordFile
}
if dbHost := os.Getenv("DB_HOST"); dbHost != "" {
config.DbHost = dbHost
Expand Down Expand Up @@ -651,6 +639,26 @@ func preprocessConfig(config *ServerConfig) (*ServerConfig, error) {
config.DbExtraNames = dbNameParts[1:]
}

if config.DbURL == "" && config.DbURLFile != "" {
dbURL, err := os.ReadFile(config.DbURLFile)

if err != nil {
return config, err
}

config.DbURL = strings.TrimSpace(string(dbURL))
}

if config.DbPassword == "" && config.DbPasswordFile != "" {
dbPassword, err := os.ReadFile(config.DbPasswordFile)

if err != nil {
return config, err
}

config.DbPassword = strings.TrimSpace(string(dbPassword))
}

if config.DbSslRootCertContents != "" {
config.DbSslRootCert, err = writeValueToTempfile(config.DbSslRootCertContents)
if err != nil {
Expand Down

0 comments on commit 0a6902b

Please sign in to comment.