Skip to content

Commit 28d6661

Browse files
authored
Allow LocalStack specific configuration of chmod on startup, remove DNS logic (#34)
1 parent ddc62d9 commit 28d6661

File tree

6 files changed

+34
-148
lines changed

6 files changed

+34
-148
lines changed

cmd/localstack/awsutil.go

-17
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,6 @@ func resetListener(changeChannel <-chan bool, server *CustomInteropServer) {
139139

140140
}
141141

142-
func RunDNSRewriter(opts *LsOpts, ctx context.Context) {
143-
if opts.EnableDnsServer != "1" {
144-
log.Debugln("DNS server disabled.")
145-
return
146-
}
147-
dnsForwarder, err := NewDnsForwarder(opts.LocalstackIP)
148-
if err != nil {
149-
log.Errorln("Error creating dns forwarder.")
150-
return
151-
}
152-
defer dnsForwarder.Shutdown()
153-
dnsForwarder.Start()
154-
155-
<-ctx.Done()
156-
log.Debugln("DNS server stopped")
157-
}
158-
159142
func RunHotReloadingListener(server *CustomInteropServer, targetPaths []string, ctx context.Context, fileWatcherStrategy string) {
160143
if len(targetPaths) == 1 && targetPaths[0] == "" {
161144
log.Debugln("Hot reloading disabled.")

cmd/localstack/dns.go

-71
This file was deleted.

cmd/localstack/file_utils.go

+29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
log "github.com/sirupsen/logrus"
46
"io"
57
"os"
68
"path/filepath"
9+
"strconv"
710
)
811

12+
type Chmod struct {
13+
Path string `json:"path"`
14+
Mode string `json:"mode"`
15+
}
16+
17+
// AdaptFilesystemPermissions Adapts the file system permissions to the mode specified in the chmodInfoString parameter
18+
// chmodInfoString should be a json encoded list of `Chmod` structs.
19+
// example: '[{"path": "/opt", "mode": "0755"}]'. The mode string should be an octal representation of the targeted file mode.
20+
func AdaptFilesystemPermissions(chmodInfoString string) error {
21+
var chmodInfo []Chmod
22+
err := json.Unmarshal([]byte(chmodInfoString), &chmodInfo)
23+
if err != nil {
24+
return err
25+
}
26+
for _, chmod := range chmodInfo {
27+
mode, err := strconv.ParseInt(chmod.Mode, 0, 32)
28+
if err != nil {
29+
return err
30+
}
31+
if err := ChmodRecursively(chmod.Path, os.FileMode(mode)); err != nil {
32+
log.Warnf("Could not change file mode recursively of directory %s: %s\n", chmod.Path, err)
33+
}
34+
}
35+
return nil
36+
}
37+
938
// Inspired by https://stackoverflow.com/questions/73864379/golang-change-permission-os-chmod-and-os-chowm-recursively
1039
// but using the more efficient WalkDir API
1140
func ChmodRecursively(root string, mode os.FileMode) error {

cmd/localstack/main.go

+5-25
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type LsOpts struct {
2323
CodeArchives string
2424
HotReloadingPaths []string
2525
FileWatcherStrategy string
26-
EnableDnsServer string
26+
ChmodPaths string
2727
LocalstackIP string
2828
InitLogLevel string
2929
EdgePort string
@@ -57,10 +57,10 @@ func InitLsOpts() *LsOpts {
5757
CodeArchives: os.Getenv("LOCALSTACK_CODE_ARCHIVES"),
5858
HotReloadingPaths: strings.Split(GetenvWithDefault("LOCALSTACK_HOT_RELOADING_PATHS", ""), ","),
5959
FileWatcherStrategy: os.Getenv("LOCALSTACK_FILE_WATCHER_STRATEGY"),
60-
EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"),
6160
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
6261
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
6362
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
63+
ChmodPaths: GetenvWithDefault("LOCALSTACK_CHMOD_PATHS", "[]"),
6464
}
6565
}
6666

@@ -75,12 +75,12 @@ func UnsetLsEnvs() {
7575
"LOCALSTACK_USER",
7676
"LOCALSTACK_CODE_ARCHIVES",
7777
"LOCALSTACK_HOT_RELOADING_PATHS",
78-
"LOCALSTACK_ENABLE_DNS_SERVER",
7978
"LOCALSTACK_ENABLE_XRAY_TELEMETRY",
8079
"LOCALSTACK_INIT_LOG_LEVEL",
8180
"LOCALSTACK_POST_INVOKE_WAIT_MS",
8281
"LOCALSTACK_FUNCTION_ACCOUNT_ID",
8382
"LOCALSTACK_MAX_PAYLOAD_SIZE",
83+
"LOCALSTACK_CHMOD_PATHS",
8484

8585
// Docker container ID
8686
"HOSTNAME",
@@ -139,31 +139,13 @@ func main() {
139139
}
140140
interop.MaxPayloadSize = payloadSize
141141

142-
// enable dns server
143-
dnsServerContext, stopDnsServer := context.WithCancel(context.Background())
144-
go RunDNSRewriter(lsOpts, dnsServerContext)
145-
146142
// download code archive if env variable is set
147143
if err := DownloadCodeArchives(lsOpts.CodeArchives); err != nil {
148144
log.Fatal("Failed to download code archives: " + err.Error())
149145
}
150146

151-
// set file permissions of the tmp directory for better AWS parity
152-
if err := ChmodRecursively("/tmp", 0700); err != nil {
153-
log.Warnln("Could not change file mode recursively of directory /tmp:", err)
154-
}
155-
// set file permissions of the layers directory for better AWS parity
156-
if err := ChmodRecursively("/opt", 0755); err != nil {
157-
log.Warnln("Could not change file mode recursively of directory /opt:", err)
158-
}
159-
// set file permissions of the code directory if at least one layer is present for better AWS parity
160-
// Limitation: hot reloading likely breaks file permission parity for /var/task in combination with layers
161-
// Heuristic for detecting the presence of layers. It might fail for an empty layer or image-based Lambda.
162-
if isDirEmpty, _ := IsDirEmpty("/opt"); !isDirEmpty {
163-
log.Debugln("Detected layer present")
164-
if err := ChmodRecursively("/var/task", 0755); err != nil {
165-
log.Warnln("Could not change file mode recursively of directory /var/task:", err)
166-
}
147+
if err := AdaptFilesystemPermissions(lsOpts.ChmodPaths); err != nil {
148+
log.Warnln("Could not change file mode of code directories:", err)
167149
}
168150

169151
// parse CLI args
@@ -200,8 +182,6 @@ func main() {
200182
AddShutdownFunc(func() {
201183
log.Debugln("Stopping file watcher")
202184
cancelFileWatcher()
203-
log.Debugln("Stopping DNS server")
204-
stopDnsServer()
205185
}).
206186
SetExtensionsFlag(true).
207187
SetInitCachingFlag(true).

go.mod

-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/go-chi/chi v4.1.2+incompatible
1212
github.com/google/uuid v1.3.0
1313
github.com/jessevdk/go-flags v1.5.0
14-
github.com/miekg/dns v1.1.50
1514
github.com/shirou/gopsutil v2.19.10+incompatible
1615
github.com/sirupsen/logrus v1.9.3
1716
github.com/stretchr/testify v1.8.4
@@ -26,10 +25,8 @@ require (
2625
github.com/jmespath/go-jmespath v0.4.0 // indirect
2726
github.com/pmezard/go-difflib v1.0.0 // indirect
2827
github.com/stretchr/objx v0.5.0 // indirect
29-
golang.org/x/mod v0.8.0 // indirect
3028
golang.org/x/net v0.18.0 // indirect
3129
golang.org/x/text v0.14.0 // indirect
32-
golang.org/x/tools v0.6.0 // indirect
3330
gopkg.in/yaml.v2 v2.2.8 // indirect
3431
gopkg.in/yaml.v3 v3.0.1 // indirect
3532
)

go.sum

-32
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
2525
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
2626
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
2727
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
28-
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
29-
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
3028
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3129
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3230
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -43,53 +41,23 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
4341
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4442
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
4543
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
46-
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
47-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
48-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
49-
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
50-
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
51-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
52-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
53-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
54-
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
55-
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
5644
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
5745
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
5846
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
59-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
60-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6147
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
6248
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
63-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
64-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
65-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6649
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
67-
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
68-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
69-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7050
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7251
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7352
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7453
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7554
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
7655
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
77-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
7856
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
79-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
80-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
81-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
8257
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
8358
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
8459
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
8560
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
86-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
87-
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
88-
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
89-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
90-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
91-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
92-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9361
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9462
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9563
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=

0 commit comments

Comments
 (0)