Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions source/common/http/filter_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,16 @@ void FilterManager::sendLocalReplyViaFilterChain(
absl::string_view& content_type) -> void {
// TODO(snowp): This &get() business isn't nice, rework LocalReply and others to accept
// opt refs.
ENVOY_STREAM_LOG(
trace, "sendLocalReply local_reply_.rewrite code={}, body={}, content_type={}",
*this, code, body, content_type);
local_reply_.rewrite(filter_manager_callbacks_.requestHeaders().ptr(), response_headers,
stream_info_, code, body, content_type);
// Note that we did a local reply rewrite so that we don't try to do it again in
// encodeHeaders. This isn't very clean but we're trying to support local reply rewrites
// as well as upstream rewrites, where the match + rewrite logic makes the most sense in
// encodeHeaders/Data.
did_rewrite_ = true;
},
[this, modify_headers](ResponseHeaderMapPtr&& headers, bool end_stream) -> void {
filter_manager_callbacks_.setResponseHeaders(std::move(headers));
Expand Down Expand Up @@ -1044,9 +1052,45 @@ void FilterManager::encodeHeaders(ActiveStreamEncoderFilter* filter, ResponseHea
}
}

const bool modified_end_stream = (end_stream && continue_data_entry == encoder_filters_.end());
// See if the response would be written by local_reply_.
if (!did_rewrite_) {
do_rewrite_ =
local_reply_.match(filter_manager_callbacks_.requestHeaders().ptr(),
*filter_manager_callbacks_.responseHeaders(),
filter_manager_callbacks_.responseTrailers().ptr(), stream_info_);
}

bool modified_end_stream = (end_stream && continue_data_entry == encoder_filters_.end());
const bool original_modified_end_stream = modified_end_stream;
ENVOY_STREAM_LOG(
debug, "FilterManager::encodeHeaders: end_stream={}, modified_end_stream={}, do_rewrite_={}",
*this, end_stream, modified_end_stream, do_rewrite_);
if (do_rewrite_) {
// _This_ actually sets buffered_response_data_ internally.
rewriteResponse();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point in the code, we've already run through the filter chain.

Question: should we do response rewriting before or after the filter chain? If we do it before, then the filter chain has an opportunity to see the rewritten response and make its own changes. If we do it after, then we give response rewriting the final say and the highest precedence.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, if we can tell ahead of time we're doing a rewrite, I think we want to let the filters see the reply that is going to the client. If they do their own stats tracking of say "how many 500s happened" I think they want to see what will be written.
@snowp @lizan can I get a non-googly take on this question?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One interesting case to consider is when a filter itself returns a status (or sets a header) that would match a rewrite rule. For example, if ext authz returned 403, the user may want the same 403 rewrite rule to apply as it would have if an upstream service had returned it. Same story might reply to a sentinel header like x-my-custom-failure-header that could get set by your upstream services or by a custom filter. Either way the user might want the same rewrite rules to match/apply.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a tricky one. I haven't yet thought about this a huge amount, but I think in a perfect world we would do something like the following:

  1. Unconditional rewriting happens before the encoder filter chain runs.
  2. Conditional rewriting (matching) actually is attempted after each encoder filter runs. This I think is the behavior we want where it allows each filter to change something and then possibly have a transform happen before the next filter runs.

I do wonder if somehow this could be built using the new matching infra that @snowp created but I haven't looked at it in detail yet.

Let me know if it would be easier to chat about this in a short meeting or in a doc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that matched my thought, where (I think) this one qualifies as unconditional and would happen first.


if (buffered_response_data_->length() > 0) {
// If we're going to rewrite the response here, then modified_end_stream can no longer be true
// because we have a body now.
modified_end_stream = false;
}
}

state_.non_100_response_headers_encoded_ = true;
filter_manager_callbacks_.encodeHeaders(headers, modified_end_stream);

// Encode the rewritten response right away if the original `modified_end_stream` was true.
// If it wasn't, then we know a body will be encoded later, and we'll let that function
// take care of encoding the rewritten response.
if (do_rewrite_ && original_modified_end_stream && buffered_response_data_->length() > 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the constant checking for buffered_response_data->length() > 0 would be improved if the local_reply_.rewrite interface provided a way of returning a null optional for the rewritten body, in the case where there is no configured body rewrite.

ASSERT(!did_rewrite_);
ENVOY_STREAM_LOG(trace,
"FilterManager::encodeData calling filter_manager_callbacks_ with {} bytes "
"from buffered_response_data and modified_end_stream={}",
*this, buffered_response_data_->length(), modified_end_stream);
filter_manager_callbacks_.encodeData(*buffered_response_data_, modified_end_stream);
}

maybeEndEncode(modified_end_stream);

if (!modified_end_stream) {
Expand Down Expand Up @@ -1180,7 +1224,24 @@ void FilterManager::encodeData(ActiveStreamEncoderFilter* filter, Buffer::Instan
}

const bool modified_end_stream = end_stream && trailers_added_entry == encoder_filters_.end();
filter_manager_callbacks_.encodeData(data, modified_end_stream);
if (do_rewrite_ && buffered_response_data_->length() > 0) {
// If we're doing a rewrite and modified_end_stream=true, encode the buffered_response_data_
// that was set by rewriteResponse() earlier in encodeHeaders().
if (modified_end_stream) {
ENVOY_STREAM_LOG(trace,
"FilterManager::encodeData calling filter_manager_callbacks_ with {} bytes "
"from buffered_response_data and modified_end_stream={}",
*this, buffered_response_data_->length(), modified_end_stream);
filter_manager_callbacks_.encodeData(*buffered_response_data_, modified_end_stream);
}
} else {
// We're not rewriting the response, so encode the data as is.
ENVOY_STREAM_LOG(trace,
"FilterManager::encodeData calling filter_manager_callbacks_.encodeData with "
"{} bytes and modified_end_stream={}",
*this, data.length(), modified_end_stream);
filter_manager_callbacks_.encodeData(data, modified_end_stream);
}
maybeEndEncode(modified_end_stream);

// If trailers were adding during encodeData we need to trigger decodeTrailers in order
Expand Down Expand Up @@ -1225,6 +1286,35 @@ void FilterManager::maybeEndEncode(bool end_stream) {
}
}

void FilterManager::rewriteResponse() {
ASSERT(do_rewrite_);
ASSERT(!did_rewrite_);

auto response_headers = filter_manager_callbacks_.responseHeaders();
ASSERT(response_headers.ptr() != nullptr);

std::string rewritten_body{};
absl::string_view rewritten_content_type{};
Http::Code rewritten_code{static_cast<Http::Code>(Utility::getResponseStatus(*response_headers))};

ENVOY_STREAM_LOG(trace, "rewriteResponse: calling local_reply_.rewrite with code={}", *this,
rewritten_code);
local_reply_.rewrite(filter_manager_callbacks_.requestHeaders().ptr(), *response_headers,
stream_info_, rewritten_code, rewritten_body, rewritten_content_type);
ENVOY_STREAM_LOG(
trace, "rewriteResponse: local_reply_.rewrite returned body=\"{}\", content_type={}, code={}",
*this, rewritten_body, rewritten_content_type, rewritten_code);

buffered_response_data_ = std::make_unique<Buffer::OwnedImpl>(rewritten_body);
if (!rewritten_body.empty()) {
// Since we overwrote the response body, we need to set the content-length too.
response_headers->setContentLength(buffered_response_data_->length());
}
if (!rewritten_content_type.empty()) {
response_headers->setContentType(rewritten_content_type);
}
}

bool FilterManager::processNewlyAddedMetadata() {
if (request_metadata_map_vector_ == nullptr) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions source/common/http/filter_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,11 @@ class FilterManager : public ScopeTrackedObject,
*/
void maybeEndEncode(bool end_stream);

/**
* Rewrite the response headers and body using local_reply_.
*/
void rewriteResponse();

void sendLocalReply(bool is_grpc_request, Code code, absl::string_view body,
const std::function<void(ResponseHeaderMap& headers)>& modify_headers,
const absl::optional<Grpc::Status::GrpcStatus> grpc_status,
Expand Down Expand Up @@ -1076,6 +1081,8 @@ class FilterManager : public ScopeTrackedObject,

FilterChainFactory& filter_chain_factory_;
const LocalReply::LocalReply& local_reply_;
bool do_rewrite_{};
bool did_rewrite_{};
OverridableRemoteSocketAddressSetterStreamInfo stream_info_;
// TODO(snowp): Once FM has been moved to its own file we'll make these private classes of FM,
// at which point they no longer need to be friends.
Expand Down
54 changes: 52 additions & 2 deletions source/common/local_reply/local_reply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "common/formatter/substitution_format_string.h"
#include "common/formatter/substitution_formatter.h"
#include "common/http/header_map_impl.h"
#include "common/http/utility.h"
#include "common/router/header_parser.h"

namespace Envoy {
Expand Down Expand Up @@ -80,10 +81,48 @@ class ResponseMapper {
StreamInfo::StreamInfo& stream_info, Http::Code& code, std::string& body,
BodyFormatter*& final_formatter) const {
// If not matched, just bail out.
if (!filter_->evaluate(stream_info, request_headers, response_headers, response_trailers)) {
if (!match(&request_headers, response_headers, &response_trailers, stream_info)) {
return false;
}

rewrite(request_headers, response_headers, stream_info, code, body, final_formatter);
return true;
}

// Decide if a request/response pair matches this mapper.
bool match(const Http::RequestHeaderMap* request_headers,
const Http::ResponseHeaderMap& response_headers,
const Http::ResponseTrailerMap* response_trailers,
StreamInfo::StreamInfo& stream_info) const {
// Set response code on the stream_info because it's used by the StatusCode filter.
// Further, we know that the status header present on the upstream response headers
// is the status we want to match on. It may not be the status we send downstream
// to the client, though, because we may call `rewrite` later.
//
// Under normal circumstances we should have a response status by this point, because
// either the upstream set it or the router filter set it. If for whatever reason we
// don't, skip setting the stream info's response code and just let our evaluation
// logic do without it. We can't do much better, and we certainly don't want to throw
// an exception and crash here.
if (response_headers.Status() != nullptr) {
stream_info.setResponseCode(
static_cast<uint32_t>(Http::Utility::getResponseStatus(response_headers)));
}

if (request_headers == nullptr) {
request_headers = Http::StaticEmptyHeaders::get().request_headers.get();
}

if (response_trailers == nullptr) {
response_trailers = Http::StaticEmptyHeaders::get().response_trailers.get();
}

return filter_->evaluate(stream_info, *request_headers, response_headers, *response_trailers);
}

void rewrite(const Http::RequestHeaderMap&, Http::ResponseHeaderMap& response_headers,
StreamInfo::StreamInfo& stream_info, Http::Code& code, std::string& body,
BodyFormatter*& final_formatter) const {
if (body_.has_value()) {
body = body_.value();
}
Expand All @@ -99,7 +138,6 @@ class ResponseMapper {
if (body_formatter_) {
final_formatter = body_formatter_.get();
}
return true;
}

private:
Expand Down Expand Up @@ -128,6 +166,18 @@ class LocalReplyImpl : public LocalReply {
}
}

bool match(const Http::RequestHeaderMap* request_headers,
const Http::ResponseHeaderMap& response_headers,
const Http::ResponseTrailerMap* response_trailers,
StreamInfo::StreamInfo& stream_info) const override {
for (const auto& mapper : mappers_) {
if (mapper->match(request_headers, response_headers, response_trailers, stream_info)) {
return true;
}
}
return false;
}

void rewrite(const Http::RequestHeaderMap* request_headers,
Http::ResponseHeaderMap& response_headers, StreamInfo::StreamInfo& stream_info,
Http::Code& code, std::string& body,
Expand Down
5 changes: 5 additions & 0 deletions source/common/local_reply/local_reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class LocalReply {
Http::ResponseHeaderMap& response_headers,
StreamInfo::StreamInfo& stream_info, Http::Code& code, std::string& body,
absl::string_view& content_type) const PURE;

virtual bool match(const Http::RequestHeaderMap* request_headers,
const Http::ResponseHeaderMap& response_headers,
const Http::ResponseTrailerMap* response_trailers,
StreamInfo::StreamInfo& stream_info) const PURE;
};

using LocalReplyPtr = std::unique_ptr<LocalReply>;
Expand Down
66 changes: 66 additions & 0 deletions test/integration/local_reply_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,70 @@ TEST_P(LocalReplyIntegrationTest, ShouldFormatResponseToEmptyBody) {
EXPECT_EQ(response->body(), "");
}

// Should match and rewrite an upstream response that does not contain a body.
TEST_P(LocalReplyIntegrationTest, LocalyReplyMatchUpstreamStatusHeadersOnly) {
const std::string yaml = R"EOF(
mappers:
- filter:
status_code_filter:
comparison:
op: EQ
value:
default_value: 429
runtime_key: key_b
status_code: 450
body_format:
text_format_source:
inline_string: "%RESPONSE_CODE%: %RESPONSE_CODE_DETAILS%"
)EOF";
setLocalReplyConfig(yaml);
initialize();

codec_client_ = makeHttpConnection(lookupPort("http"));

auto response = codec_client_->makeHeaderOnlyRequest(Http::TestRequestHeaderMapImpl{
{":method", "GET"}, {":path", "/"}, {":scheme", "http"}, {":authority", "host"}});
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "429"}}, true);
response->waitForHeaders();

EXPECT_EQ("450", response->headers().Status()->value().getStringView());
EXPECT_EQ(response->body(), "450: via_upstream");

cleanupUpstreamAndDownstream();
}

TEST_P(LocalReplyIntegrationTest, LocalyReplyMatchUpstreamStatusWithBody) {
const std::string yaml = R"EOF(
mappers:
- filter:
status_code_filter:
comparison:
op: EQ
value:
default_value: 429
runtime_key: key_b
status_code: 451
body_format:
text_format_source:
inline_string: "%RESPONSE_CODE%: %RESPONSE_CODE_DETAILS%"
)EOF";
setLocalReplyConfig(yaml);
initialize();

codec_client_ = makeHttpConnection(lookupPort("http"));

auto response = codec_client_->makeHeaderOnlyRequest(Http::TestRequestHeaderMapImpl{
{":method", "GET"}, {":path", "/"}, {":scheme", "http"}, {":authority", "host"}});
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "429"}}, false);
upstream_request_->encodeData(512, true);
response->waitForHeaders();

EXPECT_EQ("451", response->headers().Status()->value().getStringView());
EXPECT_EQ(response->body(), "451: via_upstream");

cleanupUpstreamAndDownstream();
}

} // namespace Envoy
7 changes: 7 additions & 0 deletions test/mocks/local_reply/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class MockLocalReply : public LocalReply {
Http::ResponseHeaderMap& response_headers, StreamInfo::StreamInfo& stream_info,
Http::Code& code, std::string& body, absl::string_view& content_type),
(const));

MOCK_METHOD(bool, match,
(const Http::RequestHeaderMap* request_headers,
const Http::ResponseHeaderMap& response_headers,
const Http::ResponseTrailerMap* response_trailers,
StreamInfo::StreamInfo& stream_info),
(const));
};
} // namespace LocalReply
} // namespace Envoy