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 ""
} }
return string(fileContents) return strings.TrimSuffix(string(fileContents), "\n")
} }

View file

@ -289,25 +289,44 @@ func TestGetOrFile_ReadsFiles(t *testing.T) {
varEnvFileName := "TEST_LEGO_ENV_VAR_FILE" varEnvFileName := "TEST_LEGO_ENV_VAR_FILE"
varEnvName := "TEST_LEGO_ENV_VAR" varEnvName := "TEST_LEGO_ENV_VAR"
err := os.Unsetenv(varEnvFileName) testCases := []struct {
require.NoError(t, err) desc string
err = os.Unsetenv(varEnvName) fileContent []byte
require.NoError(t, err) }{
{
desc: "simple",
fileContent: []byte("lego_file"),
},
{
desc: "with an empty last line",
fileContent: []byte("lego_file\n"),
},
}
file, err := ioutil.TempFile("", "lego") for _, test := range testCases {
require.NoError(t, err) t.Run(test.desc, func(t *testing.T) {
defer os.Remove(file.Name())
err = ioutil.WriteFile(file.Name(), []byte("lego_file"), 0644) err := os.Unsetenv(varEnvFileName)
require.NoError(t, err) require.NoError(t, err)
err = os.Unsetenv(varEnvName)
require.NoError(t, err)
err = os.Setenv(varEnvFileName, file.Name()) file, err := ioutil.TempFile("", "lego")
require.NoError(t, err) require.NoError(t, err)
defer os.Unsetenv(varEnvFileName) 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) { func TestGetOrFile_PrefersEnvVars(t *testing.T) {