Skip to content

Commit e5004d8

Browse files
authored
lightning: fix lightning TLS.WithHost to access the correct host (pingcap#45749) (pingcap#47734)
close pingcap#45747
1 parent 7a83625 commit e5004d8

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

br/pkg/lightning/common/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ go_test(
102102
],
103103
embed = [":common"],
104104
flaky = True,
105-
shard_count = 19,
105+
shard_count = 20,
106106
deps = [
107107
"//br/pkg/errors",
108108
"//br/pkg/lightning/log",

br/pkg/lightning/common/security.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net"
2121
"net/http"
2222
"net/http/httptest"
23+
"strings"
2324

2425
"github.com/pingcap/errors"
2526
"github.com/pingcap/tidb/br/pkg/httputil"
@@ -88,8 +89,15 @@ func NewTLSFromMockServer(server *httptest.Server) *TLS {
8889
}
8990
}
9091

92+
// GetMockTLSUrl returns tls's host for mock test
93+
func GetMockTLSUrl(tls *TLS) string {
94+
return tls.url
95+
}
96+
9197
// WithHost creates a new TLS instance with the host replaced.
9298
func (tc *TLS) WithHost(host string) *TLS {
99+
host = strings.TrimPrefix(host, "http://")
100+
host = strings.TrimPrefix(host, "https://")
93101
var url string
94102
if tc.inner != nil {
95103
url = "https://" + host

br/pkg/lightning/common/security_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,49 @@ func TestGetJSONSecure(t *testing.T) {
7070
require.Equal(t, "/dddd", result.Path)
7171
}
7272

73+
func TestWithHost(t *testing.T) {
74+
mockTLSServer := httptest.NewTLSServer(http.HandlerFunc(respondPathHandler))
75+
defer mockTLSServer.Close()
76+
mockServer := httptest.NewServer(http.HandlerFunc(respondPathHandler))
77+
defer mockServer.Close()
78+
79+
testCases := []struct {
80+
expected string
81+
host string
82+
secure bool
83+
}{
84+
{
85+
"https://127.0.0.1:2379",
86+
"http://127.0.0.1:2379",
87+
true,
88+
},
89+
{
90+
"http://127.0.0.1:2379",
91+
"https://127.0.0.1:2379",
92+
false,
93+
},
94+
{
95+
"http://127.0.0.1:2379/pd/api/v1/stores",
96+
"127.0.0.1:2379/pd/api/v1/stores",
97+
false,
98+
},
99+
{
100+
"https://127.0.0.1:2379",
101+
"127.0.0.1:2379",
102+
true,
103+
},
104+
}
105+
106+
for _, testCase := range testCases {
107+
server := mockServer
108+
if testCase.secure {
109+
server = mockTLSServer
110+
}
111+
tls := common.NewTLSFromMockServer(server)
112+
require.Equal(t, testCase.expected, common.GetMockTLSUrl(tls.WithHost(testCase.host)))
113+
}
114+
}
115+
73116
func TestInvalidTLS(t *testing.T) {
74117
tempDir := t.TempDir()
75118
caPath := filepath.Join(tempDir, "ca.pem")

br/tests/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
set -eu
1818
export PATH="tests/_utils:bin:$PATH"
1919
export TEST_DIR=/tmp/backup_restore_test
20+
export COV_DIR="/tmp/group_cover"
2021

2122
# Create COV_DIR if not exists
2223
if [ -d "$COV_DIR" ]; then

0 commit comments

Comments
 (0)