Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions opts/envfile.go → cli/compose/loader/envfile.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package opts
package loader

import (
"os"

"github.com/docker/cli/pkg/kvfile"
)

// ParseEnvFile reads a file with environment variables enumerated by lines
// parseEnvFile reads a file with environment variables enumerated by lines
//
// “Environment variable names used by the utilities in the Shell and
// Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase
// Utilities volume of [IEEE Std 1003.1-2001] consist solely of uppercase
// letters, digits, and the '_' (underscore) from the characters defined in
// Portable Character Set and do not begin with a digit. *But*, other
// characters may be permitted by an implementation; applications shall
// tolerate the presence of such names.”
// -- http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
//
// As of #16585, it's up to application inside docker to validate or not
// As of [moby-16585], it's up to application inside docker to validate or not
// environment variables, that's why we just strip leading whitespace and
// nothing more.
func ParseEnvFile(filename string) ([]string, error) {
//
// [IEEE Std 1003.1-2001]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
// [moby-16585]: https://github.com/moby/moby/issues/16585
func parseEnvFile(filename string) ([]string, error) {
return kvfile.Parse(filename, os.LookupEnv)
}
10 changes: 5 additions & 5 deletions opts/envfile_test.go → cli/compose/loader/envfile_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package opts
package loader

import (
"os"
Expand All @@ -17,13 +17,13 @@ func tmpFileWithContent(t *testing.T, content string) string {
return fileName
}

// Test ParseEnvFile for a non existent file
// Test parseEnvFile for a non existent file
func TestParseEnvFileNonExistentFile(t *testing.T) {
_, err := ParseEnvFile("no_such_file")
_, err := parseEnvFile("no_such_file")
assert.Check(t, is.ErrorType(err, os.IsNotExist))
}

// ParseEnvFile with environment variable import definitions
// parseEnvFile with environment variable import definitions
func TestParseEnvVariableDefinitionsFile(t *testing.T) {
content := `# comment=
UNDEFINED_VAR
Expand All @@ -32,7 +32,7 @@ DEFINED_VAR
tmpFile := tmpFileWithContent(t, content)

t.Setenv("DEFINED_VAR", "defined-value")
variables, err := ParseEnvFile(tmpFile)
variables, err := parseEnvFile(tmpFile)
assert.NilError(t, err)

expectedLines := []string{"DEFINED_VAR=defined-value"}
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l

for _, file := range serviceConfig.EnvFile {
filePath := absPath(workingDir, file)
fileVars, err := opts.ParseEnvFile(filePath)
fileVars, err := parseEnvFile(filePath)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions opts/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

// ValidateEnv validates an environment variable and returns it.
// If no value is specified, it obtains its value from the current environment
// If no value is specified, it obtains its value from the current environment.
//
// As on ParseEnvFile and related to #16585, environment variable names
// are not validated, and it's up to the application inside the container
// to validate them or not.
// Environment variable names are not validated, and it's up to the application
// inside the container to validate them (see [moby-16585]). The only validation
// here is to check if name is empty, per [moby-25099].
//
// The only validation here is to check if name is empty, per #25099
// [moby-16585]: https://github.com/moby/moby/issues/16585
// [moby-25099]: https://github.com/moby/moby/issues/25099
func ValidateEnv(val string) (string, error) {
k, _, hasValue := strings.Cut(val, "=")
if k == "" {
Expand Down
14 changes: 14 additions & 0 deletions opts/envfile_deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package opts

import (
"os"

"github.com/docker/cli/pkg/kvfile"
)

// ParseEnvFile reads a file with environment variables enumerated by lines
//
// Deprecated: use [kvfile.Parse] and pass [os.LookupEnv] to lookup env-vars from the current environment.
func ParseEnvFile(filename string) ([]string, error) {
return kvfile.Parse(filename, os.LookupEnv)
}
Loading