Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to use env variables in config #2819

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions api/internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"

"github.com/gorilla/sessions"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -184,6 +186,16 @@ func InitConf() {
initSchema()
}

func unmarshalConfig() (*Config, error) {
config := Config{}
err := viper.Unmarshal(&config, viper.DecodeHook(substituteEnvironmentVariables()))
if err != nil {
return nil, err
}

return &config, nil
}

func setupConfig() {
// setup config file path
if ConfigFile == "" {
Expand All @@ -204,8 +216,7 @@ func setupConfig() {
}

// unmarshal config
config := Config{}
err := viper.Unmarshal(&config)
config, err := unmarshalConfig()
if err != nil {
panic(fmt.Sprintf("fail to unmarshal configuration: %s, err: %s", ConfigFile, err.Error()))
}
Expand Down Expand Up @@ -432,3 +443,18 @@ func initSecurity(conf Security) {
ContentSecurityPolicy: DefaultCSP,
}
}

// substituteEnvironmentVariables substitutes environment variables in the configuration with real values
func substituteEnvironmentVariables() mapstructure.DecodeHookFuncKind {
return func(
f reflect.Kind,
t reflect.Kind,
data interface{},
) (interface{}, error) {
if f != reflect.String {
return data, nil
}

return os.ExpandEnv(data.(string)), nil
}
}
64 changes: 64 additions & 0 deletions api/internal/conf/conf_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package conf

import (
"bytes"
"encoding/json"
"os"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -61,3 +64,64 @@ func Test_mergeSchema(t *testing.T) {
})
}
}

func Test_unmarshalConfig(t *testing.T) {
tests := []struct {
name string
init func()
config []byte
assert func(*Config, *testing.T)
}{
{
name: "should correctly parse config without environment variables",
init: func() {},
config: []byte("conf:\n listen:\n port: \"9000\""),
assert: func(config *Config, t *testing.T) {
assert.Equal(t, 9000, config.Conf.Listen.Port)
},
},
{
name: "should correctly substitute int port from environment variables",
init: func() {
os.Setenv("PORT", "8080")
},
config: []byte("conf:\n listen:\n port: \"$PORT\""),
assert: func(config *Config, t *testing.T) {
assert.Equal(t, 8080, config.Conf.Listen.Port)
},
},
{
name: "should correctly substitute string etcd endpoint from environment variables",
init: func() {
os.Setenv("ETCD_ENDPOINT", "127.0.0.1:2379")
},
config: []byte("conf:\n etcd:\n endpoints:\n - $ETCD_ENDPOINT"),
assert: func(config *Config, t *testing.T) {
assert.Equal(t, "127.0.0.1:2379", config.Conf.Etcd.Endpoints[0])
},
},
}

for _, tt := range tests {

t.Run(tt.name, func(t *testing.T) {
viper.SetConfigType("yaml")
viper.AutomaticEnv()

tt.init()

err := viper.ReadConfig(bytes.NewBuffer(tt.config))
if err != nil {
t.Errorf("unable to read config: %v", err)
return
}
config, err := unmarshalConfig()
if err != nil {
t.Errorf("unable to unmarshall config: %v", err)
return
}

tt.assert(config, t)
})
}
}