osquery_rust_ng/plugin/_enums/
plugin.rs

1use crate::_osquery as osquery;
2use crate::_osquery::{ExtensionPluginRequest, ExtensionResponse};
3use crate::plugin::config::{ConfigPlugin, ConfigPluginWrapper};
4use crate::plugin::logger::{LoggerPlugin, LoggerPluginWrapper};
5use crate::plugin::table::{ReadOnlyTable, TablePlugin};
6use crate::plugin::Registry;
7use crate::plugin::{OsqueryPlugin, Table};
8use std::sync::Arc;
9
10#[derive(Clone)]
11pub enum Plugin {
12    Config(Arc<dyn OsqueryPlugin>),
13    Logger(Arc<dyn OsqueryPlugin>),
14    Table(TablePlugin),
15}
16
17impl Plugin {
18    pub fn table<T: Table + 'static>(t: T) -> Self {
19        Plugin::Table(TablePlugin::from_writeable_table(t))
20    }
21
22    pub fn readonly_table<T: ReadOnlyTable + 'static>(t: T) -> Self {
23        Plugin::Table(TablePlugin::from_readonly_table(t))
24    }
25
26    pub fn config<C: ConfigPlugin + 'static>(c: C) -> Self {
27        Plugin::Config(Arc::new(ConfigPluginWrapper::new(c)))
28    }
29
30    pub fn logger<L: LoggerPlugin + 'static>(l: L) -> Self {
31        Plugin::Logger(Arc::new(LoggerPluginWrapper::new(l)))
32    }
33}
34
35impl OsqueryPlugin for Plugin {
36    // Name is the name used to refer to the plugin (e.g. the name of the
37    // table the plugin implements).
38    fn name(&self) -> String {
39        match self {
40            Plugin::Config(c) => c.name(),
41            Plugin::Logger(l) => l.name(),
42            Plugin::Table(t) => t.name(),
43        }
44    }
45
46    // Registry is which "registry" the plugin should be added to.
47    fn registry(&self) -> Registry {
48        match self {
49            Plugin::Config(_) => Registry::Config,
50            Plugin::Logger(_) => Registry::Logger,
51            Plugin::Table(_) => Registry::Table,
52        }
53    }
54
55    // Routes returns detailed information about the interface exposed
56    // by the plugin. See the example plugins for implementation details.
57    //pub(crate) fn routes(&self) -> osquery::ExtensionPluginResponse {
58    fn routes(&self) -> osquery::ExtensionPluginResponse {
59        match self {
60            Plugin::Config(c) => c.routes(),
61            Plugin::Logger(l) => l.routes(),
62            Plugin::Table(t) => t.routes(),
63        }
64    }
65
66    // Ping implements the plugin's health check. If the plugin is in a
67    // healthy state, Status OK should be returned.
68    fn ping(&self) -> osquery::ExtensionStatus {
69        match self {
70            Plugin::Config(c) => c.ping(),
71            Plugin::Logger(l) => l.ping(),
72            Plugin::Table(t) => t.ping(),
73        }
74    }
75
76    // Call requests the plugin to perform its defined behavior, returning
77    // a response containing the result.
78    fn handle_call(&self, request: ExtensionPluginRequest) -> ExtensionResponse {
79        match self {
80            Plugin::Config(c) => c.handle_call(request),
81            Plugin::Logger(l) => l.handle_call(request),
82            Plugin::Table(t) => t.handle_call(request),
83        }
84    }
85
86    // Shutdown notifies the plugin to stop.
87    fn shutdown(&self) {
88        match self {
89            Plugin::Config(c) => c.shutdown(),
90            Plugin::Logger(l) => l.shutdown(),
91            Plugin::Table(t) => t.shutdown(),
92        }
93    }
94}