mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 01:41:02 +02:00
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:
@@ -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
|
||||
|
||||
+15
-4
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
"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
|
||||
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
|
||||
|
||||
@@ -59,7 +59,7 @@ func Test_addDefaultHOME(t *testing.T) {
|
||||
},
|
||||
expected: []string{
|
||||
"PATH=/something/else",
|
||||
"HOME=/home/newuser",
|
||||
"HOME=/",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user