Skip to content

Commit

Permalink
Add DB_URL_FILE and DB_PASSWORD_FILE (#629)
Browse files Browse the repository at this point in the history
This allows passing sensitive DB passwords through files instead of environment
variables. This makes collector work better with [systemd
credentials](https://systemd.io/CREDENTIALS/) and NixOS flakes.

Fixes #540

---------

Co-authored-by: Philip Munksgaard <[email protected]>
  • Loading branch information
msakrejda and Munksgaard authored Nov 12, 2024
1 parent 573d357 commit c8032bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 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
26 changes: 26 additions & 0 deletions config/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func getDefaultConfig() *ServerConfig {
if dbURL := os.Getenv("DB_URL"); dbURL != "" {
config.DbURL = dbURL
}
if dbURLFile := os.Getenv("DB_URL_FILE"); dbURLFile != "" {
config.DbURLFile = dbURLFile
}
if dbName := os.Getenv("DB_NAME"); dbName != "" {
config.DbName = dbName
}
Expand All @@ -113,6 +116,9 @@ func getDefaultConfig() *ServerConfig {
if dbPassword := os.Getenv("DB_PASSWORD"); dbPassword != "" {
config.DbPassword = dbPassword
}
if dbPasswordFile := os.Getenv("DB_PASSWORD_FILE"); dbPasswordFile != "" {
config.DbPasswordFile = dbPasswordFile
}
if dbHost := os.Getenv("DB_HOST"); dbHost != "" {
config.DbHost = dbHost
}
Expand Down Expand Up @@ -625,6 +631,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 c8032bb

Please sign in to comment.