Skip to content

Commit

Permalink
api: fix TLS when redirecting the request (tikv#1740)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Sep 27, 2019
1 parent dc03c83 commit b23ef35
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ tags
/.retools/
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"

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

var initHTTPClientOnce sync.Once

type redirector struct {
s *server.Server
}
Expand Down Expand Up @@ -67,7 +71,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 @@ -69,6 +69,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 b23ef35

Please sign in to comment.