Skip to content

Commit

Permalink
make chmod paths configurable from localstack
Browse files Browse the repository at this point in the history
  • Loading branch information
dfangl committed Mar 11, 2024
1 parent d0a10b0 commit 704e4e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
26 changes: 26 additions & 0 deletions cmd/localstack/file_utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package main

import (
"encoding/json"
log "github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
"strconv"
)

type Chmod struct {
Path string `json:"path"`
Mode string `json:"mode"`
}

func AdaptFilesystemPermissions(chmodInfoString string) error {
var chmodInfo []Chmod
err := json.Unmarshal([]byte(chmodInfoString), &chmodInfo)
if err != nil {
return err
}
for _, chmod := range chmodInfo {
mode, err := strconv.ParseInt(chmod.Mode, 0, 32)
if err != nil {
return err
}
if err := ChmodRecursively(chmod.Path, os.FileMode(mode)); err != nil {
log.Warnf("Could not change file mode recursively of directory %s: %s\n", chmod.Path, err)
}
}
return nil
}

// 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 {
Expand Down
8 changes: 7 additions & 1 deletion cmd/localstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type LsOpts struct {
CodeArchives string
HotReloadingPaths []string
FileWatcherStrategy string
ChmodPaths string
LocalstackIP string
InitLogLevel string
EdgePort string
Expand Down Expand Up @@ -59,6 +60,7 @@ func InitLsOpts() *LsOpts {
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
ChmodPaths: GetenvWithDefault("LOCALSTACK_CHMOD_PATHS", "[]"),
}
}

Expand All @@ -73,12 +75,12 @@ func UnsetLsEnvs() {
"LOCALSTACK_USER",
"LOCALSTACK_CODE_ARCHIVES",
"LOCALSTACK_HOT_RELOADING_PATHS",
"LOCALSTACK_ENABLE_DNS_SERVER",
"LOCALSTACK_ENABLE_XRAY_TELEMETRY",
"LOCALSTACK_INIT_LOG_LEVEL",
"LOCALSTACK_POST_INVOKE_WAIT_MS",
"LOCALSTACK_FUNCTION_ACCOUNT_ID",
"LOCALSTACK_MAX_PAYLOAD_SIZE",
"LOCALSTACK_CHMOD_PATHS",

// Docker container ID
"HOSTNAME",
Expand Down Expand Up @@ -142,6 +144,10 @@ func main() {
log.Fatal("Failed to download code archives: " + err.Error())
}

if err := AdaptFilesystemPermissions(lsOpts.ChmodPaths); err != nil {
log.Warnln("Could not change file mode of code directories:", err)
}

// set file 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)
Expand Down

0 comments on commit 704e4e3

Please sign in to comment.