Skip to content

Commit

Permalink
Merge 7fa73a3 into c0dab5d
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu committed Feb 12, 2019
2 parents c0dab5d + 7fa73a3 commit 8341e21
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cert_go1.10_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestProxyTransport(t *testing.T) {
spec.Proxy.Transport.ProxyURL = proxy.URL
})

client := getTLSClient(nil, nil)
client := getTLSClient(nil, nil, false)
client.Transport = &http.Transport{
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
Expand Down
79 changes: 64 additions & 15 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
"testing"
"time"

"golang.org/x/net/http2"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/certs"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/test"
"github.com/TykTechnologies/tyk/user"
)

func getTLSClient(cert *tls.Certificate, caCert []byte) *http.Client {
func getTLSClient(cert *tls.Certificate, caCert []byte, isHttp2 bool) *http.Client {
// Setup HTTPS client
tlsConfig := &tls.Config{}

Expand All @@ -42,7 +44,12 @@ func getTLSClient(cert *tls.Certificate, caCert []byte) *http.Client {
tlsConfig.InsecureSkipVerify = true
}

transport := &http.Transport{TLSClientConfig: tlsConfig}
var transport http.RoundTripper
if isHttp2 {
transport = &http2.Transport{TLSClientConfig: tlsConfig}
} else {
transport = &http.Transport{TLSClientConfig: tlsConfig}
}

return &http.Client{Transport: transport}
}
Expand Down Expand Up @@ -91,7 +98,7 @@ func TestGatewayTLS(t *testing.T) {
dir, _ := ioutil.TempDir("", "certs")
defer os.RemoveAll(dir)

client := getTLSClient(nil, nil)
client := getTLSClient(nil, nil, false)

t.Run("Without certificates", func(t *testing.T) {
globalConf := config.Global()
Expand Down Expand Up @@ -204,9 +211,9 @@ func TestGatewayControlAPIMutualTLS(t *testing.T) {
}()

clientCertPem, _, _, clientCert := genCertificate(&x509.Certificate{})
clientWithCert := getTLSClient(&clientCert, serverCertPem)
clientWithCert := getTLSClient(&clientCert, serverCertPem, false)

clientWithoutCert := getTLSClient(nil, nil)
clientWithoutCert := getTLSClient(nil, nil, false)

t.Run("Separate domain", func(t *testing.T) {
certID, _ := CertificateManager.Add(combinedPEM, "")
Expand Down Expand Up @@ -276,7 +283,7 @@ func TestAPIMutualTLS(t *testing.T) {

t.Run("SNI and domain per API", func(t *testing.T) {
t.Run("API without mutual TLS", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
client := getTLSClient(&clientCert, serverCertPem, false)

buildAndLoadAPI(func(spec *APISpec) {
spec.Domain = "localhost"
Expand All @@ -287,7 +294,7 @@ func TestAPIMutualTLS(t *testing.T) {
})

t.Run("MutualTLSCertificate not set", func(t *testing.T) {
client := getTLSClient(nil, nil)
client := getTLSClient(nil, nil, false)

buildAndLoadAPI(func(spec *APISpec) {
spec.Domain = "localhost"
Expand All @@ -303,7 +310,7 @@ func TestAPIMutualTLS(t *testing.T) {
})

t.Run("Client certificate match", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
client := getTLSClient(&clientCert, serverCertPem, false)
clientCertID, _ := CertificateManager.Add(clientCertPem, "")

buildAndLoadAPI(func(spec *APISpec) {
Expand All @@ -320,14 +327,14 @@ func TestAPIMutualTLS(t *testing.T) {
CertificateManager.Delete(clientCertID)
CertificateManager.FlushCache()

client = getTLSClient(&clientCert, serverCertPem)
client = getTLSClient(&clientCert, serverCertPem, false)
ts.Run(t, test.TestCase{
Client: client, Domain: "localhost", ErrorMatch: badcertErr,
})
})

t.Run("Client certificate differ", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
client := getTLSClient(&clientCert, serverCertPem, false)

clientCertPem2, _, _, _ := genCertificate(&x509.Certificate{})
clientCertID2, _ := CertificateManager.Add(clientCertPem2, "")
Expand Down Expand Up @@ -364,7 +371,7 @@ func TestAPIMutualTLS(t *testing.T) {
}

t.Run("Without certificate", func(t *testing.T) {
clientWithoutCert := getTLSClient(nil, nil)
clientWithoutCert := getTLSClient(nil, nil, false)

loadAPIS()

Expand All @@ -385,7 +392,7 @@ func TestAPIMutualTLS(t *testing.T) {
})

t.Run("Client certificate not match", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
client := getTLSClient(&clientCert, serverCertPem, false)

loadAPIS()

Expand All @@ -401,7 +408,7 @@ func TestAPIMutualTLS(t *testing.T) {

t.Run("Client certificate match", func(t *testing.T) {
loadAPIS(clientCertID)
client := getTLSClient(&clientCert, serverCertPem)
client := getTLSClient(&clientCert, serverCertPem, false)

ts.Run(t, test.TestCase{
Path: "/with_mutual",
Expand Down Expand Up @@ -431,7 +438,7 @@ func TestUpstreamMutualTLS(t *testing.T) {
defer upstream.Close()

t.Run("Without API", func(t *testing.T) {
client := getTLSClient(&clientCert, nil)
client := getTLSClient(&clientCert, nil, false)

if _, err := client.Get(upstream.URL); err == nil {
t.Error("Should reject without certificate")
Expand Down Expand Up @@ -495,7 +502,7 @@ func TestKeyWithCertificateTLS(t *testing.T) {
spec.Proxy.ListenPath = "/"
})

client := getTLSClient(&clientCert, nil)
client := getTLSClient(&clientCert, nil, false)

t.Run("Cert unknown", func(t *testing.T) {
ts.Run(t, test.TestCase{Code: 403, Client: client})
Expand Down Expand Up @@ -648,3 +655,45 @@ func TestCipherSuites(t *testing.T) {
ts.Run(t, test.TestCase{Client: client, Path: "/", ErrorMatch: "tls: handshake failure"})
})
}

func TestHTTP2(t *testing.T) {

// Certificates
serverCertPem, serverPrivPem, _, _ := genServerCertificate()
_, _, _, clientCert := genCertificate(&x509.Certificate{})

dir, _ := ioutil.TempDir("", "certs")
defer os.RemoveAll(dir)
certFilePath := filepath.Join(dir, "server.crt")
ioutil.WriteFile(certFilePath, serverCertPem, 0666)

certKeyPath := filepath.Join(dir, "server.key")
ioutil.WriteFile(certKeyPath, serverPrivPem, 0666)

// Configuration
globalConf := config.Global()
globalConf.HttpServerOptions.EnableHttp2 = true
globalConf.HttpServerOptions.Certificates = []config.CertData{{
Name: "localhost",
CertFile: certFilePath,
KeyFile: certKeyPath,
}}
globalConf.HttpServerOptions.UseSSL = true
config.SetGlobal(globalConf)
defer resetTestConfig()

ts := newTykTestServer()
defer ts.Close()

buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.UseKeylessAccess = true
spec.Proxy.TargetURL = "https://http2.golang.org" // HTTP/2 Upstream
})

// Client
http2Client := getTLSClient(&clientCert, serverCertPem, true)

ts.Run(t, test.TestCase{Client: http2Client, Path: "", Code: 200, BodyMatch: "<h1>Go + HTTP/2</h1>"})

}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type HttpServerOptionsConfig struct {
WriteTimeout int `json:"write_timeout"`
UseSSL bool `json:"use_ssl"`
UseLE_SSL bool `json:"use_ssl_le"`
EnableHttp2 bool `json:"enable_http2"`
SSLInsecureSkipVerify bool `json:"ssl_insecure_skip_verify"`
EnableWebSockets bool `json:"enable_websockets"`
Certificates []CertData `json:"certificates"`
Expand Down
2 changes: 1 addition & 1 deletion dnscache/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"

"github.com/Sirupsen/logrus"
"github.com/pmylund/go-cache"
cache "github.com/pmylund/go-cache"
)

// DnsCacheItem represents single record in cache
Expand Down
3 changes: 1 addition & 2 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
"strings"
"time"

"github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/request"
cache "github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/user"
Expand Down
4 changes: 2 additions & 2 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"testing"
"time"

"github.com/dgrijalva/jwt-go"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
Expand Down
2 changes: 1 addition & 1 deletion host_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/jeffail/tunny"
"github.com/pmylund/go-cache"
cache "github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/config"
)
Expand Down
1 change: 1 addition & 0 deletions install/data/tyk.self_contained.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
}
},
"http_server_options": {
"enable_http2": true,
"enable_websockets": true
},
"hostname": "",
Expand Down
1 change: 1 addition & 0 deletions install/data/tyk.with_dash.conf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"disable_cached_session_state": false
},
"http_server_options": {
"enable_http2": true,
"enable_websockets": true
},
"uptime_tests": {
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (
"sync"
"time"

"golang.org/x/net/http2"

newrelic "github.com/newrelic/go-agent"

"github.com/TykTechnologies/tyk/checkup"

"github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/hooks/syslog"
logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
logstashHook "github.com/bshuster-repo/logrus-logstash-hook"
"github.com/evalphobia/logrus_sentry"
"github.com/facebookgo/pidfile"
Expand Down Expand Up @@ -1145,6 +1147,10 @@ func generateListener(listenPort int) (net.Listener, error) {
CipherSuites: getCipherAliases(httpServerOptions.Ciphers),
}

if httpServerOptions.EnableHttp2 {
tlsConfig.NextProtos = append(tlsConfig.NextProtos, http2.NextProtoTLS)
}

tlsConfig.GetConfigForClient = getTLSConfigForClient(&tlsConfig, listenPort)

return tls.Listen("tcp", targetPort, &tlsConfig)
Expand Down
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/Sirupsen/logrus"
"github.com/gocraft/health"
"github.com/justinas/alice"
"github.com/newrelic/go-agent"
newrelic "github.com/newrelic/go-agent"
"github.com/paulbellamy/ratecounter"
"github.com/pmylund/go-cache"
cache "github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
Expand Down
2 changes: 1 addition & 1 deletion mw_js_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"github.com/Sirupsen/logrus"
"github.com/x-cray/logrus-prefixed-formatter"
prefixed "github.com/x-cray/logrus-prefixed-formatter"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
Expand Down
2 changes: 1 addition & 1 deletion reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"time"

"github.com/Sirupsen/logrus"
"github.com/pmylund/go-cache"
cache "github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
Expand Down
7 changes: 4 additions & 3 deletions test/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
var (
muDefaultResolver sync.RWMutex
DomainsToAddresses = map[string][]string{
"host1.local.": {"127.0.0.1"},
"host2.local.": {"127.0.0.1"},
"host3.local.": {"127.0.0.1"},
"host1.local.": {"127.0.0.1"},
"host2.local.": {"127.0.0.1"},
"host3.local.": {"127.0.0.1"},
"http2.golang.org.": {"130.211.116.44"}, // HTTP/2 server
}
)

Expand Down

0 comments on commit 8341e21

Please sign in to comment.