forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ccl/sqlproxyccl: fix panic on nil conn when failing to dial SQL backend
In cockroachdb#80446, we updated the code to ensure that the connection gets cleaned up whenever BackendDial fails. With the code as-is, there is a possibility where conn becomes nil whenever sslOverlay returned an error. This would result in a panic whenever the defer callback gets executed to close the connection object. This commit folds all the functions (i.e. sslOverlay and relayStartupMsg) into BackendDial to avoid this panic issue altogether. That way, there is no possibility where the connection object can be nil. Release note: None
- Loading branch information
1 parent
6f1582d
commit 910a90f
Showing
3 changed files
with
74 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package sqlproxyccl | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
pgproto3 "github.com/jackc/pgproto3/v2" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBackendDialTLS(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
startupMsg := &pgproto3.StartupMessage{ProtocolVersion: pgproto3.ProtocolVersionNumber} | ||
tlsConfig := &tls.Config{InsecureSkipVerify: true} | ||
|
||
t.Run("insecure server", func(t *testing.T) { | ||
sql, _, _ := serverutils.StartServer(t, base.TestServerArgs{Insecure: true}) | ||
defer sql.Stopper().Stop(ctx) | ||
|
||
conn, err := BackendDial(startupMsg, sql.ServingSQLAddr(), tlsConfig) | ||
require.Error(t, err) | ||
require.Regexp(t, "target server refused TLS connection", err) | ||
require.Nil(t, conn) | ||
}) | ||
|
||
t.Run("secure server", func(t *testing.T) { | ||
sql, _, _ := serverutils.StartServer(t, base.TestServerArgs{Insecure: false}) | ||
defer sql.Stopper().Stop(ctx) | ||
|
||
conn, err := BackendDial(startupMsg, sql.ServingSQLAddr(), tlsConfig) | ||
require.NoError(t, err) | ||
require.NotNil(t, conn) | ||
}) | ||
} |