diff --git a/source/extensions/access_loggers/wasm/config.cc b/source/extensions/access_loggers/wasm/config.cc index f9559951b0c8..30aa8d7cbeba 100644 --- a/source/extensions/access_loggers/wasm/config.cc +++ b/source/extensions/access_loggers/wasm/config.cc @@ -14,8 +14,6 @@ namespace Extensions { namespace AccessLoggers { namespace Wasm { -using Common::Wasm::PluginHandleSharedPtrThreadLocal; - AccessLog::InstanceSharedPtr WasmAccessLogFactory::createAccessLogInstance(const Protobuf::Message& proto_config, AccessLog::FilterPtr&& filter, @@ -24,31 +22,10 @@ WasmAccessLogFactory::createAccessLogInstance(const Protobuf::Message& proto_con const envoy::extensions::access_loggers::wasm::v3::WasmAccessLog&>( proto_config, context.messageValidationVisitor()); - auto plugin = std::make_shared( - config.config(), envoy::config::core::v3::TrafficDirection::UNSPECIFIED, - context.serverFactoryContext().localInfo(), nullptr /* listener_metadata */); - - auto access_log = std::make_shared(plugin, nullptr, std::move(filter)); - - auto callback = [access_log, &context, plugin](Common::Wasm::WasmHandleSharedPtr base_wasm) { - // NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call. - auto tls_slot = ThreadLocal::TypedSlot::makeUnique( - context.serverFactoryContext().threadLocal()); - tls_slot->set([base_wasm, plugin](Event::Dispatcher& dispatcher) { - return std::make_shared( - Common::Wasm::getOrCreateThreadLocalPlugin(base_wasm, plugin, dispatcher)); - }); - access_log->setTlsSlot(std::move(tls_slot)); - }; - - if (!Common::Wasm::createWasm( - plugin, context.scope().createScope(""), context.serverFactoryContext().clusterManager(), - context.initManager(), context.serverFactoryContext().mainThreadDispatcher(), - context.serverFactoryContext().api(), context.serverFactoryContext().lifecycleNotifier(), - remote_data_provider_, std::move(callback))) { - throw Common::Wasm::WasmException( - fmt::format("Unable to create Wasm access log {}", plugin->name_)); - } + auto plugin_config = std::make_unique( + config.config(), context.serverFactoryContext(), context.scope(), context.initManager(), + envoy::config::core::v3::TrafficDirection::UNSPECIFIED, /*metadata=*/nullptr, false); + auto access_log = std::make_shared(std::move(plugin_config), std::move(filter)); context.serverFactoryContext().api().customStatNamespaces().registerStatNamespace( Extensions::Common::Wasm::CustomStatNamespace); diff --git a/source/extensions/access_loggers/wasm/config.h b/source/extensions/access_loggers/wasm/config.h index ef8257a9b3be..43fbce159ca4 100644 --- a/source/extensions/access_loggers/wasm/config.h +++ b/source/extensions/access_loggers/wasm/config.h @@ -25,7 +25,6 @@ class WasmAccessLogFactory : public AccessLog::AccessLogInstanceFactory, private: absl::flat_hash_map convertJsonFormatToMap(ProtobufWkt::Struct config); - RemoteAsyncDataProviderPtr remote_data_provider_; }; } // namespace Wasm diff --git a/source/extensions/access_loggers/wasm/wasm_access_log_impl.h b/source/extensions/access_loggers/wasm/wasm_access_log_impl.h index fbfb17fa70dc..cca80a75c54a 100644 --- a/source/extensions/access_loggers/wasm/wasm_access_log_impl.h +++ b/source/extensions/access_loggers/wasm/wasm_access_log_impl.h @@ -15,10 +15,9 @@ using Envoy::Extensions::Common::Wasm::PluginSharedPtr; class WasmAccessLog : public AccessLog::Instance { public: - WasmAccessLog(const PluginSharedPtr& plugin, - ThreadLocal::TypedSlotPtr&& tls_slot, + WasmAccessLog(Extensions::Common::Wasm::PluginConfigPtr plugin_config, AccessLog::FilterPtr filter) - : plugin_(plugin), tls_slot_(std::move(tls_slot)), filter_(std::move(filter)) {} + : plugin_config_(std::move(plugin_config)), filter_(std::move(filter)) {} void log(const Formatter::HttpFormatterContext& log_context, const StreamInfo::StreamInfo& stream_info) override { @@ -28,23 +27,13 @@ class WasmAccessLog : public AccessLog::Instance { } } - if (tls_slot_ != nullptr) { - if (auto handle = tls_slot_->get()->handle(); handle != nullptr) { - if (handle->wasmHandle()) { - handle->wasmHandle()->wasm()->log(plugin_, log_context, stream_info); - } - } + if (Common::Wasm::Wasm* wasm = plugin_config_->wasm(); wasm != nullptr) { + wasm->log(plugin_config_->plugin(), log_context, stream_info); } } - void setTlsSlot(ThreadLocal::TypedSlotPtr&& tls_slot) { - ASSERT(tls_slot_ == nullptr); - tls_slot_ = std::move(tls_slot); - } - private: - PluginSharedPtr plugin_; - ThreadLocal::TypedSlotPtr tls_slot_; + Common::Wasm::PluginConfigPtr plugin_config_; AccessLog::FilterPtr filter_; }; diff --git a/source/extensions/bootstrap/wasm/config.cc b/source/extensions/bootstrap/wasm/config.cc index 4f34f7e88dab..e78023eff620 100644 --- a/source/extensions/bootstrap/wasm/config.cc +++ b/source/extensions/bootstrap/wasm/config.cc @@ -16,53 +16,16 @@ namespace Wasm { void WasmServiceExtension::onServerInitialized() { createWasm(context_); } void WasmServiceExtension::createWasm(Server::Configuration::ServerFactoryContext& context) { - auto plugin = std::make_shared( - config_.config(), envoy::config::core::v3::TrafficDirection::UNSPECIFIED, context.localInfo(), - nullptr); - - auto callback = [this, &context, plugin](Common::Wasm::WasmHandleSharedPtr base_wasm) { - if (!base_wasm) { - if (plugin->fail_open_) { - ENVOY_LOG(error, "Unable to create Wasm service {}", plugin->name_); - } else { - ENVOY_LOG(critical, "Unable to create Wasm service {}", plugin->name_); - } - return; - } - if (config_.singleton()) { - // Return a Wasm VM which will be stored as a singleton by the Server. - wasm_service_ = std::make_unique( - plugin, Common::Wasm::getOrCreateThreadLocalPlugin(base_wasm, plugin, - context.mainThreadDispatcher())); - return; - } - // Per-thread WASM VM. - // NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call. - auto tls_slot = - ThreadLocal::TypedSlot::makeUnique( - context.threadLocal()); - tls_slot->set([base_wasm, plugin](Event::Dispatcher& dispatcher) { - return std::make_shared( - Common::Wasm::getOrCreateThreadLocalPlugin(base_wasm, plugin, dispatcher)); - }); - wasm_service_ = std::make_unique(plugin, std::move(tls_slot)); - }; - - if (!Common::Wasm::createWasm(plugin, context.scope().createScope(""), context.clusterManager(), - context.initManager(), context.mainThreadDispatcher(), - context.api(), context.lifecycleNotifier(), remote_data_provider_, - std::move(callback))) { - // NB: throw if we get a synchronous configuration failures as this is how such failures are - // reported to xDS. - throw Common::Wasm::WasmException( - fmt::format("Unable to create Wasm service {}", plugin->name_)); - } + plugin_config_ = std::make_unique( + config_.config(), context, context.scope(), context.initManager(), + envoy::config::core::v3::TrafficDirection::UNSPECIFIED, /*metadata=*/nullptr, + config_.singleton()); } Server::BootstrapExtensionPtr WasmFactory::createBootstrapExtension(const Protobuf::Message& config, Server::Configuration::ServerFactoryContext& context) { - auto typed_config = + const auto& typed_config = MessageUtil::downcastAndValidate( config, context.messageValidationContext().staticValidationVisitor()); context.api().customStatNamespaces().registerStatNamespace( diff --git a/source/extensions/bootstrap/wasm/config.h b/source/extensions/bootstrap/wasm/config.h index fc24e085cc7c..e9c91690e9df 100644 --- a/source/extensions/bootstrap/wasm/config.h +++ b/source/extensions/bootstrap/wasm/config.h @@ -16,26 +16,12 @@ namespace Extensions { namespace Bootstrap { namespace Wasm { +using Common::Wasm::PluginConfig; +using Common::Wasm::PluginConfigPtr; using Common::Wasm::PluginHandleSharedPtrThreadLocal; using Envoy::Extensions::Common::Wasm::PluginHandleSharedPtr; using Envoy::Extensions::Common::Wasm::PluginSharedPtr; -class WasmService { -public: - WasmService(PluginSharedPtr plugin, PluginHandleSharedPtr singleton) - : plugin_(plugin), singleton_(std::move(singleton)) {} - WasmService(PluginSharedPtr plugin, - ThreadLocal::TypedSlotPtr&& tls_slot) - : plugin_(plugin), tls_slot_(std::move(tls_slot)) {} - -private: - PluginSharedPtr plugin_; - PluginHandleSharedPtr singleton_; - ThreadLocal::TypedSlotPtr tls_slot_; -}; - -using WasmServicePtr = std::unique_ptr; - class WasmFactory : public Server::Configuration::BootstrapExtensionFactory { public: ~WasmFactory() override = default; @@ -53,9 +39,9 @@ class WasmServiceExtension : public Server::BootstrapExtension, Logger::Loggable WasmServiceExtension(const envoy::extensions::wasm::v3::WasmService& config, Server::Configuration::ServerFactoryContext& context) : config_(config), context_(context) {} - WasmService& wasmService() { - ASSERT(wasm_service_ != nullptr); - return *wasm_service_; + PluginConfig& wasmService() { + ASSERT(plugin_config_ != nullptr); + return *plugin_config_; } void onServerInitialized() override; @@ -64,8 +50,7 @@ class WasmServiceExtension : public Server::BootstrapExtension, Logger::Loggable envoy::extensions::wasm::v3::WasmService config_; Server::Configuration::ServerFactoryContext& context_; - WasmServicePtr wasm_service_; - RemoteAsyncDataProviderPtr remote_data_provider_; + PluginConfigPtr plugin_config_; }; } // namespace Wasm diff --git a/source/extensions/common/wasm/remote_async_datasource.cc b/source/extensions/common/wasm/remote_async_datasource.cc index b3e8989e7a9a..ec51b0facb5d 100644 --- a/source/extensions/common/wasm/remote_async_datasource.cc +++ b/source/extensions/common/wasm/remote_async_datasource.cc @@ -13,12 +13,6 @@ static constexpr uint32_t RetryInitialDelayMilliseconds = 1000; static constexpr uint32_t RetryMaxDelayMilliseconds = 10 * 1000; static constexpr uint32_t RetryCount = 1; -absl::optional getPath(const envoy::config::core::v3::DataSource& source) { - return source.specifier_case() == envoy::config::core::v3::DataSource::SpecifierCase::kFilename - ? absl::make_optional(source.filename()) - : absl::nullopt; -} - RemoteAsyncDataProvider::RemoteAsyncDataProvider( Upstream::ClusterManager& cm, Init::Manager& manager, const envoy::config::core::v3::RemoteDataSource& source, Event::Dispatcher& dispatcher, diff --git a/source/extensions/common/wasm/wasm.cc b/source/extensions/common/wasm/wasm.cc index bbb45a4ac311..79092fdc5599 100644 --- a/source/extensions/common/wasm/wasm.cc +++ b/source/extensions/common/wasm/wasm.cc @@ -485,6 +485,121 @@ getOrCreateThreadLocalPlugin(const WasmHandleSharedPtr& base_wasm, const PluginS getPluginHandleFactory())); } +PluginConfig::PluginConfig(const envoy::extensions::wasm::v3::PluginConfig& config, + Server::Configuration::ServerFactoryContext& context, + Stats::Scope& scope, Init::Manager& init_manager, + envoy::config::core::v3::TrafficDirection direction, + const envoy::config::core::v3::Metadata* metadata, bool singleton) + : is_singleton_handle_(singleton) { + + ASSERT_IS_MAIN_OR_TEST_THREAD(); + + plugin_ = std::make_shared(config, direction, context.localInfo(), metadata); + + auto callback = [this, &context](WasmHandleSharedPtr base_wasm) { + if (base_wasm == nullptr) { + ENVOY_LOG(critical, "Plugin {} failed to load", plugin_->name_); + } + + if (is_singleton_handle_) { + plugin_handle_ = + getOrCreateThreadLocalPlugin(base_wasm, plugin_, context.mainThreadDispatcher()); + return; + } + auto thread_local_handle = + ThreadLocal::TypedSlot::makeUnique( + context.threadLocal()); + // NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call. + thread_local_handle->set([base_wasm, plugin = this->plugin_](Event::Dispatcher& dispatcher) { + return std::make_shared( + getOrCreateThreadLocalPlugin(base_wasm, plugin, dispatcher)); + }); + plugin_handle_ = std::move(thread_local_handle); + }; + + if (!Common::Wasm::createWasm(plugin_, scope.createScope(""), context.clusterManager(), + init_manager, context.mainThreadDispatcher(), context.api(), + context.lifecycleNotifier(), remote_data_provider_, + std::move(callback))) { + // TODO(wbpcode): use absl::Status to return error rather than throw. + throw Common::Wasm::WasmException( + fmt::format("Unable to create Wasm plugin {}", plugin_->name_)); + } +} + +// Simple helper function to get the Wasm* from a WasmHandle. +Wasm* wasmHandleWasm(WasmHandleSharedPtr& h) { return h != nullptr ? h->wasm().get() : nullptr; } + +std::shared_ptr PluginConfig::createContext() { + if (absl::holds_alternative(plugin_handle_)) { + return nullptr; + } + + if (is_singleton_handle_) { + // Use critical log because this error should not happen in production. + ENVOY_LOG(critical, "CreateContext() only works for thread local plugins."); + return nullptr; + } + + ASSERT(absl::holds_alternative(plugin_handle_)); + auto thread_local_handle = absl::get(plugin_handle_).get(); + + if (!thread_local_handle->currentThreadRegistered()) { + return nullptr; + } + auto plugin_holder = thread_local_handle->get(); + if (!plugin_holder.has_value()) { + return nullptr; + } + + if (plugin_holder->handle == nullptr) { + return nullptr; + } + + Wasm* wasm = wasmHandleWasm(plugin_holder->handle->wasmHandle()); + + if (!wasm || wasm->isFailed()) { + if (plugin_->fail_open_) { + return nullptr; // Fail open skips adding this filter to callbacks. + } else { + return std::make_shared( + nullptr, 0, + plugin_holder->handle); // Fail closed is handled by an empty Context. + } + } + return std::make_shared(wasm, plugin_holder->handle->rootContextId(), + plugin_holder->handle); +} + +Wasm* PluginConfig::wasm() { + if (absl::holds_alternative(plugin_handle_)) { + return nullptr; + } + + if (is_singleton_handle_) { + ASSERT(absl::holds_alternative(plugin_handle_)); + PluginHandleSharedPtr singleton_handle = absl::get(plugin_handle_); + return singleton_handle != nullptr ? wasmHandleWasm(singleton_handle->wasmHandle()) : nullptr; + } + + ASSERT(absl::holds_alternative(plugin_handle_)); + auto thread_local_handle = absl::get(plugin_handle_).get(); + + if (!thread_local_handle->currentThreadRegistered()) { + return nullptr; + } + auto plugin_holder = thread_local_handle->get(); + if (!plugin_holder.has_value()) { + return nullptr; + } + + if (plugin_holder->handle == nullptr) { + return nullptr; + } + + return wasmHandleWasm(plugin_holder->handle->wasmHandle()); +} + } // namespace Wasm } // namespace Common } // namespace Extensions diff --git a/source/extensions/common/wasm/wasm.h b/source/extensions/common/wasm/wasm.h index 9e2a10c1b7b4..01d630009370 100644 --- a/source/extensions/common/wasm/wasm.h +++ b/source/extensions/common/wasm/wasm.h @@ -6,6 +6,7 @@ #include #include "envoy/common/exception.h" +#include "envoy/extensions/wasm/v3/wasm.pb.h" #include "envoy/extensions/wasm/v3/wasm.pb.validate.h" #include "envoy/http/filter.h" #include "envoy/server/lifecycle_notifier.h" @@ -154,11 +155,8 @@ using PluginHandleSharedPtr = std::shared_ptr; class PluginHandleSharedPtrThreadLocal : public ThreadLocal::ThreadLocalObject { public: - PluginHandleSharedPtrThreadLocal(PluginHandleSharedPtr handle) : handle_(handle){}; - PluginHandleSharedPtr& handle() { return handle_; } - -private: - PluginHandleSharedPtr handle_; + PluginHandleSharedPtrThreadLocal(PluginHandleSharedPtr handle) : handle(std::move(handle)) {} + PluginHandleSharedPtr handle; }; using CreateWasmCallback = std::function; @@ -183,6 +181,34 @@ void clearCodeCacheForTesting(); void setTimeOffsetForCodeCacheForTesting(MonotonicTime::duration d); WasmEvent toWasmEvent(const std::shared_ptr& wasm); +class PluginConfig : Logger::Loggable { +public: + // TODO(wbpcode): the code of PluginConfig will be shared cross all Wasm extensions (loggers, + // http filters, etc.), we may extend the constructor to takes a static string view to tell + // the type of the plugin if needed. + PluginConfig(const envoy::extensions::wasm::v3::PluginConfig& config, + Server::Configuration::ServerFactoryContext& context, Stats::Scope& scope, + Init::Manager& init_manager, envoy::config::core::v3::TrafficDirection direction, + const envoy::config::core::v3::Metadata* metadata, bool singleton); + + std::shared_ptr createContext(); + Wasm* wasm(); + const PluginSharedPtr& plugin() { return plugin_; } + +private: + using SinglePluginHandle = PluginHandleSharedPtr; + using ThreadLocalPluginHandle = ThreadLocal::TypedSlotPtr; + + PluginSharedPtr plugin_; + RemoteAsyncDataProviderPtr remote_data_provider_; + const bool is_singleton_handle_{}; + + absl::variant plugin_handle_; +}; + +using PluginConfigPtr = std::unique_ptr; +using PluginConfigSharedPtr = std::shared_ptr; + } // namespace Wasm } // namespace Common } // namespace Extensions diff --git a/source/extensions/filters/http/wasm/config.h b/source/extensions/filters/http/wasm/config.h index f3b1141465f9..e6094ad77529 100644 --- a/source/extensions/filters/http/wasm/config.h +++ b/source/extensions/filters/http/wasm/config.h @@ -55,7 +55,7 @@ class WasmFilterConfig Extensions::Common::Wasm::CustomStatNamespace); auto filter_config = std::make_shared(proto_config, context); return [filter_config](Http::FilterChainFactoryCallbacks& callbacks) -> void { - auto filter = filter_config->createFilter(); + auto filter = filter_config->createContext(); if (!filter) { // Fail open return; } diff --git a/source/extensions/filters/http/wasm/wasm_filter.cc b/source/extensions/filters/http/wasm/wasm_filter.cc index ba1133766191..2f53cd148efd 100644 --- a/source/extensions/filters/http/wasm/wasm_filter.cc +++ b/source/extensions/filters/http/wasm/wasm_filter.cc @@ -6,44 +6,16 @@ namespace HttpFilters { namespace Wasm { FilterConfig::FilterConfig(const envoy::extensions::filters::http::wasm::v3::Wasm& config, - Server::Configuration::FactoryContext& context) { - auto& server = context.serverFactoryContext(); - const auto plugin = std::make_shared( - config.config(), context.listenerInfo().direction(), server.localInfo(), - &context.listenerInfo().metadata()); - createWasm(plugin, server, context.scope().createScope(""), context.initManager()); -} + Server::Configuration::FactoryContext& context) + : Extensions::Common::Wasm::PluginConfig( + config.config(), context.serverFactoryContext(), context.scope(), context.initManager(), + context.listenerInfo().direction(), &context.listenerInfo().metadata(), false) {} FilterConfig::FilterConfig(const envoy::extensions::filters::http::wasm::v3::Wasm& config, - Server::Configuration::UpstreamFactoryContext& context) { - auto& server = context.serverFactoryContext(); - const auto plugin = std::make_shared( - config.config(), envoy::config::core::v3::TrafficDirection::OUTBOUND, server.localInfo(), - nullptr); - createWasm(plugin, server, context.scope().createScope(""), context.initManager()); -} - -void FilterConfig::createWasm(PluginSharedPtr plugin, - Envoy::Server::Configuration::ServerFactoryContext& server, - const Stats::ScopeSharedPtr& scope, - Envoy::Init::Manager& init_manager) { - tls_slot_ = ThreadLocal::TypedSlot::makeUnique( - server.threadLocal()); - auto callback = [plugin, this](const Common::Wasm::WasmHandleSharedPtr& base_wasm) { - // NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call. - tls_slot_->set([base_wasm, plugin](Event::Dispatcher& dispatcher) { - return std::make_shared( - Common::Wasm::getOrCreateThreadLocalPlugin(base_wasm, plugin, dispatcher)); - }); - }; - - if (!Common::Wasm::createWasm( - plugin, scope, server.clusterManager(), init_manager, server.mainThreadDispatcher(), - server.api(), server.lifecycleNotifier(), remote_data_provider_, std::move(callback))) { - throw Common::Wasm::WasmException( - fmt::format("Unable to create Wasm HTTP filter {}", plugin->name_)); - } -} + Server::Configuration::UpstreamFactoryContext& context) + : Extensions::Common::Wasm::PluginConfig( + config.config(), context.serverFactoryContext(), context.scope(), context.initManager(), + envoy::config::core::v3::TrafficDirection::OUTBOUND, nullptr, false) {} } // namespace Wasm } // namespace HttpFilters diff --git a/source/extensions/filters/http/wasm/wasm_filter.h b/source/extensions/filters/http/wasm/wasm_filter.h index ad6a3a1b4c36..46ca1955f542 100644 --- a/source/extensions/filters/http/wasm/wasm_filter.h +++ b/source/extensions/filters/http/wasm/wasm_filter.h @@ -16,49 +16,13 @@ namespace Extensions { namespace HttpFilters { namespace Wasm { -using Envoy::Extensions::Common::Wasm::Context; -using Envoy::Extensions::Common::Wasm::PluginHandleSharedPtr; -using Envoy::Extensions::Common::Wasm::PluginHandleSharedPtrThreadLocal; -using Envoy::Extensions::Common::Wasm::PluginSharedPtr; -using Envoy::Extensions::Common::Wasm::Wasm; - -class FilterConfig : Logger::Loggable { +class FilterConfig : public Extensions::Common::Wasm::PluginConfig { public: FilterConfig(const envoy::extensions::filters::http::wasm::v3::Wasm& config, Server::Configuration::FactoryContext& context); FilterConfig(const envoy::extensions::filters::http::wasm::v3::Wasm& config, Server::Configuration::UpstreamFactoryContext& context); - - std::shared_ptr createFilter() { - Wasm* wasm = nullptr; - if (!tls_slot_->currentThreadRegistered()) { - return nullptr; - } - PluginHandleSharedPtr handle = tls_slot_->get()->handle(); - if (!handle) { - return nullptr; - } - if (handle->wasmHandle()) { - wasm = handle->wasmHandle()->wasm().get(); - } - if (!wasm || wasm->isFailed()) { - if (handle->plugin()->fail_open_) { - return nullptr; // Fail open skips adding this filter to callbacks. - } else { - return std::make_shared(nullptr, 0, - handle); // Fail closed is handled by an empty Context. - } - } - return std::make_shared(wasm, handle->rootContextId(), handle); - } - -private: - void createWasm(PluginSharedPtr plugin, - Envoy::Server::Configuration::ServerFactoryContext& server, - const Stats::ScopeSharedPtr& scope, Envoy::Init::Manager& init_manager); - ThreadLocal::TypedSlotPtr tls_slot_; - RemoteAsyncDataProviderPtr remote_data_provider_; }; using FilterConfigSharedPtr = std::shared_ptr; diff --git a/source/extensions/filters/network/wasm/config.cc b/source/extensions/filters/network/wasm/config.cc index 0fdde8db7a79..8723bef43e21 100644 --- a/source/extensions/filters/network/wasm/config.cc +++ b/source/extensions/filters/network/wasm/config.cc @@ -20,7 +20,7 @@ Network::FilterFactoryCb WasmFilterConfig::createFilterFactoryFromProtoTyped( Extensions::Common::Wasm::CustomStatNamespace); auto filter_config = std::make_shared(proto_config, context); return [filter_config](Network::FilterManager& filter_manager) -> void { - auto filter = filter_config->createFilter(); + auto filter = filter_config->createContext(); if (filter) { filter_manager.addFilter(filter); } // else fail open diff --git a/source/extensions/filters/network/wasm/wasm_filter.cc b/source/extensions/filters/network/wasm/wasm_filter.cc index 55fe126d8253..8b9772ecd947 100644 --- a/source/extensions/filters/network/wasm/wasm_filter.cc +++ b/source/extensions/filters/network/wasm/wasm_filter.cc @@ -7,29 +7,9 @@ namespace Wasm { FilterConfig::FilterConfig(const envoy::extensions::filters::network::wasm::v3::Wasm& config, Server::Configuration::FactoryContext& context) - : tls_slot_(ThreadLocal::TypedSlot::makeUnique( - context.serverFactoryContext().threadLocal())) { - const auto plugin = std::make_shared( - config.config(), context.listenerInfo().direction(), - context.serverFactoryContext().localInfo(), &context.listenerInfo().metadata()); - - auto callback = [plugin, this](Common::Wasm::WasmHandleSharedPtr base_wasm) { - // NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call. - tls_slot_->set([base_wasm, plugin](Event::Dispatcher& dispatcher) { - return std::make_shared( - Common::Wasm::getOrCreateThreadLocalPlugin(base_wasm, plugin, dispatcher)); - }); - }; - - if (!Common::Wasm::createWasm( - plugin, context.scope().createScope(""), context.serverFactoryContext().clusterManager(), - context.initManager(), context.serverFactoryContext().mainThreadDispatcher(), - context.serverFactoryContext().api(), context.serverFactoryContext().lifecycleNotifier(), - remote_data_provider_, std::move(callback))) { - throw Common::Wasm::WasmException( - fmt::format("Unable to create Wasm network filter {}", plugin->name_)); - } -} + : Extensions::Common::Wasm::PluginConfig( + config.config(), context.serverFactoryContext(), context.scope(), context.initManager(), + context.listenerInfo().direction(), &context.listenerInfo().metadata(), false) {} } // namespace Wasm } // namespace NetworkFilters diff --git a/source/extensions/filters/network/wasm/wasm_filter.h b/source/extensions/filters/network/wasm/wasm_filter.h index 521c227fcf0f..3bdd4401997a 100644 --- a/source/extensions/filters/network/wasm/wasm_filter.h +++ b/source/extensions/filters/network/wasm/wasm_filter.h @@ -16,45 +16,10 @@ namespace Extensions { namespace NetworkFilters { namespace Wasm { -using Envoy::Extensions::Common::Wasm::Context; -using Envoy::Extensions::Common::Wasm::PluginHandleSharedPtr; -using Envoy::Extensions::Common::Wasm::PluginHandleSharedPtrThreadLocal; -using Envoy::Extensions::Common::Wasm::PluginSharedPtr; -using Envoy::Extensions::Common::Wasm::Wasm; - -class FilterConfig : Logger::Loggable { +class FilterConfig : public Extensions::Common::Wasm::PluginConfig { public: FilterConfig(const envoy::extensions::filters::network::wasm::v3::Wasm& proto_config, Server::Configuration::FactoryContext& context); - - std::shared_ptr createFilter() { - Wasm* wasm = nullptr; - if (!tls_slot_->currentThreadRegistered()) { - return nullptr; - } - PluginHandleSharedPtr handle = tls_slot_->get()->handle(); - if (!handle) { - return nullptr; - } - if (handle->wasmHandle()) { - wasm = handle->wasmHandle()->wasm().get(); - } - if (!wasm || wasm->isFailed()) { - if (handle->plugin()->fail_open_) { - return nullptr; // Fail open skips adding this filter to callbacks. - } else { - return std::make_shared(nullptr, 0, - handle); // Fail closed is handled by an empty Context. - } - } - return std::make_shared(wasm, handle->rootContextId(), handle); - } - - Wasm* wasmForTest() { return tls_slot_->get()->handle()->wasmHandle()->wasm().get(); } - -private: - ThreadLocal::TypedSlotPtr tls_slot_; - RemoteAsyncDataProviderPtr remote_data_provider_; }; using FilterConfigSharedPtr = std::shared_ptr; diff --git a/source/extensions/stat_sinks/wasm/config.cc b/source/extensions/stat_sinks/wasm/config.cc index be72fb3d1861..ef629dad80aa 100644 --- a/source/extensions/stat_sinks/wasm/config.cc +++ b/source/extensions/stat_sinks/wasm/config.cc @@ -21,36 +21,14 @@ WasmSinkFactory::createStatsSink(const Protobuf::Message& proto_config, MessageUtil::downcastAndValidate( proto_config, context.messageValidationContext().staticValidationVisitor()); - auto plugin = std::make_shared( - config.config(), envoy::config::core::v3::TrafficDirection::UNSPECIFIED, context.localInfo(), - nullptr); - - auto wasm_sink = std::make_unique(plugin, nullptr); - - auto callback = [&wasm_sink, &context, plugin](Common::Wasm::WasmHandleSharedPtr base_wasm) { - if (!base_wasm) { - if (plugin->fail_open_) { - ENVOY_LOG(error, "Unable to create Wasm Stat Sink {}", plugin->name_); - } else { - ENVOY_LOG(critical, "Unable to create Wasm Stat Sink {}", plugin->name_); - } - return; - } - wasm_sink->setSingleton(Common::Wasm::getOrCreateThreadLocalPlugin( - base_wasm, plugin, context.mainThreadDispatcher())); - }; - - if (!Common::Wasm::createWasm(plugin, context.scope().createScope(""), context.clusterManager(), - context.initManager(), context.mainThreadDispatcher(), - context.api(), context.lifecycleNotifier(), remote_data_provider_, - std::move(callback))) { - throw Common::Wasm::WasmException( - fmt::format("Unable to create Wasm Stat Sink {}", plugin->name_)); - } + auto plugin_config = std::make_unique( + config.config(), context, context.scope(), context.initManager(), + envoy::config::core::v3::TrafficDirection::UNSPECIFIED, nullptr, true); context.api().customStatNamespaces().registerStatNamespace( Extensions::Common::Wasm::CustomStatNamespace); - return wasm_sink; + + return std::make_unique(std::move(plugin_config)); } ProtobufTypes::MessagePtr WasmSinkFactory::createEmptyConfigProto() { diff --git a/source/extensions/stat_sinks/wasm/wasm_stat_sink_impl.h b/source/extensions/stat_sinks/wasm/wasm_stat_sink_impl.h index d9459bb45fc8..ffa372dc997b 100644 --- a/source/extensions/stat_sinks/wasm/wasm_stat_sink_impl.h +++ b/source/extensions/stat_sinks/wasm/wasm_stat_sink_impl.h @@ -17,16 +17,13 @@ using Envoy::Extensions::Common::Wasm::PluginSharedPtr; class WasmStatSink : public Stats::Sink { public: - WasmStatSink(const PluginSharedPtr& plugin, PluginHandleSharedPtr singleton) - : plugin_(plugin), singleton_(singleton) {} + WasmStatSink(Common::Wasm::PluginConfigPtr plugin_config) + : plugin_config_(std::move(plugin_config)) {} void flush(Stats::MetricSnapshot& snapshot) override { - singleton_->wasmHandle()->wasm()->onStatsUpdate(plugin_, snapshot); - } - - void setSingleton(PluginHandleSharedPtr singleton) { - ASSERT(singleton != nullptr); - singleton_ = singleton; + if (Common::Wasm::Wasm* wasm = plugin_config_->wasm(); wasm != nullptr) { + wasm->onStatsUpdate(plugin_config_->plugin(), snapshot); + } } void onHistogramComplete(const Stats::Histogram& histogram, uint64_t value) override { @@ -35,8 +32,7 @@ class WasmStatSink : public Stats::Sink { } private: - PluginSharedPtr plugin_; - PluginHandleSharedPtr singleton_; + Common::Wasm::PluginConfigPtr plugin_config_; }; } // namespace Wasm diff --git a/test/extensions/access_loggers/wasm/config_test.cc b/test/extensions/access_loggers/wasm/config_test.cc index 4e14e1b47590..d06845105634 100644 --- a/test/extensions/access_loggers/wasm/config_test.cc +++ b/test/extensions/access_loggers/wasm/config_test.cc @@ -88,7 +88,7 @@ TEST_P(WasmAccessLogConfigTest, CreateWasmFromEmpty) { AccessLog::InstanceSharedPtr instance; EXPECT_THROW_WITH_MESSAGE( instance = factory->createAccessLogInstance(*message, std::move(filter), context_), - Common::Wasm::WasmException, "Unable to create Wasm access log "); + Common::Wasm::WasmException, "Unable to create Wasm plugin "); } TEST_P(WasmAccessLogConfigTest, CreateWasmFromWASM) { @@ -161,7 +161,7 @@ TEST_P(WasmAccessLogConfigTest, YamlLoadFromFileWasmInvalidConfig) { TestUtility::loadFromYaml(invalid_yaml, proto_config); EXPECT_THROW_WITH_MESSAGE(factory->createAccessLogInstance(proto_config, nullptr, context_), Envoy::Extensions::Common::Wasm::WasmException, - "Unable to create Wasm access log "); + "Unable to create Wasm plugin "); const std::string valid_yaml = TestEnvironment::substitute(absl::StrCat(R"EOF( config: diff --git a/test/extensions/bootstrap/wasm/config_test.cc b/test/extensions/bootstrap/wasm/config_test.cc index 717ee20bb038..d05cabd95d9f 100644 --- a/test/extensions/bootstrap/wasm/config_test.cc +++ b/test/extensions/bootstrap/wasm/config_test.cc @@ -19,8 +19,6 @@ namespace Envoy { namespace Extensions { namespace Wasm { -using Extensions::Bootstrap::Wasm::WasmServicePtr; - class WasmFactoryTest : public testing::TestWithParam> { protected: WasmFactoryTest() { @@ -101,7 +99,7 @@ TEST_P(WasmFactoryTest, MissingImport) { TestEnvironment::substitute( "{{ test_rundir }}/test/extensions/bootstrap/wasm/test_data/missing_cpp.wasm")); EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config_), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm service test"); + "Unable to create Wasm plugin test"); } TEST_P(WasmFactoryTest, UnspecifiedRuntime) { @@ -117,7 +115,7 @@ TEST_P(WasmFactoryTest, UnknownRuntime) { config_.mutable_config()->mutable_vm_config()->set_runtime("envoy.wasm.runtime.invalid"); EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config_), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm service test"); + "Unable to create Wasm plugin test"); } TEST_P(WasmFactoryTest, StartFailed) { @@ -127,7 +125,7 @@ TEST_P(WasmFactoryTest, StartFailed) { plugin_configuration); EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config_), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm service test"); + "Unable to create Wasm plugin test"); } TEST_P(WasmFactoryTest, StartFailedOpen) { @@ -138,7 +136,7 @@ TEST_P(WasmFactoryTest, StartFailedOpen) { config_.mutable_config()->set_fail_open(true); EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config_), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm service test"); + "Unable to create Wasm plugin test"); } TEST_P(WasmFactoryTest, ConfigureFailed) { @@ -147,7 +145,7 @@ TEST_P(WasmFactoryTest, ConfigureFailed) { config_.mutable_config()->mutable_configuration()->PackFrom(plugin_configuration); EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config_), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm service test"); + "Unable to create Wasm plugin test"); } } // namespace Wasm diff --git a/test/extensions/common/wasm/wasm_vm_test.cc b/test/extensions/common/wasm/wasm_vm_test.cc index 7046ad9e3309..5eb298f930cb 100644 --- a/test/extensions/common/wasm/wasm_vm_test.cc +++ b/test/extensions/common/wasm/wasm_vm_test.cc @@ -27,6 +27,17 @@ namespace Common { namespace Wasm { namespace { +TEST(EnvoyWasmVmIntegrationTest, EnvoyWasmVmIntegrationTest) { + { + EnvoyWasmVmIntegration wasm_vm_integration; + for (const auto l : {spdlog::level::trace, spdlog::level::debug, spdlog::level::info, + spdlog::level::warn, spdlog::level::err, spdlog::level::critical}) { + Logger::Registry::getLog(Logger::Id::wasm).set_level(l); + EXPECT_EQ(wasm_vm_integration.getLogLevel(), static_cast(l)); + } + } +} + class TestNullVmPlugin : public proxy_wasm::NullVmPlugin { public: TestNullVmPlugin() = default; diff --git a/test/extensions/filters/http/wasm/config_test.cc b/test/extensions/filters/http/wasm/config_test.cc index c3f819340a71..2123a78094a6 100644 --- a/test/extensions/filters/http/wasm/config_test.cc +++ b/test/extensions/filters/http/wasm/config_test.cc @@ -268,7 +268,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromFileWasmInvalidConfig) { TestUtility::loadFromYaml(invalid_yaml, proto_config); WasmFilterConfig factory; EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); const std::string valid_yaml = TestEnvironment::substitute(absl::StrCat(R"EOF( config: @@ -337,7 +337,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadInlineBadCode) { TestUtility::loadFromYaml(yaml, proto_config); WasmFilterConfig factory; EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); } TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasm) { @@ -430,7 +430,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmFailOnUncachedThenSucceed) { })); EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); EXPECT_CALL(init_watcher_, ready()); initializeContextInitManager(init_watcher_); @@ -506,10 +506,10 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmFailCachedThenSucceed) { // Case 1: fail and fetch in the background, got 503, cache failure. EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); // Fail a second time because we are in-progress. EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); async_callbacks->onSuccess( request, Http::ResponseMessagePtr{new Http::ResponseMessageImpl(Http::ResponseHeaderMapPtr{ new Http::TestResponseHeaderMapImpl{{":status", "503"}}})}); @@ -524,7 +524,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmFailCachedThenSucceed) { setupContextInitManager(init_manager2); EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); EXPECT_CALL(init_watcher2, ready()); init_manager2.initialize(init_watcher2); @@ -552,7 +552,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmFailCachedThenSucceed) { setupContextInitManager(init_manager3); EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); EXPECT_CALL(init_watcher3, ready()); init_manager3.initialize(init_watcher3); @@ -609,7 +609,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmFailCachedThenSucceed) { setupContextInitManager(init_manager5); EXPECT_THROW_WITH_MESSAGE(getFilterFactoryCb(proto_config2, factory).status().IgnoreError(), - WasmException, "Unable to create Wasm HTTP filter "); + WasmException, "Unable to create Wasm plugin "); EXPECT_CALL(init_watcher_, ready()); initializeContextInitManager(init_watcher_); @@ -967,7 +967,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteSuccessBadcodeFailOpen) { cb(filter_callback); } -TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmCreateFilter) { +TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmcreateContext) { const std::string code = TestEnvironment::readFileToStringForTest(TestEnvironment::substitute( "{{ test_rundir }}/test/extensions/filters/http/wasm/test_data/test_cpp.wasm")); const std::string sha256 = Hex::encode( @@ -1008,7 +1008,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmCreateFilter) { setupContextServerFactoryThreadLocal(threadlocal); threadlocal.registered_ = false; auto filter_config = getFilterConfig(proto_config); - EXPECT_EQ(filter_config->createFilter(), nullptr); + EXPECT_EQ(filter_config->createContext(), nullptr); EXPECT_CALL(init_watcher_, ready()); initializeContextInitManager(init_watcher_); auto response = Http::ResponseMessagePtr{new Http::ResponseMessageImpl( @@ -1017,7 +1017,7 @@ TEST_P(WasmFilterConfigTest, YamlLoadFromRemoteWasmCreateFilter) { async_callbacks->onSuccess(request, std::move(response)); EXPECT_EQ(getContextInitManagerState(), Init::Manager::State::Initialized); threadlocal.registered_ = true; - EXPECT_NE(filter_config->createFilter(), nullptr); + EXPECT_NE(filter_config->createContext(), nullptr); } TEST_P(WasmFilterConfigTest, FailedToGetThreadLocalPlugin) { @@ -1042,11 +1042,12 @@ TEST_P(WasmFilterConfigTest, FailedToGetThreadLocalPlugin) { threadlocal.registered_ = true; auto filter_config = getFilterConfig(proto_config); ASSERT_EQ(threadlocal.current_slot_, 1); - ASSERT_NE(filter_config->createFilter(), nullptr); + ASSERT_NE(filter_config->createContext(), nullptr); - // If the thread local plugin handle returns nullptr, `createFilter` should return nullptr - threadlocal.data_[0] = std::make_shared(nullptr); - EXPECT_EQ(filter_config->createFilter(), nullptr); + // If the thread local plugin handle returns nullptr, `createContext` should return nullptr + threadlocal.data_[0] = + std::make_shared(nullptr); + EXPECT_EQ(filter_config->createContext(), nullptr); } } // namespace Wasm diff --git a/test/extensions/filters/network/wasm/config_test.cc b/test/extensions/filters/network/wasm/config_test.cc index dfa5810f4fb9..d7a3a2ba5aca 100644 --- a/test/extensions/filters/network/wasm/config_test.cc +++ b/test/extensions/filters/network/wasm/config_test.cc @@ -154,7 +154,7 @@ TEST_P(WasmNetworkFilterConfigTest, YamlLoadInlineBadCode) { WasmFilterConfig factory; EXPECT_THROW_WITH_MESSAGE( factory.createFilterFactoryFromProto(proto_config, context_).IgnoreError(), - Extensions::Common::Wasm::WasmException, "Unable to create Wasm network filter test"); + Extensions::Common::Wasm::WasmException, "Unable to create Wasm plugin test"); } TEST_P(WasmNetworkFilterConfigTest, YamlLoadInlineBadCodeFailOpenNackConfig) { @@ -174,7 +174,7 @@ TEST_P(WasmNetworkFilterConfigTest, YamlLoadInlineBadCodeFailOpenNackConfig) { WasmFilterConfig factory; EXPECT_THROW_WITH_MESSAGE( factory.createFilterFactoryFromProto(proto_config, context_).IgnoreError(), - Extensions::Common::Wasm::WasmException, "Unable to create Wasm network filter test"); + Extensions::Common::Wasm::WasmException, "Unable to create Wasm plugin test"); } TEST_P(WasmNetworkFilterConfigTest, FilterConfigFailClosed) { @@ -194,8 +194,8 @@ TEST_P(WasmNetworkFilterConfigTest, FilterConfigFailClosed) { envoy::extensions::filters::network::wasm::v3::Wasm proto_config; TestUtility::loadFromYaml(yaml, proto_config); NetworkFilters::Wasm::FilterConfig filter_config(proto_config, context_); - filter_config.wasmForTest()->fail(proxy_wasm::FailState::RuntimeError, ""); - auto context = filter_config.createFilter(); + filter_config.wasm()->fail(proxy_wasm::FailState::RuntimeError, ""); + auto context = filter_config.createContext(); EXPECT_EQ(context->wasm(), nullptr); EXPECT_TRUE(context->isFailed()); } @@ -218,8 +218,8 @@ TEST_P(WasmNetworkFilterConfigTest, FilterConfigFailOpen) { envoy::extensions::filters::network::wasm::v3::Wasm proto_config; TestUtility::loadFromYaml(yaml, proto_config); NetworkFilters::Wasm::FilterConfig filter_config(proto_config, context_); - filter_config.wasmForTest()->fail(proxy_wasm::FailState::RuntimeError, ""); - EXPECT_EQ(filter_config.createFilter(), nullptr); + filter_config.wasm()->fail(proxy_wasm::FailState::RuntimeError, ""); + EXPECT_EQ(filter_config.createContext(), nullptr); } TEST_P(WasmNetworkFilterConfigTest, FilterConfigCapabilitiesUnrestrictedByDefault) { @@ -241,12 +241,12 @@ TEST_P(WasmNetworkFilterConfigTest, FilterConfigCapabilitiesUnrestrictedByDefaul envoy::extensions::filters::network::wasm::v3::Wasm proto_config; TestUtility::loadFromYaml(yaml, proto_config); NetworkFilters::Wasm::FilterConfig filter_config(proto_config, context_); - auto wasm = filter_config.wasmForTest(); + auto wasm = filter_config.wasm(); EXPECT_TRUE(wasm->capabilityAllowed("proxy_log")); EXPECT_TRUE(wasm->capabilityAllowed("proxy_on_vm_start")); EXPECT_TRUE(wasm->capabilityAllowed("proxy_http_call")); EXPECT_TRUE(wasm->capabilityAllowed("proxy_on_log")); - EXPECT_FALSE(filter_config.createFilter() == nullptr); + EXPECT_FALSE(filter_config.createContext() == nullptr); } TEST_P(WasmNetworkFilterConfigTest, FilterConfigCapabilityRestriction) { @@ -270,12 +270,12 @@ TEST_P(WasmNetworkFilterConfigTest, FilterConfigCapabilityRestriction) { envoy::extensions::filters::network::wasm::v3::Wasm proto_config; TestUtility::loadFromYaml(yaml, proto_config); NetworkFilters::Wasm::FilterConfig filter_config(proto_config, context_); - auto wasm = filter_config.wasmForTest(); + auto wasm = filter_config.wasm(); EXPECT_TRUE(wasm->capabilityAllowed("proxy_log")); EXPECT_TRUE(wasm->capabilityAllowed("proxy_on_new_connection")); EXPECT_FALSE(wasm->capabilityAllowed("proxy_http_call")); EXPECT_FALSE(wasm->capabilityAllowed("proxy_on_log")); - EXPECT_FALSE(filter_config.createFilter() == nullptr); + EXPECT_FALSE(filter_config.createContext() == nullptr); } TEST_P(WasmNetworkFilterConfigTest, FilterConfigAllowOnVmStart) { @@ -336,7 +336,7 @@ TEST_P(WasmNetworkFilterConfigTest, YamlLoadFromFileWasmInvalidConfig) { WasmFilterConfig factory; EXPECT_THROW_WITH_MESSAGE( factory.createFilterFactoryFromProto(proto_config, context_).IgnoreError(), - Envoy::Extensions::Common::Wasm::WasmException, "Unable to create Wasm network filter "); + Envoy::Extensions::Common::Wasm::WasmException, "Unable to create Wasm plugin "); const std::string valid_yaml = TestEnvironment::substitute(absl::StrCat(R"EOF( config: @@ -409,7 +409,7 @@ TEST_P(WasmNetworkFilterConfigTest, YamlLoadFromRemoteWasmCreateFilter) { .WillRepeatedly(ReturnRef(threadlocal)); threadlocal.registered_ = false; auto filter_config = std::make_unique(proto_config, context_); - EXPECT_EQ(filter_config->createFilter(), nullptr); + EXPECT_EQ(filter_config->createContext(), nullptr); EXPECT_CALL(init_watcher_, ready()); context_.initManager().initialize(init_watcher_); auto response = Http::ResponseMessagePtr{new Http::ResponseMessageImpl( @@ -418,7 +418,7 @@ TEST_P(WasmNetworkFilterConfigTest, YamlLoadFromRemoteWasmCreateFilter) { async_callbacks->onSuccess(request, std::move(response)); EXPECT_EQ(context_.initManager().state(), Init::Manager::State::Initialized); threadlocal.registered_ = true; - EXPECT_NE(filter_config->createFilter(), nullptr); + EXPECT_NE(filter_config->createContext(), nullptr); } TEST_P(WasmNetworkFilterConfigTest, FailedToGetThreadLocalPlugin) { @@ -449,11 +449,12 @@ TEST_P(WasmNetworkFilterConfigTest, FailedToGetThreadLocalPlugin) { threadlocal.registered_ = true; auto filter_config = std::make_unique(proto_config, context_); ASSERT_EQ(threadlocal.current_slot_, 1); - ASSERT_NE(filter_config->createFilter(), nullptr); + ASSERT_NE(filter_config->createContext(), nullptr); - // If the thread local plugin handle returns nullptr, `createFilter` should return nullptr - threadlocal.data_[0] = std::make_shared(nullptr); - EXPECT_EQ(filter_config->createFilter(), nullptr); + // If the thread local plugin handle returns nullptr, `createContext` should return nullptr + threadlocal.data_[0] = + std::make_shared(nullptr); + EXPECT_EQ(filter_config->createContext(), nullptr); } } // namespace Wasm diff --git a/test/extensions/filters/network/wasm/wasm_filter_test.cc b/test/extensions/filters/network/wasm/wasm_filter_test.cc index c13975701fdf..68479616fdaf 100644 --- a/test/extensions/filters/network/wasm/wasm_filter_test.cc +++ b/test/extensions/filters/network/wasm/wasm_filter_test.cc @@ -28,7 +28,8 @@ using proxy_wasm::ContextBase; class TestFilter : public Context { public: - TestFilter(Wasm* wasm, uint32_t root_context_id, PluginHandleSharedPtr plugin_handle) + TestFilter(Wasm* wasm, uint32_t root_context_id, + Common::Wasm::PluginHandleSharedPtr plugin_handle) : Context(wasm, root_context_id, plugin_handle) {} MOCK_CONTEXT_LOG_; diff --git a/test/extensions/stats_sinks/wasm/config_test.cc b/test/extensions/stats_sinks/wasm/config_test.cc index cb46b22b1c55..9c471783d7bf 100644 --- a/test/extensions/stats_sinks/wasm/config_test.cc +++ b/test/extensions/stats_sinks/wasm/config_test.cc @@ -71,14 +71,14 @@ INSTANTIATE_TEST_SUITE_P(Runtimes, WasmStatSinkConfigTest, TEST_P(WasmStatSinkConfigTest, CreateWasmFromEmpty) { envoy::extensions::stat_sinks::wasm::v3::Wasm config; EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm Stat Sink "); + "Unable to create Wasm plugin "); } TEST_P(WasmStatSinkConfigTest, CreateWasmFailOpen) { envoy::extensions::stat_sinks::wasm::v3::Wasm config; config.mutable_config()->set_fail_open(true); EXPECT_THROW_WITH_MESSAGE(initializeWithConfig(config), Extensions::Common::Wasm::WasmException, - "Unable to create Wasm Stat Sink "); + "Unable to create Wasm plugin "); } TEST_P(WasmStatSinkConfigTest, CreateWasmFromWASM) {