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

lightning: fix lightning TLS.WithHost to access the correct host (#45749) #47734

Merged
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
2 changes: 1 addition & 1 deletion br/pkg/lightning/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ go_test(
],
embed = [":common"],
flaky = True,
shard_count = 19,
shard_count = 20,
deps = [
"//br/pkg/errors",
"//br/pkg/lightning/log",
Expand Down
8 changes: 8 additions & 0 deletions br/pkg/lightning/common/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/httputil"
Expand Down Expand Up @@ -88,8 +89,15 @@ func NewTLSFromMockServer(server *httptest.Server) *TLS {
}
}

// GetMockTLSUrl returns tls's host for mock test
func GetMockTLSUrl(tls *TLS) string {
return tls.url
}

// WithHost creates a new TLS instance with the host replaced.
func (tc *TLS) WithHost(host string) *TLS {
host = strings.TrimPrefix(host, "http://")
host = strings.TrimPrefix(host, "https://")
var url string
if tc.inner != nil {
url = "https://" + host
Expand Down
43 changes: 43 additions & 0 deletions br/pkg/lightning/common/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,49 @@ func TestGetJSONSecure(t *testing.T) {
require.Equal(t, "/dddd", result.Path)
}

func TestWithHost(t *testing.T) {
mockTLSServer := httptest.NewTLSServer(http.HandlerFunc(respondPathHandler))
defer mockTLSServer.Close()
mockServer := httptest.NewServer(http.HandlerFunc(respondPathHandler))
defer mockServer.Close()

testCases := []struct {
expected string
host string
secure bool
}{
{
"https://127.0.0.1:2379",
"http://127.0.0.1:2379",
true,
},
{
"http://127.0.0.1:2379",
"https://127.0.0.1:2379",
false,
},
{
"http://127.0.0.1:2379/pd/api/v1/stores",
"127.0.0.1:2379/pd/api/v1/stores",
false,
},
{
"https://127.0.0.1:2379",
"127.0.0.1:2379",
true,
},
}

for _, testCase := range testCases {
server := mockServer
if testCase.secure {
server = mockTLSServer
}
tls := common.NewTLSFromMockServer(server)
require.Equal(t, testCase.expected, common.GetMockTLSUrl(tls.WithHost(testCase.host)))
}
}

func TestInvalidTLS(t *testing.T) {
tempDir := t.TempDir()
caPath := filepath.Join(tempDir, "ca.pem")
Expand Down
1 change: 1 addition & 0 deletions br/tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
set -eu
export PATH="tests/_utils:bin:$PATH"
export TEST_DIR=/tmp/backup_restore_test
export COV_DIR="/tmp/group_cover"

# Create COV_DIR if not exists
if [ -d "$COV_DIR" ]; then
Expand Down