Skip to content

Commit

Permalink
Fix filesystem permission parity (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
joe4dev authored Oct 16, 2023
1 parent e4d28d8 commit 605e17d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
22 changes: 22 additions & 0 deletions cmd/localstack/file_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"os"
"path/filepath"
)

// Inspired by https://stackoverflow.com/questions/73864379/golang-change-permission-os-chmod-and-os-chowm-recursively
// but using the more efficient WalkDir API
func ChmodRecursively(root string, mode os.FileMode) error {
return filepath.WalkDir(root,
func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
err = os.Chmod(path, mode)
if err != nil {
return err
}
return nil
})
}
19 changes: 16 additions & 3 deletions cmd/localstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ func main() {
log.Fatal("Failed to download code archives: " + err.Error())
}

// fix permissions of the layers directory for better AWS parity
if err := ChmodRecursively("/opt", 0755); err != nil {
log.Warnln("Could not change file mode recursively of directory /opt:", err)
}
// fix permissions of the tmp directory for better AWS parity
if err := ChmodRecursively("/tmp", 0700); err != nil {
log.Warnln("Could not change file mode recursively of directory /tmp:", err)
}

// parse CLI args
bootstrap, handler := getBootstrap(os.Args)

Expand All @@ -141,11 +150,15 @@ func main() {
gid := 990
AddUser(lsOpts.User, uid, gid)
if err := os.Chown("/tmp", uid, gid); err != nil {
log.Warnln("Could not change owner of /tmp:", err)
log.Warnln("Could not change owner of directory /tmp:", err)
}
UserLogger().Debugln("Process running as root user.")
DropPrivileges(lsOpts.User)
UserLogger().Debugln("Process running as non-root user.")
err := DropPrivileges(lsOpts.User)
if err != nil {
log.Warnln("Could not drop root privileges.", err)
} else {
UserLogger().Debugln("Process running as non-root user.")
}
}

logCollector := NewLogCollector()
Expand Down
4 changes: 2 additions & 2 deletions cmd/localstack/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func UserLogger() *log.Entry {
}
uid := os.Getuid()
uidString := strconv.Itoa(uid)
user, err := user.LookupId(uidString)
userObject, err := user.LookupId(uidString)
if err != nil {
log.Warnln("Could not look up user by uid:", uid, err)
}
return log.WithFields(log.Fields{
"username": user.Username,
"username": userObject.Username,
"uid": uid,
"euid": os.Geteuid(),
"gid": os.Getgid(),
Expand Down

0 comments on commit 605e17d

Please sign in to comment.