Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/awelzel/171-bro-zeek-plugin'
Browse files Browse the repository at this point in the history
* origin/topic/awelzel/171-bro-zeek-plugin:
  manager: Support __zeek_plugin__ magic file
  • Loading branch information
awelzel committed Nov 3, 2023
2 parents 3858bc8 + fa2eb17 commit 6dfe12f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
10 changes: 5 additions & 5 deletions testing/tests/plugin
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# @TEST-EXEC: bash %INPUT
# @TEST-EXEC: zkg install rot13

# @TEST-EXEC: test -f plugins/packages/rot13/__bro_plugin__
# @TEST-EXEC: test -f plugins/packages/rot13/__bro_plugin__ || test -f plugins/packages/rot13/__zeek_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: test -f plugins/packages/rot13/__bro_plugin__.disabled
# @TEST-EXEC: test ! -f plugins/packages/rot13/__bro_plugin__ && test ! -f plugins/packages/rot13/__zeek_plugin__
# @TEST-EXEC: test -f plugins/packages/rot13/__bro_plugin__.disabled || test -f plugins/packages/rot13/__zeek_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__
# @TEST-EXEC: test ! -f plugins/packages/rot13/__bro_plugin__.disabled
# @TEST-EXEC: test -f plugins/packages/rot13/__bro_plugin__ || test -f plugins/packages/rot13/__zeek_plugin__
# @TEST-EXEC: test ! -f plugins/packages/rot13/__bro_plugin__.disabled && test ! -f plugins/packages/rot13/__zeek_plugin__.disabled

echo "$(pwd)/packages/rot13" >> sources/one/bob/zkg.index
cd sources/one
Expand Down
2 changes: 1 addition & 1 deletion testing/tests/plugin_tarfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# @TEST-EXEC: bash %INPUT
# @TEST-EXEC: zkg install rot13

# @TEST-EXEC: test -f plugins/packages/rot13/__bro_plugin__
# @TEST-EXEC: test -f plugins/packages/rot13/__bro_plugin__ || test -f plugins/packages/rot13/__zeek_plugin__
# @TEST-EXEC: btest-diff scripts/packages/rot13/__load__.zeek

cd packages/rot13
Expand Down
69 changes: 43 additions & 26 deletions zeekpkg/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import filecmp
import json
import os
import pathlib
import re
import shutil
import subprocess
Expand Down Expand Up @@ -54,6 +55,8 @@
TRACKING_METHOD_COMMIT,
PLUGIN_MAGIC_FILE,
PLUGIN_MAGIC_FILE_DISABLED,
LEGACY_PLUGIN_MAGIC_FILE,
LEGACY_PLUGIN_MAGIC_FILE_DISABLED,
name_from_path,
aliases,
canonical_url,
Expand Down Expand Up @@ -390,43 +393,57 @@ 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
__zeek_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
existing __zeek_plugin__.disabled file to __zeek_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.
Until Zeek 6.1, the magic file was named __bro_plugin__. zkg implements
a fallback for recognizing the older name so that newer zkg versions
continue to work with older Zeek versions for some time longer.
"""
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
)
package_dir = pathlib.Path(self.plugin_dir) / ipkg.package.name

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",
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",
type(exception).__name__,
exception,
)
magic_paths_enabled = [
package_dir / PLUGIN_MAGIC_FILE,
package_dir / LEGACY_PLUGIN_MAGIC_FILE,
]

magic_paths_disabled = [
package_dir / PLUGIN_MAGIC_FILE_DISABLED,
package_dir / LEGACY_PLUGIN_MAGIC_FILE_DISABLED,
]

for path_enabled, path_disabled in zip(
magic_paths_enabled, magic_paths_disabled
):
if ipkg.status.is_loaded:
if path_disabled.exists():
try:
path_disabled.rename(path_enabled)
except OSError as exception:
LOG.error(
"could not enable plugin: %s %s",
type(exception).__name__,
exception,
)
else:
if path_enabled.exists():
try:
path_enabled.rename(path_disabled)
except OSError as exception:
LOG.error(
"could not disable plugin: %s %s",
type(exception).__name__,
exception,
)

def _read_manifest(self):
"""Read the manifest file containing the list of installed packages.
Expand Down
6 changes: 4 additions & 2 deletions zeekpkg/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
BUILTIN_SOURCE = "zeek-builtin"
BUILTIN_SCHEME = "zeek-builtin://"

PLUGIN_MAGIC_FILE = "__bro_plugin__"
PLUGIN_MAGIC_FILE_DISABLED = "__bro_plugin__.disabled"
PLUGIN_MAGIC_FILE = "__zeek_plugin__"
PLUGIN_MAGIC_FILE_DISABLED = "__zeek_plugin__.disabled"
LEGACY_PLUGIN_MAGIC_FILE = "__bro_plugin__"
LEGACY_PLUGIN_MAGIC_FILE_DISABLED = "__bro_plugin__.disabled"


def name_from_path(path):
Expand Down

0 comments on commit 6dfe12f

Please sign in to comment.