From 7a89896235fd80d8128dbb6809951b9c08215bc2 Mon Sep 17 00:00:00 2001 From: Daniel Canter Date: Thu, 23 Sep 2021 17:40:49 -0700 Subject: [PATCH] Set PATHEXT for job containers to handle binaries with no extension This change sets the PATHEXT environment variable which Go checks during exec.Cmd startup to do some path resolution. PATHEXT contains a semicolon separated list of extensions to check against if a path ended without one (e.g. /path/to/my/binary). This simply adds an empty string entry to the end so that binaries with no extension can be launched correctly. Although this isn't a common occurrence it's still a good thing to support. Windows Server containers are able to handle this fine, and CreateProcess is perfectly happy launching a valid executable without an extension. This is mainly to support the agnhost image which is a common k8s testing image whose entrypoint is a binary named agnhost with no extension. https://github.com/kubernetes/kubernetes/blob/d64e91878517b1208a0bce7e2b7944645ace8ede/test/images/agnhost/Dockerfile_windows Signed-off-by: Daniel Canter --- internal/jobcontainers/jobcontainer.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/jobcontainers/jobcontainer.go b/internal/jobcontainers/jobcontainer.go index 86a98e26c2..ad4e64796b 100644 --- a/internal/jobcontainers/jobcontainer.go +++ b/internal/jobcontainers/jobcontainer.go @@ -211,6 +211,16 @@ func (c *JobContainer) CreateProcess(ctx context.Context, config interface{}) (_ env = append(env, envMapToSlice(conf.Environment)...) env = append(env, sandboxMountPointEnvVar+"="+c.sandboxMount) + // exec.Cmd internally does its own path resolution and as part of this checks some well known file extensions on the file given (e.g. if + // the user just provided /path/to/mybinary). CreateProcess is perfectly capable of launching an executable that doesn't have the .exe extension + // so this adds an empty string entry to the end of what extensions GO checks against so that a binary with no extension can be launched. + // The extensions are checked in order, so that if mybinary.exe and mybinary both existed in the same directory, mybinary.exe would be chosen. + // This is mostly to handle a common Kubernetes test image named agnhost that has the main entrypoint as a binary named agnhost with no extension. + // https://github.com/kubernetes/kubernetes/blob/d64e91878517b1208a0bce7e2b7944645ace8ede/test/images/agnhost/Dockerfile_windows + if err := os.Setenv("PATHEXT", ".COM;.EXE;.BAT;.CMD; "); err != nil { + return nil, errors.Wrap(err, "failed to set PATHEXT") + } + cmd := &exec.Cmd{ Env: env, Dir: workDir,