Skip to content

Commit

Permalink
Merge pull request #995 from tejal29/507
Browse files Browse the repository at this point in the history
Fix home and group set for user command
  • Loading branch information
tejal29 authored Jan 25, 2020
2 parents 68e8c6e + 048de00 commit e8fffda
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@ 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 = addDefaultHOME(config.User, replacementEnvs)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

var userStr string
// If specified, run the command as a specific user
if config.User != "" {
userAndGroup := strings.Split(config.User, ":")
userStr := userAndGroup[0]
userStr = userAndGroup[0]
var groupStr string
if len(userAndGroup) > 1 {
groupStr = userAndGroup[1]
Expand All @@ -101,6 +100,7 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid}
}
cmd.Env = addDefaultHOME(userStr, replacementEnvs)

if err := cmd.Start(); err != nil {
return errors.Wrap(err, "starting command")
Expand Down
7 changes: 3 additions & 4 deletions pkg/commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ func (r *UserCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
if err != nil {
return err
}
var groupStr string
groupStr := userStr
if len(userAndGroup) > 1 {
groupStr, err = util.ResolveEnvironmentReplacement(userAndGroup[1], replacementEnvs, false)
if err != nil {
return err
}
}

if groupStr != "" {
userStr = userStr + ":" + groupStr
}
userStr = userStr + ":" + groupStr

config.User = userStr
return nil
}
Expand Down
20 changes: 13 additions & 7 deletions pkg/commands/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ import (
var userTests = []struct {
user string
expectedUID string
expectedGID string
}{
{
user: "root",
expectedUID: "root",
expectedUID: "root:root",
},
{
user: "root-add",
expectedUID: "root-add",
expectedUID: "root-add:root-add",
},
{
user: "0",
expectedUID: "0",
expectedUID: "0:0",
},
{
user: "fakeUser",
expectedUID: "fakeUser",
expectedUID: "fakeUser:fakeUser",
},
{
user: "root:root",
Expand All @@ -56,18 +57,23 @@ var userTests = []struct {
{
user: "root:0",
expectedUID: "root:0",
expectedGID: "f0",
},
{
user: "0:0",
expectedUID: "0:0",
},
{
user: "$envuser",
expectedUID: "root",
expectedUID: "root:root",
},
{
user: "root:$envgroup",
expectedUID: "root:root",
expectedUID: "root:grp",
},
{
user: "some:grp",
expectedUID: "some:grp",
},
}

Expand All @@ -76,7 +82,7 @@ func TestUpdateUser(t *testing.T) {
cfg := &v1.Config{
Env: []string{
"envuser=root",
"envgroup=root",
"envgroup=grp",
},
}
cmd := UserCommand{
Expand Down

0 comments on commit e8fffda

Please sign in to comment.