mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Merge remote-tracking branch 'origin/topic/awelzel/find-in-zeekpath'
* origin/topic/awelzel/find-in-zeekpath: NEWS: Mention can_load() and find_in_zeekpath() utils: Introduce packages.zeek with can_load() helper zeek.bif: Add find_in_zeekpath() helper
This commit is contained in:
commit
77d57fccfd
12 changed files with 171 additions and 0 deletions
9
NEWS
9
NEWS
|
@ -18,6 +18,15 @@ New Functionality
|
||||||
delete v;
|
delete v;
|
||||||
assert |v| == 0;
|
assert |v| == 0;
|
||||||
|
|
||||||
|
- A new helper function ``can_load()`` backed by a new bif ``find_in_zeekpath()``
|
||||||
|
was added to determine if a non-relative ``@load`` directive might work. This
|
||||||
|
can be used to guard ``@load`` directives when script packages may or may not
|
||||||
|
be installed.
|
||||||
|
|
||||||
|
@if ( can_load("my-package") )
|
||||||
|
@load my-package
|
||||||
|
@endif
|
||||||
|
|
||||||
Changed Functionality
|
Changed Functionality
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
@load base/utils/geoip-distance
|
@load base/utils/geoip-distance
|
||||||
@load base/utils/hash_hrw
|
@load base/utils/hash_hrw
|
||||||
@load base/utils/numbers
|
@load base/utils/numbers
|
||||||
|
@load base/utils/packages
|
||||||
@load base/utils/paths
|
@load base/utils/paths
|
||||||
@load base/utils/patterns
|
@load base/utils/patterns
|
||||||
@load base/utils/queue
|
@load base/utils/queue
|
||||||
|
|
16
scripts/base/utils/packages.zeek
Normal file
16
scripts/base/utils/packages.zeek
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
##! Rudimentary functions for helping with Zeek packages.
|
||||||
|
|
||||||
|
## Checks whether @load of a given package name could
|
||||||
|
## be successful.
|
||||||
|
##
|
||||||
|
## This tests for the existence of corresponding script files
|
||||||
|
## in ZEEKPATH. It does not attempt to parse and validate
|
||||||
|
## any actual Zeek script code.
|
||||||
|
##
|
||||||
|
## path: The filename, package or path to test.
|
||||||
|
##
|
||||||
|
## Returns: T if the given filename, package or path may load.
|
||||||
|
function can_load(p: string): bool
|
||||||
|
{
|
||||||
|
return find_in_zeekpath(p) != "";
|
||||||
|
}
|
32
src/zeek.bif
32
src/zeek.bif
|
@ -5328,3 +5328,35 @@ function table_pattern_matcher_stats%(tbl: any%) : MatcherStats
|
||||||
|
|
||||||
return std::move(result);
|
return std::move(result);
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
## Determine the path used by a non-relative @load directive.
|
||||||
|
##
|
||||||
|
## This function is package aware: Passing *package* will yield the
|
||||||
|
## path to *package.zeek*, *package/__load__.zeek* or an empty string
|
||||||
|
## if neither can be found. Note that passing a relative path or absolute
|
||||||
|
## path is an error.
|
||||||
|
##
|
||||||
|
## path: The filename, package or path to search for in ZEEKPATH.
|
||||||
|
##
|
||||||
|
## Returns: Path of script file that would be loaded by an @load directive.
|
||||||
|
function find_in_zeekpath%(p: string%): string
|
||||||
|
%{
|
||||||
|
auto path = p->ToStdString();
|
||||||
|
if ( ! path.empty() && (path[0] == '.' || path[0] == '/') )
|
||||||
|
{
|
||||||
|
zeek::reporter->Error("find_in_zeek_path: path must be relative or absolute");
|
||||||
|
return zeek::val_mgr->EmptyString();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto resolved = zeek::util::find_script_file(path, zeek::util::zeek_path());
|
||||||
|
if ( ! resolved.empty() && zeek::util::is_dir(resolved.c_str()) )
|
||||||
|
{
|
||||||
|
// If it's a directory, try opening the package using
|
||||||
|
// the absolute path. This is zeek::util::open_package()
|
||||||
|
// without the noisy error log.
|
||||||
|
resolved.append("/__load__.zeek");
|
||||||
|
resolved = zeek::util::find_file(resolved, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return zeek::make_intrusive<zeek::StringVal>(resolved.c_str());
|
||||||
|
%}
|
||||||
|
|
1
testing/btest/Baseline/bifs.find_in_zeekpath/.stderr
Normal file
1
testing/btest/Baseline/bifs.find_in_zeekpath/.stderr
Normal file
|
@ -0,0 +1 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
|
@ -0,0 +1,3 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||||
|
error: find_in_zeek_path: path must be relative or absolute
|
||||||
|
error: find_in_zeek_path: path must be relative or absolute
|
|
@ -0,0 +1,3 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||||
|
relative,
|
||||||
|
absolute,
|
10
testing/btest/Baseline/bifs.find_in_zeekpath/out
Normal file
10
testing/btest/Baseline/bifs.find_in_zeekpath/out
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||||
|
find_in_zeekpath base/protocols/conn, base/protocols/conn/__load__.zeek
|
||||||
|
find_in_zeekpath protocols/conn (empty expected, no __load__.zeek),
|
||||||
|
find_in_zeekpath protocols/conn/vlan-logging, policy/protocols/conn/vlan-logging.zeek
|
||||||
|
find_in_zeekpath pkg1, ./pkg1.zeek
|
||||||
|
find_in_zeekpath pkg1.zeek, ./pkg1.zeek
|
||||||
|
find_in_zeekpath pkg2, ./pkg2/__load__.zeek
|
||||||
|
find_in_zeekpath pkg3,
|
||||||
|
pkg1!
|
||||||
|
pkg2!
|
|
@ -290,6 +290,7 @@ scripts/base/init-default.zeek
|
||||||
scripts/base/utils/files.zeek
|
scripts/base/utils/files.zeek
|
||||||
scripts/base/utils/geoip-distance.zeek
|
scripts/base/utils/geoip-distance.zeek
|
||||||
scripts/base/utils/numbers.zeek
|
scripts/base/utils/numbers.zeek
|
||||||
|
scripts/base/utils/packages.zeek
|
||||||
scripts/base/utils/queue.zeek
|
scripts/base/utils/queue.zeek
|
||||||
scripts/base/utils/strings.zeek
|
scripts/base/utils/strings.zeek
|
||||||
scripts/base/utils/thresholds.zeek
|
scripts/base/utils/thresholds.zeek
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||||
|
no pkg3
|
||||||
|
pkg1!
|
||||||
|
pkg2!
|
59
testing/btest/bifs/find_in_zeekpath.zeek
Normal file
59
testing/btest/bifs/find_in_zeekpath.zeek
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
# @TEST-DOC: Test find_in_zeekpath() and demo conditional @load'ing.
|
||||||
|
#
|
||||||
|
# @TEST-EXEC: zeek -b %INPUT >out
|
||||||
|
# @TEST-EXEC: btest-diff out
|
||||||
|
# @TEST-EXEC: btest-diff .stderr
|
||||||
|
#
|
||||||
|
# @TEST-EXEC: zeek -b errors.zeek >errors.stdout 2>errors.stderr
|
||||||
|
# @TEST-EXEC: btest-diff errors.stdout
|
||||||
|
# @TEST-EXEC: btest-diff errors.stderr
|
||||||
|
|
||||||
|
|
||||||
|
@if ( find_in_zeekpath("pkg1") != "" )
|
||||||
|
@load pkg1
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ( find_in_zeekpath("pkg2") != "" )
|
||||||
|
@load pkg2
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ( find_in_zeekpath("pkg3") != "" )
|
||||||
|
@load pkg3
|
||||||
|
@endif
|
||||||
|
|
||||||
|
function path_tail(r: string): string
|
||||||
|
{
|
||||||
|
if ( |r| == 0 )
|
||||||
|
return r;
|
||||||
|
local parts = split_string(r, /\//);
|
||||||
|
return join_string_vec(parts[-4:], "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
print "find_in_zeekpath base/protocols/conn", path_tail(find_in_zeekpath("base/protocols/conn"));
|
||||||
|
print "find_in_zeekpath protocols/conn (empty expected, no __load__.zeek)", find_in_zeekpath("protocols/conn");
|
||||||
|
print "find_in_zeekpath protocols/conn/vlan-logging", path_tail(find_in_zeekpath("protocols/conn/vlan-logging"));
|
||||||
|
|
||||||
|
print "find_in_zeekpath pkg1", find_in_zeekpath("pkg1");
|
||||||
|
print "find_in_zeekpath pkg1.zeek", find_in_zeekpath("pkg1.zeek");
|
||||||
|
print "find_in_zeekpath pkg2", find_in_zeekpath("pkg2");
|
||||||
|
print "find_in_zeekpath pkg3", find_in_zeekpath("pkg3");
|
||||||
|
|
||||||
|
@TEST-START-FILE pkg1.zeek
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
print "pkg1!";
|
||||||
|
}
|
||||||
|
@TEST-END-FILE
|
||||||
|
|
||||||
|
@TEST-START-FILE pkg2/__load__.zeek
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
print "pkg2!";
|
||||||
|
}
|
||||||
|
@TEST-END-FILE
|
||||||
|
|
||||||
|
@TEST-START-FILE errors.zeek
|
||||||
|
# Using relative and absolute paths is an error (empty string)
|
||||||
|
print "relative", find_in_zeekpath("./pkg1.zeek");
|
||||||
|
print "absolute", find_in_zeekpath("/pkg1");
|
||||||
|
@TEST-END-FILE
|
32
testing/btest/scripts/base/utils/packages.zeek
Normal file
32
testing/btest/scripts/base/utils/packages.zeek
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# @TEST-EXEC: zeek -b %INPUT >output
|
||||||
|
# @TEST-EXEC: btest-diff output
|
||||||
|
|
||||||
|
@load base/utils/packages
|
||||||
|
|
||||||
|
@if ( can_load("pkg1") )
|
||||||
|
@load pkg1
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ( can_load("pkg2") )
|
||||||
|
@load pkg2
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ( can_load("pkg3") )
|
||||||
|
@load pkg3
|
||||||
|
@else
|
||||||
|
print "no pkg3";
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@TEST-START-FILE pkg1.zeek
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
print "pkg1!";
|
||||||
|
}
|
||||||
|
@TEST-END-FILE
|
||||||
|
|
||||||
|
@TEST-START-FILE pkg2/__load__.zeek
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
print "pkg2!";
|
||||||
|
}
|
||||||
|
@TEST-END-FILE
|
Loading…
Add table
Add a link
Reference in a new issue