1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
|
(ns puppetlabs.trapperkeeper.services.webserver.jetty9-service-proxy-test
(:import [java.net URI])
(:require [clojure.test :refer :all]
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :refer :all]
[puppetlabs.trapperkeeper.testutils.webserver.common :refer :all]
[puppetlabs.trapperkeeper.app :refer [get-service]]
[puppetlabs.trapperkeeper.services :refer [service]]
[puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
[ring.middleware.params :as ring-params]
[schema.test :as schema-test]
[puppetlabs.trapperkeeper.testutils.webserver :as testutils]))
(use-fixtures :once
schema-test/validate-schemas
testutils/assert-clean-shutdown)
(defn query-params-handler
[req]
{:status 200
:body (str (:query-params req))})
(def app-wrapped
(ring-params/wrap-params query-params-handler))
(defn proxy-ring-handler
[req]
(condp = (:uri req)
"/hello/world" {:status 200 :body (str "Hello, World!"
((:headers req) "x-fancy-proxy-header")
((:headers req) "cookie"))}
"/hello/earth" {:status 200 :body (str "Hello, Earth!"
((:headers req) "x-fancy-proxy-header")
((:headers req) "cookie"))}
{:status 404 :body "D'oh"}))
(defn redirect-test-handler
[req]
(condp = (:uri req)
"/hello/world" {:status 200 :body "Hello, World!"}
"/hello/" {:status 302
:headers {"Location" "/hello/world"}
:body ""}
{:status 404 :body "D'oh"}))
(defn redirect-wrong-host
[req]
{:status 302
:headers {"Location" "http://fakehost:5/hello"}
:body ""})
(defn redirect-same-host
[req]
(condp = (:uri req)
"/hello/world" {:status 200 :body "Hello, World!"}
"/hello/" {:status 302
:headers {"Location" "http://localhost:9000/hello/world"}
:body ""}
{:status 404 :body "D'oh"}))
(defn redirect-different-proxy-path
[req]
(condp = (:uri req)
"/goodbye/world" {:status 200 :body "Hello, World!"}
"/hello/" {:status 302
:headers {"Location" "http://localhost:9000/goodbye/world"}
:body ""}
{:status 404 :body "D'oh"}))
(defn ring-handler-with-sleep
"Makes a ring handler which sleeps for a set amount of milliseconds before
responding. This is used to test timeout settings."
[sleep-time]
(fn [_]
(Thread/sleep sleep-time)
{:status 200
:body "This should have timed out."}))
(defprotocol TkProxyService)
(defn proxy-service
[proxy-config proxy-opts proxy-path]
(service TkProxyService
[[:WebserverService add-proxy-route]]
(init [this context]
(if proxy-opts
(add-proxy-route proxy-config
proxy-path
proxy-opts)
(add-proxy-route proxy-config
proxy-path))
context)))
(defmacro with-target-and-proxy-servers
[{:keys [target proxy proxy-config proxy-opts ring-handler
register-proxy-route-before-server-start?]} & body]
(let [proxy-path "/hello-proxy"]
`(with-app-with-config proxy-target-app#
[jetty9-service]
{:webserver ~target}
(let [target-webserver# (get-service proxy-target-app# :WebserverService)]
(add-ring-handler
target-webserver#
~ring-handler
"/hello")
(add-ring-handler
target-webserver#
~ring-handler
"/goodbye"))
(if ~register-proxy-route-before-server-start?
(let [proxy-service# (proxy-service ~proxy-config
~proxy-opts
~proxy-path)]
(with-app-with-config proxy-app#
[jetty9-service proxy-service#]
{:webserver ~proxy}
~@body))
(with-app-with-config proxy-app#
[jetty9-service]
{:webserver ~proxy}
(let [proxy-webserver# (get-service proxy-app# :WebserverService)]
(if ~proxy-opts
(add-proxy-route proxy-webserver#
~proxy-config
~proxy-path
~proxy-opts)
(add-proxy-route proxy-webserver#
~proxy-config
~proxy-path)))
~@body)))))
(def common-ssl-config
{:ssl-cert "./dev-resources/config/jetty/ssl/certs/localhost.pem"
:ssl-key "./dev-resources/config/jetty/ssl/private_keys/localhost.pem"
:ssl-ca-cert "./dev-resources/config/jetty/ssl/certs/ca.pem"})
(defn rewrite-uri-callback-fn
[target-uri req]
(URI.
(.getScheme target-uri)
nil
(.getHost target-uri)
(.getPort target-uri)
"/hello/earth"
nil nil))
(defn callback-fn
[proxy-req req]
(.header proxy-req "x-fancy-proxy-header" "!!!"))
(defn failure-callback-fn
[req resp proxy-resp failure]
(.setStatus resp 500)
(.print (.getWriter resp) (str "Proxying failed: " (.getMessage failure))))
(deftest test-basic-proxy-support
(testing "basic proxy support when proxy handler registered after server start"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "basic proxy support when proxy handler registered before server start"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:ring-handler proxy-ring-handler
:register-proxy-route-before-server-start? true}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "basic proxy support with add-proxy-route-to"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:foo {:host "0.0.0.0"
:port 10000}
:bar {:host "0.0.0.0"
:port 8085
:default-server true}}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:server-id :foo}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!"))))))
(deftest proxy-large-cookie
(testing "proxy does not explode on a large cookie when properly configured"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000
:request-header-max-size 16192}
:proxy {:host "0.0.0.0"
:port 10000
:request-header-max-size 16192}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:request-buffer-size 16192}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world"
{:headers {"Cookie" absurdly-large-cookie}
:as :text})]
(is (= (:status response) 200))
(is (= (:body response) (str "Hello, World!" absurdly-large-cookie)))))))
(deftest proxy-with-orig-scheme
(testing "basic proxy support with explicit :orig scheme"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:scheme :orig}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "basic proxy support with explicit \"orig\" scheme as string"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:scheme "orig"}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!"))))))
(deftest basic-https-proxy
(testing "basic https proxy support (pass-through https config)"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9001})
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9001/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "basic https proxy support (pass-through https config) with explicit :orig scheme"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9001})
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:proxy-opts {:scheme :orig}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9001/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "basic https proxy support (pass-through https config via explicit :use-server-config)"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9001})
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:proxy-opts {:ssl-config :use-server-config}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9001/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!"))))))
(deftest http-https-proxy-support
(testing "http->https proxy support with explicit ssl config for proxy"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9000})
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:scheme :https
:ssl-config common-ssl-config}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9000/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "http->https proxy support with scheme as string value"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9000})
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:scheme "https"
:ssl-config common-ssl-config}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9000/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!"))))))
(deftest https-http-proxy-support
(testing "https->http proxy support"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9001}
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:proxy-opts {:scheme :http}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9001/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "https->http proxy support with scheme as string"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9001}
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:proxy-opts {:scheme "http"}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9001/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!"))))))
(deftest proxy-support-with-callback
(testing "basic http proxy support with callback function"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:callback-fn callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!!!!")))))
(testing "basic https proxy support (pass-through https config) with callback function"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9001})
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:proxy-opts {:callback-fn callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9001/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!!!!")))))
(testing "http->https proxy support with explicit ssl config and callback function for proxy"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9000})
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:scheme :https
:ssl-config common-ssl-config
:callback-fn callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9000/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!!!!"))))))
(deftest proxy-with-rewrite-uri-callback
(testing "basic http proxy support with rewrite uri callback function"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:rewrite-uri-callback-fn rewrite-uri-callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:9000/hello/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, Earth!")))))
(testing "basic https proxy support (pass-through https config) with rewrite uri callback function"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9001})
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:proxy-opts {:rewrite-uri-callback-fn rewrite-uri-callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9001/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "https://localhost:10001/hello-proxy/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, Earth!")))))
(testing "http->https proxy support with explicit ssl config and rewrite uri callback function for proxy"
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9000})
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:scheme :https
:ssl-config common-ssl-config
:rewrite-uri-callback-fn rewrite-uri-callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "https://localhost:9000/hello/world" default-options-for-https-client)]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 200))
(is (= (:body response) "Hello, Earth!"))))))
(deftest proxy-with-query-params
(testing "basic proxy support with query parameters"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:ring-handler app-wrapped}
(let [response (http-get "http://localhost:9000/hello?foo=bar")]
(is (= (:status response) 200))
(is (= (:body response) (str {"foo" "bar"}))))
(let [response (http-get "http://localhost:10000/hello-proxy?foo=bar")]
(is (= (:status response) 200))
(is (= (:body response) (str {"foo" "bar"}))))))
(testing "basic proxy support with url encodable query parameters"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:ring-handler app-wrapped}
(let [response (http-get "http://localhost:9000/hello?hello%5B%5D=hello%20world")]
(is (= (:status response) 200))
(is (= (:body response) (str {"hello[]" "hello world"}))))
(let [response (http-get "http://localhost:10000/hello-proxy?hello%5B%5D=hello%20world")]
(is (= (:status response) 200))
(is (= (:body response) (str {"hello[]" "hello world"}))))))
(testing "basic proxy support with multiple query parameters"
(let [params {"foo" "bar"
"baz" "lux"
"hello" "world"}
query "?foo=bar&baz=lux&hello=world"]
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:ring-handler app-wrapped}
(let [response (http-get (str "http://localhost:9000/hello" query))]
(is (= (:status response) 200))
(is (= (read-string (:body response)) params)))
(let [response (http-get (str "http://localhost:10000/hello-proxy" query))]
(is (= (:status response) 200))
(is (= (read-string (:body response)) params)))))))
(deftest proxy-with-redirect
(testing "redirect test with proxy"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:follow-redirects true}
:ring-handler redirect-test-handler}
(let [response (http-get (str "http://localhost:9000/hello/"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:9000/hello/world"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:10000/hello-proxy/world"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "proxy redirect fails if :follow-redirects not configured properly"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:ring-handler redirect-test-handler}
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= (:status response) 404)))))
(testing "proxy-redirect to non-target host fails"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:follow-redirects true}
:ring-handler redirect-wrong-host}
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= (:status response) 502)))))
(testing "proxy redirect to correct host in fully qualified url works"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:follow-redirects true}
:ring-handler redirect-same-host}
(let [response (http-get (str "http://localhost:9000/hello/"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:9000/hello/world"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:10000/hello-proxy/world"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))))
(testing "proxy-redirect to non-proxied path on correct host succeeds"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:follow-redirects true}
:ring-handler redirect-different-proxy-path}
(let [response (http-get (str "http://localhost:9000/hello/"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!")))
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= (:status response) 200))
(is (= (:body response) "Hello, World!"))))))
(deftest proxy-failure
(testing "proxying failure - default handler"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 123456789 ; illegal port number
:path "/hello"}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 502))
(is (= (:body response) "")))))
(testing "proxying failure - custom handler"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 123456789 ; illegal port number
:path "/hello"}
:proxy-opts {:failure-callback-fn failure-callback-fn}
:ring-handler proxy-ring-handler}
(let [response (http-get "http://localhost:10000/hello-proxy/world")]
(is (= (:status response) 500))
(is (= (:body response) "Proxying failed: port out of range:123456789")))))
(testing "setting an idle timeout fails properly"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:idle-timeout 1}
:ring-handler (ring-handler-with-sleep 1250)}
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= 504 (:status response))))))
(testing "a response before a timeout occurs succeeds"
(with-target-and-proxy-servers
{:target {:host "0.0.0.0"
:port 9000}
:proxy {:host "0.0.0.0"
:port 10000}
:proxy-config {:host "localhost"
:port 9000
:path "/hello"}
:proxy-opts {:idle-timeout 1}
:ring-handler (ring-handler-with-sleep 100)}
(let [response (http-get (str "http://localhost:10000/hello-proxy"))]
(is (= 200 (:status response)))))))
; Modified from proxy-ring-handler
(defn count-ring-handler
"Increments counter if the endpoint is /goodbye"
[counter req]
(condp = (:uri req)
"/hello/world/" {:status 200 :body (str "Hello, World!")}
"/hello/" {:status 200 :body (str "Hello, You!")}
"/goodbye/" {:status 200 :body (str "Goodbye! Count: " (swap! counter inc))}
{:status 404 :body "count-ring-handler couldn't find the route"}))
(deftest test-path-traversal-attacks
(testing "proxies aren't vulnerable to basic path traversal attacks"
; goodbye-counter is used to make sure that no requests to the proxy
; endpoint, /hello-proxy, end up hitting /goodbye, as they should only be
; able to reach /hello
(let [goodbye-counter (atom 0)
hello-goodbye-count-ring-handler (partial count-ring-handler goodbye-counter)
; Should not succeed in hitting the goodbye endpoint
bad-proxy-requests [; Encodings of '../'
"https://localhost:10001/hello-proxy/../goodbye/"
"https://localhost:10001/hello-proxy/%2e%2e%2fgoodbye/"
"https://localhost:10001/hello-proxy/%2e%2e/goodbye/"
"https://localhost:10001/hello-proxy/..%2fgoodbye/"
; Encodings of '../../'
"https://localhost:10001/hello-proxy/world/../../goodbye/"
"https://localhost:10001/hello-proxy/world/%2e%2e%2f%2e%2e%2fgoodbye/"
"https://localhost:10001/hello-proxy/world/%2e%2e/%2e%2e/goodbye/"
"https://localhost:10001/hello-proxy/world/..%2f..%2fgoodbye/"]]
(with-target-and-proxy-servers
{:target (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 9001})
:proxy (merge common-ssl-config
{:ssl-host "0.0.0.0"
:ssl-port 10001})
:proxy-config {:host "localhost"
:port 9001
:path "/hello"}
:ring-handler hello-goodbye-count-ring-handler}
(testing "proxy is up and running"
(let [response (http-get "https://localhost:10001/hello-proxy/" default-options-for-https-client)]
(is (= 200 (:status response)))
(is (= "Hello, You!" (:body response))))
(let [response (http-get "https://localhost:10001/hello-proxy/world/" default-options-for-https-client)]
(is (= 200 (:status response)))
(is (= "Hello, World!" (:body response)))))
(testing "non-proxied endpoint doesn't see any traffic"
(doall (for [bad-request bad-proxy-requests]
(let [response (http-get bad-request default-options-for-https-client)]
(is (= 404 (:status response)))
; Make sure the 404 is because jetty couldn't find the page, not
; because the ring handler didn't understand the request, which
; would have a different message in the body. The request shouldn't
; get as far as the hello-goodbye-count-ring-handler
(is (re-find #"Problem accessing /hello-proxy" (:body response))))))
; Counter should still be at 0
(is (= 0 (deref goodbye-counter))))
(testing "counter is working correctly"
; A valid request to bump the counter
(http-get "https://localhost:9001/goodbye/" default-options-for-https-client)
(is (= 1 (deref goodbye-counter))))))))
|