Skip to content

Commit

Permalink
Merge pull request #364 from fabriciojs/patch/exec-user
Browse files Browse the repository at this point in the history
fix exec checking for user within container
  • Loading branch information
danielsuguimoto authored Aug 9, 2021
2 parents d7603ed + 98aa786 commit 4366dd5
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ func NewKoolExec() *KoolExec {

// Execute runs the exec logic with incoming arguments.
func (e *KoolExec) Execute(args []string) (err error) {
if !e.IsTerminal() {
e.composeExec.AppendArgs("-T")
}

if asuser := e.env.Get("KOOL_ASUSER"); asuser != "" {
// we have a KOOL_ASUSER env; now we need to know whether
// the image of the target service have such user
passwd, _ := e.Exec(e.composeExec, args[0], "cat", "/etc/passwd")
// kool:x:UID
if strings.Contains(passwd, fmt.Sprintf("kool:x:%s", asuser)) {
// since user existing within the container, we use it
var passwd string
if passwd, err = e.Exec(e.composeExec, args[0], "cat", "/etc/passwd"); err != nil {
e.Warning("failed to check running container for kool user; not setting a user (err: %s)", err.Error())
err = nil
} else if strings.Contains(passwd, fmt.Sprintf("kool:x:%s", asuser)) {
// since user (kool:x:UID) exists within the container, we set it
e.composeExec.AppendArgs("--user", asuser)
}
}

if !e.IsTerminal() {
e.composeExec.AppendArgs("-T")
}

if aware, ok := e.composeExec.(compose.TtyAware); ok {
// let DockerCompose know about whether we are under TTY or not
aware.SetIsTTY(e.IsTerminal())
Expand Down

0 comments on commit 4366dd5

Please sign in to comment.