forked from TrueCloudLab/lego
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")
|
||||
}
|
||||
|
|
21
platform/config/env/env_test.go
vendored
21
platform/config/env/env_test.go
vendored
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue