Skip to content

Commit

Permalink
api: fix TLS when redirecting the request (#1740)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored and sre-bot committed Sep 17, 2019
1 parent e120222 commit cfa5706
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tags
vendor
default*
*.bak
.vscode/
19 changes: 18 additions & 1 deletion server/api/redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package api

import (
"crypto/tls"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"

"github.com/pingcap/log"
"github.com/pingcap/pd/server"
Expand All @@ -34,6 +36,8 @@ const (
errRedirectToNotLeader = "redirect to not leader"
)

var initHTTPClientOnce sync.Once

type redirector struct {
s *server.Server
}
Expand Down Expand Up @@ -68,7 +72,20 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

initHTTPClientOnce.Do(func() {
var tlsConfig *tls.Config
tlsConfig, err = h.s.GetSecurityConfig().ToTLSConfig()
dialClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newCustomReverseProxies(urls).ServeHTTP(w, r)
}

Expand Down
1 change: 1 addition & 0 deletions tests/pdctl/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *clusterTestSuite) TestClusterAndPing(c *C) {
temp.Close()
os.Stdout = old
out, _ := ioutil.ReadFile(fname)
os.Remove(fname)
c.Assert(strings.Contains(string(out), "no such file or directory"), IsTrue)

// cluster status
Expand Down
1 change: 1 addition & 0 deletions tests/pdctl/region/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (s *regionTestSuite) TestRegionKeyFormat(c *C) {
temp.Close()
os.Stdout = old
out, _ := ioutil.ReadFile(fname)
os.Remove(fname)
c.Assert(strings.Contains(string(out), "unknown flag"), IsFalse)
}

Expand Down

0 comments on commit cfa5706

Please sign in to comment.