zeek-setup: Validate plugin debug streams during startup

Providing an unknown plugin debug stream with -B was previously silently
accepted. This caused user confusing as the behavior is "no output, but
seems to work".

Check the enabled debug streams once all plugins have been loaded and
exit early on for invalid streams.

    $ ZEEK_PLUGIN_PATH=./build zeek -B plugin-zeek-myplugin -e 'print zeek_version();'
    error in <command line>, line 3: No plugin debug stream 'plugin-zeek-myplugin' found

Closes #913.
This commit is contained in:
Arne Welzel 2022-07-06 18:45:40 +02:00
parent 8ba44c656a
commit d703033ae6
5 changed files with 58 additions and 3 deletions

View file

@ -115,7 +115,7 @@ void DebugLogger::EnableStreams(const char* s)
exit(0);
}
if ( strncmp(tok, "plugin-", strlen("plugin-")) == 0 )
if ( util::starts_with(tok, "plugin-") )
{
// Cannot verify this at this time, plugins may not
// have been loaded.
@ -144,6 +144,29 @@ void DebugLogger::EnableStreams(const char* s)
delete[] tmp;
}
bool DebugLogger::CheckStreams(const std::set<std::string>& plugin_names)
{
bool ok = true;
std::set<std::string> available_plugin_streams;
for ( const auto& p : plugin_names )
available_plugin_streams.insert(PluginStreamName(p));
for ( auto const& stream : enabled_streams )
{
if ( ! util::starts_with(stream, "plugin-") )
continue;
if ( available_plugin_streams.count(stream) == 0 )
{
reporter->Error("No plugin debug stream '%s' found", stream.c_str());
ok = false;
}
}
return ok;
}
void DebugLogger::Log(DebugStream stream, const char* fmt, ...)
{
Stream* g = &streams[int(stream)];
@ -168,8 +191,7 @@ void DebugLogger::Log(DebugStream stream, const char* fmt, ...)
void DebugLogger::Log(const plugin::Plugin& plugin, const char* fmt, ...)
{
std::string tok = std::string("plugin-") + plugin.Name();
tok = util::strreplace(tok, "::", "-");
std::string tok = PluginStreamName(plugin.Name());
if ( enabled_streams.find(tok) == enabled_streams.end() )
return;

View file

@ -11,6 +11,8 @@
#include <set>
#include <string>
#include "zeek/util.h"
#define DBG_LOG(stream, args...) \
if ( ::zeek::detail::debug_logger.IsEnabled(stream) ) \
::zeek::detail::debug_logger.Log(stream, args)
@ -84,6 +86,9 @@ public:
// Takes comma-seperated list of stream prefixes.
void EnableStreams(const char* streams);
// Check the enabled streams for invalid ones.
bool CheckStreams(const std::set<std::string>& plugin_names);
bool IsEnabled(DebugStream stream) const { return streams[int(stream)].enabled; }
void SetVerbose(bool arg_verbose) { verbose = arg_verbose; }
@ -105,6 +110,11 @@ private:
std::set<std::string> enabled_streams;
static Stream streams[NUM_DBGS];
const std::string PluginStreamName(const std::string& plugin_name)
{
return "plugin-" + util::strreplace(plugin_name, "::", "-");
}
};
extern DebugLogger debug_logger;

View file

@ -847,6 +847,20 @@ SetupResult setup(int argc, char** argv, Options* zopts)
exit(success ? 0 : 1);
}
#ifdef DEBUG
// Check debug streams. Specifically that all plugin-
// streams are valid now that the active plugins are known.
std::set<std::string> active_plugins;
for ( const auto p : plugin_mgr->ActivePlugins() )
active_plugins.insert(p->Name());
if ( ! debug_logger.CheckStreams(active_plugins) )
{
early_shutdown();
exit(1);
}
#endif
packet_mgr->InitPostScript(options.unprocessed_output_file.value_or(""));
analyzer_mgr->InitPostScript();
file_mgr->InitPostScript();

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <command line>, line 3: No plugin debug stream 'plugin-zeek-http' found

View file

@ -0,0 +1,7 @@
# This requires Zeek with debug streams support.
# @TEST-REQUIRES: test "$(zeek-config --build_type)" = "debug"
# @TEST-EXEC: zeek -B plugin-Zeek-HTTP -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr
# @TEST-EXEC-FAIL: zeek -B plugin-zeek-http -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr
# @TEST-EXEC: btest-diff zeek.stderr