Skip to content

Commit

Permalink
Merge branch 'topic/christian/plugin-deactivation' of https://github.…
Browse files Browse the repository at this point in the history
…com/ckreibich/package-manager

- Added missing test baselines

* 'topic/christian/plugin-deactivation' of https://github.com/ckreibich/package-manager:
  When loading/unloading a Zeek package, also enable/disable any plugin
  • Loading branch information
jsiwek committed Aug 26, 2019
2 parents b1a6316 + ba73dd2 commit 883e221
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@

2.0.2-2 | 2019-08-26 14:16:36 -0700

* When loading/unloading a Zeek package, also enable/disable any plugin (Christian Kreibich, Corelight)

This patch does this via renaming of the __bro_plugin__ magic file
that Zeek looks for when scanning ZEEK_PLUGIN_PATH. To disable a
plugin, zkg renames the file to __bro_plugin__.disabled, and renames
that file back to __bro_plugin__ when enabling.

2.0.2 | 2019-07-17 10:46:14 -0700

* Release 2.0.2.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.2
2.0.2-2
2 changes: 1 addition & 1 deletion doc/man/zkg.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Man page generated from reStructuredText.
.
.TH "ZKG" "1" "Jul 17, 2019" "2.0.2" "Zeek Package Manager"
.TH "ZKG" "1" "Aug 26, 2019" "2.0.2-2" "Zeek Package Manager"
.SH NAME
zkg \- Zeek Package Manager
.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Demo::Rot13
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Demo::Rot13
14 changes: 14 additions & 0 deletions testing/tests/plugin
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
# @TEST-EXEC: btest-diff plugins/packages/rot13/__bro_plugin__
# @TEST-EXEC: btest-diff scripts/packages/rot13/__load__.zeek

# Unloading the package should also disable the plugin, which we
# detect via the renamed __bro_plugin__ magic file.
# @TEST-EXEC: zkg unload rot13

# @TEST-EXEC: test ! -f plugins/packages/rot13/__bro_plugin__
# @TEST-EXEC: btest-diff plugins/packages/rot13/__bro_plugin__.disabled

# (Re-)loading the package should also (re-)enable the plugin.
# @TEST-EXEC: zkg load rot13

# @TEST-EXEC: test ! -f plugins/packages/rot13/__bro_plugin__.disabled
# @TEST-EXEC: cp plugins/packages/rot13/__bro_plugin__ __bro_plugin__.after_enabling
# @TEST-EXEC: btest-diff __bro_plugin__.after_enabling

echo "$(pwd)/packages/rot13" >> sources/one/bob/zkg.index
cd sources/one
git commit -am 'add rot13 package'
2 changes: 1 addition & 1 deletion zeekpkg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import logging

__version__ = "2.0.2"
__version__ = "2.0.2-2"
__all__ = ['manager', 'package', 'source']

LOG = logging.getLogger(__name__)
Expand Down
61 changes: 58 additions & 3 deletions zeekpkg/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
TRACKING_METHOD_VERSION,
TRACKING_METHOD_BRANCH,
TRACKING_METHOD_COMMIT,
PLUGIN_MAGIC_FILE,
PLUGIN_MAGIC_FILE_DISABLED,
name_from_path,
aliases,
user_vars,
Expand Down Expand Up @@ -253,6 +255,42 @@ def _write_autoloader(self):

f.write(content)

def _write_plugin_magic(self, ipkg):
"""Enables/disables any Zeek plugin included with a package.
Zeek's plugin code scans its plugin directories for
__bro_plugin__ magic files, which indicate presence of a
plugin directory. When this file does not exist, Zeek does not
recognize a plugin.
When we're loading a package, this function renames an
existing __bro_plugin__.disabled file to __bro_plugin__, and
vice versa when we're unloading a package.
When the package doesn't include a plugin, or when the plugin
directory already contains a correctly named magic file, this
function does nothing.
"""
magic_path = os.path.join(
self.plugin_dir, ipkg.package.name, PLUGIN_MAGIC_FILE)
magic_path_disabled = os.path.join(
self.plugin_dir, ipkg.package.name, PLUGIN_MAGIC_FILE_DISABLED)

if ipkg.status.is_loaded:
if os.path.exists(magic_path_disabled):
try:
os.rename(magic_path_disabled, magic_path)
except OSError as exception:
LOG.warning('could not enable plugin: %s: %s'.format(
type(exception).__name__, exception))
else:
if os.path.exists(magic_path):
try:
os.rename(magic_path, magic_path_disabled)
except OSError as exception:
LOG.warning('could not disable plugin: %s: %s'.format(
type(exception).__name__, exception))

def _read_manifest(self):
"""Read the manifest file containing the list of installed packages.
Expand Down Expand Up @@ -450,6 +488,19 @@ def has_scripts(self, installed_pkg):
return os.path.exists(os.path.join(self.script_dir,
installed_pkg.package.name))

def has_plugin(self, installed_pkg):
"""Return whether a :class:`.package.InstalledPackage` installed a plugin.
Args:
installed_pkg(:class:`.package.InstalledPackage`): the installed
package to check for whether it has installed a Zeek plugin.
Returns:
bool: True if the package has installed a Zeek plugin.
"""
return os.path.exists(os.path.join(self.plugin_dir,
installed_pkg.package.name))

def save_temporary_config_files(self, installed_pkg):
"""Return a list of temporary package config file backups.
Expand Down Expand Up @@ -969,14 +1020,17 @@ def load(self, pkg_path):
pkg_load_fallback = os.path.join(self.script_dir, ipkg.package.name,
'__load__.bro')

if not os.path.exists(pkg_load_script) and not os.path.exists(pkg_load_fallback):
LOG.debug('loading "%s": %s does not exist',
if (not os.path.exists(pkg_load_script) and
not os.path.exists(pkg_load_fallback) and
not self.has_plugin(self, ipkg)):
LOG.debug('loading "%s": %s not found and package has no plugin',
pkg_path, pkg_load_script)
return 'no __load__.zeek within package script_dir'
return 'no __load__.zeek within package script_dir and no plugin included'

ipkg.status.is_loaded = True
self._write_autoloader()
self._write_manifest()
self._write_plugin_magic(ipkg)
LOG.debug('loaded "%s"', pkg_path)
return ''

Expand Down Expand Up @@ -1014,6 +1068,7 @@ def unload(self, pkg_path):
ipkg.status.is_loaded = False
self._write_autoloader()
self._write_manifest()
self._write_plugin_magic(ipkg)
LOG.debug('unloaded "%s"', pkg_path)
return True

Expand Down
3 changes: 3 additions & 0 deletions zeekpkg/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
TRACKING_METHOD_BRANCH = 'branch'
TRACKING_METHOD_COMMIT = 'commit'

PLUGIN_MAGIC_FILE = '__bro_plugin__'
PLUGIN_MAGIC_FILE_DISABLED = '__bro_plugin__.disabled'

def name_from_path(path):
"""Returns the name of a package given a path to its git repository."""
return canonical_url(path).split('/')[-1]
Expand Down

0 comments on commit 883e221

Please sign in to comment.