From ac1a7508ee5b69f9f3c2a7026ef2e8a5a8c2fd0e Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Tue, 30 Apr 2024 08:16:01 +0200 Subject: [PATCH] Move enabled/disabled functionality from analyzers into `Component` base class API. The different analyzers types all had their own methods for enabling/disabling their availability. This change abstracts that into a new API inside their base class (`plugin::Component`) so that they can be toggled in a unified way. In principle, other types of components could/should use this as well now, so that, e.g., an input reader's availability could be toggled at runtime. The code doesn't make that broader change for now because it would requires a series of changes wherever these other component types are being used. However, that means that one now could try toggling some other component through the new API without that having any effect. To catch that, there's a runtime check in place that turns any such attempt into an internal error. --- src/analyzer/Component.cc | 4 ++-- src/analyzer/Component.h | 15 --------------- src/file_analysis/Component.cc | 4 ++-- src/file_analysis/Component.h | 15 --------------- src/packet_analysis/Component.cc | 8 ++++---- src/packet_analysis/Component.h | 15 +-------------- src/plugin/Component.cc | 24 +++++++++++++++++++++--- src/plugin/Component.h | 20 ++++++++++++++++++++ 8 files changed, 50 insertions(+), 55 deletions(-) diff --git a/src/analyzer/Component.cc b/src/analyzer/Component.cc index 77896b662f..57025c7219 100644 --- a/src/analyzer/Component.cc +++ b/src/analyzer/Component.cc @@ -13,8 +13,8 @@ Component::Component(const std::string& name, factory_callback arg_factory, zeek : plugin::Component(arg_adapter ? plugin::component::SESSION_ADAPTER : plugin::component::ANALYZER, name, arg_subtype, analyzer_mgr->GetTagType()) { factory = arg_factory; - enabled = arg_enabled; partial = arg_partial; + SetEnabled(arg_enabled); } void Component::Initialize() { @@ -29,7 +29,7 @@ void Component::DoDescribe(ODesc* d) const { d->Add(", "); } - d->Add(enabled ? "enabled" : "disabled"); + d->Add(Enabled() ? "enabled" : "disabled"); } } // namespace zeek::analyzer diff --git a/src/analyzer/Component.h b/src/analyzer/Component.h index f457d4522c..3b4307134e 100644 --- a/src/analyzer/Component.h +++ b/src/analyzer/Component.h @@ -85,20 +85,6 @@ public: */ bool Partial() const { return partial; } - /** - * Returns true if the analyzer is currently enabled and hence - * available for use. - */ - bool Enabled() const { return enabled; } - - /** - * Enables or disables this analyzer. - * - * @param arg_enabled True to enabled, false to disable. - * - */ - void SetEnabled(bool arg_enabled) { enabled = arg_enabled; } - protected: /** * Overridden from plugin::Component. @@ -108,7 +94,6 @@ protected: private: factory_callback factory; // The analyzer's factory callback. bool partial; // True if the analyzer supports partial connections. - bool enabled; // True if the analyzer is enabled. }; } // namespace analyzer diff --git a/src/file_analysis/Component.cc b/src/file_analysis/Component.cc index 089e5f058d..3c0ccd545f 100644 --- a/src/file_analysis/Component.cc +++ b/src/file_analysis/Component.cc @@ -11,7 +11,7 @@ namespace zeek::file_analysis { Component::Component(const std::string& name, factory_function arg_factory, Tag::subtype_t subtype, bool arg_enabled) : plugin::Component(plugin::component::FILE_ANALYZER, name, subtype, file_mgr->GetTagType()) { factory_func = arg_factory; - enabled = arg_enabled; + SetEnabled(arg_enabled); } void Component::Initialize() { @@ -26,7 +26,7 @@ void Component::DoDescribe(ODesc* d) const { d->Add(", "); } - d->Add(enabled ? "enabled" : "disabled"); + d->Add(Enabled() ? "enabled" : "disabled"); } } // namespace zeek::file_analysis diff --git a/src/file_analysis/Component.h b/src/file_analysis/Component.h index a18179f20c..f3a9facc3c 100644 --- a/src/file_analysis/Component.h +++ b/src/file_analysis/Component.h @@ -70,20 +70,6 @@ public: */ factory_function FactoryFunction() const { return factory_func; } - /** - * Returns true if the analyzer is currently enabled and hence - * available for use. - */ - bool Enabled() const { return enabled; } - - /** - * Enables or disables this analyzer. - * - * @param arg_enabled True to enabled, false to disable. - * - */ - void SetEnabled(bool arg_enabled) { enabled = arg_enabled; } - protected: /** * Overridden from plugin::Component. @@ -94,7 +80,6 @@ private: friend class Manager; factory_function factory_func; // The analyzer's factory callback. - bool enabled; // True if the analyzer is enabled. }; } // namespace file_analysis diff --git a/src/packet_analysis/Component.cc b/src/packet_analysis/Component.cc index d0c21b0262..fc9137a5b5 100644 --- a/src/packet_analysis/Component.cc +++ b/src/packet_analysis/Component.cc @@ -18,11 +18,11 @@ void Component::Initialize() { } void Component::SetEnabled(bool arg_enabled) { - enabled = arg_enabled; + plugin::Component::SetEnabled(arg_enabled); // If we already have instantiated an analyzer, update its state. - if ( auto analyzer = packet_mgr->GetAnalyzer(Tag().AsVal().get()) ) - analyzer->SetEnabled(enabled); + if ( auto analyzer = packet_mgr->Lookup(Tag().AsVal().get()) ) + analyzer->SetEnabled(arg_enabled); } void Component::DoDescribe(ODesc* d) const { @@ -32,5 +32,5 @@ void Component::DoDescribe(ODesc* d) const { d->Add(", "); } - d->Add(enabled ? "enabled" : "disabled"); + d->Add(Enabled() ? "enabled" : "disabled"); } diff --git a/src/packet_analysis/Component.h b/src/packet_analysis/Component.h index 295133817c..96df483b69 100644 --- a/src/packet_analysis/Component.h +++ b/src/packet_analysis/Component.h @@ -34,19 +34,7 @@ public: */ factory_callback Factory() const { return factory; } - /** - * Returns true if the analyzer is currently enabled and hence - * available for use. - */ - bool Enabled() const { return enabled; } - - /** - * Enables or disables this analyzer. - * - * @param arg_enabled True to enabled, false to disable. - * - */ - void SetEnabled(bool arg_enabled); + void SetEnabled(bool arg_enabled) override; protected: /** @@ -56,7 +44,6 @@ protected: private: factory_callback factory; // The analyzer's factory callback. - bool enabled = true; // True if the analyzer is enabled. }; } // namespace zeek::packet_analysis diff --git a/src/plugin/Component.cc b/src/plugin/Component.cc index fc25e41644..5740d7f19a 100644 --- a/src/plugin/Component.cc +++ b/src/plugin/Component.cc @@ -59,12 +59,30 @@ void Component::InitializeTag() { tag = zeek::Tag(etype, ++type_counter, tag_subtype); } -/** - * @return The component's tag. - */ zeek::Tag Component::Tag() const { assert(tag_initialized); return tag; } +void Component::SetEnabled(bool arg_enabled) { + switch ( type ) { + case component::ANALYZER: + case component::PACKET_ANALYZER: + case component::FILE_ANALYZER: + case component::SESSION_ADAPTER: + // For these types we have logic in place to ignore the component + // if disabled. + enabled = arg_enabled; + break; + + default: + // It wouldn't be hard to add support for other component types. We + // just need to make sure the enabled flag is checked somewhere to + // skip using the component if off. + ODesc d; + Describe(&d); + reporter->InternalError("SetEnabled() called on unsupported component (%s)", d.Description()); + break; + } +} } // namespace zeek::plugin diff --git a/src/plugin/Component.h b/src/plugin/Component.h index 408dd90a88..d4eb296bf3 100644 --- a/src/plugin/Component.h +++ b/src/plugin/Component.h @@ -118,6 +118,25 @@ public: */ zeek::Tag Tag() const; + /** + * Returns true if the component is currently enabled and hence + * available for use. + */ + bool Enabled() const { return enabled; } + + /** + * Enables or disables this component. Derived classes may override this if + * they need to initiate additional actions, but must then call the base + * class version. + * + * @param arg_enabled True to enabled, false to disable. + * + * Note: This method is currently supported for protocol, file, and packet + * analyzers, as well as session adapters. Using it on other types of + * component will result in an internal error. + */ + virtual void SetEnabled(bool arg_enabled); + protected: /** * Adds type specific information to the output of Describe(). @@ -139,6 +158,7 @@ private: EnumTypePtr etype; Tag::subtype_t tag_subtype; bool tag_initialized = false; + bool enabled = true; /** Used to generate globally unique tags */ static Tag::type_t type_counter;