From cb120059d2d19a38ab2866c63b741a3cbd6399b6 Mon Sep 17 00:00:00 2001 From: Alyssa Wilk Date: Tue, 5 Nov 2024 15:54:17 +0000 Subject: [PATCH] proto: moving a utility to the one call location Signed-off-by: Alyssa Wilk --- source/common/protobuf/utility.cc | 8 -------- source/common/protobuf/utility.h | 11 ----------- source/extensions/common/tap/extension_config_base.cc | 3 +-- source/extensions/common/tap/tap_config_base.cc | 6 +++++- tools/code_format/config.yaml | 1 + 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/source/common/protobuf/utility.cc b/source/common/protobuf/utility.cc index 31bb31530154..49ab500c4410 100644 --- a/source/common/protobuf/utility.cc +++ b/source/common/protobuf/utility.cc @@ -706,14 +706,6 @@ void MessageUtil::redact(Protobuf::Message& message) { ::Envoy::redact(&message, /* ancestor_is_sensitive = */ false); } -void MessageUtil::wireCast(const Protobuf::Message& src, Protobuf::Message& dst) { - // This should should generally succeed, but if there are malformed UTF-8 strings in a message, - // this can fail. - if (!dst.ParseFromString(src.SerializeAsString())) { - throwEnvoyExceptionOrPanic("Unable to deserialize during wireCast()"); - } -} - std::string MessageUtil::toTextProto(const Protobuf::Message& message) { #if defined(ENVOY_ENABLE_FULL_PROTOS) std::string text_format; diff --git a/source/common/protobuf/utility.h b/source/common/protobuf/utility.h index f322d890c9be..0f1b4f43ddbc 100644 --- a/source/common/protobuf/utility.h +++ b/source/common/protobuf/utility.h @@ -585,17 +585,6 @@ class MessageUtil { */ static void redact(Protobuf::Message& message); - /** - * Reinterpret a Protobuf message as another Protobuf message by converting to wire format and - * back. This only works for messages that can be effectively duck typed this way, e.g. with a - * subtype relationship modulo field name. - * - * @param src source message. - * @param dst destination message. - * @throw EnvoyException if a conversion error occurs. - */ - static void wireCast(const Protobuf::Message& src, Protobuf::Message& dst); - /** * Sanitizes a string to contain only valid UTF-8. Invalid UTF-8 characters will be replaced. If * the input string is valid UTF-8, it will be returned unmodified. diff --git a/source/extensions/common/tap/extension_config_base.cc b/source/extensions/common/tap/extension_config_base.cc index e802a31f0692..8b13b09e4e81 100644 --- a/source/extensions/common/tap/extension_config_base.cc +++ b/source/extensions/common/tap/extension_config_base.cc @@ -16,14 +16,13 @@ ExtensionConfigBase::ExtensionConfigBase( : proto_config_(proto_config), config_factory_(std::move(config_factory)), tls_slot_(tls) { tls_slot_.set([](Event::Dispatcher&) { return std::make_shared(); }); - switch (proto_config_.config_type_case()) { + switch (proto_config_.config_type_case()) case envoy::extensions::common::tap::v3::CommonExtensionConfig::ConfigTypeCase::kAdminConfig: { admin_handler_ = AdminHandler::getSingleton(admin, singleton_manager, main_thread_dispatcher); admin_handler_->registerConfig(*this, proto_config_.admin_config().config_id()); ENVOY_LOG(debug, "initializing tap extension with admin endpoint (config_id={})", proto_config_.admin_config().config_id()); break; - } case envoy::extensions::common::tap::v3::CommonExtensionConfig::ConfigTypeCase::kStaticConfig: { // Right now only one sink is supported. ASSERT(proto_config_.static_config().output_config().sinks().size() == 1); diff --git a/source/extensions/common/tap/tap_config_base.cc b/source/extensions/common/tap/tap_config_base.cc index 1b67808112b2..3bbb088133bf 100644 --- a/source/extensions/common/tap/tap_config_base.cc +++ b/source/extensions/common/tap/tap_config_base.cc @@ -137,7 +137,11 @@ TapConfigBaseImpl::TapConfigBaseImpl(const envoy::config::tap::v3::TapConfig& pr // Fallback to use the deprecated match_config field and upgrade (wire cast) it to the new // MatchPredicate which is backward compatible with the old MatchPredicate originally // introduced in the Tap filter. - MessageUtil::wireCast(proto_config.match_config(), match); + if (!match.ParseFromString(proto_config.match_config().SerializeAsString())) { + // This should should generally succeed, but if there are malformed UTF-8 strings in a + // message, this can fail. + throw EnvoyException("Unable to deserialize during wireCast()"); + } } else { throw EnvoyException(fmt::format("Neither match nor match_config is set in TapConfig: {}", proto_config.DebugString())); diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index d0a366fbf830..e3ab19b7da34 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -330,6 +330,7 @@ paths: - test/common/grpc/codec_fuzz_test.cc - test/common/grpc/codec_test.cc - test/common/protobuf/utility_test.cc + - source/extensions/common/tap/tap_config_base.cc - test/extensions/bootstrap/wasm/test_data/speed_cpp.cc - test/extensions/filters/common/expr/context_test.cc - test/extensions/filters/http/common/fuzz/uber_filter.h