orchestrator_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. package orchestration
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net"
  8. "net/http"
  9. "net/http/httptest"
  10. "sync"
  11. "testing"
  12. "time"
  13. "github.com/gobwas/ws/wsutil"
  14. "github.com/google/uuid"
  15. gows "github.com/gorilla/websocket"
  16. "github.com/rs/zerolog"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. "github.com/cloudflare/cloudflared/cmd/cloudflared/flags"
  20. "github.com/cloudflare/cloudflared/config"
  21. "github.com/cloudflare/cloudflared/connection"
  22. "github.com/cloudflare/cloudflared/ingress"
  23. "github.com/cloudflare/cloudflared/management"
  24. "github.com/cloudflare/cloudflared/tracing"
  25. "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
  26. )
  27. var (
  28. testLogger = zerolog.Nop()
  29. testTags = []pogs.Tag{
  30. {
  31. Name: "package",
  32. Value: "orchestration",
  33. },
  34. {
  35. Name: "purpose",
  36. Value: "test",
  37. },
  38. }
  39. testDefaultDialer = ingress.NewDialer(ingress.WarpRoutingConfig{
  40. ConnectTimeout: config.CustomDuration{Duration: 1 * time.Second},
  41. TCPKeepAlive: config.CustomDuration{Duration: 15 * time.Second},
  42. MaxActiveFlows: 0,
  43. })
  44. )
  45. // TestUpdateConfiguration tests that
  46. // - configurations can be deserialized
  47. // - proxy can be updated
  48. // - last applied version and error are returned
  49. // - configurations can be deserialized
  50. // - receiving an old version is noop
  51. func TestUpdateConfiguration(t *testing.T) {
  52. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  53. DefaultDialer: testDefaultDialer,
  54. TCPWriteTimeout: 1 * time.Second,
  55. }, &testLogger)
  56. initConfig := &Config{
  57. Ingress: &ingress.Ingress{},
  58. OriginDialerService: originDialer,
  59. }
  60. orchestrator, err := NewOrchestrator(t.Context(), initConfig, testTags, []ingress.Rule{ingress.NewManagementRule(management.New("management.argotunnel.com", false, "1.1.1.1:80", uuid.Nil, "", &testLogger, nil))}, &testLogger)
  61. require.NoError(t, err)
  62. initOriginProxy, err := orchestrator.GetOriginProxy()
  63. require.NoError(t, err)
  64. require.Implements(t, (*connection.OriginProxy)(nil), initOriginProxy)
  65. configJSONV2 := []byte(`
  66. {
  67. "unknown_field": "not_deserialized",
  68. "originRequest": {
  69. "connectTimeout": 90,
  70. "noHappyEyeballs": true
  71. },
  72. "ingress": [
  73. {
  74. "hostname": "jira.tunnel.org",
  75. "path": "^\/login",
  76. "service": "http://192.16.19.1:443",
  77. "originRequest": {
  78. "noTLSVerify": true,
  79. "connectTimeout": 10
  80. }
  81. },
  82. {
  83. "hostname": "jira.tunnel.org",
  84. "service": "http://172.32.20.6:80",
  85. "originRequest": {
  86. "noTLSVerify": true,
  87. "connectTimeout": 30
  88. }
  89. },
  90. {
  91. "service": "http_status:404"
  92. }
  93. ],
  94. "warp-routing": {
  95. "connectTimeout": 10
  96. }
  97. }
  98. `)
  99. updateWithValidation(t, orchestrator, 2, configJSONV2)
  100. configV2 := orchestrator.config
  101. // Validate internal ingress rules
  102. require.Equal(t, "management.argotunnel.com", configV2.Ingress.InternalRules[0].Hostname)
  103. require.True(t, configV2.Ingress.InternalRules[0].Matches("management.argotunnel.com", "/ping"))
  104. require.Equal(t, "management", configV2.Ingress.InternalRules[0].Service.String())
  105. // Validate ingress rule 0
  106. require.Equal(t, "jira.tunnel.org", configV2.Ingress.Rules[0].Hostname)
  107. require.True(t, configV2.Ingress.Rules[0].Matches("jira.tunnel.org", "/login"))
  108. require.True(t, configV2.Ingress.Rules[0].Matches("jira.tunnel.org", "/login/2fa"))
  109. require.False(t, configV2.Ingress.Rules[0].Matches("jira.tunnel.org", "/users"))
  110. require.Equal(t, "http://192.16.19.1:443", configV2.Ingress.Rules[0].Service.String())
  111. require.Len(t, configV2.Ingress.Rules, 3)
  112. // originRequest of this ingress rule overrides global default
  113. require.Equal(t, config.CustomDuration{Duration: time.Second * 10}, configV2.Ingress.Rules[0].Config.ConnectTimeout)
  114. require.True(t, configV2.Ingress.Rules[0].Config.NoTLSVerify)
  115. // Inherited from global default
  116. require.True(t, configV2.Ingress.Rules[0].Config.NoHappyEyeballs)
  117. // Validate ingress rule 1
  118. require.Equal(t, "jira.tunnel.org", configV2.Ingress.Rules[1].Hostname)
  119. require.True(t, configV2.Ingress.Rules[1].Matches("jira.tunnel.org", "/users"))
  120. require.Equal(t, "http://172.32.20.6:80", configV2.Ingress.Rules[1].Service.String())
  121. // originRequest of this ingress rule overrides global default
  122. require.Equal(t, config.CustomDuration{Duration: time.Second * 30}, configV2.Ingress.Rules[1].Config.ConnectTimeout)
  123. require.True(t, configV2.Ingress.Rules[1].Config.NoTLSVerify)
  124. // Inherited from global default
  125. require.True(t, configV2.Ingress.Rules[1].Config.NoHappyEyeballs)
  126. // Validate ingress rule 2, it's the catch-all rule
  127. require.True(t, configV2.Ingress.Rules[2].Matches("blogs.tunnel.io", "/2022/02/10"))
  128. // Inherited from global default
  129. require.Equal(t, config.CustomDuration{Duration: time.Second * 90}, configV2.Ingress.Rules[2].Config.ConnectTimeout)
  130. require.False(t, configV2.Ingress.Rules[2].Config.NoTLSVerify)
  131. require.True(t, configV2.Ingress.Rules[2].Config.NoHappyEyeballs)
  132. require.Equal(t, 10*time.Second, configV2.WarpRouting.ConnectTimeout.Duration)
  133. originProxyV2, err := orchestrator.GetOriginProxy()
  134. require.NoError(t, err)
  135. require.Implements(t, (*connection.OriginProxy)(nil), originProxyV2)
  136. require.NotEqual(t, originProxyV2, initOriginProxy)
  137. // Should not downgrade to an older version
  138. resp := orchestrator.UpdateConfig(1, nil)
  139. require.NoError(t, resp.Err)
  140. require.Equal(t, int32(2), resp.LastAppliedVersion)
  141. invalidJSON := []byte(`
  142. {
  143. "originRequest":
  144. }
  145. `)
  146. resp = orchestrator.UpdateConfig(3, invalidJSON)
  147. require.Error(t, resp.Err)
  148. require.Equal(t, int32(2), resp.LastAppliedVersion)
  149. originProxyV3, err := orchestrator.GetOriginProxy()
  150. require.NoError(t, err)
  151. require.Equal(t, originProxyV2, originProxyV3)
  152. configJSONV10 := []byte(`
  153. {
  154. "ingress": [
  155. {
  156. "service": "hello-world"
  157. }
  158. ],
  159. "warp-routing": {
  160. }
  161. }
  162. `)
  163. updateWithValidation(t, orchestrator, 10, configJSONV10)
  164. configV10 := orchestrator.config
  165. require.Len(t, configV10.Ingress.Rules, 1)
  166. require.True(t, configV10.Ingress.Rules[0].Matches("blogs.tunnel.io", "/2022/02/10"))
  167. require.Equal(t, ingress.HelloWorldService, configV10.Ingress.Rules[0].Service.String())
  168. originProxyV10, err := orchestrator.GetOriginProxy()
  169. require.NoError(t, err)
  170. require.Implements(t, (*connection.OriginProxy)(nil), originProxyV10)
  171. require.NotEqual(t, originProxyV10, originProxyV2)
  172. }
  173. // Validates that a new version 0 will be applied if the configuration is loaded locally.
  174. // This will happen when a locally managed tunnel is migrated to remote configuration and receives its first configuration.
  175. func TestUpdateConfiguration_FromMigration(t *testing.T) {
  176. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  177. DefaultDialer: testDefaultDialer,
  178. TCPWriteTimeout: 1 * time.Second,
  179. }, &testLogger)
  180. initConfig := &Config{
  181. Ingress: &ingress.Ingress{},
  182. OriginDialerService: originDialer,
  183. }
  184. orchestrator, err := NewOrchestrator(t.Context(), initConfig, testTags, []ingress.Rule{}, &testLogger)
  185. require.NoError(t, err)
  186. initOriginProxy, err := orchestrator.GetOriginProxy()
  187. require.NoError(t, err)
  188. require.Implements(t, (*connection.OriginProxy)(nil), initOriginProxy)
  189. configJSONV2 := []byte(`
  190. {
  191. "ingress": [
  192. {
  193. "service": "http_status:404"
  194. }
  195. ],
  196. "warp-routing": {
  197. }
  198. }
  199. `)
  200. updateWithValidation(t, orchestrator, 0, configJSONV2)
  201. require.Len(t, orchestrator.config.Ingress.Rules, 1)
  202. }
  203. // Validates that the default ingress rule will be set if there is no rule provided from the remote.
  204. func TestUpdateConfiguration_WithoutIngressRule(t *testing.T) {
  205. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  206. DefaultDialer: testDefaultDialer,
  207. TCPWriteTimeout: 1 * time.Second,
  208. }, &testLogger)
  209. initConfig := &Config{
  210. Ingress: &ingress.Ingress{},
  211. OriginDialerService: originDialer,
  212. }
  213. orchestrator, err := NewOrchestrator(t.Context(), initConfig, testTags, []ingress.Rule{}, &testLogger)
  214. require.NoError(t, err)
  215. initOriginProxy, err := orchestrator.GetOriginProxy()
  216. require.NoError(t, err)
  217. require.Implements(t, (*connection.OriginProxy)(nil), initOriginProxy)
  218. // We need to create an empty RemoteConfigJSON because that will get unmarshalled to a RemoteConfig
  219. emptyConfig := &ingress.RemoteConfigJSON{}
  220. configBytes, err := json.Marshal(emptyConfig)
  221. if err != nil {
  222. require.FailNow(t, "The RemoteConfigJSON shouldn't fail while being marshalled")
  223. }
  224. updateWithValidation(t, orchestrator, 0, configBytes)
  225. require.Len(t, orchestrator.config.Ingress.Rules, 1)
  226. }
  227. // TestConcurrentUpdateAndRead makes sure orchestrator can receive updates and return origin proxy concurrently
  228. func TestConcurrentUpdateAndRead(t *testing.T) {
  229. const (
  230. concurrentRequests = 200
  231. hostname = "public.tunnels.org"
  232. expectedHost = "internal.tunnels.svc.cluster.local"
  233. tcpBody = "testProxyTCP"
  234. )
  235. httpOrigin := httptest.NewServer(&validateHostHandler{
  236. expectedHost: expectedHost,
  237. body: t.Name(),
  238. })
  239. defer httpOrigin.Close()
  240. tcpOrigin, err := net.Listen("tcp", "127.0.0.1:0")
  241. require.NoError(t, err)
  242. defer tcpOrigin.Close()
  243. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  244. DefaultDialer: testDefaultDialer,
  245. TCPWriteTimeout: 1 * time.Second,
  246. }, &testLogger)
  247. var (
  248. configJSONV1 = []byte(fmt.Sprintf(`
  249. {
  250. "originRequest": {
  251. "connectTimeout": 90,
  252. "noHappyEyeballs": true
  253. },
  254. "ingress": [
  255. {
  256. "hostname": "%s",
  257. "service": "%s",
  258. "originRequest": {
  259. "httpHostHeader": "%s",
  260. "connectTimeout": 10
  261. }
  262. },
  263. {
  264. "service": "http_status:404"
  265. }
  266. ],
  267. "warp-routing": {
  268. }
  269. }
  270. `, hostname, httpOrigin.URL, expectedHost))
  271. configJSONV2 = []byte(`
  272. {
  273. "ingress": [
  274. {
  275. "service": "http_status:204"
  276. }
  277. ],
  278. "warp-routing": {
  279. }
  280. }
  281. `)
  282. configJSONV3 = []byte(`
  283. {
  284. "ingress": [
  285. {
  286. "service": "http_status:418"
  287. }
  288. ],
  289. "warp-routing": {
  290. }
  291. }
  292. `)
  293. // appliedV2 makes sure v3 is applied after v2
  294. appliedV2 = make(chan struct{})
  295. initConfig = &Config{
  296. Ingress: &ingress.Ingress{},
  297. OriginDialerService: originDialer,
  298. }
  299. )
  300. ctx, cancel := context.WithCancel(t.Context())
  301. defer cancel()
  302. orchestrator, err := NewOrchestrator(ctx, initConfig, testTags, []ingress.Rule{}, &testLogger)
  303. require.NoError(t, err)
  304. updateWithValidation(t, orchestrator, 1, configJSONV1)
  305. var wg sync.WaitGroup
  306. // tcpOrigin will be closed when the test exits. Only the handler routines are included in the wait group
  307. go func() {
  308. serveTCPOrigin(t, tcpOrigin, &wg)
  309. }()
  310. for i := range concurrentRequests {
  311. originProxy, err := orchestrator.GetOriginProxy()
  312. require.NoError(t, err)
  313. wg.Add(1)
  314. go func(i int, originProxy connection.OriginProxy) {
  315. defer wg.Done()
  316. resp, err := proxyHTTP(originProxy, hostname)
  317. assert.NoError(t, err, "proxyHTTP %d failed %v", i, err)
  318. defer resp.Body.Close()
  319. // The response can be from initOrigin, http_status:204 or http_status:418
  320. switch resp.StatusCode {
  321. // v1 proxy
  322. case 200:
  323. body, err := io.ReadAll(resp.Body)
  324. assert.NoError(t, err)
  325. assert.Equal(t, t.Name(), string(body))
  326. // v2 proxy
  327. case 204:
  328. assert.Greater(t, i, concurrentRequests/4)
  329. // v3 proxy
  330. case 418:
  331. assert.Greater(t, i, concurrentRequests/2)
  332. }
  333. // Once we have originProxy, it won't be changed by configuration updates.
  334. // We can infer the version by the ProxyHTTP response code
  335. pr, pw := io.Pipe()
  336. w := newRespReadWriteFlusher()
  337. // Write TCP message and make sure it's echo back. This has to be done in a go routune since ProxyTCP doesn't
  338. // return until the stream is closed.
  339. wg.Add(1)
  340. go func() {
  341. defer wg.Done()
  342. defer pw.Close()
  343. tcpEyeball(t, pw, tcpBody, w)
  344. }()
  345. err = proxyTCP(ctx, originProxy, tcpOrigin.Addr().String(), w, pr)
  346. assert.NoError(t, err, "proxyTCP %d failed %v", i, err)
  347. }(i, originProxy)
  348. if i == concurrentRequests/4 {
  349. wg.Add(1)
  350. go func() {
  351. defer wg.Done()
  352. updateWithValidation(t, orchestrator, 2, configJSONV2)
  353. close(appliedV2)
  354. }()
  355. }
  356. if i == concurrentRequests/2 {
  357. wg.Add(1)
  358. go func() {
  359. defer wg.Done()
  360. // Makes sure v2 is applied before v3
  361. <-appliedV2
  362. updateWithValidation(t, orchestrator, 3, configJSONV3)
  363. }()
  364. }
  365. }
  366. wg.Wait()
  367. }
  368. // TestOverrideWarpRoutingConfigWithLocalValues tests that if a value is defined in the Config.ConfigurationFlags,
  369. // it will override the value that comes from the remote result.
  370. func TestOverrideWarpRoutingConfigWithLocalValues(t *testing.T) {
  371. ctx, cancel := context.WithCancel(t.Context())
  372. defer cancel()
  373. assertMaxActiveFlows := func(orchestrator *Orchestrator, expectedValue uint64) {
  374. configJson, err := orchestrator.GetConfigJSON()
  375. require.NoError(t, err)
  376. var result map[string]interface{}
  377. err = json.Unmarshal(configJson, &result)
  378. require.NoError(t, err)
  379. warpRouting := result["warp-routing"].(map[string]interface{})
  380. require.EqualValues(t, expectedValue, warpRouting["maxActiveFlows"])
  381. }
  382. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  383. DefaultDialer: testDefaultDialer,
  384. TCPWriteTimeout: 1 * time.Second,
  385. }, &testLogger)
  386. // All the possible values set for MaxActiveFlows from the various points that can provide it:
  387. // 1. Initialized value
  388. // 2. Local CLI flag config
  389. // 3. Remote configuration value
  390. initValue := uint64(0)
  391. localValue := uint64(100)
  392. remoteValue := uint64(500)
  393. initConfig := &Config{
  394. Ingress: &ingress.Ingress{},
  395. WarpRouting: ingress.WarpRoutingConfig{
  396. MaxActiveFlows: initValue,
  397. },
  398. OriginDialerService: originDialer,
  399. ConfigurationFlags: map[string]string{
  400. flags.MaxActiveFlows: fmt.Sprintf("%d", localValue),
  401. },
  402. }
  403. // We expect the local configuration flag to be the starting value
  404. orchestrator, err := NewOrchestrator(ctx, initConfig, testTags, []ingress.Rule{}, &testLogger)
  405. require.NoError(t, err)
  406. assertMaxActiveFlows(orchestrator, localValue)
  407. // Assigning the MaxActiveFlows in the remote config should be ignored over the local config
  408. remoteWarpConfig := ingress.WarpRoutingConfig{
  409. MaxActiveFlows: remoteValue,
  410. }
  411. // Force a configuration refresh
  412. err = orchestrator.updateIngress(ingress.Ingress{}, remoteWarpConfig)
  413. require.NoError(t, err)
  414. // Check the value being used is the local one
  415. assertMaxActiveFlows(orchestrator, localValue)
  416. }
  417. func proxyHTTP(originProxy connection.OriginProxy, hostname string) (*http.Response, error) {
  418. req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s", hostname), nil)
  419. if err != nil {
  420. return nil, err
  421. }
  422. w := httptest.NewRecorder()
  423. log := zerolog.Nop()
  424. respWriter, err := connection.NewHTTP2RespWriter(req, w, connection.TypeHTTP, &log)
  425. if err != nil {
  426. return nil, err
  427. }
  428. err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, 0, &log), false)
  429. if err != nil {
  430. return nil, err
  431. }
  432. return w.Result(), nil
  433. }
  434. // nolint: testifylint // this is used inside go routines so it can't use `require.`
  435. func tcpEyeball(t *testing.T, reqWriter io.WriteCloser, body string, respReadWriter *respReadWriteFlusher) {
  436. writeN, err := reqWriter.Write([]byte(body))
  437. assert.NoError(t, err)
  438. readBuffer := make([]byte, writeN)
  439. n, err := respReadWriter.Read(readBuffer)
  440. assert.NoError(t, err)
  441. assert.Equal(t, body, string(readBuffer[:n]))
  442. assert.Equal(t, writeN, n)
  443. }
  444. func proxyTCP(ctx context.Context, originProxy connection.OriginProxy, originAddr string, w http.ResponseWriter, reqBody io.ReadCloser) error {
  445. req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s", originAddr), reqBody)
  446. if err != nil {
  447. return err
  448. }
  449. log := zerolog.Nop()
  450. respWriter, err := connection.NewHTTP2RespWriter(req, w, connection.TypeTCP, &log)
  451. if err != nil {
  452. return err
  453. }
  454. tcpReq := &connection.TCPRequest{
  455. Dest: originAddr,
  456. CFRay: "123",
  457. LBProbe: false,
  458. }
  459. rws := connection.NewHTTPResponseReadWriterAcker(respWriter, w.(http.Flusher), req)
  460. return originProxy.ProxyTCP(ctx, rws, tcpReq)
  461. }
  462. func serveTCPOrigin(t *testing.T, tcpOrigin net.Listener, wg *sync.WaitGroup) {
  463. for {
  464. conn, err := tcpOrigin.Accept()
  465. if err != nil {
  466. return
  467. }
  468. wg.Add(1)
  469. go func() {
  470. defer wg.Done()
  471. defer conn.Close()
  472. echoTCP(t, conn)
  473. }()
  474. }
  475. }
  476. // nolint: testifylint // this is used inside go routines so it can't use `require.`
  477. func echoTCP(t *testing.T, conn net.Conn) {
  478. readBuf := make([]byte, 1000)
  479. readN, err := conn.Read(readBuf)
  480. assert.NoError(t, err)
  481. writeN, err := conn.Write(readBuf[:readN])
  482. assert.NoError(t, err)
  483. assert.Equal(t, readN, writeN)
  484. }
  485. type validateHostHandler struct {
  486. expectedHost string
  487. body string
  488. }
  489. func (vhh *validateHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  490. if r.Host != vhh.expectedHost {
  491. w.WriteHeader(http.StatusBadRequest)
  492. return
  493. }
  494. w.WriteHeader(http.StatusOK)
  495. _, _ = w.Write([]byte(vhh.body))
  496. }
  497. // nolint: testifylint // this is used inside go routines so it can't use `require.`
  498. func updateWithValidation(t *testing.T, orchestrator *Orchestrator, version int32, config []byte) {
  499. resp := orchestrator.UpdateConfig(version, config)
  500. assert.NoError(t, resp.Err)
  501. assert.Equal(t, version, resp.LastAppliedVersion)
  502. }
  503. // TestClosePreviousProxies makes sure proxies started in the previous configuration version are shutdown
  504. func TestClosePreviousProxies(t *testing.T) {
  505. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  506. DefaultDialer: testDefaultDialer,
  507. TCPWriteTimeout: 1 * time.Second,
  508. }, &testLogger)
  509. var (
  510. hostname = "hello.tunnel1.org"
  511. configWithHelloWorld = []byte(fmt.Sprintf(`
  512. {
  513. "ingress": [
  514. {
  515. "hostname": "%s",
  516. "service": "hello-world"
  517. },
  518. {
  519. "service": "http_status:404"
  520. }
  521. ],
  522. "warp-routing": {
  523. }
  524. }
  525. `, hostname))
  526. configTeapot = []byte(`
  527. {
  528. "ingress": [
  529. {
  530. "service": "http_status:418"
  531. }
  532. ],
  533. "warp-routing": {
  534. }
  535. }
  536. `)
  537. initConfig = &Config{
  538. Ingress: &ingress.Ingress{},
  539. OriginDialerService: originDialer,
  540. }
  541. )
  542. ctx, cancel := context.WithCancel(t.Context())
  543. orchestrator, err := NewOrchestrator(ctx, initConfig, testTags, []ingress.Rule{}, &testLogger)
  544. require.NoError(t, err)
  545. updateWithValidation(t, orchestrator, 1, configWithHelloWorld)
  546. originProxyV1, err := orchestrator.GetOriginProxy()
  547. require.NoError(t, err)
  548. // nolint: bodyclose
  549. resp, err := proxyHTTP(originProxyV1, hostname)
  550. require.NoError(t, err)
  551. require.Equal(t, http.StatusOK, resp.StatusCode)
  552. updateWithValidation(t, orchestrator, 2, configTeapot)
  553. originProxyV2, err := orchestrator.GetOriginProxy()
  554. require.NoError(t, err)
  555. // nolint: bodyclose
  556. resp, err = proxyHTTP(originProxyV2, hostname)
  557. require.NoError(t, err)
  558. require.Equal(t, http.StatusTeapot, resp.StatusCode)
  559. // The hello-world server in config v1 should have been stopped. We wait a bit since it's closed asynchronously.
  560. time.Sleep(time.Millisecond * 10)
  561. // nolint: bodyclose
  562. resp, err = proxyHTTP(originProxyV1, hostname)
  563. require.Error(t, err)
  564. require.Nil(t, resp)
  565. // Apply the config with hello world server again, orchestrator should spin up another hello world server
  566. updateWithValidation(t, orchestrator, 3, configWithHelloWorld)
  567. originProxyV3, err := orchestrator.GetOriginProxy()
  568. require.NoError(t, err)
  569. require.NotEqual(t, originProxyV1, originProxyV3)
  570. // nolint: bodyclose
  571. resp, err = proxyHTTP(originProxyV3, hostname)
  572. require.NoError(t, err)
  573. require.Equal(t, http.StatusOK, resp.StatusCode)
  574. // cancel the context should terminate the last proxy
  575. cancel()
  576. // Wait for proxies to shutdown
  577. time.Sleep(time.Millisecond * 10)
  578. // nolint: bodyclose
  579. resp, err = proxyHTTP(originProxyV3, hostname)
  580. require.Error(t, err)
  581. require.Nil(t, resp)
  582. }
  583. // TestPersistentConnection makes sure updating the ingress doesn't intefere with existing connections
  584. func TestPersistentConnection(t *testing.T) {
  585. const (
  586. hostname = "http://ws.tunnel.org"
  587. )
  588. msg := t.Name()
  589. originDialer := ingress.NewOriginDialer(ingress.OriginConfig{
  590. DefaultDialer: testDefaultDialer,
  591. TCPWriteTimeout: 1 * time.Second,
  592. }, &testLogger)
  593. initConfig := &Config{
  594. Ingress: &ingress.Ingress{},
  595. OriginDialerService: originDialer,
  596. }
  597. orchestrator, err := NewOrchestrator(t.Context(), initConfig, testTags, []ingress.Rule{}, &testLogger)
  598. require.NoError(t, err)
  599. wsOrigin := httptest.NewServer(http.HandlerFunc(wsEcho))
  600. defer wsOrigin.Close()
  601. tcpOrigin, err := net.Listen("tcp", "127.0.0.1:0")
  602. require.NoError(t, err)
  603. defer tcpOrigin.Close()
  604. configWithWSAndWarp := []byte(fmt.Sprintf(`
  605. {
  606. "ingress": [
  607. {
  608. "service": "%s"
  609. }
  610. ],
  611. "warp-routing": {
  612. }
  613. }
  614. `, wsOrigin.URL))
  615. updateWithValidation(t, orchestrator, 1, configWithWSAndWarp)
  616. originProxy, err := orchestrator.GetOriginProxy()
  617. require.NoError(t, err)
  618. wsReqReader, wsReqWriter := io.Pipe()
  619. wsRespReadWriter := newRespReadWriteFlusher()
  620. tcpReqReader, tcpReqWriter := io.Pipe()
  621. tcpRespReadWriter := newRespReadWriteFlusher()
  622. ctx, cancel := context.WithCancel(t.Context())
  623. defer cancel()
  624. var wg sync.WaitGroup
  625. wg.Add(3)
  626. // Start TCP origin
  627. go func() {
  628. defer wg.Done()
  629. conn, err := tcpOrigin.Accept()
  630. assert.NoError(t, err)
  631. defer conn.Close()
  632. // Expect 3 TCP messages
  633. for i := 0; i < 3; i++ {
  634. echoTCP(t, conn)
  635. }
  636. }()
  637. // Simulate cloudflared receiving a TCP connection
  638. go func() {
  639. defer wg.Done()
  640. assert.NoError(t, proxyTCP(ctx, originProxy, tcpOrigin.Addr().String(), tcpRespReadWriter, tcpReqReader))
  641. }()
  642. // Simulate cloudflared receiving a WS connection
  643. go func() {
  644. defer wg.Done()
  645. req, err := http.NewRequest(http.MethodGet, hostname, wsReqReader)
  646. assert.NoError(t, err)
  647. // ProxyHTTP will add Connection, Upgrade and Sec-Websocket-Version headers
  648. req.Header.Add("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
  649. log := zerolog.Nop()
  650. respWriter, err := connection.NewHTTP2RespWriter(req, wsRespReadWriter, connection.TypeWebsocket, &log)
  651. assert.NoError(t, err)
  652. err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, 0, &log), true)
  653. assert.NoError(t, err)
  654. }()
  655. // Simulate eyeball WS and TCP connections
  656. validateWsEcho(t, msg, wsReqWriter, wsRespReadWriter)
  657. tcpEyeball(t, tcpReqWriter, msg, tcpRespReadWriter)
  658. configNoWSAndWarp := []byte(`
  659. {
  660. "ingress": [
  661. {
  662. "service": "http_status:404"
  663. }
  664. ],
  665. "warp-routing": {
  666. }
  667. }
  668. `)
  669. updateWithValidation(t, orchestrator, 2, configNoWSAndWarp)
  670. // Make sure connection is still up
  671. validateWsEcho(t, msg, wsReqWriter, wsRespReadWriter)
  672. tcpEyeball(t, tcpReqWriter, msg, tcpRespReadWriter)
  673. updateWithValidation(t, orchestrator, 3, configWithWSAndWarp)
  674. // Make sure connection is still up
  675. validateWsEcho(t, msg, wsReqWriter, wsRespReadWriter)
  676. tcpEyeball(t, tcpReqWriter, msg, tcpRespReadWriter)
  677. wsReqWriter.Close()
  678. tcpReqWriter.Close()
  679. wg.Wait()
  680. }
  681. func TestSerializeLocalConfig(t *testing.T) {
  682. c := &newLocalConfig{
  683. RemoteConfig: ingress.RemoteConfig{
  684. Ingress: ingress.Ingress{},
  685. },
  686. ConfigurationFlags: map[string]string{"a": "b"},
  687. }
  688. result, err := json.Marshal(c)
  689. require.NoError(t, err)
  690. require.JSONEq(t, `{"__configuration_flags":{"a":"b"},"ingress":[],"warp-routing":{"connectTimeout":0,"tcpKeepAlive":0}}`, string(result))
  691. }
  692. func wsEcho(w http.ResponseWriter, r *http.Request) {
  693. upgrader := gows.Upgrader{}
  694. conn, err := upgrader.Upgrade(w, r, nil)
  695. if err != nil {
  696. return
  697. }
  698. defer conn.Close()
  699. for {
  700. mt, message, err := conn.ReadMessage()
  701. if err != nil {
  702. fmt.Println("read message err", err)
  703. break
  704. }
  705. err = conn.WriteMessage(mt, message)
  706. if err != nil {
  707. fmt.Println("write message err", err)
  708. break
  709. }
  710. }
  711. }
  712. func validateWsEcho(t *testing.T, msg string, reqWriter io.Writer, respReadWriter io.ReadWriter) {
  713. err := wsutil.WriteClientText(reqWriter, []byte(msg))
  714. require.NoError(t, err)
  715. receivedMsg, err := wsutil.ReadServerText(respReadWriter)
  716. require.NoError(t, err)
  717. require.Equal(t, msg, string(receivedMsg))
  718. }
  719. type respReadWriteFlusher struct {
  720. io.Reader
  721. w io.Writer
  722. headers http.Header
  723. statusCode int
  724. setStatusOnce sync.Once
  725. hasStatus chan struct{}
  726. }
  727. func newRespReadWriteFlusher() *respReadWriteFlusher {
  728. pr, pw := io.Pipe()
  729. return &respReadWriteFlusher{
  730. Reader: pr,
  731. w: pw,
  732. headers: make(http.Header),
  733. hasStatus: make(chan struct{}),
  734. }
  735. }
  736. func (rrw *respReadWriteFlusher) Write(buf []byte) (int, error) {
  737. rrw.WriteHeader(http.StatusOK)
  738. return rrw.w.Write(buf)
  739. }
  740. func (rrw *respReadWriteFlusher) Flush() {}
  741. func (rrw *respReadWriteFlusher) Header() http.Header {
  742. return rrw.headers
  743. }
  744. func (rrw *respReadWriteFlusher) WriteHeader(statusCode int) {
  745. rrw.setStatusOnce.Do(func() {
  746. rrw.statusCode = statusCode
  747. close(rrw.hasStatus)
  748. })
  749. }