Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/localstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ func main() {
}
}

EnsureHome()

// file watcher for hot-reloading
fileWatcherContext, cancelFileWatcher := context.WithCancel(context.Background())

Expand Down
18 changes: 17 additions & 1 deletion cmd/localstack/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main

import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"os/user"
"strconv"
"strings"
"syscall"

log "github.com/sirupsen/logrus"
)

// AddUser adds a UNIX user (e.g., sbx_user1051) to the passwd and shadow files if not already present
Expand Down Expand Up @@ -82,6 +83,21 @@ func UserLogger() *log.Entry {
})
}

// EnsureHome sets HOME=/tmp if the current process has no /etc/passwd entry.
// UnsetLsEnvs strips HOME for AWS parity, which is fine in the normal
// root-start flow where AddUser has written a passwd entry. But when the
// container is launched with --user=1000:1000, AddUser is never called and
// Node's os.homedir() / AWS SDK config loading fail with ENOENT.
func EnsureHome() {
if _, err := user.Current(); err != nil {
if setErr := os.Setenv("HOME", "/tmp"); setErr != nil {
log.Warnln("Could not set HOME=/tmp for non-passwd user:", setErr)
} else {
log.Debugln("No /etc/passwd entry for current UID; HOME set to /tmp")
}
}
}

// DropPrivileges switches to another UNIX user by dropping root privileges
// Initially based on https://stackoverflow.com/a/75545491/6875981
func DropPrivileges(userToSwitchTo string) error {
Expand Down