-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Local reply mapper - implementation #8921
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
Changes from 2 commits
44762f6
2d14f71
54f3ff4
8d5bd74
525956e
2b3d7de
b71957b
84660a6
f9d6086
3e0d621
bdcaa07
be3a848
ed817a5
b4c82b6
b18f1cb
2407530
f4d9a07
613203d
7af677d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import "envoy/api/v2/core/protocol.proto"; | |
| import "envoy/api/v2/rds.proto"; | ||
| import "envoy/api/v2/srds.proto"; | ||
| import "envoy/config/filter/accesslog/v2/accesslog.proto"; | ||
| import "envoy/type/format.proto"; | ||
| import "envoy/type/percent.proto"; | ||
|
|
||
| import "google/protobuf/any.proto"; | ||
|
|
@@ -23,7 +24,7 @@ import "validate/validate.proto"; | |
| // [#protodoc-title: HTTP connection manager] | ||
| // HTTP connection manager :ref:`configuration overview <config_http_conn_man>`. | ||
|
|
||
| // [#next-free-field: 36] | ||
| // [#next-free-field: 37] | ||
| message HttpConnectionManager { | ||
| enum CodecType { | ||
| // For every new connection, the connection manager will determine which | ||
|
|
@@ -467,6 +468,36 @@ message HttpConnectionManager { | |
| // with `prefix` match set to `/dir`. Defaults to `false`. Note that slash merging is not part of | ||
| // `HTTP spec <https://tools.ietf.org/html/rfc3986>` and is provided for convenience. | ||
| bool merge_slashes = 33; | ||
|
|
||
| // [#not-implemented-hide:] | ||
|
Member
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. Please uncomment if you are going to land this in this PR. |
||
| // Configuration of local reply returned by Envoy. Allows to specify mappings and format of | ||
| // response. | ||
| LocalReplyConfig local_reply_config = 36; | ||
| } | ||
|
|
||
| message LocalReplyConfig { | ||
| // Configuration of list of mappers which allows to filter and change local response. | ||
| // The client will iterate through mappers until first match. | ||
| repeated ResponseMapper mapper = 1; | ||
|
|
||
| // Allows to define custom format of local reply. | ||
| // It is allowed to use :ref:`command operators <config_access_log_command_operators>` | ||
| // which will be resolved to actual data. | ||
| type.StringOrJson format = 2; | ||
|
Contributor
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. Can the
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. During config proposition we aggred that this might be added later. So I might add it in next PR.
Contributor
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. Have read the comments for prior version of pull request. It resolves the comment so no concerns from my side here. |
||
| } | ||
|
|
||
| message ResponseMapper { | ||
| // Filter is used to determine if the response should be changed. | ||
| accesslog.v2.AccessLogFilter filter = 1 [(validate.rules).message = {required: true}]; | ||
|
|
||
| // Rewriter defines which values in local reply should be changed. | ||
| ResponseRewriter rewriter = 2 [(validate.rules).message = {required: true}]; | ||
| } | ||
|
|
||
| // Configuration of new value for matched local response. | ||
| message ResponseRewriter { | ||
|
Contributor
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. Should there be an ability to override header values?
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. It might be added later.
Contributor
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. Agree. |
||
| // Status code for matched response. | ||
| google.protobuf.UInt32Value status_code = 1; | ||
| } | ||
|
|
||
| message Rds { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package envoy.type; | ||
|
|
||
| option java_outer_classname = "FormatProto"; | ||
| option java_multiple_files = true; | ||
| option java_package = "io.envoyproxy.envoy.type"; | ||
|
|
||
| import "google/protobuf/struct.proto"; | ||
|
|
||
| // [#protodoc-title: Format] | ||
|
|
||
| // Allows to define one of format: flat plain text or structured json. | ||
| message StringOrJson { | ||
| oneof format { | ||
| // Allows to specify flat plain text format. | ||
| // | ||
| // .. code-block:: yaml | ||
| // | ||
| // string_format: "My custom message" | ||
| // | ||
| string string_format = 1; | ||
|
Contributor
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. should this field be
Contributor
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. Actually, it looks like
Member
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 guess one reasonable question is "which API fields in general should be data sources?". Do we have a governing principle? Is it basically anything you might want to load from a disk? Do you have a concrete needs for this @euroelessar?
Contributor
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.
We want an ability to return custom hard-coded user-facing html page on errors generated by envoy. Combination of this two factors implies that we need an ability to return potentially large binary body. To clarify, I'm fine with it not being part of this particular pull request, we can contribute the necessary pieces as follow ups, assuming the API can be extended to this needs.
Member
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 it would be nice to get the API clean here, what you're asking for isn't unreasonable. The actual code to consume from data source is a 1 liner, so if folks are in agreement let's do this. |
||
|
|
||
| // Allows to specify structured data as json. | ||
| // | ||
| // .. code-block:: yaml | ||
| // | ||
| // json_format: | ||
| // protocol: "HTTP/1.1" | ||
| // message: "My message" | ||
| // | ||
| // The following JSON object would be created: | ||
| // | ||
| // .. code-block:: json | ||
| // | ||
| // { | ||
| // "protocol": "HTTP/1.1", | ||
| // "message": "My message" | ||
| // } | ||
| // | ||
| google.protobuf.Struct json_format = 2; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package envoy.type.v3alpha; | ||
|
|
||
| option java_outer_classname = "FormatProto"; | ||
| option java_multiple_files = true; | ||
| option java_package = "io.envoyproxy.envoy.type.v3alpha"; | ||
|
|
||
| import "google/protobuf/struct.proto"; | ||
|
|
||
| // [#protodoc-title: Format] | ||
|
|
||
| // Allows to define one of format: flat plain text or structured json. | ||
| message StringOrJson { | ||
| oneof format { | ||
| // Allows to specify flat plain text format. | ||
| // | ||
| // .. code-block:: yaml | ||
| // | ||
| // string_format: "My custom message" | ||
| // | ||
| string string_format = 1; | ||
|
|
||
| // Allows to specify structured data as json. | ||
| // | ||
| // .. code-block:: yaml | ||
| // | ||
| // json_format: | ||
| // protocol: "HTTP/1.1" | ||
| // message: "My message" | ||
| // | ||
| // The following JSON object would be created: | ||
| // | ||
| // .. code-block:: json | ||
| // | ||
| // { | ||
| // "protocol": "HTTP/1.1", | ||
| // "message": "My message" | ||
| // } | ||
| // | ||
| google.protobuf.Struct json_format = 2; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,12 +104,14 @@ FormatterImpl::FormatterImpl(const std::string& format) { | |
| std::string FormatterImpl::format(const Http::HeaderMap& request_headers, | ||
| const Http::HeaderMap& response_headers, | ||
| const Http::HeaderMap& response_trailers, | ||
| const StreamInfo::StreamInfo& stream_info) const { | ||
| const StreamInfo::StreamInfo& stream_info, | ||
| const absl::string_view& response_body) const { | ||
| std::string log_line; | ||
| log_line.reserve(256); | ||
|
|
||
| for (const FormatterProviderPtr& provider : providers_) { | ||
| log_line += provider->format(request_headers, response_headers, response_trailers, stream_info); | ||
| log_line += provider->format(request_headers, response_headers, response_trailers, stream_info, | ||
| response_body); | ||
| } | ||
|
|
||
| return log_line; | ||
|
|
@@ -125,8 +127,10 @@ JsonFormatterImpl::JsonFormatterImpl(std::unordered_map<std::string, std::string | |
| std::string JsonFormatterImpl::format(const Http::HeaderMap& request_headers, | ||
| const Http::HeaderMap& response_headers, | ||
| const Http::HeaderMap& response_trailers, | ||
| const StreamInfo::StreamInfo& stream_info) const { | ||
| const auto output_map = toMap(request_headers, response_headers, response_trailers, stream_info); | ||
| const StreamInfo::StreamInfo& stream_info, | ||
| const absl::string_view& body) const { | ||
|
Member
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. This only applies to locally generated responses, right? We don't have to worry about the remote having a 100MB response? What if someone specifies RESP_BODY on a regular access log formatter?
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. Body is passed only for
Member
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 direct response route action also goes through |
||
| const auto output_map = | ||
| toMap(request_headers, response_headers, response_trailers, stream_info, body); | ||
|
|
||
| ProtobufWkt::Struct output_struct; | ||
| for (const auto& pair : output_map) { | ||
|
|
@@ -147,11 +151,12 @@ std::string JsonFormatterImpl::format(const Http::HeaderMap& request_headers, | |
|
|
||
| std::unordered_map<std::string, std::string> JsonFormatterImpl::toMap( | ||
| const Http::HeaderMap& request_headers, const Http::HeaderMap& response_headers, | ||
| const Http::HeaderMap& response_trailers, const StreamInfo::StreamInfo& stream_info) const { | ||
| const Http::HeaderMap& response_trailers, const StreamInfo::StreamInfo& stream_info, | ||
| const absl::string_view& body) const { | ||
| std::unordered_map<std::string, std::string> output; | ||
| for (const auto& pair : json_output_format_) { | ||
| output.emplace(pair.first, pair.second->format(request_headers, response_headers, | ||
| response_trailers, stream_info)); | ||
| response_trailers, stream_info, body)); | ||
| } | ||
| return output; | ||
| } | ||
|
|
@@ -271,6 +276,8 @@ std::vector<FormatterProviderPtr> AccessLogFormatParser::parse(const std::string | |
|
|
||
| formatters.emplace_back(FormatterProviderPtr{ | ||
| new ResponseTrailerFormatter(main_header, alternative_header, max_length)}); | ||
| } else if (absl::StartsWith(token, "RESP_BODY")) { | ||
| formatters.emplace_back(FormatterProviderPtr{new BodyFormatter()}); | ||
| } else if (absl::StartsWith(token, DYNAMIC_META_TOKEN)) { | ||
| std::string filter_namespace; | ||
| absl::optional<size_t> max_length; | ||
|
|
@@ -505,18 +512,27 @@ StreamInfoFormatter::StreamInfoFormatter(const std::string& field_name) { | |
|
|
||
| std::string StreamInfoFormatter::format(const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const Http::HeaderMap&, | ||
| const StreamInfo::StreamInfo& stream_info) const { | ||
| const StreamInfo::StreamInfo& stream_info, | ||
| const absl::string_view&) const { | ||
| return field_extractor_(stream_info); | ||
| } | ||
|
|
||
| PlainStringFormatter::PlainStringFormatter(const std::string& str) : str_(str) {} | ||
|
|
||
| std::string PlainStringFormatter::format(const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const Http::HeaderMap&, | ||
| const StreamInfo::StreamInfo&) const { | ||
| const Http::HeaderMap&, const StreamInfo::StreamInfo&, | ||
| const absl::string_view&) const { | ||
| return str_; | ||
| } | ||
|
|
||
| BodyFormatter::BodyFormatter() {} | ||
|
|
||
| std::string BodyFormatter::format(const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const Http::HeaderMap&, const StreamInfo::StreamInfo&, | ||
| const absl::string_view& response_body) const { | ||
| return response_body.data(); | ||
| } | ||
|
|
||
| HeaderFormatter::HeaderFormatter(const std::string& main_header, | ||
| const std::string& alternative_header, | ||
| absl::optional<size_t> max_length) | ||
|
|
@@ -550,8 +566,8 @@ ResponseHeaderFormatter::ResponseHeaderFormatter(const std::string& main_header, | |
|
|
||
| std::string ResponseHeaderFormatter::format(const Http::HeaderMap&, | ||
| const Http::HeaderMap& response_headers, | ||
| const Http::HeaderMap&, | ||
| const StreamInfo::StreamInfo&) const { | ||
| const Http::HeaderMap&, const StreamInfo::StreamInfo&, | ||
| const absl::string_view&) const { | ||
| return HeaderFormatter::format(response_headers); | ||
| } | ||
|
|
||
|
|
@@ -562,7 +578,8 @@ RequestHeaderFormatter::RequestHeaderFormatter(const std::string& main_header, | |
|
|
||
| std::string RequestHeaderFormatter::format(const Http::HeaderMap& request_headers, | ||
| const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const StreamInfo::StreamInfo&) const { | ||
| const StreamInfo::StreamInfo&, | ||
| const absl::string_view&) const { | ||
| return HeaderFormatter::format(request_headers); | ||
| } | ||
|
|
||
|
|
@@ -573,7 +590,8 @@ ResponseTrailerFormatter::ResponseTrailerFormatter(const std::string& main_heade | |
|
|
||
| std::string ResponseTrailerFormatter::format(const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const Http::HeaderMap& response_trailers, | ||
| const StreamInfo::StreamInfo&) const { | ||
| const StreamInfo::StreamInfo&, | ||
| const absl::string_view&) const { | ||
| return HeaderFormatter::format(response_trailers); | ||
| } | ||
|
|
||
|
|
@@ -615,15 +633,17 @@ DynamicMetadataFormatter::DynamicMetadataFormatter(const std::string& filter_nam | |
|
|
||
| std::string DynamicMetadataFormatter::format(const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const Http::HeaderMap&, | ||
| const StreamInfo::StreamInfo& stream_info) const { | ||
| const StreamInfo::StreamInfo& stream_info, | ||
| const absl::string_view&) const { | ||
| return MetadataFormatter::format(stream_info.dynamicMetadata()); | ||
| } | ||
|
|
||
| StartTimeFormatter::StartTimeFormatter(const std::string& format) : date_formatter_(format) {} | ||
|
|
||
| std::string StartTimeFormatter::format(const Http::HeaderMap&, const Http::HeaderMap&, | ||
| const Http::HeaderMap&, | ||
| const StreamInfo::StreamInfo& stream_info) const { | ||
| const StreamInfo::StreamInfo& stream_info, | ||
| const absl::string_view&) const { | ||
| if (date_formatter_.formatString().empty()) { | ||
| return AccessLogDateTimeFormatter::fromTime(stream_info.startTime()); | ||
| } else { | ||
|
|
||
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.
Nit: please stick to 110 character line lengths, so this shouldn't wrap.