diff --git a/opts/envfile.go b/opts/envfile.go index 26aa3c3a9094..9ea4afeeb419 100644 --- a/opts/envfile.go +++ b/opts/envfile.go @@ -1,6 +1,7 @@ package opts import ( + "fmt" "os" ) @@ -18,5 +19,14 @@ import ( // environment variables, that's why we just strip leading whitespace and // nothing more. func ParseEnvFile(filename string) ([]string, error) { - return parseKeyValueFile(filename, os.LookupEnv) + fh, err := os.Open(filename) + if err != nil { + return []string{}, err + } + out, err := parseKeyValueFile(fh, os.LookupEnv) + _ = fh.Close() + if err != nil { + return []string{}, fmt.Errorf("invalid env file (%s): %v", filename, err) + } + return out, nil } diff --git a/opts/file.go b/opts/file.go index 654288cbbd98..2b69b84aeba4 100644 --- a/opts/file.go +++ b/opts/file.go @@ -5,28 +5,22 @@ import ( "bytes" "fmt" "io" - "os" "strings" "unicode" "unicode/utf8" ) -const whiteSpaces = " \t" - -func parseKeyValueFile(filename string, lookupFn func(string) (string, bool)) ([]string, error) { - fh, err := os.Open(filename) - if err != nil { - return []string{}, err - } - defer fh.Close() - return ParseKeyValueFile(fh, filename, lookupFn) -} - // ParseKeyValueFile parse a file containing key,value pairs separated by equal sign // Lines starting with `#` are ignored // If a key is declared without a value (no equal sign), lookupFn is requested to provide value for the given key // value is returned as-is, without any kind of parsing but removal of leading whitespace -func ParseKeyValueFile(r io.Reader, filename string, lookupFn func(string) (string, bool)) ([]string, error) { +func ParseKeyValueFile(r io.Reader, lookupFn func(string) (string, bool)) ([]string, error) { + return parseKeyValueFile(r, lookupFn) +} + +const whiteSpaces = " \t" + +func parseKeyValueFile(r io.Reader, lookupFn func(string) (string, bool)) ([]string, error) { lines := []string{} scanner := bufio.NewScanner(r) currentLine := 0 @@ -34,7 +28,7 @@ func ParseKeyValueFile(r io.Reader, filename string, lookupFn func(string) (stri for scanner.Scan() { scannedBytes := scanner.Bytes() if !utf8.Valid(scannedBytes) { - return []string{}, fmt.Errorf("env file %s contains invalid utf8 bytes at line %d: %v", filename, currentLine+1, scannedBytes) + return []string{}, fmt.Errorf("invalid utf8 bytes at line %d: %v", currentLine+1, scannedBytes) } // We trim UTF8 BOM if currentLine == 0 { diff --git a/opts/file_test.go b/opts/file_test.go index d0e6835c00f0..38f3aa079ed1 100644 --- a/opts/file_test.go +++ b/opts/file_test.go @@ -20,7 +20,7 @@ ZOT`) return v, ok } - got, err := ParseKeyValueFile(bytes.NewReader(b), "(inlined)", lookupFn) + got, err := ParseKeyValueFile(bytes.NewReader(b), lookupFn) assert.NilError(t, err) assert.DeepEqual(t, got, []string{"FOO=BAR", "ZOT=QIX"}) } diff --git a/opts/parse.go b/opts/parse.go index 584b55ef61f4..5927f6be9d18 100644 --- a/opts/parse.go +++ b/opts/parse.go @@ -2,6 +2,7 @@ package opts import ( "errors" + "fmt" "os" "strconv" "strings" @@ -25,9 +26,14 @@ func ReadKVEnvStrings(files []string, override []string) ([]string, error) { func readKVStrings(files []string, override []string, emptyFn func(string) (string, bool)) ([]string, error) { var variables []string for _, ef := range files { - parsedVars, err := parseKeyValueFile(ef, emptyFn) + fh, err := os.Open(ef) if err != nil { - return nil, err + return []string{}, err + } + parsedVars, err := parseKeyValueFile(fh, emptyFn) + _ = fh.Close() + if err != nil { + return nil, fmt.Errorf("invalid env file (%s): %v", ef, err) } variables = append(variables, parsedVars...) }