Skip to content

local_reply_config : support content-type in SubstitutionFormatString#13019

Merged
htuch merged 13 commits intoenvoyproxy:masterfrom
deveshkandpal24121990:devesh24/custom-error-page
Sep 16, 2020
Merged

local_reply_config : support content-type in SubstitutionFormatString#13019
htuch merged 13 commits intoenvoyproxy:masterfrom
deveshkandpal24121990:devesh24/custom-error-page

Conversation

@deveshkandpal24121990
Copy link
Contributor

@deveshkandpal24121990 deveshkandpal24121990 commented Sep 9, 2020

For an explanation of how to fill out the fields, please see the relevant section
in PULL_REQUESTS.md

Commit Message: local_reply_config : support content-type in SubstitutionFormatString
Additional Description: Prior to this fix only text_format and json_format were supported which would result in text/plain or application/json content-type. This Introduces content_type field which supports setting content-type for body_format / body_format_override in local_reply_config.
Risk Level: low
Testing: Unit Testing, Manual Testing
Docs Changes: Introduces new field content_type in substitution_format_string.proto that supports setting content-type for body_format / body_format_override in local_reply_config.
Release Notes: Added release notes.
[Optional Fixes #Issue] Fixes #11313

Devesh Kandpal added 2 commits September 8, 2020 21:20
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
@repokitteh-read-only
Copy link

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to api/envoy/.
CC @envoyproxy/api-watchers: FYI only for changes made to api/envoy/.

🐱

Caused by: #13019 was opened by deveshkandpal24121990.

see: more, trace.

@deveshkandpal24121990 deveshkandpal24121990 changed the title Devesh24/custom error page local_reply_config : support html format Sep 9, 2020
Devesh Kandpal added 2 commits September 8, 2020 22:34
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
@deveshkandpal24121990
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines, to retry CircleCI checks, use /retest-circle.
Cannot retry non-completed check: envoy-presubmit, please wait.

🐱

Caused by: a #13019 (comment) was created by @deveshkandpal24121990.

see: more, trace.

Copy link
Contributor

@ramaraochavali ramaraochavali left a comment

Choose a reason for hiding this comment

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

auto local = Factory::create(config_, context_);

// code=400 matches the first filter; rewrite code and body
// has its own formatter
Copy link
Contributor

Choose a reason for hiding this comment

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

nit end with .

@dio
Copy link
Member

dio commented Sep 9, 2020

Instead of this, WDYT of adding a facility to override content_type (or headers, not sure if we want to override more headers) to ResponseMapper instead?

@ramaraochavali
Copy link
Contributor

Sure. Is there any benefit of doing that rather than this? I could see one benefit i.e. not polluting SubstitutionFormatString with html_format which is not applicable for access logs? Is that what you are thinking too?

@dio
Copy link
Member

dio commented Sep 9, 2020

@ramaraochavali I think that could be a reason, and setting content-type seems one of the mentioned suggestion here: #11313 (comment). And perhaps we can stay to have "structured" via JSON and "unstructured" via text (it seems text/html falls into this, along with e.g. text/xml).

@ramaraochavali
Copy link
Contributor

Sure. I am fine with that. @deveshkandpal24121990 WDYT?

google.protobuf.Struct json_format = 2 [(validate.rules).message = {required: true}];

// Specify a format with command operators to form a html string.
// Its details is described in :ref:`format string<config_access_log_format_strings>`.
Copy link
Member

Choose a reason for hiding this comment

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

Please update these docs with a description of HTML format.

//
// .. code-block::
//
// html_format: <p>%LOCAL_REPLY_BODY%:%RESPONSE_CODE%:path=$REQ(:path)%</p>
Copy link
Member

Choose a reason for hiding this comment

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

IIUC, the only difference is that you are setting content type? Would it be preferable to be able to just specify the content-type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@htuch - do you mean adding just content-type to ResponseMapper as is suggested here -> #13019 (comment) ?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah. I'm thinking there's probably a lot of different variations on content type that might be asked for over time, so adding a special purpose field here is limiting. It makes sense to distinguish text/JSON, since they are structurally different.

@htuch htuch self-assigned this Sep 9, 2020
Devesh Kandpal added 2 commits September 14, 2020 00:16
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Copy link
Contributor

@ramaraochavali ramaraochavali left a comment

Choose a reason for hiding this comment

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

Few comments. @htuch PTAL

// * for ``json_format`` the keys with null values are omitted in the output structure.
bool omit_empty_values = 3;

// Specify a content_type for text_format. This will be ignored for json_format
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: end with .

//
// content_type: "text/html; charset=UTF-8"
//
string content_type = 4 [(validate.rules).string = {min_bytes: 1}];
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this validation since it is only valid for text_format?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also specify currently supported content type values are text/html; charset=UTF-8 and text/plain defaulted to text/plain ?

* access log: added support for :ref:`%DOWNSTREAM_PEER_FINGERPRINT_1% <config_access_log_format_response_flags>` as a response flag.
* access log: added support for nested objects in :ref:`JSON logging mode <config_access_log_format_dictionaries>`.
* access log: added :ref:`omit_empty_values<envoy_v3_api_field_config.core.v3.SubstitutionFormatString.omit_empty_values>` option to omit unset value from formatted log.
* access log: added :ref:`content_type<envoy_v3_api_field_config.core.v3.SubstitutionFormatString.content_type>` option to set content-type for textformat.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is applicable for local reply (not so for access log) - so may be we should release note it under local_reply config?


absl::string_view SubstitutionFormatStringUtils::getContentType(
const envoy::config::core::v3::SubstitutionFormatString& config) {
const std::string contentType = config.content_type();
Copy link
Contributor

Choose a reason for hiding this comment

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

Simplify this as follows as those are the only ones supported now ?

if (contentType == Http::Headers::get().ContentTypeValues.Html)
    return Http::Headers::get().ContentTypeValues.Html;
  else
    return Http::Headers::get().ContentTypeValues.Text;

Copy link
Member

Choose a reason for hiding this comment

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

Why can't we plumb through the content type directly, without having to rely on well-known values?

Devesh Kandpal added 3 commits September 14, 2020 07:53
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
@deveshkandpal24121990
Copy link
Contributor Author

addressed comments. @ramaraochavali @htuch PTAL. Thanks!

@deveshkandpal24121990 deveshkandpal24121990 changed the title local_reply_config : support html format local_reply_config : support html content-type for textformat Sep 14, 2020
Devesh Kandpal added 2 commits September 14, 2020 13:35
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
@deveshkandpal24121990
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines, to retry CircleCI checks, use /retest-circle.
Cannot retry non-completed check: envoy-presubmit (Linux multi-arch docker), please wait.

🐱

Caused by: a #13019 (comment) was created by @deveshkandpal24121990.

see: more, trace.

@deveshkandpal24121990
Copy link
Contributor Author

deveshkandpal24121990 commented Sep 14, 2020

the integration test logs complains about //test/extensions/filters/http/oauth2:oauth_integration_test failing. I ran this locally but seems to work just fine. https://dev.azure.com/cncf/4684fb3d-0389-4e0b-8251-221942316e06/_apis/build/builds/51207/logs/108

@deveshkandpal24121990
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines, to retry CircleCI checks, use /retest-circle.
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #13019 (comment) was created by @deveshkandpal24121990.

see: more, trace.

Comment on lines +31 to +33
if (config.content_type() == Http::Headers::get().ContentTypeValues.Html) {
return Http::Headers::get().ContentTypeValues.Html;
}
Copy link
Member

Choose a reason for hiding this comment

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

Not sure, but can we pass config.content_type() value as is? And yes, when it is not set and it is not JSON we return text/plain. WDYT @htuch?

Copy link
Contributor Author

@deveshkandpal24121990 deveshkandpal24121990 Sep 14, 2020

Choose a reason for hiding this comment

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

@dio - I tried doing that initially, however config.content_type() returns std:string& and after the value would get assigned to absl:string_view content_type_ , the address it points to is not valid anymore after the scope of the method is over. My understanding about string_view is that it's a read-only pointer to the string object being assigned. One way to fix that was to change the datatype of content_type_ to std::string, however I wasn't sure if that is encouraged ? Any other ideas ?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, make content_type_ a std::string.

envoy::config::core::v3::SubstitutionFormatString::FormatCase::kJsonFormat
? Http::Headers::get().ContentTypeValues.Json
: Http::Headers::get().ContentTypeValues.Text) {}
: Formatter::SubstitutionFormatStringUtils::getContentType(config)) {}
Copy link
Member

Choose a reason for hiding this comment

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

So, given: https://github.com/envoyproxy/envoy/pull/13019/files#r488273786, here we can have: config.content_type().empty() ? Http::Headers::get().ContentTypeValues.Text : config.content_type().

Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

Thanks, API LGTM modulo comments.
/wait


// Specify a content_type for text_format. This will be ignored for json_format.
// Currently supported content-type value includes ``text/html; charset=UTF-8`` and ``text/plain``
// with default value being ``text/plain``.
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 we can avoid relying on well-known values, see below comments. Other than this, this API is fine.

// * for ``json_format`` the keys with null values are omitted in the output structure.
bool omit_empty_values = 3;

// Specify a content_type for text_format. This will be ignored for json_format.
Copy link
Member

Choose a reason for hiding this comment

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

Please specify defaults when no content_type is specified.

// * for ``json_format`` the keys with null values are omitted in the output structure.
bool omit_empty_values = 3;

// Specify a content_type for text_format. This will be ignored for json_format.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: *content_type* for *text_format*

// * for ``json_format`` the keys with null values are omitted in the output structure.
bool omit_empty_values = 3;

// Specify a content_type for text_format. This will be ignored for json_format.
Copy link
Member

Choose a reason for hiding this comment

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

Why ignore for JSON? I feel there are JSON content type variants you might want to support, e.g. https://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type

@deveshkandpal24121990 deveshkandpal24121990 changed the title local_reply_config : support html content-type for textformat local_reply_config : support content-type in SubstitutionFormatString Sep 15, 2020
Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
@deveshkandpal24121990
Copy link
Contributor Author

@htuch @ramaraochavali @dio - PTAL. Thanks!

Copy link
Contributor

@ramaraochavali ramaraochavali left a comment

Choose a reason for hiding this comment

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

LGTM with minor nits

envoy::config::core::v3::SubstitutionFormatString::FormatCase::kJsonFormat
? Http::Headers::get().ContentTypeValues.Json
: Http::Headers::get().ContentTypeValues.Text) {}
content_type_(Formatter::SubstitutionFormatStringUtils::getContentType(config)) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: may be you can inline this initialization

!config.content_type().empty()? config.content_type() : config.format_case() ==
envoy::config::core::v3::SubstitutionFormatString::FormatCase::kJsonFormat
? Http::Headers::get().ContentTypeValues.Json
: Http::Headers::get().ContentTypeValues.Text;
and avoid the method?

* load balancer: added a :ref:`configuration<envoy_v3_api_msg_config.cluster.v3.Cluster.LeastRequestLbConfig>` option to specify the active request bias used by the least request load balancer.
* load balancer: added an :ref:`option <envoy_v3_api_field_config.cluster.v3.Cluster.LbSubsetConfig.LbSubsetSelector.single_host_per_subset>` to optimize subset load balancing when there is only one host per subset.
* load balancer: added support for bounded load per host for consistent hash load balancers via :ref:`hash_balance_factor <envoy_api_field_Cluster.CommonLbConfig.consistent_hashing_lb_config>`.
* local_reply config: added :ref:`content_type<envoy_v3_api_field_config.core.v3.SubstitutionFormatString.content_type>` option to set appropriate content-type for text_format or json_format.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: suggestion
added :ref:content_type<envoy_v3_api_field_config.core.v3.SubstitutionFormatString.content_type> option to set content-type.

Specifying text_format or json_format here is redundant here as we support for all formats now.

Signed-off-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
@deveshkandpal24121990
Copy link
Contributor Author

@htuch - whenever you get a chance, could you PTAL ? Thanks in advance!

Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@htuch htuch merged commit a8b946e into envoyproxy:master Sep 16, 2020
@deveshkandpal24121990 deveshkandpal24121990 deleted the devesh24/custom-error-page branch September 16, 2020 19:41
esmet pushed a commit to gravitee-io/envoy that referenced this pull request Oct 28, 2020
…envoyproxy#13019)

Prior to this fix only text_format and json_format were supported which would result in text/plain or application/json content-type. This Introduces content_type field which supports setting content-type for body_format / body_format_override in local_reply_config.

Risk Level: low
Testing: Unit Testing, Manual Testing
Docs Changes: Introduces new field content_type in substitution_format_string.proto that supports setting content-type for body_format / body_format_override in local_reply_config.
Release Notes: Added release notes.

Fixes envoyproxy#11313

Co-authored-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
(cherry picked from commit a8b946e)
LukeShu pushed a commit to gravitee-io/envoy that referenced this pull request Aug 10, 2021
…envoyproxy#13019)

Prior to this fix only text_format and json_format were supported which would result in text/plain or application/json content-type. This Introduces content_type field which supports setting content-type for body_format / body_format_override in local_reply_config.

Risk Level: low
Testing: Unit Testing, Manual Testing
Docs Changes: Introduces new field content_type in substitution_format_string.proto that supports setting content-type for body_format / body_format_override in local_reply_config.
Release Notes: Added release notes.

Fixes envoyproxy#11313

Co-authored-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
(cherry picked from commit a8b946e)
aidanhahn pushed a commit to gravitee-io/envoy that referenced this pull request Aug 12, 2021
…envoyproxy#13019)

Prior to this fix only text_format and json_format were supported which would result in text/plain or application/json content-type. This Introduces content_type field which supports setting content-type for body_format / body_format_override in local_reply_config.

Risk Level: low
Testing: Unit Testing, Manual Testing
Docs Changes: Introduces new field content_type in substitution_format_string.proto that supports setting content-type for body_format / body_format_override in local_reply_config.
Release Notes: Added release notes.

Fixes envoyproxy#11313

Co-authored-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
(cherry picked from commit a8b946e)
LukeShu pushed a commit to gravitee-io/envoy that referenced this pull request Aug 13, 2021
…envoyproxy#13019)

Prior to this fix only text_format and json_format were supported which would result in text/plain or application/json content-type. This Introduces content_type field which supports setting content-type for body_format / body_format_override in local_reply_config.

Risk Level: low
Testing: Unit Testing, Manual Testing
Docs Changes: Introduces new field content_type in substitution_format_string.proto that supports setting content-type for body_format / body_format_override in local_reply_config.
Release Notes: Added release notes.

Fixes envoyproxy#11313

Co-authored-by: Devesh Kandpal <devesh.kandpal@dkandpal-ltm.internal.salesforce.com>
(cherry picked from commit a8b946e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom error html page

4 participants