From 5616ea3d1cc9aba0e16a25107f36acc08a2a7432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Harasimowicz?= Date: Wed, 21 Jul 2021 15:03:59 +0200 Subject: [PATCH] feat: allow to override some nested values in config (#374) Currently it is not possible to override some values in the config if they are nested such as `SELFSERVICE_METHODS_OIDC_CONFIG_PROVIDERS`. It is still not possible to override some values that are nested inside `array` but this at least should allow to replace the whole array with some serialized JSON struct. It is not ideal but it should have some more options to deal with injecting secrets into Kratos et al. --- configx/koanf_env.go | 12 ++++++++++-- configx/provider_test.go | 1 + configx/stub/kratos/expected.json | 13 ++++++++++++- configx/stub/kratos/kratos.yaml | 2 ++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/configx/koanf_env.go b/configx/koanf_env.go index 0ab635c6..0129ab40 100644 --- a/configx/koanf_env.go +++ b/configx/koanf_env.go @@ -42,8 +42,16 @@ func NewKoanfEnv(prefix string, schema []byte) (*env.Env, error) { } decode := func(value string) (v interface{}) { - _ = json.Unmarshal([]byte(value), v) - return v + b := []byte(value) + var arr []interface{} + if err := json.Unmarshal(b, &arr); err == nil { + return &arr + } + h := map[string]interface{}{} + if err := json.Unmarshal(b, &h); err == nil { + return &h + } + return nil } return env.ProviderWithValue(prefix, ".", func(key string, value string) (string, interface{}) { diff --git a/configx/provider_test.go b/configx/provider_test.go index 53e4191b..56ab9cfd 100644 --- a/configx/provider_test.go +++ b/configx/provider_test.go @@ -109,6 +109,7 @@ func TestAdvancedConfigs(t *testing.T) { stub: "kratos", configs: []string{"stub/kratos/kratos.yaml"}, isValid: true, envs: [][2]string{ + {"SELFSERVICE_METHODS_OIDC_CONFIG_PROVIDERS", `[{"id":"google","provider":"google","mapper_url":"file:///etc/config/kratos/oidc.google.jsonnet","client_id":"client@example.com","client_secret":"secret"}]`}, {"DSN", "sqlite:///var/lib/sqlite/db.sqlite?_fk=true"}, }}, { diff --git a/configx/stub/kratos/expected.json b/configx/stub/kratos/expected.json index 57a6a8e3..1e666e6d 100644 --- a/configx/stub/kratos/expected.json +++ b/configx/stub/kratos/expected.json @@ -81,7 +81,18 @@ "enabled": true }, "oidc": { - "enabled": false + "enabled": true, + "config": { + "providers": [ + { + "id": "google", + "provider": "google", + "mapper_url": "file:///etc/config/kratos/oidc.google.jsonnet", + "client_id": "client@example.com", + "client_secret": "secret" + } + ] + } }, "password": { "enabled": true diff --git a/configx/stub/kratos/kratos.yaml b/configx/stub/kratos/kratos.yaml index 1e0bdb78..9848e3b2 100644 --- a/configx/stub/kratos/kratos.yaml +++ b/configx/stub/kratos/kratos.yaml @@ -18,6 +18,8 @@ selfservice: methods: password: enabled: true + oidc: + enabled: true flows: error: