From 1b9632531a5729ed5faa694c4d45d95c5f31da40 Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Wed, 14 Jul 2021 17:13:13 +0100 Subject: [PATCH] Integration tests for all new header manip features --- agent/structs/config_entry_test.go | 12 ++-- .../config_entries.hcl | 30 +++++++++ .../case-cfg-router-features/verify.bats | 41 ++++++++++++ .../config_entries.hcl | 24 +++++++ .../case-cfg-splitter-features/verify.bats | 45 +++++++++++++ .../config_entries.hcl | 20 ++++++ .../case-ingress-gateway-http/verify.bats | 63 +++++++++++++++++++ 7 files changed, 229 insertions(+), 6 deletions(-) diff --git a/agent/structs/config_entry_test.go b/agent/structs/config_entry_test.go index 2104bf6ea5..4f08718540 100644 --- a/agent/structs/config_entry_test.go +++ b/agent/structs/config_entry_test.go @@ -465,7 +465,7 @@ func TestDecodeConfigEntry(t *testing.T) { retry_on_status_codes = [401, 209] request_headers { add { - foo = "bar" + x-foo = "bar" } set { bar = "baz" @@ -474,7 +474,7 @@ func TestDecodeConfigEntry(t *testing.T) { } response_headers { add { - foo = "bar" + x-foo = "bar" } set { bar = "baz" @@ -566,7 +566,7 @@ func TestDecodeConfigEntry(t *testing.T) { RetryOnStatusCodes = [401, 209] RequestHeaders { Add { - foo = "bar" + x-foo = "bar" } Set { bar = "baz" @@ -575,7 +575,7 @@ func TestDecodeConfigEntry(t *testing.T) { } ResponseHeaders { Add { - foo = "bar" + x-foo = "bar" } Set { bar = "baz" @@ -666,12 +666,12 @@ func TestDecodeConfigEntry(t *testing.T) { RetryOnConnectFailure: true, RetryOnStatusCodes: []uint32{401, 209}, RequestHeaders: &HTTPHeaderModifiers{ - Add: map[string]string{"foo": "bar"}, + Add: map[string]string{"x-foo": "bar"}, Set: map[string]string{"bar": "baz"}, Remove: []string{"qux"}, }, ResponseHeaders: &HTTPHeaderModifiers{ - Add: map[string]string{"foo": "bar"}, + Add: map[string]string{"x-foo": "bar"}, Set: map[string]string{"bar": "baz"}, Remove: []string{"qux"}, }, diff --git a/test/integration/connect/envoy/case-cfg-router-features/config_entries.hcl b/test/integration/connect/envoy/case-cfg-router-features/config_entries.hcl index da8b8c751d..5eaafeda96 100644 --- a/test/integration/connect/envoy/case-cfg-router-features/config_entries.hcl +++ b/test/integration/connect/envoy/case-cfg-router-features/config_entries.hcl @@ -291,6 +291,36 @@ config_entries { prefix_rewrite = "/debug" } }, + { + match { http { + path_exact = "/header-manip/debug" + } }, + destination { + service_subset = "v2" + prefix_rewrite = "/debug" + request_headers { + set { + x-foo = "request-bar" + } + remove = ["x-bad-req"] + } + } + }, + { + match { http { + path_exact = "/header-manip/echo" + } }, + destination { + service_subset = "v2" + prefix_rewrite = "/" + response_headers { + add { + x-foo = "response-bar" + } + remove = ["x-bad-resp"] + } + } + }, ] } } diff --git a/test/integration/connect/envoy/case-cfg-router-features/verify.bats b/test/integration/connect/envoy/case-cfg-router-features/verify.bats index 15457a7d0e..7af248f3ca 100644 --- a/test/integration/connect/envoy/case-cfg-router-features/verify.bats +++ b/test/integration/connect/envoy/case-cfg-router-features/verify.bats @@ -104,3 +104,44 @@ load helpers @test "test method match" { assert_expected_fortio_name s2-v2 localhost 5000 /method-match } + +@test "test request header manipulation" { + run retry_default curl -s -f \ + -H "X-Bad-Req: true" \ + "localhost:5000/header-manip/debug?env=dump" + + echo "GOT: $output" + + [ "$status" == "0" ] + + # Should have been routed to the right server + echo "$output" | grep -E "^FORTIO_NAME=s2-v2" + + # Route should have added the right request header + echo "$output" | grep -E "^X-Foo: request-bar" + + # Route should have removed the bad request header + if echo "$output" | grep -E "^X-Bad-Req: true"; then + echo "X-Bad-Req request header should have been stripped but was still present" + exit 1 + fi +} + +@test "test response header manipulation" { + # Add a response header that should be stripped by the route. + run retry_default curl -v -f -X PUT \ + "localhost:5000/header-manip/echo?header=x-bad-resp:true" + + echo "GOT: $output" + + [ "$status" == "0" ] + + # Route should have added the right response header (this is output by curl -v) + echo "$output" | grep -E "^< x-foo: response-bar" + + # Route should have removed the bad response header + if echo "$output" | grep -E "^< x-bad-resp: true"; then + echo "X-Bad-Resp response header should have been stripped but was still present" + exit 1 + fi +} diff --git a/test/integration/connect/envoy/case-cfg-splitter-features/config_entries.hcl b/test/integration/connect/envoy/case-cfg-splitter-features/config_entries.hcl index c95c2f1c85..1ea93fb5fc 100644 --- a/test/integration/connect/envoy/case-cfg-splitter-features/config_entries.hcl +++ b/test/integration/connect/envoy/case-cfg-splitter-features/config_entries.hcl @@ -31,10 +31,34 @@ config_entries { { weight = 50, service_subset = "v2" + request_headers { + set { + x-split-leg = "v2" + } + remove = ["x-bad-req"] + } + response_headers { + add { + x-svc-version = "v2" + } + remove = ["x-bad-resp"] + } }, { weight = 50, service_subset = "v1" + request_headers { + set { + x-split-leg = "v1" + } + remove = ["x-bad-req"] + } + response_headers { + add { + x-svc-version = "v1" + } + remove = ["x-bad-resp"] + } }, ] } diff --git a/test/integration/connect/envoy/case-cfg-splitter-features/verify.bats b/test/integration/connect/envoy/case-cfg-splitter-features/verify.bats index 4c6dfa4c42..2d0f2832c3 100644 --- a/test/integration/connect/envoy/case-cfg-splitter-features/verify.bats +++ b/test/integration/connect/envoy/case-cfg-splitter-features/verify.bats @@ -50,3 +50,48 @@ load helpers @test "s1 upstream should be able to connect to s2-v1 or s2-v2 via upstream s2" { assert_expected_fortio_name_pattern ^FORTIO_NAME=s2-v[12]$ } + +@test "test request header manipulation" { + run retry_default curl -s -f \ + -H "X-Bad-Req: true" \ + "localhost:5000/debug?env=dump" + + + echo "GOT: $output" + + [ "$status" == "0" ] + + # Figure out which version we hit. This will fail the test if the grep can't + # find a match while capturing the v1 or v2 from the server name in VERSION + VERSION=$(echo "$output" | grep -o -E "^FORTIO_NAME=s2-v[12]" | grep -o 'v[12]$') + + # Route should have added the right request header + GOT_HEADER=$(echo "$output" | grep -E "^X-Split-Leg: v[12]" | grep -o 'v[12]$') + + [ "$GOT_HEADER" == "$VERSION" ] + + # Route should have removed the bad request header + if echo "$output" | grep -E "^X-Bad-Req: true"; then + echo "X-Bad-Req request header should have been stripped but was still present" + exit 1 + fi +} + +@test "test response header manipulation" { + # Add a response header that should be stripped by the route. + run retry_default curl -v -f -X PUT \ + "localhost:5000/header-manip/echo?header=x-bad-resp:true" + + echo "GOT: $output" + + [ "$status" == "0" ] + + # Splitter should have added the right response header (this is output by curl -v) + echo "$output" | grep -E "^< x-svc-version: v[12]" + + # Splitter should have removed the bad response header + if echo "$output" | grep -E "^< x-bad-resp: true"; then + echo "X-Bad-Resp response header should have been stripped but was still present" + exit 1 + fi +} diff --git a/test/integration/connect/envoy/case-ingress-gateway-http/config_entries.hcl b/test/integration/connect/envoy/case-ingress-gateway-http/config_entries.hcl index c11c0ecf7a..e4128ed636 100644 --- a/test/integration/connect/envoy/case-ingress-gateway-http/config_entries.hcl +++ b/test/integration/connect/envoy/case-ingress-gateway-http/config_entries.hcl @@ -18,6 +18,26 @@ config_entries { services = [ { name = "router" + request_headers { + add { + x-foo = "bar-req" + x-existing-1 = "appended-req" + } + set { + x-existing-2 = "replaced-req" + } + remove = ["x-bad-req"] + } + response_headers { + add { + x-foo = "bar-resp" + x-existing-1 = "appended-resp" + } + set { + x-existing-2 = "replaced-resp" + } + remove = ["x-bad-resp"] + } } ] } diff --git a/test/integration/connect/envoy/case-ingress-gateway-http/verify.bats b/test/integration/connect/envoy/case-ingress-gateway-http/verify.bats index 51be6cf226..fef09b444e 100644 --- a/test/integration/connect/envoy/case-ingress-gateway-http/verify.bats +++ b/test/integration/connect/envoy/case-ingress-gateway-http/verify.bats @@ -38,3 +38,66 @@ load helpers assert_expected_fortio_name s2 router.ingress.consul 9999 /s2 } +@test "test request header manipulation" { + run retry_default curl -s -f \ + -H "Host: router.ingress.consul" \ + -H "X-Existing-1: original" \ + -H "X-Existing-2: original" \ + -H "X-Bad-Req: true" \ + "localhost:9999/s2/debug?env=dump" + + echo "GOT: $output" + + [ "$status" == "0" ] + + # Should have been routed to the right server + echo "$output" | grep -E "^FORTIO_NAME=s2" + + # Ingress should have added the new request header + echo "$output" | grep -E "^X-Foo: bar-req" + + # Ingress should have appended the first existing header - both should be + # present + echo "$output" | grep -E "^X-Existing-1: original,appended-req" + + # Ingress should have replaced the second existing header + echo "$output" | grep -E "^X-Existing-2: replaced-req" + + # Ingress should have removed the bad request header + if echo "$output" | grep -E "^X-Bad-Req: true"; then + echo "X-Bad-Req request header should have been stripped but was still present" + exit 1 + fi +} + +@test "test response header manipulation" { + # Add a response header that should be stripped by the route. + run retry_default curl -v -s -f -X PUT \ + -H "Host: router.ingress.consul" \ + "localhost:9999/s2/echo?header=x-bad-resp:true&header=x-existing-1:original&header=x-existing-2:original" + + echo "GOT: $output" + + [ "$status" == "0" ] + + # Ingress should have added the new response header + echo "$output" | grep -E "^< x-foo: bar-resp" + + # Ingress should have appended the first existing header - both should be + # present + echo "$output" | grep -E "^< x-existing-1: original" + echo "$output" | grep -E "^< x-existing-1: appended-resp" + + # Ingress should have replaced the second existing header + echo "$output" | grep -E "^< x-existing-2: replaced-resp" + if echo "$output" | grep -E "^< x-existing-2: original"; then + echo "x-existing-2 response header should have been overridden, original still present" + exit 1 + fi + + # Ingress should have removed the bad response header + if echo "$output" | grep -E "^< x-bad-resp: true"; then + echo "X-Bad-Resp response header should have been stripped but was still present" + exit 1 + fi +}