Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getSysConn to work with TLS connections #918

Merged
merged 2 commits into from
May 2, 2024
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
14 changes: 13 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package tchannel

import (
"crypto/tls"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -971,7 +972,18 @@ func (c *Connection) getLastActivityWriteTime() time.Time {
}

func getSysConn(conn net.Conn, log Logger) syscall.RawConn {
connSyscall, ok := conn.(syscall.Conn)
var (
connSyscall syscall.Conn
ok bool
)
switch v := conn.(type) {
case syscall.Conn:
connSyscall = v
ok = true
case *tls.Conn:
connSyscall, ok = v.NetConn().(syscall.Conn)
}

if !ok {
log.WithFields(LogField{"connectionType", fmt.Sprintf("%T", conn)}).
Error("Connection does not implement SyscallConn.")
Expand Down
43 changes: 43 additions & 0 deletions connection_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package tchannel

import (
"bytes"
"crypto/tls"
"net"
"net/http/httptest"
"syscall"
"testing"

Expand Down Expand Up @@ -79,4 +81,45 @@ func TestGetSysConn(t *testing.T) {
require.NotNil(t, sysConn)
assert.Empty(t, loggerBuf.String(), "expected no logs on success")
})

t.Run("SyscallConn is successful with TLS", func(t *testing.T) {
var (
loggerBuf = &bytes.Buffer{}
logger = NewLogger(loggerBuf)
server = httptest.NewTLSServer(nil)
)
defer server.Close()

conn, err := tls.Dial("tcp", server.Listener.Addr().String(), &tls.Config{InsecureSkipVerify: true})
require.NoError(t, err, "failed to dial")
defer conn.Close()

sysConn := getSysConn(conn, logger)
require.NotNil(t, sysConn)
assert.Empty(t, loggerBuf.String(), "expected no logs on success")
})

t.Run("no SyscallConn - nil net.Conn", func(t *testing.T) {
var (
loggerBuf = &bytes.Buffer{}
logger = NewLogger(loggerBuf)
syscallConn = getSysConn(nil /* conn */, logger)
)

require.Nil(t, syscallConn, "expected no syscall.RawConn to be returned")
assert.Contains(t, loggerBuf.String(), "Connection does not implement SyscallConn", "missing log")
assert.Contains(t, loggerBuf.String(), "{connectionType <nil>}", "missing type in log")
})

t.Run("no SyscallConn - TLS with no net.Conn", func(t *testing.T) {
var (
loggerBuf = &bytes.Buffer{}
logger = NewLogger(loggerBuf)
syscallConn = getSysConn(&tls.Conn{}, logger)
)

require.Nil(t, syscallConn, "expected no syscall.RawConn to be returned")
assert.Contains(t, loggerBuf.String(), "Connection does not implement SyscallConn", "missing log")
assert.Contains(t, loggerBuf.String(), "{connectionType *tls.Conn}", "missing type in log")
})
}
Loading