My team is using Golang for coding, and we put all configurations in a conf.json
, interesting part shown below, and another config_schema.json
file for the json validation.
{
"host": "192.168.0.34",
"port": "5678",
"username": "test_user",
"password": "random_pass",
"dbName": "dummy"
}
My question is, can I define environment variables .eg $USER and $PASS and use them as below or there is a proper way to achieve this for security purpose?
{
"host": "192.168.0.34",
"port": "5678",
"username": "$USER",
"password": "$PASS",
"dbName": "dummy"
}
我们最终使用了 env var,并在 Gitlab CI 环境变量中定义了用户名和密码,并启用了 masked,这样它们就不会显示在日志中。
Maybe it's too late since the question it's 1 year old But you can use a combination of https://golang.org/pkg/os/#ExpandEnv and https://golang.org/pkg/encoding/json/#Unmarshaler
In order to use Unmarshaler you should have a struct
corresponding to your json. then you can override the default method and substitute only fields you want to. The simpler example is shown below
result := os.ExpandEnv(jsonString)
live example: https://play.golang.org/p/78C2zyYP6vL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.