osquery_rust_ng/plugin/config/
mod.rs1use crate::_osquery::{ExtensionPluginResponse, ExtensionResponse, ExtensionStatus};
2use crate::plugin::{ExtensionResponseEnum, OsqueryPlugin, Registry};
3use std::collections::{BTreeMap, HashMap};
4use std::sync::Arc;
5
6pub trait ConfigPlugin: Send + Sync + 'static {
12 fn name(&self) -> String;
14
15 fn gen_config(&self) -> Result<HashMap<String, String>, String>;
20
21 fn gen_pack(&self, name: &str, _value: &str) -> Result<String, String> {
26 Err(format!("Pack '{name}' not found"))
27 }
28
29 fn shutdown(&self) {}
31}
32
33#[derive(Clone)]
35pub struct ConfigPluginWrapper {
36 plugin: Arc<dyn ConfigPlugin>,
37}
38
39impl ConfigPluginWrapper {
40 pub fn new<C: ConfigPlugin>(plugin: C) -> Self {
41 Self {
42 plugin: Arc::new(plugin),
43 }
44 }
45}
46
47impl OsqueryPlugin for ConfigPluginWrapper {
48 fn name(&self) -> String {
49 self.plugin.name()
50 }
51
52 fn registry(&self) -> Registry {
53 Registry::Config
54 }
55
56 fn routes(&self) -> ExtensionPluginResponse {
57 ExtensionPluginResponse::new()
59 }
60
61 fn ping(&self) -> ExtensionStatus {
62 ExtensionStatus::default()
63 }
64
65 fn handle_call(&self, request: crate::_osquery::ExtensionPluginRequest) -> ExtensionResponse {
66 let action = request.get("action").map(|s| s.as_str()).unwrap_or("");
68
69 match action {
70 "genConfig" => {
71 match self.plugin.gen_config() {
72 Ok(config_map) => {
73 let mut response = ExtensionPluginResponse::new();
74 let mut row = BTreeMap::new();
75
76 for (key, value) in config_map {
78 row.insert(key, value);
79 }
80
81 response.push(row);
82 let status = ExtensionStatus::default();
83 ExtensionResponse::new(status, response)
84 }
85 Err(e) => ExtensionResponseEnum::Failure(e).into(),
86 }
87 }
88 "genPack" => {
89 let name = request.get("name").cloned().unwrap_or_default();
90 let value = request.get("value").cloned().unwrap_or_default();
91
92 match self.plugin.gen_pack(&name, &value) {
93 Ok(pack_content) => {
94 let mut response = ExtensionPluginResponse::new();
95 let mut row = BTreeMap::new();
96 row.insert("pack".to_string(), pack_content);
97 response.push(row);
98 let status = ExtensionStatus::default();
99 ExtensionResponse::new(status, response)
100 }
101 Err(e) => ExtensionResponseEnum::Failure(e).into(),
102 }
103 }
104 _ => ExtensionResponseEnum::Failure(format!("Unknown config plugin action: {action}"))
105 .into(),
106 }
107 }
108
109 fn shutdown(&self) {
110 self.plugin.shutdown();
111 }
112}