Strip newlines when reading environment variables from _FILE
suffixed files. (#872)
This commit is contained in:
parent
75a4315464
commit
b9bafc582c
2 changed files with 34 additions and 15 deletions
2
platform/config/env/env.go
vendored
2
platform/config/env/env.go
vendored
|
@ -159,5 +159,5 @@ func GetOrFile(envVar string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
return string(fileContents)
|
||||
return strings.TrimSuffix(string(fileContents), "\n")
|
||||
}
|
||||
|
|
47
platform/config/env/env_test.go
vendored
47
platform/config/env/env_test.go
vendored
|
@ -289,25 +289,44 @@ func TestGetOrFile_ReadsFiles(t *testing.T) {
|
|||
varEnvFileName := "TEST_LEGO_ENV_VAR_FILE"
|
||||
varEnvName := "TEST_LEGO_ENV_VAR"
|
||||
|
||||
err := os.Unsetenv(varEnvFileName)
|
||||
require.NoError(t, err)
|
||||
err = os.Unsetenv(varEnvName)
|
||||
require.NoError(t, err)
|
||||
testCases := []struct {
|
||||
desc string
|
||||
fileContent []byte
|
||||
}{
|
||||
{
|
||||
desc: "simple",
|
||||
fileContent: []byte("lego_file"),
|
||||
},
|
||||
{
|
||||
desc: "with an empty last line",
|
||||
fileContent: []byte("lego_file\n"),
|
||||
},
|
||||
}
|
||||
|
||||
file, err := ioutil.TempFile("", "lego")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(file.Name())
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
|
||||
err = ioutil.WriteFile(file.Name(), []byte("lego_file"), 0644)
|
||||
require.NoError(t, err)
|
||||
err := os.Unsetenv(varEnvFileName)
|
||||
require.NoError(t, err)
|
||||
err = os.Unsetenv(varEnvName)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.Setenv(varEnvFileName, file.Name())
|
||||
require.NoError(t, err)
|
||||
defer os.Unsetenv(varEnvFileName)
|
||||
file, err := ioutil.TempFile("", "lego")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
value := GetOrFile(varEnvName)
|
||||
err = ioutil.WriteFile(file.Name(), []byte("lego_file\n"), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "lego_file", value)
|
||||
err = os.Setenv(varEnvFileName, file.Name())
|
||||
require.NoError(t, err)
|
||||
defer os.Unsetenv(varEnvFileName)
|
||||
|
||||
value := GetOrFile(varEnvName)
|
||||
|
||||
assert.Equal(t, "lego_file", value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrFile_PrefersEnvVars(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue