Skip to content

Commit

Permalink
upstream HTTP filters: fix warning for upstream codec (envoyproxy#34310)
Browse files Browse the repository at this point in the history
Commit Message: Check for type not the name.

---------

Signed-off-by: Kuat Yessenov <[email protected]>
kyessenov authored Jul 15, 2024

Verified

This commit was signed with the committer’s verified signature.
fedgiac Federico Giacon
1 parent 23c5d98 commit c1123b5
Showing 8 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions source/common/config/utility.h
Original file line number Diff line number Diff line change
@@ -299,9 +299,9 @@ class Utility {
* @param typed_config for the extension config.
*/
static std::string getFactoryType(const ProtobufWkt::Any& typed_config) {
static const std::string& typed_struct_type =
static const std::string typed_struct_type =
xds::type::v3::TypedStruct::default_instance().GetTypeName();
static const std::string& legacy_typed_struct_type =
static const std::string legacy_typed_struct_type =
udpa::type::v1::TypedStruct::default_instance().GetTypeName();
// Unpack methods will only use the fully qualified type name after the last '/'.
// https://github.com/protocolbuffers/protobuf/blob/3.6.x/src/google/protobuf/any.proto#L87
18 changes: 12 additions & 6 deletions source/common/upstream/upstream_impl.cc
Original file line number Diff line number Diff line change
@@ -1298,15 +1298,21 @@ ClusterInfoImpl::ClusterInfoImpl(
if (http_protocol_options_) {
Http::FilterChainUtility::FiltersList http_filters = http_protocol_options_->http_filters_;
has_configured_http_filters_ = !http_filters.empty();
static const std::string upstream_codec_type_url =
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance()
.GetTypeName();
if (http_filters.empty()) {
auto* codec_filter = http_filters.Add();
codec_filter->set_name("envoy.filters.http.upstream_codec");
codec_filter->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance());
}
if (http_filters[http_filters.size() - 1].name() != "envoy.filters.http.upstream_codec") {
throwEnvoyExceptionOrPanic(
fmt::format("The codec filter is the only valid terminal upstream HTTP filter"));
codec_filter->mutable_typed_config()->set_type_url(upstream_codec_type_url);
} else {
const auto last_type_url =
Config::Utility::getFactoryType(http_filters[http_filters.size() - 1].typed_config());
if (last_type_url != upstream_codec_type_url) {
throwEnvoyExceptionOrPanic(fmt::format(
"The codec filter is the only valid terminal upstream HTTP filter, use '{}'",
upstream_codec_type_url));
}
}

std::string prefix = stats_scope_->symbolTable().toString(stats_scope_->prefix());
1 change: 1 addition & 0 deletions test/config/BUILD
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ envoy_cc_test_library(
"@envoy_api//envoy/config/listener/v3:pkg_cc_proto",
"@envoy_api//envoy/config/route/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/access_loggers/file/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/upstream_codec/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/transport_sockets/quic/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/transport_sockets/tls/v3:pkg_cc_proto",
6 changes: 5 additions & 1 deletion test/config/utility.cc
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
#include "envoy/config/listener/v3/listener_components.pb.h"
#include "envoy/config/route/v3/route_components.pb.h"
#include "envoy/extensions/access_loggers/file/v3/file.pb.h"
#include "envoy/extensions/filters/http/upstream_codec/v3/upstream_codec.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/extensions/transport_sockets/quic/v3/quic_transport.pb.h"
#include "envoy/extensions/transport_sockets/tls/v3/cert.pb.h"
@@ -1245,7 +1246,10 @@ void ConfigHelper::prependFilter(const std::string& config, bool downstream) {
["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]);
}
if (old_protocol_options.http_filters().empty()) {
old_protocol_options.add_http_filters()->set_name("envoy.filters.http.upstream_codec");
auto* codec_filter = old_protocol_options.add_http_filters();
codec_filter->set_name("envoy.filters.http.upstream_codec");
codec_filter->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance());
}
auto* filter_list_back = old_protocol_options.add_http_filters();
#ifdef ENVOY_ENABLE_YAML
1 change: 1 addition & 0 deletions test/extensions/filters/http/ext_proc/BUILD
Original file line number Diff line number Diff line change
@@ -188,6 +188,7 @@ envoy_extension_cc_test(
"@envoy_api//envoy/config/trace/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/ext_proc/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/set_metadata/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/upstream_codec/v3:pkg_cc_proto",
"@envoy_api//envoy/service/ext_proc/v3:pkg_cc_proto",
"@ocp//ocpdiag/core/testing:status_matchers",
],
16 changes: 13 additions & 3 deletions test/extensions/filters/http/ext_proc/ext_proc_integration_test.cc
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
#include "envoy/config/trace/v3/opentelemetry.pb.h"
#include "envoy/extensions/filters/http/ext_proc/v3/ext_proc.pb.h"
#include "envoy/extensions/filters/http/set_metadata/v3/set_metadata.pb.h"
#include "envoy/extensions/filters/http/upstream_codec/v3/upstream_codec.pb.h"
#include "envoy/network/address.h"
#include "envoy/service/ext_proc/v3/external_processor.pb.h"

@@ -3929,7 +3930,10 @@ TEST_P(ExtProcIntegrationTest, GetAndSetHeadersUpstream) {
["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]);
}
if (old_protocol_options.http_filters().empty()) {
old_protocol_options.add_http_filters()->set_name("envoy.filters.http.upstream_codec");
auto* upstream_codec = old_protocol_options.add_http_filters();
upstream_codec->set_name("envoy.filters.http.upstream_codec");
upstream_codec->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance());
}
auto* ext_proc_filter = old_protocol_options.add_http_filters();
ext_proc_filter->set_name("envoy.filters.http.ext_proc");
@@ -4355,7 +4359,10 @@ TEST_P(ExtProcIntegrationTest, GetAndSetHeadersUpstreamObservabilityMode) {
["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]);
}
if (old_protocol_options.http_filters().empty()) {
old_protocol_options.add_http_filters()->set_name("envoy.filters.http.upstream_codec");
auto* http_filter = old_protocol_options.add_http_filters();
http_filter->set_name("envoy.filters.http.upstream_codec");
http_filter->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance());
}
auto* ext_proc_filter = old_protocol_options.add_http_filters();
ext_proc_filter->set_name("envoy.filters.http.ext_proc");
@@ -4423,7 +4430,10 @@ TEST_P(ExtProcIntegrationTest, GetAndSetHeadersUpstreamObservabilityModeWithLogg
["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]);
}
if (old_protocol_options.http_filters().empty()) {
old_protocol_options.add_http_filters()->set_name("envoy.filters.http.upstream_codec");
auto* http_filter = old_protocol_options.add_http_filters();
http_filter->set_name("envoy.filters.http.upstream_codec");
http_filter->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance());
}
auto* ext_proc_filter = old_protocol_options.add_http_filters();
ext_proc_filter->set_name("envoy.filters.http.ext_proc");
1 change: 1 addition & 0 deletions test/integration/BUILD
Original file line number Diff line number Diff line change
@@ -569,6 +569,7 @@ envoy_cc_test(
"//test/test_common:test_runtime_lib",
"@envoy_api//envoy/extensions/access_loggers/file/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/router/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/upstream_codec/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
],
)
29 changes: 18 additions & 11 deletions test/integration/shadow_policy_integration_test.cc
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

#include "envoy/extensions/access_loggers/file/v3/file.pb.h"
#include "envoy/extensions/filters/http/router/v3/router.pb.h"
#include "envoy/extensions/filters/http/upstream_codec/v3/upstream_codec.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"

#include "test/integration/filters/repick_cluster_filter.h"
@@ -46,7 +47,11 @@ class ShadowPolicyIntegrationTest
(*cluster->mutable_typed_extension_protocol_options())
["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]);
protocol_options.add_http_filters()->set_name(filter_name_);
protocol_options.add_http_filters()->set_name("envoy.filters.http.upstream_codec");
auto* upstream_codec = protocol_options.add_http_filters();
upstream_codec->set_name("envoy.filters.http.upstream_codec");
upstream_codec->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::
default_instance());
(*cluster->mutable_typed_extension_protocol_options())
["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]
.PackFrom(protocol_options);
@@ -797,16 +802,18 @@ TEST_P(ShadowPolicyIntegrationTest, RequestMirrorPolicyWithCluster) {
// Test request mirroring / shadowing with upstream HTTP filters in the router.
TEST_P(ShadowPolicyIntegrationTest, RequestMirrorPolicyWithRouterUpstreamFilters) {
initialConfigSetup("cluster_1", "");
config_helper_.addConfigModifier(
[](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* router_filter_config = hcm.mutable_http_filters(hcm.http_filters_size() - 1);
envoy::extensions::filters::http::router::v3::Router router_filter;
router_filter_config->typed_config().UnpackTo(&router_filter);
router_filter.add_upstream_http_filters()->set_name("add-body-filter");
router_filter.add_upstream_http_filters()->set_name("envoy.filters.http.upstream_codec");
router_filter_config->mutable_typed_config()->PackFrom(router_filter);
});
config_helper_.addConfigModifier([](envoy::extensions::filters::network::http_connection_manager::
v3::HttpConnectionManager& hcm) -> void {
auto* router_filter_config = hcm.mutable_http_filters(hcm.http_filters_size() - 1);
envoy::extensions::filters::http::router::v3::Router router_filter;
router_filter_config->typed_config().UnpackTo(&router_filter);
router_filter.add_upstream_http_filters()->set_name("add-body-filter");
auto* upstream_codec = router_filter.add_upstream_http_filters();
upstream_codec->set_name("envoy.filters.http.upstream_codec");
upstream_codec->mutable_typed_config()->PackFrom(
envoy::extensions::filters::http::upstream_codec::v3::UpstreamCodec::default_instance());
router_filter_config->mutable_typed_config()->PackFrom(router_filter);
});
filter_name_ = "add-body-filter";
initialize();
sendRequestAndValidateResponse();

0 comments on commit c1123b5

Please sign in to comment.