(g)ULP!
Loading...
Searching...
No Matches
gulp.plugin Namespace Reference

Classes

class  PluginBase
 

Functions

str _get_plugin_path (str plugin, **kwargs)
 
list[str] get_plugin_tags (str plugin)
 
 init (logging.Logger logger)
 
list[dict] list_plugins ()
 
PluginBase load_plugin (str plugin, AsyncEngine collab=None, AsyncElasticsearch elastic=None, **kwargs)
 
None plugin_cache_add (ModuleType m, str name)
 
None plugin_cache_clear ()
 
ModuleType plugin_cache_get (str plugin)
 
None plugin_cache_remove (str plugin)
 
None unload_plugin (PluginBase mod)
 

Variables

dict _cache = {}
 
logging _logger = None
 

Detailed Description

Gulp plugin base class and plugin utilities.

Function Documentation

◆ _get_plugin_path()

str _get_plugin_path ( str plugin,
** kwargs )
protected

Definition at line 1010 of file plugin.py.

1010def _get_plugin_path(plugin: str, **kwargs) -> str:
1011
1012 # try plain .py first
1013 # TODO: on license manager, disable plain .py load (only encrypted pyc)
1014 # get path according to plugin type
1015 plugin_type = kwargs.get("plugin_type", GulpPluginType.INGESTION)
1016 path_plugins = config.path_plugins(plugin_type)
1017
1018 ppy = muty.file.safe_path_join(path_plugins, plugin + ".py")
1019 if not muty.file.exists(ppy):
1020 # try pyc
1021 ppyc = muty.file.safe_path_join(path_plugins, plugin + ".pyc")
1022 if not muty.file.exists(ppyc):
1023 raise ObjectNotFound(
1024 "plugin %s not found (tried %s, %s)" % (plugin, ppy, ppyc)
1025 )
1026 return ppyc
1027 return ppy
1028
1029
Here is the caller graph for this function:

◆ get_plugin_tags()

list[str] get_plugin_tags ( str plugin)
Get the tags for a given (ingestion) plugin.

Args:
    plugin (str): The name of the plugin to get the tags for.
Returns:
    list[str]: The tags for the given plugin.

Definition at line 1118 of file plugin.py.

1118async def get_plugin_tags(plugin: str) -> list[str]:
1119 """
1120 Get the tags for a given (ingestion) plugin.
1121
1122 Args:
1123 plugin (str): The name of the plugin to get the tags for.
1124 Returns:
1125 list[str]: The tags for the given plugin.
1126 """
1127 p = load_plugin(plugin)
1128 tags = p.tags()
1129 return tags
1130
1131
Here is the call graph for this function:

◆ init()

init ( logging.Logger logger)
Initializes the plugin base module.

Definition at line 47 of file plugin.py.

47def init(logger: logging.Logger):
48 """
49 Initializes the plugin base module.
50 """
51 global _logger
52 _logger = logger
53
54

◆ list_plugins()

list[dict] list_plugins ( )
List all available plugins.

Returns:
    list[dict]: The list of available plugins.

Definition at line 1082 of file plugin.py.

1082async def list_plugins() -> list[dict]:
1083 """
1084 List all available plugins.
1085
1086 Returns:
1087 list[dict]: The list of available plugins.
1088 """
1089 path_plugins = config.path_plugins()
1090 files = await muty.file.list_directory_async(path_plugins, "*.py*", recursive=True)
1091 l = []
1092 for f in files:
1093 if "__init__" not in f and "__pycache__" not in f:
1094 try:
1095 p = load_plugin(f)
1096 n = {
1097 "name": p.name(),
1098 "type": str(p.type()),
1099 "desc": p.desc(),
1100 "filename": os.path.basename(p.path),
1101 "internal": p.internal(),
1102 "options": [o.to_dict() for o in p.options()],
1103 "depends_on": p.depends_on(),
1104 "tags": p.tags(),
1105 "event_type_field": p.event_type_field(),
1106 "version": p.version(),
1107 }
1108 l.append(n)
1109 unload_plugin(p)
1110 except Exception as ex:
1111 _logger.exception(ex)
1112 _logger.error("could not load plugin %s" % (f))
1113 continue
1114
1115 return l
1116
1117
Here is the call graph for this function:

◆ load_plugin()

PluginBase load_plugin ( str plugin,
AsyncEngine collab = None,
AsyncElasticsearch elastic = None,
** kwargs )
Load a plugin from a given path or from the default plugin path.

Args:
    plugin (str): The name or path of the plugin to load.
    req_id (str, optional): The request ID to associate with the plugin loading. Defaults to None.
    collab (AsyncEngine, optional): The SQLAlchemy engine instance. Defaults to None (not needed for pysigma plugins).
    elastic (AsyncElasticsearch, optional): The Elasticsearch client instance. Defaults to None (not needed for pysigma plugins).
    kwargs: additional arguments:
        "plugin_type" (GulpPluginType, default=GulpPluginType.INGESTION)
Returns:
    PluginBase: The loaded plugin.

Raises:
    Exception: If the plugin could not be loaded.

Definition at line 1030 of file plugin.py.

1035) -> PluginBase:
1036 """
1037 Load a plugin from a given path or from the default plugin path.
1038
1039 Args:
1040 plugin (str): The name or path of the plugin to load.
1041 req_id (str, optional): The request ID to associate with the plugin loading. Defaults to None.
1042 collab (AsyncEngine, optional): The SQLAlchemy engine instance. Defaults to None (not needed for pysigma plugins).
1043 elastic (AsyncElasticsearch, optional): The Elasticsearch client instance. Defaults to None (not needed for pysigma plugins).
1044 kwargs: additional arguments:
1045 "plugin_type" (GulpPluginType, default=GulpPluginType.INGESTION)
1046 Returns:
1047 PluginBase: The loaded plugin.
1048
1049 Raises:
1050 Exception: If the plugin could not be loaded.
1051 """
1052 _logger.debug(
1053 "load_plugin %s, collab=%s, elastic=%s ..." % (plugin, collab, elastic)
1054 )
1055
1056 m = plugin_cache_get(plugin)
1057 if m is not None:
1058 return m.Plugin(plugin, collab, elastic, **kwargs)
1059
1060 if "/" in plugin:
1061 # plugins is an absolute path
1062 path = muty.file.abspath(plugin)
1063 else:
1064 # uses "plugin_type" to load from the correct subfolder
1065 path = _get_plugin_path(plugin, **kwargs)
1066
1067 try:
1068 m: PluginBase = muty.dynload.load_dynamic_module_from_file(path)
1069 except Exception as ex:
1070 raise Exception(
1071 "load_dynamic_module_from_file() failed, could not load plugin %s !"
1072 % (path)
1073 ) from ex
1074
1075 mod: PluginBase = m.Plugin(path, collab, elastic, **kwargs)
1076 _logger.debug("loaded plugin: %s" % (mod.name()))
1077 plugin_cache_add(m, plugin)
1078
1079 return mod
1080
1081
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plugin_cache_add()

None plugin_cache_add ( ModuleType m,
str name )
Add a plugin to the process's own plugin cache.

Args:
    m (ModuleType): The plugin module to add to the cache.
    name (str): The name/path of the plugin.

Returns:
    None

Definition at line 1193 of file plugin.py.

1193def plugin_cache_add(m: ModuleType, name: str) -> None:
1194 """
1195 Add a plugin to the process's own plugin cache.
1196
1197 Args:
1198 m (ModuleType): The plugin module to add to the cache.
1199 name (str): The name/path of the plugin.
1200
1201 Returns:
1202 None
1203 """
1204 global _cache
1205 if not config.plugin_cache_enabled():
1206 return
1207
1208 mm = _cache.get(name, None)
1209 if mm is None:
1210 _logger.debug("adding plugin %s (%s) to cache" % (name, m))
1211 _cache[name] = m
1212
1213
Here is the caller graph for this function:

◆ plugin_cache_clear()

None plugin_cache_clear ( )
Clear the process's own plugin cache.

Returns:
    None

Definition at line 1157 of file plugin.py.

1157def plugin_cache_clear() -> None:
1158 """
1159 Clear the process's own plugin cache.
1160
1161 Returns:
1162 None
1163 """
1164 global _cache
1165 if not config.plugin_cache_enabled():
1166 return
1167
1168 _cache = {}
1169
1170

◆ plugin_cache_get()

ModuleType plugin_cache_get ( str plugin)
Retrieve a plugin from the process's own plugin cache.

Args:
    plugin (str): The name/path of the plugin to retrieve.

Returns:
    ModuleType: The plugin module if found in the cache, otherwise None.

Definition at line 1214 of file plugin.py.

1214def plugin_cache_get(plugin: str) -> ModuleType:
1215 """
1216 Retrieve a plugin from the process's own plugin cache.
1217
1218 Args:
1219 plugin (str): The name/path of the plugin to retrieve.
1220
1221 Returns:
1222 ModuleType: The plugin module if found in the cache, otherwise None.
1223 """
1224 global _cache
1225 if not config.plugin_cache_enabled():
1226 return None
1227
1228 p = _cache.get(plugin, None)
1229 if p is not None:
1230 _logger.debug("found plugin %s in cache" % (plugin))
1231 return p
Here is the caller graph for this function:

◆ plugin_cache_remove()

None plugin_cache_remove ( str plugin)
Remove a plugin from the process's own plugin cache.

Args:
    plugin (str): The name/path of the plugin to remove from the cache.

Returns:
    None

Definition at line 1171 of file plugin.py.

1171def plugin_cache_remove(plugin: str) -> None:
1172 """
1173 Remove a plugin from the process's own plugin cache.
1174
1175 Args:
1176 plugin (str): The name/path of the plugin to remove from the cache.
1177
1178 Returns:
1179 None
1180 """
1181 global _cache
1182 if not config.plugin_cache_enabled():
1183 return
1184
1185 if plugin in _cache:
1186 _logger.debug("removing plugin %s from cache" % (plugin))
1187
1188 # cleanup module and delete
1189 m = _cache[plugin]
1190 del _cache[plugin]
1191
1192

◆ unload_plugin()

None unload_plugin ( PluginBase mod)
Unloads a plugin module by calling its `unload` method and deletes the module object

NOTE: mod is **no more valid** after this function returns.

Args:
    mod (PluginBase): The plugin module to unload.
    run_gc (bool): if set, garbage collector is called after unloading the module. Defaults to True.

Returns:
    None

Definition at line 1132 of file plugin.py.

1132def unload_plugin(mod: PluginBase) -> None:
1133 """
1134 Unloads a plugin module by calling its `unload` method and deletes the module object
1135
1136 NOTE: mod is **no more valid** after this function returns.
1137
1138 Args:
1139 mod (PluginBase): The plugin module to unload.
1140 run_gc (bool): if set, garbage collector is called after unloading the module. Defaults to True.
1141
1142 Returns:
1143 None
1144 """
1145 if config.plugin_cache_enabled():
1146 return
1147
1148 if mod is not None:
1149 # delete from cache if any
1150 # plugin_cache_delete(mod)
1151
1152 _logger.debug("unloading plugin: %s" % (mod.name()))
1153 mod.cleanup()
1154 del mod
1155
1156
Here is the caller graph for this function:

Variable Documentation

◆ _cache

dict _cache = {}
protected

Definition at line 44 of file plugin.py.

◆ _logger

logging _logger = None
protected

Definition at line 41 of file plugin.py.