Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions sockets/unix_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,6 @@ import (
// SockOption sets up socket file's creating option
type SockOption func(string) error

// WithChown modifies the socket file's uid and gid
func WithChown(uid, gid int) SockOption {
return func(path string) error {
if err := os.Chown(path, uid, gid); err != nil {
return err
}
return nil
}
}

// WithChmod modifies socket file's access mode.
func WithChmod(mask os.FileMode) SockOption {
return func(path string) error {
if err := os.Chmod(path, mask); err != nil {
return err
}
return nil
}
}

// NewUnixSocketWithOpts creates a unix socket with the specified options.
// By default, socket permissions are 0000 (i.e.: no access for anyone); pass
// WithChmod() and WithChown() to set the desired ownership and permissions.
Expand Down Expand Up @@ -102,8 +82,3 @@ func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error

return l, nil
}

// NewUnixSocket creates a unix socket with the specified path and group.
func NewUnixSocket(path string, gid int) (net.Listener, error) {
return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
}
32 changes: 0 additions & 32 deletions sockets/unix_socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sockets
import (
"fmt"
"net"
"os"
"testing"
)

Expand Down Expand Up @@ -31,34 +30,3 @@ func runTest(t *testing.T, path string, l net.Listener, echoStr string) {
t.Fatal(fmt.Errorf("msg may lost"))
}
}

// TestNewUnixSocket run under root user.
func TestNewUnixSocket(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("requires root")
}
gid := os.Getgid()
path := "/tmp/test.sock"
echoStr := "hello"
l, err := NewUnixSocket(path, gid)
if err != nil {
t.Fatal(err)
}
defer func() { _ = l.Close() }()
runTest(t, path, l, echoStr)
}

func TestUnixSocketWithOpts(t *testing.T) {
socketFile, err := os.CreateTemp("", "test*.sock")
if err != nil {
t.Fatal(err)
}
_ = socketFile.Close()
defer func() { _ = os.Remove(socketFile.Name()) }()

l := createTestUnixSocket(t, socketFile.Name())
defer func() { _ = l.Close() }()

echoStr := "hello"
runTest(t, socketFile.Name(), l, echoStr)
}
26 changes: 26 additions & 0 deletions sockets/unix_socket_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@ package sockets

import (
"net"
"os"
"syscall"
)

// WithChown modifies the socket file's uid and gid
func WithChown(uid, gid int) SockOption {
return func(path string) error {
if err := os.Chown(path, uid, gid); err != nil {
return err
}
return nil
}
}

// WithChmod modifies socket file's access mode.
func WithChmod(mask os.FileMode) SockOption {
return func(path string) error {
if err := os.Chmod(path, mask); err != nil {
return err
}
return nil
}
}

// NewUnixSocket creates a unix socket with the specified path and group.
func NewUnixSocket(path string, gid int) (net.Listener, error) {
return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
}

func listenUnix(path string) (net.Listener, error) {
// net.Listen does not allow for permissions to be set. As a result, when
// specifying custom permissions ("WithChmod()"), there is a short time
Expand Down
36 changes: 31 additions & 5 deletions sockets/unix_socket_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
package sockets

import (
"net"
"os"
"syscall"
"testing"
)

func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
func TestUnixSocketWithOpts(t *testing.T) {
socketFile, err := os.CreateTemp("", "test*.sock")
if err != nil {
t.Fatal(err)
}
_ = socketFile.Close()
defer func() { _ = os.Remove(socketFile.Name()) }()

uid, gid := os.Getuid(), os.Getgid()
perms := os.FileMode(0660)
l, err := NewUnixSocketWithOpts(path, WithChown(uid, gid), WithChmod(perms))
l, err := NewUnixSocketWithOpts(socketFile.Name(), WithChown(uid, gid), WithChmod(perms))
if err != nil {
t.Fatal(err)
}
p, err := os.Stat(path)
p, err := os.Stat(socketFile.Name())
if err != nil {
t.Fatal(err)
}
Expand All @@ -28,5 +34,25 @@ func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
t.Fatalf("unexpected file ownership: expected: %d:%d, got: %d:%d", uid, gid, stat.Uid, stat.Gid)
}
}
return l

defer func() { _ = l.Close() }()

echoStr := "hello"
runTest(t, socketFile.Name(), l, echoStr)
}

// TestNewUnixSocket run under root user.
func TestNewUnixSocket(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("requires root")
}
gid := os.Getgid()
path := "/tmp/test.sock"
echoStr := "hello"
l, err := NewUnixSocket(path, gid)
if err != nil {
t.Fatal(err)
}
defer func() { _ = l.Close() }()
runTest(t, path, l, echoStr)
}
18 changes: 14 additions & 4 deletions sockets/unix_socket_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package sockets

import (
"net"
"os"
"testing"
)

func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
l, err := NewUnixSocketWithOpts(path)
func TestUnixSocketWithOpts(t *testing.T) {
socketFile, err := os.CreateTemp("", "test*.sock")
if err != nil {
t.Fatal(err)
}
return l
_ = socketFile.Close()
defer func() { _ = os.Remove(socketFile.Name()) }()

l, err := NewUnixSocketWithOpts(socketFile.Name())
if err != nil {
t.Fatal(err)
}
defer func() { _ = l.Close() }()

echoStr := "hello"
runTest(t, socketFile.Name(), l, echoStr)
}
Loading