Skip to content

Commit

Permalink
Apply default home value based on user for run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Aug 8, 2018
1 parent c75a59d commit 16139c2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 64 deletions.
3 changes: 3 additions & 0 deletions integration/dockerfiles/Dockerfile_test_run
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ RUN rm /etc/baz
# Test with ARG
ARG file
RUN echo "run" > $file

RUN echo "test home" > $HOME/file
COPY context/foo $HOME/foo
8 changes: 7 additions & 1 deletion integration/dockerfiles/Dockerfile_test_user_run
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ RUN groupadd testgroup
USER testuser:testgroup
RUN echo "hey" > /tmp/foo
USER testuser:1001
RUN echo "hey2" >> /tmp/foo
RUN echo "hey2" >> /tmp/foo

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

20 changes: 19 additions & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package commands

import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"

"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
cmd.Env = replacementEnvs
cmd.Env = addDefaultEnvs(config.User, replacementEnvs)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

// If specified, run the command as a specific user
Expand Down Expand Up @@ -113,6 +115,22 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
return nil
}

// addDefaultEnvs adds default values of variables (like $HOME) if they aren't already set
func addDefaultEnvs(user 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 == "" {
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))
}

// FilesToSnapshot returns nil for this command because we don't know which files
// have changed, so we snapshot the entire system.
func (r *RunCommand) FilesToSnapshot() []string {
Expand Down
72 changes: 72 additions & 0 deletions pkg/commands/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands

import (
"testing"

"github.com/GoogleContainerTools/kaniko/testutil"
)

func Test_addDefaultEnvs(t *testing.T) {
tests := []struct {
name string
user string
initial []string
expected []string
}{
{
name: "HOME already set",
user: "",
initial: []string{
"HOME=/something",
"PATH=/something/else",
},
expected: []string{
"HOME=/something",
"PATH=/something/else",
},
},
{
name: "HOME isn't set, user isn't set",
user: "",
initial: []string{
"PATH=/something/else",
},
expected: []string{
"PATH=/something/else",
"HOME=/root",
},
},
{
name: "HOME isn't set, user is set",
user: "newuser",
initial: []string{
"PATH=/something/else",
},
expected: []string{
"PATH=/something/else",
"HOME=/home/newuser",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := addDefaultEnvs(test.user, test.initial)
testutil.CheckErrorAndDeepEqual(t, false, nil, test.expected, actual)
})
}
}
17 changes: 1 addition & 16 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ limitations under the License.
package util

import (
"fmt"
"path/filepath"
"strconv"
"strings"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/k8schain"
Expand Down Expand Up @@ -75,21 +73,8 @@ func RetrieveConfigFile(sourceImage v1.Image) (*v1.ConfigFile, error) {
}
if sourceImage == empty.Image {
imageConfig.Config.Env = constants.ScratchEnvVars
return imageConfig, nil
}
return defaultConfigEnv(imageConfig), nil
}

func defaultConfigEnv(config *v1.ConfigFile) *v1.ConfigFile {
for _, env := range config.Config.Env {
split := strings.SplitN(env, "=", 2)
if split[0] == constants.HOME {
return config
}
}
config.Config.Env = append(config.Config.Env,
fmt.Sprintf("%s=%s", constants.HOME, constants.DefaultHOMEValue))
return config
return imageConfig, nil
}

func tarballImage(index int) (v1.Image, error) {
Expand Down
46 changes: 0 additions & 46 deletions pkg/util/image_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,52 +84,6 @@ func Test_TarImage(t *testing.T) {
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}

func Test_defaultConfigEnv(t *testing.T) {
tests := []struct {
name string
initial []string
expected []string
}{
{
name: "HOME already set",
initial: []string{
"HOME=/something",
"PATH=/something/else",
},
expected: []string{
"HOME=/something",
"PATH=/something/else",
},
},
{
name: "HOME isn't set",
initial: []string{
"PATH=/something/else",
},
expected: []string{
"PATH=/something/else",
"HOME=/root",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
initial := &v1.ConfigFile{
Config: v1.Config{
Env: test.initial,
},
}
expected := &v1.ConfigFile{
Config: v1.Config{
Env: test.expected,
},
}
actual := defaultConfigEnv(initial)
testutil.CheckErrorAndDeepEqual(t, false, nil, expected, actual)
})
}
}

// parse parses the contents of a Dockerfile and returns a list of commands
func parse(s string) ([]instructions.Stage, error) {
p, err := parser.Parse(bytes.NewReader([]byte(s)))
Expand Down

0 comments on commit 16139c2

Please sign in to comment.