Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit d88f71a

Browse files
authored
simplify unix socket permission format (#3014)
### Pull Request Checklist <!-- Please read https://matrix-org.github.io/dendrite/development/contributing before submitting your pull request --> * [x] I have added Go unit tests or [Complement integration tests](https://github.com/matrix-org/complement) for this PR _or_ I have justified why this PR doesn't need tests * [x] Pull request includes a [sign off below using a legally identifiable name](https://matrix-org.github.io/dendrite/development/contributing#sign-off) _or_ I have already signed off privately Signed-off-by: `Boris Rybalkin <[email protected]>`
1 parent 2c58bab commit d88f71a

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

cmd/dendrite/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
import (
1818
"flag"
19-
"io/fs"
2019

2120
"github.com/sirupsen/logrus"
2221

@@ -34,8 +33,8 @@ var (
3433
unixSocket = flag.String("unix-socket", "",
3534
"EXPERIMENTAL(unstable): The HTTP listening unix socket for the server (disables http[s]-bind-address feature)",
3635
)
37-
unixSocketPermission = flag.Int("unix-socket-permission", 0755,
38-
"EXPERIMENTAL(unstable): The HTTP listening unix socket permission for the server",
36+
unixSocketPermission = flag.String("unix-socket-permission", "755",
37+
"EXPERIMENTAL(unstable): The HTTP listening unix socket permission for the server (in chmod format like 755)",
3938
)
4039
httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server")
4140
httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server")
@@ -59,7 +58,11 @@ func main() {
5958
}
6059
httpsAddr = https
6160
} else {
62-
httpAddr = config.UnixSocketAddress(*unixSocket, fs.FileMode(*unixSocketPermission))
61+
socket, err := config.UnixSocketAddress(*unixSocket, *unixSocketPermission)
62+
if err != nil {
63+
logrus.WithError(err).Fatalf("Failed to parse unix socket")
64+
}
65+
httpAddr = socket
6366
}
6467

6568
options := []basepkg.BaseDendriteOptions{}

setup/base/base_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestLandingPage_UnixSocket(t *testing.T) {
7676
tempDir := t.TempDir()
7777
socket := path.Join(tempDir, "socket")
7878
// start base with the listener and wait for it to be started
79-
address := config.UnixSocketAddress(socket, 0755)
79+
address, err := config.UnixSocketAddress(socket, "755")
8080
assert.NoError(t, err)
8181
go b.SetupAndServeHTTP(address, nil, nil)
8282
time.Sleep(time.Millisecond * 100)

setup/config/config_address.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"io/fs"
55
"net/url"
6+
"strconv"
67
)
78

89
const (
@@ -32,8 +33,12 @@ func (s ServerAddress) Network() string {
3233
}
3334
}
3435

35-
func UnixSocketAddress(path string, perm fs.FileMode) ServerAddress {
36-
return ServerAddress{Address: path, Scheme: NetworkUnix, UnixSocketPermission: perm}
36+
func UnixSocketAddress(path string, perm string) (ServerAddress, error) {
37+
permission, err := strconv.ParseInt(perm, 8, 32)
38+
if err != nil {
39+
return ServerAddress{}, err
40+
}
41+
return ServerAddress{Address: path, Scheme: NetworkUnix, UnixSocketPermission: fs.FileMode(permission)}, nil
3742
}
3843

3944
func HTTPAddress(urlAddress string) (ServerAddress, error) {

setup/config/config_address_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ func TestHttpAddress_ParseBad(t *testing.T) {
2020
}
2121

2222
func TestUnixSocketAddress_Network(t *testing.T) {
23-
address := UnixSocketAddress("/tmp", fs.FileMode(0755))
23+
address, err := UnixSocketAddress("/tmp", "0755")
24+
assert.NoError(t, err)
2425
assert.Equal(t, "unix", address.Network())
2526
}
27+
28+
func TestUnixSocketAddress_Permission_LeadingZero_Ok(t *testing.T) {
29+
address, err := UnixSocketAddress("/tmp", "0755")
30+
assert.NoError(t, err)
31+
assert.Equal(t, fs.FileMode(0755), address.UnixSocketPermission)
32+
}
33+
34+
func TestUnixSocketAddress_Permission_NoLeadingZero_Ok(t *testing.T) {
35+
address, err := UnixSocketAddress("/tmp", "755")
36+
assert.NoError(t, err)
37+
assert.Equal(t, fs.FileMode(0755), address.UnixSocketPermission)
38+
}
39+
40+
func TestUnixSocketAddress_Permission_NonOctal_Bad(t *testing.T) {
41+
_, err := UnixSocketAddress("/tmp", "855")
42+
assert.Error(t, err)
43+
}

0 commit comments

Comments
 (0)