Skip to content

Commit

Permalink
Don't require TLS for in-process connection (#4323)
Browse files Browse the repository at this point in the history
This should fix a bug where in-process connections expect TLS over the
`net.Pipe` if TLS is configured.

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison committed Jul 20, 2023
2 parents 80fb29f + b7d6b7e commit 0347f27
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
22 changes: 19 additions & 3 deletions server/server.go
Expand Up @@ -2217,7 +2217,7 @@ func (s *Server) AcceptLoop(clr chan struct{}) {
func (s *Server) InProcessConn() (net.Conn, error) {
pl, pr := net.Pipe()
if !s.startGoRoutine(func() {
s.createClient(pl)
s.createClientInProcess(pl)
s.grWG.Done()
}) {
pl.Close()
Expand Down Expand Up @@ -2572,6 +2572,14 @@ func (c *tlsMixConn) Read(b []byte) (int, error) {
}

func (s *Server) createClient(conn net.Conn) *client {
return s.createClientEx(conn, false)
}

func (s *Server) createClientInProcess(conn net.Conn) *client {
return s.createClientEx(conn, true)
}

func (s *Server) createClientEx(conn net.Conn, inProcess bool) *client {
// Snapshot server options.
opts := s.getOpts()

Expand Down Expand Up @@ -2609,6 +2617,13 @@ func (s *Server) createClient(conn net.Conn) *client {
info.AuthRequired = false
}

// Check to see if this is an in-process connection with tls_required.
// If so, set as not required, but available.
if inProcess && info.TLSRequired {
info.TLSRequired = false
info.TLSAvailable = true
}

s.totalClients++
s.mu.Unlock()

Expand Down Expand Up @@ -2670,8 +2685,9 @@ func (s *Server) createClient(conn net.Conn) *client {

var pre []byte
// If we have both TLS and non-TLS allowed we need to see which
// one the client wants.
if !isClosed && opts.TLSConfig != nil && opts.AllowNonTLS {
// one the client wants. We'll always allow this for in-process
// connections.
if !isClosed && opts.TLSConfig != nil && (inProcess || opts.AllowNonTLS) {
pre = make([]byte, 4)
c.nc.SetReadDeadline(time.Now().Add(secondsToDuration(opts.TLSTimeout)))
n, _ := io.ReadFull(c.nc, pre[:])
Expand Down
20 changes: 20 additions & 0 deletions test/tls_test.go
Expand Up @@ -72,6 +72,26 @@ func TestTLSConnection(t *testing.T) {
}
}

// TestTLSInProcessConnection checks that even if TLS is enabled on the server,
// that an in-process connection that does *not* use TLS still connects successfully.
func TestTLSInProcessConnection(t *testing.T) {
srv, opts := RunServerWithConfig("./configs/tls.conf")
defer srv.Shutdown()

nc, err := nats.Connect("", nats.InProcessServer(srv), nats.UserInfo(opts.Username, opts.Password))
if err != nil {
t.Fatal(err)
}

if nc.TLSRequired() {
t.Fatalf("Shouldn't have required TLS for in-process connection")
}

if _, err = nc.TLSConnectionState(); err == nil {
t.Fatal("Should have got an error retrieving TLS connection state")
}
}

func TestTLSClientCertificate(t *testing.T) {
srv, opts := RunServerWithConfig("./configs/tlsverify.conf")
defer srv.Shutdown()
Expand Down

0 comments on commit 0347f27

Please sign in to comment.