Skip to content

Commit

Permalink
allow environment variable in configuration file (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlteredCoder committed Feb 4, 2021
1 parent 564c415 commit 359a9cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions docs/v1.X/docs/references/crowdsec-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Configuration example


<details>
<summary>Default configuration</summary>

Expand Down Expand Up @@ -63,6 +64,30 @@ prometheus:
</details>
## Environment variable
It is possible to set a configuration value based on an enrivonement variables.
For example, if you don't want to store your database password in the configuration file, you can do this:
```yaml
db_config:
type: mysql
user: database_user
password: ${DB_PASSWORD}
db_name: db_name
host: 192.168.0.2
port: 3306
```
And export the environment variable such as:
```bash
export DB_PASSWORD="<db_password>"
```

!!! warning
**Note**: you need to be `root` or put the environment variable in `/etc/environement`

## Configuration format

Expand Down
4 changes: 3 additions & 1 deletion pkg/csconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package csconfig
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -40,7 +41,8 @@ func (c *GlobalConfig) LoadConfigurationFile(path string) error {
if err != nil {
return errors.Wrap(err, "failed to read config file")
}
err = yaml.UnmarshalStrict(fcontent, c)
configData := os.ExpandEnv(string(fcontent))
err = yaml.UnmarshalStrict([]byte(configData), c)
if err != nil {
return errors.Wrap(err, "failed unmarshaling config")
}
Expand Down

0 comments on commit 359a9cb

Please sign in to comment.