-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Local reply and upstream response rewrites using the FilterManager #14926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8735d98
filter_manager: extend local reply rewrites to upstream responses
esmet e95f42a
Fix format
esmet e794d02
Add tests, add LocalReplyDataPtr, add encode_grpc_ to EncodeFunctions
esmet cc4b618
Fix up a test
esmet 7a2f5de
Fix test formatting
esmet a4e01f4
Fix test
esmet 12320a1
Add more tests around content type and content length
esmet cf285b6
Fix some issues found with other tests
esmet 6fbfb77
Formatting
esmet 0e7e0fe
Fix local reply mock
esmet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)); | ||
|
|
@@ -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(); | ||
|
|
||
| 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) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
@@ -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 | ||
|
|
@@ -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; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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-headerthat 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.There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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.