Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions api/envoy/config/filter/accesslog/v2/accesslog.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message AccessLog {
}
}

// [#next-major-version: In the v3 API, we should consider renaming it to more generic filter]
// [#next-free-field: 12]
message AccessLogFilter {
oneof filter_specifier {
Expand Down
1 change: 1 addition & 0 deletions api/envoy/config/filter/accesslog/v3alpha/accesslog.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ message AccessLog {
}
}

// [#next-major-version: In the v3 API, we should consider renaming it to more generic filter]
// [#next-free-field: 12]
message AccessLogFilter {
oneof filter_specifier {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -24,7 +25,7 @@ import "validate/validate.proto";
// HTTP connection manager :ref:`configuration overview <config_http_conn_man>`.
// [#extension: envoy.filters.network.http_connection_manager]

// [#next-free-field: 36]
// [#next-free-field: 37]
message HttpConnectionManager {
enum CodecType {
// For every new connection, the connection manager will determine which
Expand Down Expand Up @@ -468,6 +469,35 @@ 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;

// 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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the format be on per mapper basis, please?
Example: we want to return different body and/or content type depending on the generated status code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 rewritten.
accesslog.v2.AccessLogFilter filter = 1 [(validate.rules).message = {required: true}];

// Rewriter defines how to rewrite determined response.
ResponseRewriter rewriter = 2 [(validate.rules).message = {required: true}];
}

// Configuration of new value for matched local response.
message ResponseRewriter {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be an ability to override header values?
For example to set content-type or some application-specific chillout header.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be added later.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "envoy/api/v3alpha/core/protocol.proto";
import "envoy/api/v3alpha/rds.proto";
import "envoy/api/v3alpha/srds.proto";
import "envoy/config/filter/accesslog/v3alpha/accesslog.proto";
import "envoy/type/v3alpha/format.proto";
import "envoy/type/v3alpha/percent.proto";

import "google/protobuf/any.proto";
Expand All @@ -24,7 +25,7 @@ import "validate/validate.proto";
// HTTP connection manager :ref:`configuration overview <config_http_conn_man>`.
// [#extension: envoy.filters.network.http_connection_manager]

// [#next-free-field: 36]
// [#next-free-field: 37]
message HttpConnectionManager {
enum CodecType {
// For every new connection, the connection manager will determine which
Expand Down Expand Up @@ -455,6 +456,35 @@ 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;

// 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.v3alpha.StringOrJson format = 2;
}

message ResponseMapper {
// Filter is used to determine if the response should be rewritten.
accesslog.v3alpha.AccessLogFilter filter = 1 [(validate.rules).message = {required: true}];

// Rewriter defines how to rewrite determined response.
ResponseRewriter rewriter = 2 [(validate.rules).message = {required: true}];
}

// Configuration of new value for matched local response.
message ResponseRewriter {
// Status code for matched response.
google.protobuf.UInt32Value status_code = 1;
}

message Rds {
Expand Down
43 changes: 43 additions & 0 deletions api/envoy/type/format.proto
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

should this field be DataSource to allow fetching body from the control plane/disk?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, it looks like string_format assumes formatting.
In this case my ask is to add 3rd option with non-formattable DataSource.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have a concrete needs for this @euroelessar?

We want an ability to return custom hard-coded user-facing html page on errors generated by envoy.
In addition to that we want to be able to serve compile-time compressed version of it for clients which support gzip/brotli.

Combination of this two factors implies that we need an ability to return potentially large binary body.
Adding this bodies to the config itself is potentially non-practical, therefore the ask for DataSource.

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.

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
43 changes: 43 additions & 0 deletions api/envoy/type/v3alpha/format.proto
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;
}
}
1 change: 1 addition & 0 deletions docs/root/api-v2/types/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Types
:glob:
:maxdepth: 2

../type/format.proto
../type/hash_policy.proto
../type/http.proto
../type/http_status.proto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ HTTP connection manager
header_casing
headers
header_sanitizing
local_reply
stats
runtime
rds
107 changes: 107 additions & 0 deletions docs/root/configuration/http/http_conn_man/local_reply.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
.. _config_http_conn_man_local_reply:

Local reply modification
========================

The :ref:`HTTP connection manager <arch_overview_http_conn_man>` supports modification of local reply which is response returned by Envoy itself.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: "which is the response"


Features:

* :ref:`Local reply content modification<config_http_conn_man_local_reply_modification>`.
* :ref:`Local reply format modification<config_http_conn_man_local_reply_format>`.

.. _config_http_conn_man_local_reply_modification:

Local reply content modification
--------------------------------

There is support for modification of local replies. You can specify list of :ref:`mappers <envoy_api_field_config.filter.network.http_connection_manager.v2.LocalReplyConfig.mapper>` which contains
pairs of :ref:`filter <envoy_api_msg_config.filter.accesslog.v2.AccessLogFilter>` and :ref:`rewriter <envoy_api_msg_config.filter.network.http_connection_manager.v2.ResponseRewriter>`. Both elements in pair has to be
Copy link
Member

Choose a reason for hiding this comment

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

Nit: "Both elements in the pair have to be specified."

specified. If more than one pair is defined then first matching is used.

Example how to change status code when local reply contains any of these response flags:

.. code-block:: yaml

mapper:
filter:
response_flag_filter:
flags:
- LH
- UH
rewriter:
status_code: 504

.. _config_http_conn_man_local_reply_format:

Local reply format modification
-------------------------------

Local reply format contains command operators that extract the relevant data and insert it.
They support two formats: :ref:`format strings <config_http_conn_man_local_reply_format_string>` and
:ref:`"format dictionaries" <config_http_conn_man_local_reply_dictionaries>`. In both cases, the :ref:`command operators <config_http_conn_man_local_reply_command_operators>`
are used to extract the relevant data, which is then inserted into the specified reply format.
Only one reply format may be specified at the time.

.. _config_http_conn_man_local_reply_format_string:

Format Strings
--------------

Format strings are plain strings, specified using the ``format`` key. They may contain
either :ref:`command operators <config_http_conn_man_local_reply_command_operators>` or other characters interpreted as a plain string.
The access log formatter does not make any assumptions about a new line separator, so one
has to specified as part of the format string.

.. code-block:: none

%RESP_BODY% %RESPONSE_CODE% %RESPONSE_FLAGS% "My custom response"

Example of custom Envoy local reply format:

.. code-block:: none

upstream connect error or disconnect/reset before headers. reset reason: connection failure 204 UH My custom response


If format isn't specified then :ref:`default format <config_http_conn_man_local_reply_default_format>` is used.

.. _config_http_conn_man_local_reply_default_format:

Default Format String
---------------------

If custom format string is not specified, Envoy uses the following default format:

.. code-block:: none

%RESP_BODY%

Example of the default local reply format:

.. code-block:: none

upstream connect error or disconnect/reset before headers. reset reason: connection failure

.. _config_http_conn_man_local_reply_dictionaries:

Format Dictionaries
-------------------

Format dictionaries are dictionaries that specify a structured local reply output format,
specified using the ``json_format`` key. This allows response to be returned in a structured format
such as JSON.

More can be found in :ref:`configuration <config_access_log_format_dictionaries>`.

.. _config_http_conn_man_local_reply_command_operators:

Command Operators
-----------------

Local reply format reuse :ref:`access log operators <config_access_log_command_operators>`, so more information can be found there.
It is also possible to use new command operator provided only for local reply modification purpose.

%RESP_BODY%
HTTP response body generated by Envoy.

2 changes: 2 additions & 0 deletions docs/root/configuration/observability/access_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Format dictionaries have the following restrictions:

* The dictionary must map strings to strings (specifically, strings to command operators). Nesting is not currently supported.

.. _config_access_log_command_operators:

Command Operators
-----------------

Expand Down
7 changes: 5 additions & 2 deletions include/envoy/access_log/access_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ class Formatter {
virtual std::string format(const Http::HeaderMap& request_headers,
const Http::HeaderMap& response_headers,
const Http::HeaderMap& response_trailers,
const StreamInfo::StreamInfo& stream_info) const PURE;
const StreamInfo::StreamInfo& stream_info,
const absl::string_view& response_body) const PURE;
};

using FormatterPtr = std::unique_ptr<Formatter>;
Expand All @@ -133,12 +134,14 @@ class FormatterProvider {
* @param response_headers supplies the response headers.
* @param response_trailers supplies the response trailers.
* @param stream_info supplies the stream info.
* @param response_body supplies the response body.
* @return std::string containing a single value extracted from the given headers/trailers/stream.
*/
virtual std::string format(const Http::HeaderMap& request_headers,
const Http::HeaderMap& response_headers,
const Http::HeaderMap& response_trailers,
const StreamInfo::StreamInfo& stream_info) const PURE;
const StreamInfo::StreamInfo& stream_info,
const absl::string_view& response_body) const PURE;
};

using FormatterProviderPtr = std::unique_ptr<FormatterProvider>;
Expand Down
Loading