-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While creating demo env variable, I noticed it is impossible to pass credentials by variable. Unfortunately, `koan` does not support overriding YAML arrays directly, so I had to add this functionality.
- Loading branch information
Showing
6 changed files
with
305 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/tidwall/sjson" | ||
"os" | ||
"quesma/logger" | ||
"strings" | ||
) | ||
|
||
type Env2Json struct { | ||
prefix string | ||
separator string | ||
callback func(key string, value string) (string, interface{}) | ||
resultJson string | ||
} | ||
|
||
func Env2JsonProvider(prefix, sep string, callback func(key string, value string) (string, interface{})) *Env2Json { | ||
if len(prefix) == 0 || len(sep) == 0 { | ||
logger.Error().Msgf("Env2JsonProvider: prefix '%s' and sep '%s' is required", prefix, sep) | ||
return nil | ||
} | ||
if callback == nil { | ||
callback = func(key string, value string) (string, interface{}) { | ||
return key, value | ||
} | ||
} | ||
e := &Env2Json{ | ||
prefix: prefix, | ||
separator: sep, | ||
resultJson: "{}", | ||
callback: callback, | ||
} | ||
return e | ||
} | ||
|
||
func (e *Env2Json) ReadBytes() ([]byte, error) { | ||
var envKeyValues []string | ||
for _, keyValue := range os.Environ() { | ||
if strings.HasPrefix(keyValue, e.prefix) { | ||
envKeyValues = append(envKeyValues, strings.TrimPrefix(keyValue, e.prefix)) | ||
} | ||
} | ||
|
||
for _, keyValue := range envKeyValues { | ||
parts := strings.SplitN(keyValue, "=", 2) | ||
if len(parts) != 2 { | ||
return []byte{}, fmt.Errorf("invalid environment variable '%s', no '='", keyValue) | ||
} | ||
key, value := e.callback(parts[0], parts[1]) | ||
// Omit blank keys | ||
if key == "" { | ||
continue | ||
} | ||
|
||
if err := e.set(key, value); err != nil { | ||
return []byte{}, err | ||
} | ||
} | ||
|
||
return []byte(e.resultJson), nil | ||
} | ||
|
||
func (e *Env2Json) set(key string, value interface{}) error { | ||
resultJson, err := sjson.Set(e.resultJson, strings.Replace(key, e.separator, ".", -1), value) | ||
if err == nil { | ||
e.resultJson = resultJson | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (e *Env2Json) Read() (map[string]interface{}, error) { | ||
return nil, errors.New("env2json Provider does not support Read()") | ||
} | ||
|
||
func mergeArrayFunc(src, dest []interface{}) ([]interface{}, error) { | ||
newLen := len(src) | ||
if len(dest) > newLen { | ||
newLen = len(dest) | ||
} | ||
newArray := make([]interface{}, newLen) | ||
|
||
for i := 0; i < newLen; i++ { | ||
if i >= len(src) { | ||
newArray[i] = dest[i] | ||
} else if i >= len(dest) { | ||
newArray[i] = src[i] | ||
} else if src[i] == nil { | ||
newArray[i] = dest[i] | ||
} else if dest[i] == nil { | ||
newArray[i] = src[i] | ||
} else { | ||
if srcMap, isMap := src[i].(map[string]interface{}); isMap { | ||
if destMap, isDestMap := dest[i].(map[string]interface{}); isDestMap { | ||
if err := mergeDictFunc(srcMap, destMap); err != nil { | ||
return nil, err | ||
} | ||
newArray[i] = destMap | ||
continue | ||
} | ||
} | ||
|
||
newArray[i] = src[i] | ||
} | ||
} | ||
|
||
return newArray, nil | ||
} | ||
|
||
func mergeDictIntoArrayFunc(src map[string]interface{}, dest []interface{}) ([]interface{}, error) { | ||
newArray := make([]interface{}, len(dest)) | ||
copy(newArray, dest) | ||
for k, v := range src { | ||
foundIdx := -1 | ||
|
||
// find existing element with same name | ||
for i := range newArray { | ||
if m, isMap := newArray[i].(map[string]interface{}); isMap { | ||
if m["name"] == k { | ||
foundIdx = i | ||
break | ||
} | ||
} | ||
} | ||
|
||
// if not exist add new element | ||
if foundIdx == -1 { | ||
foundIdx = len(newArray) | ||
newMap := make(map[string]interface{}) | ||
newMap["name"] = k | ||
newArray = append(newArray, newMap) | ||
} | ||
|
||
if m, isMap := newArray[foundIdx].(map[string]interface{}); isMap { | ||
if vTyped, isMap2 := v.(map[string]interface{}); isMap2 { | ||
if err := mergeDictFunc(vTyped, m); err != nil { | ||
return nil, err | ||
} | ||
newArray[foundIdx] = m | ||
continue | ||
} | ||
} | ||
newArray[foundIdx] = v | ||
} | ||
return newArray, nil | ||
} | ||
|
||
func mergeDictFunc(src, dest map[string]interface{}) error { | ||
for k, v := range src { | ||
switch vTyped := v.(type) { | ||
case map[string]interface{}: | ||
if destV, exist := dest[k]; exist { | ||
if destMap, isMap := destV.(map[string]interface{}); isMap { | ||
if err := mergeDictFunc(vTyped, destMap); err != nil { | ||
return err | ||
} | ||
continue | ||
} else if destArray, isArray := destV.([]interface{}); isArray { | ||
if newV, err := mergeDictIntoArrayFunc(vTyped, destArray); err != nil { | ||
return err | ||
} else { | ||
dest[k] = newV | ||
} | ||
continue | ||
} | ||
} | ||
dest[k] = v | ||
case []interface{}: | ||
if destV, exist := dest[k]; exist { | ||
if destMap, isArray := destV.([]interface{}); isArray { | ||
if newV, err := mergeArrayFunc(vTyped, destMap); err != nil { | ||
return err | ||
} else { | ||
dest[k] = newV | ||
} | ||
continue | ||
} | ||
} | ||
dest[k] = v | ||
default: | ||
dest[k] = v | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestEnv2Json_arrays(t *testing.T) { | ||
provider := Env2JsonProvider("ENV2JSON_", "_", nil) | ||
os.Setenv("ENV2JSON_licenseKey", "secret_key") | ||
os.Setenv("ENV2JSON_backendConnectors_0_config_url", "http://localhost:8080") | ||
os.Setenv("ENV2JSON_backendConnectors_0_config_user", "user") | ||
os.Setenv("ENV2JSON_backendConnectors_0_config_password", "password") | ||
t.Cleanup(func() { | ||
os.Unsetenv("ENV2JSON_licenseKey") | ||
os.Unsetenv("ENV2JSON_backendConnectors_0_config_url") | ||
os.Unsetenv("ENV2JSON_backendConnectors_0_config_user") | ||
os.Unsetenv("ENV2JSON_backendConnectors_0_config_password") | ||
}) | ||
resultJson, err := provider.ReadBytes() | ||
assert.NoError(t, err) | ||
|
||
expectedJson := `{"licenseKey":"secret_key","backendConnectors":[{"config":{"url":"http://localhost:8080","user":"user","password":"password"}}]}` | ||
assert.Equal(t, expectedJson, string(resultJson)) | ||
} | ||
|
||
func TestEnv2Json_arraysByName(t *testing.T) { | ||
os.Setenv(configFileLocationEnvVar, "./test_configs/test_config_v2.yaml") | ||
os.Setenv("QUESMA_licenseKey", "secret_key") | ||
os.Setenv("QUESMA_backendConnectors_my-clickhouse-data-source_config_url", "http://localhost:9201") | ||
os.Setenv("QUESMA_backendConnectors_my-clickhouse-data-source_config_user", "user") | ||
os.Setenv("QUESMA_backendConnectors_my-clickhouse-data-source_config_password", "password") | ||
t.Cleanup(func() { | ||
os.Unsetenv("QUESMA_licenseKey") | ||
os.Unsetenv("QUESMA_backendConnectors_my-clickhouse-data-source_config_url") | ||
os.Unsetenv("QUESMA_backendConnectors_my-clickhouse-data-source_config_user") | ||
os.Unsetenv("QUESMA_backendConnectors_my-clickhouse-data-source_config_password") | ||
}) | ||
|
||
cfg := LoadV2Config() | ||
assert.Len(t, cfg.BackendConnectors, 2) | ||
clickHouseBackend := cfg.BackendConnectors[1] | ||
assert.Equal(t, "my-clickhouse-data-source", clickHouseBackend.Name) | ||
assert.Equal(t, "http://localhost:9201", clickHouseBackend.Config.Url.String()) | ||
assert.Equal(t, "user", clickHouseBackend.Config.User) | ||
assert.Equal(t, "password", clickHouseBackend.Config.Password) | ||
} | ||
|
||
func TestEnv2Json_empty(t *testing.T) { | ||
provider := Env2JsonProvider("ENV2JSON2_", "_", nil) | ||
resultJson, err := provider.ReadBytes() | ||
assert.NoError(t, err) | ||
|
||
expectedJson := `{}` | ||
assert.Equal(t, expectedJson, string(resultJson)) | ||
} | ||
|
||
func TestEnv2Json_jsonMerge(t *testing.T) { | ||
jsonA := `{"a":1,"b":2,"c":[{"d":1},{"d":2},{"d":3}]}` | ||
jsonB := `{"a":3,"l":2,"c":[null,{"e":42}]}` | ||
// turn into dicts | ||
var dictA map[string]interface{} | ||
var dictB map[string]interface{} | ||
err := json.Unmarshal([]byte(jsonA), &dictA) | ||
assert.NoError(t, err) | ||
err = json.Unmarshal([]byte(jsonB), &dictB) | ||
assert.NoError(t, err) | ||
|
||
err = mergeDictFunc(dictA, dictB) | ||
assert.NoError(t, err) | ||
mergedJson, err2 := json.Marshal(dictB) | ||
assert.NoError(t, err2) | ||
expectedJson := `{"a":1,"b":2,"c":[{"d":1},{"d":2,"e":42},{"d":3}],"l":2}` | ||
assert.Equal(t, expectedJson, string(mergedJson)) | ||
} |