Skip to content

Commit

Permalink
set default HOME env properly (#341)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sharifelgamal authored and dlorenc committed Sep 27, 2018
1 parent 1a13c81 commit 49184c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions integration/dockerfiles/Dockerfile_test_user_run
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ RUN echo "hey" > /tmp/foo
USER testuser:1001
RUN echo "hey2" >> /tmp/foo

USER root

RUN useradd -ms /bin/bash newuser
USER newuser
RUN echo "hi" > $HOME/file
COPY context/foo $HOME/foo

USER 1001
RUN echo $HOME
19 changes: 15 additions & 4 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -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
func addDefaultHOME(user string, envs []string) []string {
func addDefaultHOME(u string, envs []string) []string {
for _, env := range envs {
split := strings.SplitN(env, "=", 2)
if split[0] == constants.HOME {
return envs
}
}

// 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))
}
// 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
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Test_addDefaultHOME(t *testing.T) {
},
expected: []string{
"PATH=/something/else",
"HOME=/home/newuser",
"HOME=/",
},
},
}
Expand Down

0 comments on commit 49184c2

Please sign in to comment.