2022-02-11 10:49:06 +00:00
|
|
|
package orchestration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-27 10:51:06 +00:00
|
|
|
"encoding/json"
|
2022-02-11 10:49:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2023-04-18 08:59:55 +00:00
|
|
|
"github.com/google/uuid"
|
2022-02-11 10:49:06 +00:00
|
|
|
gows "github.com/gorilla/websocket"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2022-02-11 10:49:06 +00:00
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
|
|
|
"github.com/cloudflare/cloudflared/ingress"
|
2023-03-21 18:42:25 +00:00
|
|
|
"github.com/cloudflare/cloudflared/management"
|
2022-04-06 23:20:29 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tracing"
|
2024-05-20 23:09:25 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2022-02-11 10:49:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-02-12 18:58:55 +00:00
|
|
|
testLogger = zerolog.Nop()
|
2024-05-20 23:09:25 +00:00
|
|
|
testTags = []pogs.Tag{
|
2022-02-11 10:49:06 +00:00
|
|
|
{
|
|
|
|
Name: "package",
|
|
|
|
Value: "orchestration",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "purpose",
|
|
|
|
Value: "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestUpdateConfiguration tests that
|
|
|
|
// - configurations can be deserialized
|
|
|
|
// - proxy can be updated
|
|
|
|
// - last applied version and error are returned
|
|
|
|
// - configurations can be deserialized
|
|
|
|
// - receiving an old version is noop
|
|
|
|
func TestUpdateConfiguration(t *testing.T) {
|
|
|
|
initConfig := &Config{
|
2022-06-13 16:44:27 +00:00
|
|
|
Ingress: &ingress.Ingress{},
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
2023-07-05 20:28:30 +00:00
|
|
|
orchestrator, err := NewOrchestrator(context.Background(), initConfig, testTags, []ingress.Rule{ingress.NewManagementRule(management.New("management.argotunnel.com", false, "1.1.1.1:80", uuid.Nil, "", &testLogger, nil))}, &testLogger)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
initOriginProxy, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
2023-03-21 18:42:25 +00:00
|
|
|
require.Implements(t, (*connection.OriginProxy)(nil), initOriginProxy)
|
2022-02-11 10:49:06 +00:00
|
|
|
|
|
|
|
configJSONV2 := []byte(`
|
|
|
|
{
|
|
|
|
"unknown_field": "not_deserialized",
|
|
|
|
"originRequest": {
|
2022-03-08 16:10:24 +00:00
|
|
|
"connectTimeout": 90,
|
2022-02-11 10:49:06 +00:00
|
|
|
"noHappyEyeballs": true
|
|
|
|
},
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"hostname": "jira.tunnel.org",
|
|
|
|
"path": "^\/login",
|
|
|
|
"service": "http://192.16.19.1:443",
|
|
|
|
"originRequest": {
|
|
|
|
"noTLSVerify": true,
|
2022-03-08 16:10:24 +00:00
|
|
|
"connectTimeout": 10
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hostname": "jira.tunnel.org",
|
|
|
|
"service": "http://172.32.20.6:80",
|
|
|
|
"originRequest": {
|
|
|
|
"noTLSVerify": true,
|
2022-03-08 16:10:24 +00:00
|
|
|
"connectTimeout": 30
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service": "http_status:404"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
2022-06-13 16:44:27 +00:00
|
|
|
"connectTimeout": 10
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
2023-06-09 17:17:04 +00:00
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 2, configJSONV2)
|
|
|
|
configV2 := orchestrator.config
|
2023-06-09 17:17:04 +00:00
|
|
|
// Validate internal ingress rules
|
|
|
|
require.Equal(t, "management.argotunnel.com", configV2.Ingress.InternalRules[0].Hostname)
|
|
|
|
require.True(t, configV2.Ingress.InternalRules[0].Matches("management.argotunnel.com", "/ping"))
|
|
|
|
require.Equal(t, "management", configV2.Ingress.InternalRules[0].Service.String())
|
2022-02-11 10:49:06 +00:00
|
|
|
// Validate ingress rule 0
|
|
|
|
require.Equal(t, "jira.tunnel.org", configV2.Ingress.Rules[0].Hostname)
|
|
|
|
require.True(t, configV2.Ingress.Rules[0].Matches("jira.tunnel.org", "/login"))
|
|
|
|
require.True(t, configV2.Ingress.Rules[0].Matches("jira.tunnel.org", "/login/2fa"))
|
|
|
|
require.False(t, configV2.Ingress.Rules[0].Matches("jira.tunnel.org", "/users"))
|
|
|
|
require.Equal(t, "http://192.16.19.1:443", configV2.Ingress.Rules[0].Service.String())
|
|
|
|
require.Len(t, configV2.Ingress.Rules, 3)
|
|
|
|
// originRequest of this ingress rule overrides global default
|
2022-03-14 17:51:10 +00:00
|
|
|
require.Equal(t, config.CustomDuration{Duration: time.Second * 10}, configV2.Ingress.Rules[0].Config.ConnectTimeout)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.Equal(t, true, configV2.Ingress.Rules[0].Config.NoTLSVerify)
|
|
|
|
// Inherited from global default
|
|
|
|
require.Equal(t, true, configV2.Ingress.Rules[0].Config.NoHappyEyeballs)
|
|
|
|
// Validate ingress rule 1
|
|
|
|
require.Equal(t, "jira.tunnel.org", configV2.Ingress.Rules[1].Hostname)
|
|
|
|
require.True(t, configV2.Ingress.Rules[1].Matches("jira.tunnel.org", "/users"))
|
|
|
|
require.Equal(t, "http://172.32.20.6:80", configV2.Ingress.Rules[1].Service.String())
|
|
|
|
// originRequest of this ingress rule overrides global default
|
2022-03-14 17:51:10 +00:00
|
|
|
require.Equal(t, config.CustomDuration{Duration: time.Second * 30}, configV2.Ingress.Rules[1].Config.ConnectTimeout)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.Equal(t, true, configV2.Ingress.Rules[1].Config.NoTLSVerify)
|
|
|
|
// Inherited from global default
|
|
|
|
require.Equal(t, true, configV2.Ingress.Rules[1].Config.NoHappyEyeballs)
|
|
|
|
// Validate ingress rule 2, it's the catch-all rule
|
|
|
|
require.True(t, configV2.Ingress.Rules[2].Matches("blogs.tunnel.io", "/2022/02/10"))
|
|
|
|
// Inherited from global default
|
2022-03-14 17:51:10 +00:00
|
|
|
require.Equal(t, config.CustomDuration{Duration: time.Second * 90}, configV2.Ingress.Rules[2].Config.ConnectTimeout)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.Equal(t, false, configV2.Ingress.Rules[2].Config.NoTLSVerify)
|
|
|
|
require.Equal(t, true, configV2.Ingress.Rules[2].Config.NoHappyEyeballs)
|
2022-06-13 16:44:27 +00:00
|
|
|
require.Equal(t, configV2.WarpRouting.ConnectTimeout.Duration, 10*time.Second)
|
2022-02-11 10:49:06 +00:00
|
|
|
|
|
|
|
originProxyV2, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
2023-03-21 18:42:25 +00:00
|
|
|
require.Implements(t, (*connection.OriginProxy)(nil), originProxyV2)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NotEqual(t, originProxyV2, initOriginProxy)
|
|
|
|
|
|
|
|
// Should not downgrade to an older version
|
|
|
|
resp := orchestrator.UpdateConfig(1, nil)
|
|
|
|
require.NoError(t, resp.Err)
|
|
|
|
require.Equal(t, int32(2), resp.LastAppliedVersion)
|
|
|
|
|
|
|
|
invalidJSON := []byte(`
|
|
|
|
{
|
|
|
|
"originRequest":
|
|
|
|
}
|
2023-06-09 17:17:04 +00:00
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
resp = orchestrator.UpdateConfig(3, invalidJSON)
|
|
|
|
require.Error(t, resp.Err)
|
|
|
|
require.Equal(t, int32(2), resp.LastAppliedVersion)
|
|
|
|
originProxyV3, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, originProxyV2, originProxyV3)
|
|
|
|
|
|
|
|
configJSONV10 := []byte(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "hello-world"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
2023-06-09 17:17:04 +00:00
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
`)
|
|
|
|
updateWithValidation(t, orchestrator, 10, configJSONV10)
|
|
|
|
configV10 := orchestrator.config
|
|
|
|
require.Len(t, configV10.Ingress.Rules, 1)
|
|
|
|
require.True(t, configV10.Ingress.Rules[0].Matches("blogs.tunnel.io", "/2022/02/10"))
|
|
|
|
require.Equal(t, ingress.HelloWorldService, configV10.Ingress.Rules[0].Service.String())
|
|
|
|
|
|
|
|
originProxyV10, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
2023-03-21 18:42:25 +00:00
|
|
|
require.Implements(t, (*connection.OriginProxy)(nil), originProxyV10)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NotEqual(t, originProxyV10, originProxyV2)
|
|
|
|
}
|
|
|
|
|
2023-05-05 19:41:11 +00:00
|
|
|
// Validates that a new version 0 will be applied if the configuration is loaded locally.
|
|
|
|
// This will happen when a locally managed tunnel is migrated to remote configuration and receives its first configuration.
|
|
|
|
func TestUpdateConfiguration_FromMigration(t *testing.T) {
|
|
|
|
initConfig := &Config{
|
|
|
|
Ingress: &ingress.Ingress{},
|
|
|
|
}
|
|
|
|
orchestrator, err := NewOrchestrator(context.Background(), initConfig, testTags, []ingress.Rule{}, &testLogger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
initOriginProxy, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Implements(t, (*connection.OriginProxy)(nil), initOriginProxy)
|
|
|
|
|
|
|
|
configJSONV2 := []byte(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "http_status:404"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
2023-06-09 17:17:04 +00:00
|
|
|
}
|
2023-05-05 19:41:11 +00:00
|
|
|
`)
|
|
|
|
updateWithValidation(t, orchestrator, 0, configJSONV2)
|
|
|
|
require.Len(t, orchestrator.config.Ingress.Rules, 1)
|
|
|
|
}
|
|
|
|
|
2023-06-09 17:17:04 +00:00
|
|
|
// Validates that the default ingress rule will be set if there is no rule provided from the remote.
|
|
|
|
func TestUpdateConfiguration_WithoutIngressRule(t *testing.T) {
|
|
|
|
initConfig := &Config{
|
|
|
|
Ingress: &ingress.Ingress{},
|
|
|
|
}
|
|
|
|
orchestrator, err := NewOrchestrator(context.Background(), initConfig, testTags, []ingress.Rule{}, &testLogger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
initOriginProxy, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Implements(t, (*connection.OriginProxy)(nil), initOriginProxy)
|
|
|
|
|
|
|
|
// We need to create an empty RemoteConfigJSON because that will get unmarshalled to a RemoteConfig
|
|
|
|
emptyConfig := &ingress.RemoteConfigJSON{}
|
|
|
|
configBytes, err := json.Marshal(emptyConfig)
|
|
|
|
if err != nil {
|
|
|
|
require.FailNow(t, "The RemoteConfigJSON shouldn't fail while being marshalled")
|
|
|
|
}
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 0, configBytes)
|
|
|
|
require.Len(t, orchestrator.config.Ingress.Rules, 1)
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
// TestConcurrentUpdateAndRead makes sure orchestrator can receive updates and return origin proxy concurrently
|
|
|
|
func TestConcurrentUpdateAndRead(t *testing.T) {
|
|
|
|
const (
|
|
|
|
concurrentRequests = 200
|
|
|
|
hostname = "public.tunnels.org"
|
|
|
|
expectedHost = "internal.tunnels.svc.cluster.local"
|
|
|
|
tcpBody = "testProxyTCP"
|
|
|
|
)
|
|
|
|
|
|
|
|
httpOrigin := httptest.NewServer(&validateHostHandler{
|
|
|
|
expectedHost: expectedHost,
|
|
|
|
body: t.Name(),
|
|
|
|
})
|
|
|
|
defer httpOrigin.Close()
|
|
|
|
|
|
|
|
tcpOrigin, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tcpOrigin.Close()
|
|
|
|
|
|
|
|
var (
|
|
|
|
configJSONV1 = []byte(fmt.Sprintf(`
|
|
|
|
{
|
|
|
|
"originRequest": {
|
2022-03-08 16:10:24 +00:00
|
|
|
"connectTimeout": 90,
|
2022-02-11 10:49:06 +00:00
|
|
|
"noHappyEyeballs": true
|
|
|
|
},
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"hostname": "%s",
|
|
|
|
"service": "%s",
|
|
|
|
"originRequest": {
|
|
|
|
"httpHostHeader": "%s",
|
2022-03-08 16:10:24 +00:00
|
|
|
"connectTimeout": 10
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service": "http_status:404"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, hostname, httpOrigin.URL, expectedHost))
|
|
|
|
configJSONV2 = []byte(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "http_status:204"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
configJSONV3 = []byte(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "http_status:418"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
// appliedV2 makes sure v3 is applied after v2
|
|
|
|
appliedV2 = make(chan struct{})
|
|
|
|
|
|
|
|
initConfig = &Config{
|
2022-06-13 16:44:27 +00:00
|
|
|
Ingress: &ingress.Ingress{},
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2023-03-21 18:42:25 +00:00
|
|
|
orchestrator, err := NewOrchestrator(ctx, initConfig, testTags, []ingress.Rule{}, &testLogger)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 1, configJSONV1)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
// tcpOrigin will be closed when the test exits. Only the handler routines are included in the wait group
|
|
|
|
go func() {
|
|
|
|
serveTCPOrigin(t, tcpOrigin, &wg)
|
|
|
|
}()
|
|
|
|
for i := 0; i < concurrentRequests; i++ {
|
|
|
|
originProxy, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int, originProxy connection.OriginProxy) {
|
|
|
|
defer wg.Done()
|
2022-03-30 10:06:21 +00:00
|
|
|
resp, err := proxyHTTP(originProxy, hostname)
|
|
|
|
require.NoError(t, err, "proxyHTTP %d failed %v", i, err)
|
|
|
|
defer resp.Body.Close()
|
2022-02-11 10:49:06 +00:00
|
|
|
|
|
|
|
var warpRoutingDisabled bool
|
|
|
|
// The response can be from initOrigin, http_status:204 or http_status:418
|
|
|
|
switch resp.StatusCode {
|
|
|
|
// v1 proxy, warp enabled
|
|
|
|
case 200:
|
2023-07-15 01:42:48 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, t.Name(), string(body))
|
|
|
|
warpRoutingDisabled = false
|
|
|
|
// v2 proxy, warp disabled
|
|
|
|
case 204:
|
|
|
|
require.Greater(t, i, concurrentRequests/4)
|
|
|
|
warpRoutingDisabled = true
|
|
|
|
// v3 proxy, warp enabled
|
|
|
|
case 418:
|
|
|
|
require.Greater(t, i, concurrentRequests/2)
|
|
|
|
warpRoutingDisabled = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we have originProxy, it won't be changed by configuration updates.
|
|
|
|
// We can infer the version by the ProxyHTTP response code
|
|
|
|
pr, pw := io.Pipe()
|
2022-03-30 10:06:21 +00:00
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
w := newRespReadWriteFlusher()
|
|
|
|
|
|
|
|
// Write TCP message and make sure it's echo back. This has to be done in a go routune since ProxyTCP doesn't
|
|
|
|
// return until the stream is closed.
|
|
|
|
if !warpRoutingDisabled {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
defer pw.Close()
|
|
|
|
tcpEyeball(t, pw, tcpBody, w)
|
|
|
|
}()
|
|
|
|
}
|
2022-03-30 10:06:21 +00:00
|
|
|
|
|
|
|
err = proxyTCP(ctx, originProxy, tcpOrigin.Addr().String(), w, pr)
|
|
|
|
if warpRoutingDisabled {
|
|
|
|
require.Error(t, err, "expect proxyTCP %d to return error", i)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, "proxyTCP %d failed %v", i, err)
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
}(i, originProxy)
|
|
|
|
|
|
|
|
if i == concurrentRequests/4 {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
updateWithValidation(t, orchestrator, 2, configJSONV2)
|
|
|
|
close(appliedV2)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == concurrentRequests/2 {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-03-30 10:06:21 +00:00
|
|
|
// Makes sure v2 is applied before v3
|
2022-02-11 10:49:06 +00:00
|
|
|
<-appliedV2
|
|
|
|
updateWithValidation(t, orchestrator, 3, configJSONV3)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
func proxyHTTP(originProxy connection.OriginProxy, hostname string) (*http.Response, error) {
|
2022-02-11 10:49:06 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s", hostname), nil)
|
2022-03-30 10:06:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-03-04 09:31:40 +00:00
|
|
|
log := zerolog.Nop()
|
|
|
|
respWriter, err := connection.NewHTTP2RespWriter(req, w, connection.TypeHTTP, &log)
|
2022-03-30 10:06:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
|
2023-02-22 14:52:44 +00:00
|
|
|
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, 0, &log), false)
|
2022-02-11 10:49:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.Result(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tcpEyeball(t *testing.T, reqWriter io.WriteCloser, body string, respReadWriter *respReadWriteFlusher) {
|
|
|
|
writeN, err := reqWriter.Write([]byte(body))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
readBuffer := make([]byte, writeN)
|
|
|
|
n, err := respReadWriter.Read(readBuffer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, body, string(readBuffer[:n]))
|
|
|
|
require.Equal(t, writeN, n)
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
func proxyTCP(ctx context.Context, originProxy connection.OriginProxy, originAddr string, w http.ResponseWriter, reqBody io.ReadCloser) error {
|
2022-02-11 10:49:06 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s", originAddr), reqBody)
|
2022-03-30 10:06:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
|
2022-03-04 09:31:40 +00:00
|
|
|
log := zerolog.Nop()
|
|
|
|
respWriter, err := connection.NewHTTP2RespWriter(req, w, connection.TypeTCP, &log)
|
2022-03-30 10:06:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
|
|
|
|
tcpReq := &connection.TCPRequest{
|
|
|
|
Dest: originAddr,
|
|
|
|
CFRay: "123",
|
|
|
|
LBProbe: false,
|
|
|
|
}
|
2023-07-06 13:42:44 +00:00
|
|
|
rws := connection.NewHTTPResponseReadWriterAcker(respWriter, w.(http.Flusher), req)
|
2022-02-11 10:49:06 +00:00
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
return originProxy.ProxyTCP(ctx, rws, tcpReq)
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serveTCPOrigin(t *testing.T, tcpOrigin net.Listener, wg *sync.WaitGroup) {
|
|
|
|
for {
|
|
|
|
conn, err := tcpOrigin.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
echoTCP(t, conn)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func echoTCP(t *testing.T, conn net.Conn) {
|
|
|
|
readBuf := make([]byte, 1000)
|
|
|
|
readN, err := conn.Read(readBuf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
writeN, err := conn.Write(readBuf[:readN])
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, readN, writeN)
|
|
|
|
}
|
|
|
|
|
|
|
|
type validateHostHandler struct {
|
|
|
|
expectedHost string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vhh *validateHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Host != vhh.expectedHost {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(vhh.body))
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateWithValidation(t *testing.T, orchestrator *Orchestrator, version int32, config []byte) {
|
|
|
|
resp := orchestrator.UpdateConfig(version, config)
|
|
|
|
require.NoError(t, resp.Err)
|
|
|
|
require.Equal(t, version, resp.LastAppliedVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestClosePreviousProxies makes sure proxies started in the pervious configuration version are shutdown
|
|
|
|
func TestClosePreviousProxies(t *testing.T) {
|
|
|
|
var (
|
|
|
|
hostname = "hello.tunnel1.org"
|
|
|
|
configWithHelloWorld = []byte(fmt.Sprintf(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"hostname": "%s",
|
|
|
|
"service": "hello-world"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service": "http_status:404"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, hostname))
|
|
|
|
|
|
|
|
configTeapot = []byte(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "http_status:418"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
initConfig = &Config{
|
2022-06-13 16:44:27 +00:00
|
|
|
Ingress: &ingress.Ingress{},
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-03-21 18:42:25 +00:00
|
|
|
orchestrator, err := NewOrchestrator(ctx, initConfig, testTags, []ingress.Rule{}, &testLogger)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 1, configWithHelloWorld)
|
|
|
|
|
|
|
|
originProxyV1, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
2022-03-30 10:06:21 +00:00
|
|
|
resp, err := proxyHTTP(originProxyV1, hostname)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 2, configTeapot)
|
|
|
|
|
|
|
|
originProxyV2, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
2022-03-30 10:06:21 +00:00
|
|
|
resp, err = proxyHTTP(originProxyV2, hostname)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusTeapot, resp.StatusCode)
|
|
|
|
|
2022-09-01 18:29:11 +00:00
|
|
|
// The hello-world server in config v1 should have been stopped. We wait a bit since it's closed asynchronously.
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2022-03-30 10:06:21 +00:00
|
|
|
resp, err = proxyHTTP(originProxyV1, hostname)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, resp)
|
|
|
|
|
|
|
|
// Apply the config with hello world server again, orchestrator should spin up another hello world server
|
|
|
|
updateWithValidation(t, orchestrator, 3, configWithHelloWorld)
|
|
|
|
|
|
|
|
originProxyV3, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, originProxyV1, originProxyV3)
|
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
resp, err = proxyHTTP(originProxyV3, hostname)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// cancel the context should terminate the last proxy
|
|
|
|
cancel()
|
|
|
|
// Wait for proxies to shutdown
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
resp, err = proxyHTTP(originProxyV3, hostname)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPersistentConnection makes sure updating the ingress doesn't intefere with existing connections
|
|
|
|
func TestPersistentConnection(t *testing.T) {
|
|
|
|
const (
|
|
|
|
hostname = "http://ws.tunnel.org"
|
|
|
|
)
|
|
|
|
msg := t.Name()
|
|
|
|
initConfig := &Config{
|
2022-06-13 16:44:27 +00:00
|
|
|
Ingress: &ingress.Ingress{},
|
2022-02-11 10:49:06 +00:00
|
|
|
}
|
2023-03-21 18:42:25 +00:00
|
|
|
orchestrator, err := NewOrchestrator(context.Background(), initConfig, testTags, []ingress.Rule{}, &testLogger)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wsOrigin := httptest.NewServer(http.HandlerFunc(wsEcho))
|
|
|
|
defer wsOrigin.Close()
|
|
|
|
|
|
|
|
tcpOrigin, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tcpOrigin.Close()
|
|
|
|
|
|
|
|
configWithWSAndWarp := []byte(fmt.Sprintf(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "%s"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, wsOrigin.URL))
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 1, configWithWSAndWarp)
|
|
|
|
|
|
|
|
originProxy, err := orchestrator.GetOriginProxy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wsReqReader, wsReqWriter := io.Pipe()
|
|
|
|
wsRespReadWriter := newRespReadWriteFlusher()
|
|
|
|
|
|
|
|
tcpReqReader, tcpReqWriter := io.Pipe()
|
|
|
|
tcpRespReadWriter := newRespReadWriteFlusher()
|
|
|
|
|
2022-03-30 10:06:21 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(3)
|
|
|
|
// Start TCP origin
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
conn, err := tcpOrigin.Accept()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Expect 3 TCP messages
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
echoTCP(t, conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Simulate cloudflared recieving a TCP connection
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-03-30 10:06:21 +00:00
|
|
|
require.NoError(t, proxyTCP(ctx, originProxy, tcpOrigin.Addr().String(), tcpRespReadWriter, tcpReqReader))
|
2022-02-11 10:49:06 +00:00
|
|
|
}()
|
|
|
|
// Simulate cloudflared recieving a WS connection
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, hostname, wsReqReader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// ProxyHTTP will add Connection, Upgrade and Sec-Websocket-Version headers
|
|
|
|
req.Header.Add("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
|
|
|
|
|
2022-03-04 09:31:40 +00:00
|
|
|
log := zerolog.Nop()
|
|
|
|
respWriter, err := connection.NewHTTP2RespWriter(req, wsRespReadWriter, connection.TypeWebsocket, &log)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-22 14:52:44 +00:00
|
|
|
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, 0, &log), true)
|
2022-02-11 10:49:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Simulate eyeball WS and TCP connections
|
|
|
|
validateWsEcho(t, msg, wsReqWriter, wsRespReadWriter)
|
|
|
|
tcpEyeball(t, tcpReqWriter, msg, tcpRespReadWriter)
|
|
|
|
|
|
|
|
configNoWSAndWarp := []byte(`
|
|
|
|
{
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"service": "http_status:404"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"warp-routing": {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 2, configNoWSAndWarp)
|
|
|
|
// Make sure connection is still up
|
|
|
|
validateWsEcho(t, msg, wsReqWriter, wsRespReadWriter)
|
|
|
|
tcpEyeball(t, tcpReqWriter, msg, tcpRespReadWriter)
|
|
|
|
|
|
|
|
updateWithValidation(t, orchestrator, 3, configWithWSAndWarp)
|
|
|
|
// Make sure connection is still up
|
|
|
|
validateWsEcho(t, msg, wsReqWriter, wsRespReadWriter)
|
|
|
|
tcpEyeball(t, tcpReqWriter, msg, tcpRespReadWriter)
|
|
|
|
|
|
|
|
wsReqWriter.Close()
|
|
|
|
tcpReqWriter.Close()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:51:06 +00:00
|
|
|
func TestSerializeLocalConfig(t *testing.T) {
|
|
|
|
c := &newLocalConfig{
|
|
|
|
RemoteConfig: ingress.RemoteConfig{
|
2022-06-13 16:44:27 +00:00
|
|
|
Ingress: ingress.Ingress{},
|
2022-04-27 10:51:06 +00:00
|
|
|
},
|
|
|
|
ConfigurationFlags: map[string]string{"a": "b"},
|
|
|
|
}
|
|
|
|
|
|
|
|
result, _ := json.Marshal(c)
|
|
|
|
fmt.Println(string(result))
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func wsEcho(w http.ResponseWriter, r *http.Request) {
|
|
|
|
upgrader := gows.Upgrader{}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
mt, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("read message err", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = conn.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("write message err", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateWsEcho(t *testing.T, msg string, reqWriter io.Writer, respReadWriter io.ReadWriter) {
|
|
|
|
err := wsutil.WriteClientText(reqWriter, []byte(msg))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
receivedMsg, err := wsutil.ReadServerText(respReadWriter)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msg, string(receivedMsg))
|
|
|
|
}
|
|
|
|
|
|
|
|
type respReadWriteFlusher struct {
|
|
|
|
io.Reader
|
|
|
|
w io.Writer
|
|
|
|
headers http.Header
|
|
|
|
statusCode int
|
|
|
|
setStatusOnce sync.Once
|
|
|
|
hasStatus chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRespReadWriteFlusher() *respReadWriteFlusher {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
return &respReadWriteFlusher{
|
|
|
|
Reader: pr,
|
|
|
|
w: pw,
|
|
|
|
headers: make(http.Header),
|
|
|
|
hasStatus: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rrw *respReadWriteFlusher) Write(buf []byte) (int, error) {
|
|
|
|
rrw.WriteHeader(http.StatusOK)
|
|
|
|
return rrw.w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rrw *respReadWriteFlusher) Flush() {}
|
|
|
|
|
|
|
|
func (rrw *respReadWriteFlusher) Header() http.Header {
|
|
|
|
return rrw.headers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rrw *respReadWriteFlusher) WriteHeader(statusCode int) {
|
|
|
|
rrw.setStatusOnce.Do(func() {
|
|
|
|
rrw.statusCode = statusCode
|
|
|
|
close(rrw.hasStatus)
|
|
|
|
})
|
|
|
|
}
|