Skip to content

Commit

Permalink
refactor: module metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
banditopazzo committed May 15, 2024
1 parent 6d9502a commit b29a945
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 174 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 3 additions & 15 deletions crates/modules/desktop-notifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,17 @@ use anyhow::{Context, Result};
use pulsar_core::{
event::Threat,
pdk::{
CleanExit, ConfigError, Event, ModuleConfig, ModuleContext, ModuleDetails, ModuleError,
ModuleName, PulsarModule, ShutdownSignal, Version,
CleanExit, ConfigError, Event, ModuleConfig, ModuleContext, ModuleError, PulsarModule,
ShutdownSignal,
},
};

const MODULE_NAME: &str = "desktop-notifier";

pub struct DesktopNotifierModule;

impl PulsarModule for DesktopNotifierModule {
const MODULE_NAME: &'static str = "desktop-notifier";
const DEFAULT_ENABLED: bool = false;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
16 changes: 3 additions & 13 deletions crates/modules/file-system-monitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,18 @@ pub mod pulsar {
use pulsar_core::{
event::FileFlags,
pdk::{
CleanExit, ConfigError, Event, IntoPayload, ModuleConfig, ModuleContext, ModuleDetails,
ModuleError, ModuleName, ModuleSender, Payload, PulsarModule, ShutdownSignal, Version,
CleanExit, ConfigError, Event, IntoPayload, ModuleConfig, ModuleContext, ModuleError,
ModuleSender, Payload, PulsarModule, ShutdownSignal,
},
};
use tokio::{fs::File, io::AsyncReadExt};

pub struct FileSystemMonitorModule;

impl PulsarModule for FileSystemMonitorModule {
const MODULE_NAME: &'static str = MODULE_NAME;
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
17 changes: 3 additions & 14 deletions crates/modules/logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pulsar_core::pdk::{
CleanExit, ConfigError, Event, ModuleConfig, ModuleContext, ModuleDetails, ModuleError,
ModuleName, PulsarModule, ShutdownSignal, Version,
CleanExit, ConfigError, Event, ModuleConfig, ModuleContext, ModuleError, PulsarModule,
ShutdownSignal,
};
use std::{
borrow::Cow,
Expand All @@ -17,24 +17,13 @@ use std::{
use thiserror::Error;

const UNIX_SOCK_PATHS: [&str; 3] = ["/dev/log", "/var/run/syslog", "/var/run/log"];
const MODULE_NAME: &str = "logger";

pub struct LoggerModule;

impl PulsarModule for LoggerModule {
const MODULE_NAME: &'static str = "threat-logger";
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
16 changes: 3 additions & 13 deletions crates/modules/network-monitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,17 @@ pub mod pulsar {
use pulsar_core::{
event::{DnsAnswer, DnsQuestion, Host},
pdk::{
CleanExit, IntoPayload, ModuleContext, ModuleDetails, ModuleError, ModuleName, Payload,
PulsarModule, ShutdownSignal, Version,
CleanExit, IntoPayload, ModuleContext, ModuleError, Payload, PulsarModule,
ShutdownSignal,
},
};

pub struct NetworkMonitorModule;

impl PulsarModule for NetworkMonitorModule {
const MODULE_NAME: &'static str = MODULE_NAME;
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
16 changes: 3 additions & 13 deletions crates/modules/process-monitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,17 @@ pub mod pulsar {
use super::*;
use bpf_common::{containers::ContainerId, program::BpfEvent, BpfSenderWrapper};
use pulsar_core::pdk::{
process_tracker::TrackerUpdate, CleanExit, IntoPayload, ModuleContext, ModuleDetails,
ModuleError, ModuleName, Payload, PulsarModule, ShutdownSignal, Version,
process_tracker::TrackerUpdate, CleanExit, IntoPayload, ModuleContext, ModuleError,
Payload, PulsarModule, ShutdownSignal,
};
use tokio::sync::mpsc;

pub struct ProcessMonitorModule;

impl PulsarModule for ProcessMonitorModule {
const MODULE_NAME: &'static str = MODULE_NAME;
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
16 changes: 2 additions & 14 deletions crates/modules/rules-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::path::PathBuf;

use engine::PulsarEngine;
use pulsar_core::pdk::{
CleanExit, ConfigError, ModuleConfig, ModuleContext, ModuleDetails, ModuleError, ModuleName,
PulsarModule, ShutdownSignal, Version,
CleanExit, ConfigError, ModuleConfig, ModuleContext, ModuleError, PulsarModule, ShutdownSignal,
};

mod dsl;
Expand All @@ -13,24 +12,13 @@ mod ruleset;
pub use engine::RuleEngineData;

const DEFAULT_RULES_PATH: &str = "/var/lib/pulsar/rules";
const MODULE_NAME: &str = "rules-engine";

pub struct RuleEngineModule;

impl PulsarModule for RuleEngineModule {
const MODULE_NAME: &'static str = "rules-engine";
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
18 changes: 3 additions & 15 deletions crates/modules/smtp-notifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,19 @@ use lettre::{
use pulsar_core::{
event::Threat,
pdk::{
CleanExit, ConfigError, ModuleConfig, ModuleContext, ModuleDetails, ModuleError,
ModuleName, PulsarModule, ShutdownSignal, Version,
CleanExit, ConfigError, ModuleConfig, ModuleContext, ModuleError, PulsarModule,
ShutdownSignal,
},
};

mod template;

const MODULE_NAME: &str = "smtp-notifier";

pub struct SmtpNotifierModule;

impl PulsarModule for SmtpNotifierModule {
const MODULE_NAME: &'static str = "smtp-notifier";
const DEFAULT_ENABLED: bool = false;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
1 change: 0 additions & 1 deletion crates/pulsar-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ validatron = { path = "../validatron" }
serde = { workspace = true, features = ["derive"] }
toml_edit = { workspace = true, features = ["easy"] }
tokio = { workspace = true, features = ["full"] }
semver = { workspace = true, features = ["serde"] }
anyhow = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions crates/pulsar-core/src/pdk/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;

use semver::Version;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::{mpsc, oneshot};
Expand Down Expand Up @@ -209,6 +208,5 @@ pub enum PulsarDaemonCommand {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModuleOverview {
pub name: String,
pub version: Version,
pub status: ModuleStatus,
}
1 change: 0 additions & 1 deletion crates/pulsar-core/src/pdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ pub use config::*;
pub use daemon::*;
pub use module::*;
pub use module_context::*;
pub use semver::Version;
12 changes: 1 addition & 11 deletions crates/pulsar-core/src/pdk/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
};
use anyhow::Result;
use bpf_common::{program::BpfEvent, time::Timestamp, Pid};
use semver::Version;
use serde::{Deserialize, Serialize};
use tokio::sync::{
broadcast::{self, error::RecvError},
Expand All @@ -25,10 +24,9 @@ pub type ModuleStartFn = dyn Fn(ModuleContext, ShutdownSignal) -> Box<PulsarModu

/// Trait to implement to create a pulsar pluggable module
pub trait PulsarModule: Send {
const MODULE_NAME: &'static str;
const DEFAULT_ENABLED: bool;

fn name(&self) -> ModuleName;
fn details(&self) -> ModuleDetails;
fn start(
&self,
ctx: ModuleContext,
Expand Down Expand Up @@ -92,14 +90,6 @@ impl Validatron for ModuleName {
}
}

/// Contains module informations
#[derive(Debug, Clone)]
pub struct ModuleDetails {
pub version: Version,
pub enabled_by_default: bool,
// pub author: String,
}

/// Used to send events out from a module.
///
#[derive(Clone)]
Expand Down
17 changes: 2 additions & 15 deletions examples/pulsar-embedded-agent/proxy_module.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
use pulsar_core::pdk::{
CleanExit, Event, ModuleContext, ModuleDetails, ModuleError, ModuleName, PulsarModule,
ShutdownSignal, Version,
CleanExit, Event, ModuleContext, ModuleError, PulsarModule, ShutdownSignal,
};
use std::sync::Arc;
use tokio::sync::mpsc;

const MODULE_NAME: &str = "proxy-module";

pub struct ProxyModule {
pub tx_proxy: mpsc::Sender<Arc<Event>>,
}

impl PulsarModule for ProxyModule {
const MODULE_NAME: &'static str = "proxy-module";
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
18 changes: 3 additions & 15 deletions examples/pulsar-extension-module/my_custom_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,16 @@
use std::{collections::HashMap, sync::Arc};

use pulsar_core::pdk::{
CleanExit, ConfigError, Event, ModuleConfig, ModuleContext, ModuleDetails, ModuleError,
ModuleName, Payload, PulsarModule, ShutdownSignal, Version,
CleanExit, ConfigError, Event, ModuleConfig, ModuleContext, ModuleError, Payload, PulsarModule,
ShutdownSignal,
};

const MODULE_NAME: &str = "my-custom-module";

pub struct MyCustomModule;

impl PulsarModule for MyCustomModule {
const MODULE_NAME: &'static str = "my-custom-module";
const DEFAULT_ENABLED: bool = true;

fn name(&self) -> ModuleName {
MODULE_NAME.into()
}

fn details(&self) -> ModuleDetails {
ModuleDetails {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
enabled_by_default: Self::DEFAULT_ENABLED,
}
}

fn start(
&self,
ctx: ModuleContext,
Expand Down
2 changes: 0 additions & 2 deletions src/pulsar/term_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl TermPrintable for Vec<ModuleOverview> {

table.set_header(vec![
Cell::new("MODULE").add_attribute(Attribute::Bold),
Cell::new("VERSION").add_attribute(Attribute::Bold),
Cell::new("STATUS").add_attribute(Attribute::Bold),
]);

Expand All @@ -48,7 +47,6 @@ impl TermPrintable for Vec<ModuleOverview> {
Cell::new(module.name)
.fg(Color::Cyan)
.add_attribute(Attribute::Bold),
Cell::new(module.version),
Cell::new(status)
.fg(status_color)
.add_attribute(Attribute::Bold),
Expand Down
Loading

0 comments on commit b29a945

Please sign in to comment.