Skip to content

Commit 3335deb

Browse files
authored
fix: HOME fallback for custom container user (#88)
1 parent d930866 commit 3335deb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

cmd/localstack/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ func main() {
170170
}
171171
}
172172

173+
EnsureHome()
174+
173175
// file watcher for hot-reloading
174176
fileWatcherContext, cancelFileWatcher := context.WithCancel(context.Background())
175177

cmd/localstack/user.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package main
33

44
import (
55
"fmt"
6-
log "github.com/sirupsen/logrus"
76
"os"
87
"os/user"
98
"strconv"
109
"strings"
1110
"syscall"
11+
12+
log "github.com/sirupsen/logrus"
1213
)
1314

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

86+
// EnsureHome sets HOME=/tmp if the current process has no /etc/passwd entry.
87+
// UnsetLsEnvs strips HOME for AWS parity, which is fine in the normal
88+
// root-start flow where AddUser has written a passwd entry. But when the
89+
// container is launched with --user=1000:1000, AddUser is never called and
90+
// Node's os.homedir() / AWS SDK config loading fail with ENOENT.
91+
func EnsureHome() {
92+
if _, err := user.Current(); err != nil {
93+
if setErr := os.Setenv("HOME", "/tmp"); setErr != nil {
94+
log.Warnln("Could not set HOME=/tmp for non-passwd user:", setErr)
95+
} else {
96+
log.Debugln("No /etc/passwd entry for current UID; HOME set to /tmp")
97+
}
98+
}
99+
}
100+
85101
// DropPrivileges switches to another UNIX user by dropping root privileges
86102
// Initially based on https://stackoverflow.com/a/75545491/6875981
87103
func DropPrivileges(userToSwitchTo string) error {

0 commit comments

Comments
 (0)