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.
This commit is contained in:
Robin Sommer 2024-04-30 08:16:01 +02:00
parent d7e30d9ee2
commit ac1a7508ee
No known key found for this signature in database
GPG key ID: D8187293B3FFE5D0
8 changed files with 50 additions and 55 deletions

View file

@ -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

View file

@ -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;