set default HOME env properly (#341)

* set default HOME env properly

* set HOME to / if user is set by uid

* fix test

* continue to skip user_run test

* fix unit test to match new functionality
This commit is contained in:
Sharif Elgamal 2018-09-27 07:31:51 -07:00 committed by dlorenc
parent 1a13c81be8
commit 49184c2114
3 changed files with 20 additions and 5 deletions

View File

@ -20,8 +20,12 @@ RUN echo "hey" > /tmp/foo
USER testuser:1001 USER testuser:1001
RUN echo "hey2" >> /tmp/foo RUN echo "hey2" >> /tmp/foo
USER root
RUN useradd -ms /bin/bash newuser RUN useradd -ms /bin/bash newuser
USER newuser USER newuser
RUN echo "hi" > $HOME/file RUN echo "hi" > $HOME/file
COPY context/foo $HOME/foo COPY context/foo $HOME/foo
USER 1001
RUN echo $HOME

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"os/user"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@ -116,19 +117,29 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
} }
// addDefaultHOME adds the default value for HOME if it isn't already set // addDefaultHOME adds the default value for HOME if it isn't already set
func addDefaultHOME(user string, envs []string) []string { func addDefaultHOME(u string, envs []string) []string {
for _, env := range envs { for _, env := range envs {
split := strings.SplitN(env, "=", 2) split := strings.SplitN(env, "=", 2)
if split[0] == constants.HOME { if split[0] == constants.HOME {
return envs return envs
} }
} }
// If user isn't set, set default value of HOME // If user isn't set, set default value of HOME
if user == "" { if u == "" {
return append(envs, fmt.Sprintf("%s=%s", constants.HOME, constants.DefaultHOMEValue)) return append(envs, fmt.Sprintf("%s=%s", constants.HOME, constants.DefaultHOMEValue))
} }
// If user is set, set value of HOME to /home/${user}
return append(envs, fmt.Sprintf("%s=/home/%s", constants.HOME, user)) // If user is set to username, set value of HOME to /home/${user}
// Otherwise the user is set to uid and HOME is /
home := fmt.Sprintf("%s=/", constants.HOME)
userObj, err := user.Lookup(u)
if err == nil {
u = userObj.Username
home = fmt.Sprintf("%s=/home/%s", constants.HOME, u)
}
return append(envs, home)
} }
// FilesToSnapshot returns nil for this command because we don't know which files // FilesToSnapshot returns nil for this command because we don't know which files

View File

@ -59,7 +59,7 @@ func Test_addDefaultHOME(t *testing.T) {
}, },
expected: []string{ expected: []string{
"PATH=/something/else", "PATH=/something/else",
"HOME=/home/newuser", "HOME=/",
}, },
}, },
} }