Strip newlines when reading environment variables from _FILE suffixed files. (#872)

This commit is contained in:
Alexey Zagarin 2019-05-09 18:58:24 +07:00 committed by Ludovic Fernandez
parent 75a4315464
commit b9bafc582c
2 changed files with 34 additions and 15 deletions

View file

@ -159,5 +159,5 @@ func GetOrFile(envVar string) string {
return ""
}
return string(fileContents)
return strings.TrimSuffix(string(fileContents), "\n")
}

View file

@ -289,6 +289,23 @@ func TestGetOrFile_ReadsFiles(t *testing.T) {
varEnvFileName := "TEST_LEGO_ENV_VAR_FILE"
varEnvName := "TEST_LEGO_ENV_VAR"
testCases := []struct {
desc string
fileContent []byte
}{
{
desc: "simple",
fileContent: []byte("lego_file"),
},
{
desc: "with an empty last line",
fileContent: []byte("lego_file\n"),
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
err := os.Unsetenv(varEnvFileName)
require.NoError(t, err)
err = os.Unsetenv(varEnvName)
@ -298,7 +315,7 @@ func TestGetOrFile_ReadsFiles(t *testing.T) {
require.NoError(t, err)
defer os.Remove(file.Name())
err = ioutil.WriteFile(file.Name(), []byte("lego_file"), 0644)
err = ioutil.WriteFile(file.Name(), []byte("lego_file\n"), 0644)
require.NoError(t, err)
err = os.Setenv(varEnvFileName, file.Name())
@ -308,6 +325,8 @@ func TestGetOrFile_ReadsFiles(t *testing.T) {
value := GetOrFile(varEnvName)
assert.Equal(t, "lego_file", value)
})
}
}
func TestGetOrFile_PrefersEnvVars(t *testing.T) {