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 <ribalkin@gmail.com>`
This commit is contained in:
Boris Rybalkin 2023-03-16 07:51:21 +00:00 committed by GitHub
parent 2c58bab6a8
commit d88f71ab71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 8 deletions

View File

@ -16,7 +16,6 @@ package main
import ( import (
"flag" "flag"
"io/fs"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -34,8 +33,8 @@ var (
unixSocket = flag.String("unix-socket", "", unixSocket = flag.String("unix-socket", "",
"EXPERIMENTAL(unstable): The HTTP listening unix socket for the server (disables http[s]-bind-address feature)", "EXPERIMENTAL(unstable): The HTTP listening unix socket for the server (disables http[s]-bind-address feature)",
) )
unixSocketPermission = flag.Int("unix-socket-permission", 0755, unixSocketPermission = flag.String("unix-socket-permission", "755",
"EXPERIMENTAL(unstable): The HTTP listening unix socket permission for the server", "EXPERIMENTAL(unstable): The HTTP listening unix socket permission for the server (in chmod format like 755)",
) )
httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server") httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server")
httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server") httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server")
@ -59,7 +58,11 @@ func main() {
} }
httpsAddr = https httpsAddr = https
} else { } else {
httpAddr = config.UnixSocketAddress(*unixSocket, fs.FileMode(*unixSocketPermission)) socket, err := config.UnixSocketAddress(*unixSocket, *unixSocketPermission)
if err != nil {
logrus.WithError(err).Fatalf("Failed to parse unix socket")
}
httpAddr = socket
} }
options := []basepkg.BaseDendriteOptions{} options := []basepkg.BaseDendriteOptions{}

View File

@ -76,7 +76,7 @@ func TestLandingPage_UnixSocket(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
socket := path.Join(tempDir, "socket") socket := path.Join(tempDir, "socket")
// start base with the listener and wait for it to be started // start base with the listener and wait for it to be started
address := config.UnixSocketAddress(socket, 0755) address, err := config.UnixSocketAddress(socket, "755")
assert.NoError(t, err) assert.NoError(t, err)
go b.SetupAndServeHTTP(address, nil, nil) go b.SetupAndServeHTTP(address, nil, nil)
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)

View File

@ -3,6 +3,7 @@ package config
import ( import (
"io/fs" "io/fs"
"net/url" "net/url"
"strconv"
) )
const ( const (
@ -32,8 +33,12 @@ func (s ServerAddress) Network() string {
} }
} }
func UnixSocketAddress(path string, perm fs.FileMode) ServerAddress { func UnixSocketAddress(path string, perm string) (ServerAddress, error) {
return ServerAddress{Address: path, Scheme: NetworkUnix, UnixSocketPermission: perm} permission, err := strconv.ParseInt(perm, 8, 32)
if err != nil {
return ServerAddress{}, err
}
return ServerAddress{Address: path, Scheme: NetworkUnix, UnixSocketPermission: fs.FileMode(permission)}, nil
} }
func HTTPAddress(urlAddress string) (ServerAddress, error) { func HTTPAddress(urlAddress string) (ServerAddress, error) {

View File

@ -20,6 +20,24 @@ func TestHttpAddress_ParseBad(t *testing.T) {
} }
func TestUnixSocketAddress_Network(t *testing.T) { func TestUnixSocketAddress_Network(t *testing.T) {
address := UnixSocketAddress("/tmp", fs.FileMode(0755)) address, err := UnixSocketAddress("/tmp", "0755")
assert.NoError(t, err)
assert.Equal(t, "unix", address.Network()) assert.Equal(t, "unix", address.Network())
} }
func TestUnixSocketAddress_Permission_LeadingZero_Ok(t *testing.T) {
address, err := UnixSocketAddress("/tmp", "0755")
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0755), address.UnixSocketPermission)
}
func TestUnixSocketAddress_Permission_NoLeadingZero_Ok(t *testing.T) {
address, err := UnixSocketAddress("/tmp", "755")
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0755), address.UnixSocketPermission)
}
func TestUnixSocketAddress_Permission_NonOctal_Bad(t *testing.T) {
_, err := UnixSocketAddress("/tmp", "855")
assert.Error(t, err)
}