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

*: use https protocol between tidb-pd tidb-tidb when cluster-ssl configured (#15342) (#15419) #15430

Merged
merged 2 commits into from
Mar 18, 2020
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
14 changes: 8 additions & 6 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/pdapi"
Expand Down Expand Up @@ -1012,8 +1013,8 @@ func (h tableHandler) addScatterSchedule(startKey, endKey []byte, name string) e
if err != nil {
return err
}
scheduleURL := fmt.Sprintf("http://%s/pd/api/v1/schedulers", pdAddrs[0])
resp, err := http.Post(scheduleURL, "application/json", bytes.NewBuffer(v))
scheduleURL := fmt.Sprintf("%s://%s/pd/api/v1/schedulers", util.InternalHTTPSchema(), pdAddrs[0])
resp, err := util.InternalHTTPClient().Post(scheduleURL, "application/json", bytes.NewBuffer(v))
if err != nil {
return err
}
Expand All @@ -1028,12 +1029,12 @@ func (h tableHandler) deleteScatterSchedule(name string) error {
if err != nil {
return err
}
scheduleURL := fmt.Sprintf("http://%s/pd/api/v1/schedulers/scatter-range-%s", pdAddrs[0], name)
scheduleURL := fmt.Sprintf("%s://%s/pd/api/v1/schedulers/scatter-range-%s", util.InternalHTTPSchema(), pdAddrs[0], name)
req, err := http.NewRequest(http.MethodDelete, scheduleURL, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -1201,12 +1202,13 @@ func (h tableHandler) handleDiskUsageRequest(schema infoschema.InfoSchema, tbl t
startKey = codec.EncodeBytes([]byte{}, startKey)
endKey = codec.EncodeBytes([]byte{}, endKey)

statURL := fmt.Sprintf("http://%s/pd/api/v1/stats/region?start_key=%s&end_key=%s",
statURL := fmt.Sprintf("%s://%s/pd/api/v1/stats/region?start_key=%s&end_key=%s",
util.InternalHTTPSchema(),
pdAddrs[0],
url.QueryEscape(string(startKey)),
url.QueryEscape(string(endKey)))

resp, err := http.Get(statURL)
resp, err := util.InternalHTTPClient().Get(statURL)
if err != nil {
writeError(w, err)
return
Expand Down
3 changes: 2 additions & 1 deletion server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/printer"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (s *Server) startHTTPServer() {
host = "localhost"
}
baseURL := &url.URL{
Scheme: "http",
Scheme: util.InternalHTTPSchema(),
Host: fmt.Sprintf("%s:%s", host, port),
}
router.HandleFunc("/web/trace", traceapp.HandleTiDB).Name("Trace Viewer")
Expand Down
19 changes: 8 additions & 11 deletions store/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ import (
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/pdapi"
"go.uber.org/zap"
)

const (
protocol = "http://"
)

// Helper is a middleware to get some information from tikv/pd. It can be used for TiDB's http api or mem table.
type Helper struct {
Store tikv.Storage
Expand Down Expand Up @@ -119,11 +116,11 @@ func (h *Helper) FetchHotRegion(rw string) (map[uint64]RegionMetric, error) {
if len(pdHosts) == 0 {
return nil, errors.New("pd unavailable")
}
req, err := http.NewRequest("GET", protocol+pdHosts[0]+rw, nil)
req, err := http.NewRequest("GET", util.InternalHTTPSchema()+"://"+pdHosts[0]+rw, nil)
if err != nil {
return nil, errors.Trace(err)
}
resp, err := http.DefaultClient.Do(req)
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -431,12 +428,12 @@ func (h *Helper) requestPD(method, uri string, body io.Reader, res interface{})
return errors.New("pd unavailable")
}

logutil.Logger(context.Background()).Debug("RequestPD URL", zap.String("url", protocol+pdHosts[0]+uri))
req, err := http.NewRequest(method, protocol+pdHosts[0]+uri, body)
logutil.Logger(context.Background()).Debug("RequestPD URL", zap.String("url", util.InternalHTTPSchema()+"://"+pdHosts[0]+uri))
req, err := http.NewRequest(method, util.InternalHTTPSchema()+"://"+pdHosts[0]+uri, body)
if err != nil {
return errors.Trace(err)
}
resp, err := http.DefaultClient.Do(req)
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -511,11 +508,11 @@ func (h *Helper) GetStoresStat() (*StoresStat, error) {
if len(pdHosts) == 0 {
return nil, errors.New("pd unavailable")
}
req, err := http.NewRequest("GET", protocol+pdHosts[0]+pdapi.Stores, nil)
req, err := http.NewRequest("GET", util.InternalHTTPSchema()+"://"+pdHosts[0]+pdapi.Stores, nil)
if err != nil {
return nil, errors.Trace(err)
}
resp, err := http.DefaultClient.Do(req)
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
3 changes: 3 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/gcworker"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/printer"
Expand Down Expand Up @@ -543,6 +544,8 @@ func setupLog() {

err = logutil.InitLogger(cfg.Log.ToLogConfig())
terror.MustNil(err)
// trigger internal http(s) client init.
util.InternalHTTPClient()
}

func printInfo() {
Expand Down
36 changes: 36 additions & 0 deletions util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"crypto/x509/pkix"
"fmt"
"io/ioutil"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -359,3 +361,37 @@ func IsTLSExpiredError(err error) bool {
}
return true
}

var (
internalClientInit sync.Once
internalHTTPClient *http.Client
internalHTTPSchema string
)

// InternalHTTPClient is used by TiDB-Server to request other components.
func InternalHTTPClient() *http.Client {
internalClientInit.Do(initInternalClient)
return internalHTTPClient
}

// InternalHTTPSchema specifies use http or https to request other components.
func InternalHTTPSchema() string {
internalClientInit.Do(initInternalClient)
return internalHTTPSchema
}

func initInternalClient() {
tlsCfg, err := config.GetGlobalConfig().Security.ToTLSConfig()
if err != nil {
logutil.Logger(context.Background()).Fatal("could not load cluster ssl", zap.Error(err))
}
if tlsCfg == nil {
internalHTTPSchema = "http"
internalHTTPClient = http.DefaultClient
return
}
internalHTTPSchema = "https"
internalHTTPClient = &http.Client{
Transport: &http.Transport{TLSClientConfig: tlsCfg},
}
}