Skip to content

Commit 5ef2c29

Browse files
committed
fix: Fix peer credentials call on Darwin
1 parent 53ba9fe commit 5ef2c29

File tree

6 files changed

+111
-38
lines changed

6 files changed

+111
-38
lines changed

server/muxer/grpc.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ func (*UnixPeerCredentials) ServerHandshake(conn net.Conn) (net.Conn, credential
8888
return conn, &ai, nil
8989
}
9090

91-
creds, err := util.ReadPeerCreds(c.Conn)
91+
uid, err := util.ReadPeerCreds(c.Conn)
9292
if err != nil {
9393
return conn, &ai, nil //nolint:nilerr
9494
}
9595

96-
log.Debug().Msgf("grpc server handshake. user id=%v", creds.Uid)
96+
log.Debug().Msgf("grpc server handshake. user id=%v", uid)
9797

98-
ai.LocalRoot = creds.Uid == 0
98+
ai.LocalRoot = uid == 0
9999

100100
if ai.LocalRoot {
101101
log.Debug().Msg("Local root user detected")

server/muxer/http.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func (s *HTTPServer) Start(mux cmux.CMux) error {
108108
nc = c.Conn
109109
}
110110

111-
creds, err := util.ReadPeerCreds(nc)
112-
if err == nil && creds.Uid == 0 {
111+
uid, err := util.ReadPeerCreds(nc)
112+
if err == nil && uid == 0 {
113113
log.Debug().Msgf("local root on http")
114114
return request.SetLocalRoot(ctx)
115115
}

test/v1/server/unix_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ func TestReadPeerCreds(t *testing.T) {
8484
conn, err := l.Accept()
8585
require.NoError(t, err)
8686

87-
creds, err := util.ReadPeerCreds(conn)
87+
uid, err := util.ReadPeerCreds(conn)
8888
require.NoError(t, err)
8989

90-
require.Equal(t, os.Geteuid(), int(creds.Uid))
91-
require.Equal(t, os.Getegid(), int(creds.Gid))
90+
require.Equal(t, os.Geteuid(), int(uid))
9291
}

util/util.go

-30
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"io"
22-
"net"
2322
"os"
2423
"strings"
2524
"text/template"
@@ -29,7 +28,6 @@ import (
2928
"github.com/rs/zerolog/log"
3029
"github.com/tigrisdata/tigris/lib/container"
3130
ulog "github.com/tigrisdata/tigris/util/log"
32-
"golang.org/x/sys/unix"
3331
)
3432

3533
const (
@@ -189,31 +187,3 @@ func RawMessageToByte(arr []jsoniter.RawMessage) [][]byte {
189187
ptr := unsafe.Pointer(&arr)
190188
return *(*[][]byte)(ptr)
191189
}
192-
193-
func ReadPeerCreds(c net.Conn) (*unix.Ucred, error) {
194-
var cred *unix.Ucred
195-
196-
uc, ok := c.(*net.UnixConn)
197-
if !ok {
198-
return nil, ErrNotUnixConn
199-
}
200-
201-
raw, err := uc.SyscallConn()
202-
if err != nil {
203-
return nil, fmt.Errorf("error getting raw connection: %s", err)
204-
}
205-
206-
err1 := raw.Control(func(fd uintptr) {
207-
cred, err = unix.GetsockoptUcred(int(fd), unix.SOL_SOCKET, unix.SO_PEERCRED)
208-
})
209-
210-
if err != nil {
211-
return nil, fmt.Errorf("getsockoptUcred error: %s", err)
212-
}
213-
214-
if err1 != nil {
215-
return nil, fmt.Errorf("control error: %s", err1)
216-
}
217-
218-
return cred, nil
219-
}

util/util_darwin.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2022-2023 Tigris Data, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build darwin
16+
17+
package util
18+
19+
import (
20+
"fmt"
21+
"net"
22+
23+
"golang.org/x/sys/unix"
24+
)
25+
26+
func ReadPeerCreds(c net.Conn) (uint32, error) {
27+
var cred *unix.Xucred
28+
29+
uc, ok := c.(*net.UnixConn)
30+
if !ok {
31+
return 0, ErrNotUnixConn
32+
}
33+
34+
raw, err := uc.SyscallConn()
35+
if err != nil {
36+
return 0, fmt.Errorf("error getting raw connection: %s", err)
37+
}
38+
39+
err1 := raw.Control(func(fd uintptr) {
40+
cred, err = unix.GetsockoptXucred(int(fd), unix.SOL_LOCAL, unix.LOCAL_PEERCRED)
41+
})
42+
43+
if err != nil {
44+
return 0, fmt.Errorf("getsockoptxucred error: %s", err)
45+
}
46+
47+
if err1 != nil {
48+
return 0, fmt.Errorf("control error: %s", err1)
49+
}
50+
51+
return cred.Uid, nil
52+
}

util/util_linux.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2022-2023 Tigris Data, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build linux
16+
17+
package util
18+
19+
import (
20+
"fmt"
21+
"net"
22+
23+
"golang.org/x/sys/unix"
24+
)
25+
26+
func ReadPeerCreds(c net.Conn) (uint32, error) {
27+
var cred *unix.Ucred
28+
29+
uc, ok := c.(*net.UnixConn)
30+
if !ok {
31+
return 0, ErrNotUnixConn
32+
}
33+
34+
raw, err := uc.SyscallConn()
35+
if err != nil {
36+
return 0, fmt.Errorf("error getting raw connection: %s", err)
37+
}
38+
39+
err1 := raw.Control(func(fd uintptr) {
40+
cred, err = unix.GetsockoptUcred(int(fd), unix.SOL_SOCKET, unix.SO_PEERCRED)
41+
})
42+
43+
if err != nil {
44+
return 0, fmt.Errorf("getsockoptUcred error: %s", err)
45+
}
46+
47+
if err1 != nil {
48+
return 0, fmt.Errorf("control error: %s", err1)
49+
}
50+
51+
return cred.Uid, nil
52+
}

0 commit comments

Comments
 (0)