Skip to content

Commit

Permalink
Enable inialisation of tls client for dash comms (#1850)
Browse files Browse the repository at this point in the history
* enable inialisation of tls client for dash comms

* remove unneccesary code in client initialisation
  • Loading branch information
joshblakeley authored and buger committed Aug 9, 2018
1 parent afad576 commit a30cc5e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 1 addition & 3 deletions api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@ func (a APIDefinitionLoader) FromDashboardService(endpoint, secret string) ([]*A

newRequest.Header.Set("x-tyk-nonce", ServiceNonce)

c := &http.Client{
Timeout: 120 * time.Second,
}
c := initialiseClient(120 * time.Second)
resp, err := c.Do(newRequest)
if err != nil {
return nil, err
Expand Down
23 changes: 19 additions & 4 deletions dashboard_register.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -35,6 +36,21 @@ type HTTPDashboardHandler struct {
heartBeatStopSentinel bool
}

func initialiseClient(timeout time.Duration) *http.Client {
client := &http.Client{}
if config.Global().HttpServerOptions.UseSSL {
// Setup HTTPS client
tlsConfig := &tls.Config{
InsecureSkipVerify: config.Global().HttpServerOptions.SSLInsecureSkipVerify,
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
client = &http.Client{Transport: transport, Timeout: timeout}
} else {
client = &http.Client{Timeout: timeout}
}
return client
}

func reLogin() {
if !config.Global().UseDBAppConfigs {
return
Expand Down Expand Up @@ -80,8 +96,7 @@ func (h *HTTPDashboardHandler) Init() error {

func (h *HTTPDashboardHandler) Register() error {
req := h.newRequest(h.RegistrationEndpoint)

c := &http.Client{Timeout: 5 * time.Second}
c := initialiseClient(5 * time.Second)
resp, err := c.Do(req)

if err != nil {
Expand Down Expand Up @@ -152,8 +167,8 @@ func (h *HTTPDashboardHandler) sendHeartBeat() error {
req := h.newRequest(h.HeartBeatEndpoint)
req.Header.Set("x-tyk-nodeid", NodeID)
req.Header.Set("x-tyk-nonce", ServiceNonce)
c := initialiseClient(5 * time.Second)

c := &http.Client{Timeout: 5 * time.Second}
resp, err := c.Do(req)
if err != nil || resp.StatusCode != 200 {
return errors.New("dashboard is down? Heartbeat is failing")
Expand All @@ -178,7 +193,7 @@ func (h *HTTPDashboardHandler) DeRegister() error {
req.Header.Set("x-tyk-nodeid", NodeID)
req.Header.Set("x-tyk-nonce", ServiceNonce)

c := &http.Client{Timeout: 5 * time.Second}
c := initialiseClient(5 * time.Second)
resp, err := c.Do(req)

if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func LoadPoliciesFromDashboard(endpoint, secret string, allowExplicit bool) map[
log.WithFields(logrus.Fields{
"prefix": "policy",
}).Info("Mutex lock acquired... calling")
c := &http.Client{
Timeout: 10 * time.Second,
}
c := initialiseClient(10 * time.Second)

log.WithFields(logrus.Fields{
"prefix": "policy",
Expand Down

0 comments on commit a30cc5e

Please sign in to comment.