mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
config: accept env vars set to "", discern from unset
Fixes #7831 to a certain extent. Signed-off-by: Stephan Renatus <stephan@styra.com>
This commit is contained in:
committed by
Stephan Renatus
parent
deb2cecced
commit
4a5d9735c8
@@ -138,7 +138,8 @@ func subEnvVars(s string) string {
|
||||
// Lookup the variable in the environment. We do not
|
||||
// play by bash rules: if its undefined we'll keep it
|
||||
// as-is, it could be replaced somewhere down the line.
|
||||
if lu := os.Getenv(varName); lu != "" {
|
||||
// If it's set to "", we'll return that.
|
||||
if lu, ok := os.LookupEnv(varName); ok {
|
||||
return lu
|
||||
}
|
||||
return s
|
||||
|
||||
@@ -96,7 +96,7 @@ func TestSubEnvVarsVarsSubMissingEnvVar(t *testing.T) {
|
||||
envKey := setTestEnvVar(t, "var1", "foo")
|
||||
configYaml := fmt.Sprintf("field1: '${%s}'", envKey)
|
||||
|
||||
// Remove the env var and expect the system to sub in ""
|
||||
// Remove the env var and expect the system to keep the key as-is
|
||||
os.Unsetenv(envKey)
|
||||
expected := configYaml // untouched
|
||||
|
||||
@@ -118,6 +118,20 @@ func TestSubEnvVarsVarsSubEmptyVarName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubEnvVarsVarsSubEmptyEnvVar(t *testing.T) {
|
||||
envKey := setTestEnvVar(t, "var1", "foo")
|
||||
configYaml := fmt.Sprintf("field1: '${%s}'", envKey)
|
||||
|
||||
t.Setenv(envKey, "")
|
||||
expected := "field1: ''"
|
||||
|
||||
actual := subEnvVars(configYaml)
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected: '%s'\nActual: '%s'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeValuesNoOverride(t *testing.T) {
|
||||
dest := map[string]any{}
|
||||
src := map[string]any{
|
||||
|
||||
Reference in New Issue
Block a user