Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 6 additions & 48 deletions crates/openshell-ocsf/src/builders/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,9 @@ impl<'a> HttpActivityBuilder<'a, MissingHttpContext> {
/// be built.
///
/// ```compile_fail
/// use openshell_ocsf::{EventContext, EventOrigin, HttpActivityBuilder};
/// use openshell_ocsf::HttpActivityBuilder;
///
/// let ctx = EventContext {
/// sandbox_id: String::new(),
/// sandbox_name: String::new(),
/// container_image: String::new(),
/// hostname: String::new(),
/// product_version: String::new(),
/// proxy_ip: "127.0.0.1".parse().unwrap(),
/// proxy_port: 3128,
/// origin: EventOrigin::Supervisor,
/// };
/// HttpActivityBuilder::new(&ctx).build();
/// HttpActivityBuilder::new(openshell_ocsf::ctx::ctx()).build();
/// ```
#[must_use]
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
Expand Down Expand Up @@ -164,24 +154,6 @@ impl<'a, Context> HttpActivityBuilder<'a, Context> {
self
}

#[must_use]
pub fn activity(mut self, id: ActivityId) -> Self {
self.activity = id;
self
}

#[must_use]
pub fn action(mut self, id: ActionId) -> Self {
self.action = Some(id);
self
}

#[must_use]
pub fn disposition(mut self, id: DispositionId) -> Self {
self.disposition = Some(id);
self
}

#[must_use]
pub fn actor_process(mut self, process: crate::objects::Process) -> Self {
self.actor = Some(Actor::process(process));
Expand All @@ -199,26 +171,12 @@ impl<'a, Context> HttpActivityBuilder<'a, Context> {
self.firewall_rule = Some(FirewallRule::new(name, rule_type));
self
}

#[must_use]
pub fn severity(mut self, id: SeverityId) -> Self {
self.severity = id;
self
}

#[must_use]
pub fn status(mut self, id: StatusId) -> Self {
self.status = Some(id);
self
}

#[must_use]
pub fn message(mut self, msg: impl Into<String>) -> Self {
self.message = Some(msg.into());
self
}
}

impl_activity_setter!(HttpActivityBuilder<Context>);
impl_action_disposition_setters!(HttpActivityBuilder<Context>);
impl_builder_setters!(HttpActivityBuilder<Context>);

impl HttpActivityBuilder<'_, HasHttpContext> {
#[must_use]
pub fn build(self) -> OcsfEvent {
Expand Down
21 changes: 11 additions & 10 deletions crates/openshell-ocsf/src/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
/// ```ignore
/// impl_builder_setters!(MyBuilder); // severity + status + message
/// impl_builder_setters!(MyBuilder, no_status); // severity + message only
/// impl_builder_setters!(MyBuilder<State>); // for a type-state builder
/// ```
macro_rules! impl_builder_setters {
($builder:ident) => {
impl<'a> $builder<'a> {
($builder:ident $(<$state:ident>)?) => {
impl<'a $(, $state)?> $builder<'a $(, $state)?> {
/// Set the event severity.
#[must_use]
pub fn severity(mut self, id: $crate::enums::SeverityId) -> Self {
Expand All @@ -37,8 +38,8 @@ macro_rules! impl_builder_setters {
}
}
};
($builder:ident, no_status) => {
impl<'a> $builder<'a> {
($builder:ident $(<$state:ident>)?, no_status) => {
impl<'a $(, $state)?> $builder<'a $(, $state)?> {
/// Set the event severity.
#[must_use]
pub fn severity(mut self, id: $crate::enums::SeverityId) -> Self {
Expand All @@ -58,8 +59,8 @@ macro_rules! impl_builder_setters {
/// Generate the `activity` setter used by every event builder that carries an
/// `activity` field.
macro_rules! impl_activity_setter {
($builder:ident) => {
impl<'a> $builder<'a> {
($builder:ident $(<$state:ident>)?) => {
impl<'a $(, $state)?> $builder<'a $(, $state)?> {
/// Set the event activity identifier.
#[must_use]
pub fn activity(mut self, id: $crate::enums::ActivityId) -> Self {
Expand All @@ -70,11 +71,11 @@ macro_rules! impl_activity_setter {
};
}

/// Generate `action` and `disposition` setters shared by network, HTTP, SSH,
/// process, and detection-finding builders.
/// Generate `action` and `disposition` setters shared by every builder whose
/// event records an action and its disposition.
macro_rules! impl_action_disposition_setters {
($builder:ident) => {
impl<'a> $builder<'a> {
($builder:ident $(<$state:ident>)?) => {
impl<'a $(, $state)?> $builder<'a $(, $state)?> {
/// Set the action taken.
#[must_use]
pub fn action(mut self, id: $crate::enums::ActionId) -> Self {
Expand Down
54 changes: 6 additions & 48 deletions crates/openshell-ocsf/src/builders/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,9 @@ impl<'a> NetworkActivityBuilder<'a, MissingNetworkEndpoint> {
/// it can be built.
///
/// ```compile_fail
/// use openshell_ocsf::{EventContext, EventOrigin, NetworkActivityBuilder};
/// use openshell_ocsf::NetworkActivityBuilder;
///
/// let ctx = EventContext {
/// sandbox_id: String::new(),
/// sandbox_name: String::new(),
/// container_image: String::new(),
/// hostname: String::new(),
/// product_version: String::new(),
/// proxy_ip: "127.0.0.1".parse().unwrap(),
/// proxy_port: 3128,
/// origin: EventOrigin::Supervisor,
/// };
/// NetworkActivityBuilder::new(&ctx).build();
/// NetworkActivityBuilder::new(openshell_ocsf::ctx::ctx()).build();
/// ```
#[must_use]
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
Expand Down Expand Up @@ -202,24 +192,6 @@ impl<'a, EndpointState> NetworkActivityBuilder<'a, EndpointState> {
self
}

#[must_use]
pub fn activity(mut self, id: ActivityId) -> Self {
self.activity = id;
self
}

#[must_use]
pub fn action(mut self, id: ActionId) -> Self {
self.action = Some(id);
self
}

#[must_use]
pub fn disposition(mut self, id: DispositionId) -> Self {
self.disposition = Some(id);
self
}

#[must_use]
pub fn actor_process(mut self, process: crate::objects::Process) -> Self {
self.actor = Some(Actor::process(process));
Expand All @@ -231,26 +203,12 @@ impl<'a, EndpointState> NetworkActivityBuilder<'a, EndpointState> {
self.firewall_rule = Some(FirewallRule::new(name, rule_type));
self
}

#[must_use]
pub fn severity(mut self, id: SeverityId) -> Self {
self.severity = id;
self
}

#[must_use]
pub fn status(mut self, id: StatusId) -> Self {
self.status = Some(id);
self
}

#[must_use]
pub fn message(mut self, msg: impl Into<String>) -> Self {
self.message = Some(msg.into());
self
}
}

impl_activity_setter!(NetworkActivityBuilder<EndpointState>);
impl_action_disposition_setters!(NetworkActivityBuilder<EndpointState>);
impl_builder_setters!(NetworkActivityBuilder<EndpointState>);

impl NetworkActivityBuilder<'_, HasNetworkEndpoint> {
/// Finalize and return the `OcsfEvent`.
#[must_use]
Expand Down
49 changes: 6 additions & 43 deletions crates/openshell-ocsf/src/builders/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,9 @@ impl<'a> ProcessActivityBuilder<'a, MissingActor> {
/// A Process Activity must name its acting process before it can be built.
///
/// ```compile_fail
/// use openshell_ocsf::{EventContext, EventOrigin, ProcessActivityBuilder};
/// use openshell_ocsf::ProcessActivityBuilder;
///
/// let ctx = EventContext {
/// sandbox_id: String::new(),
/// sandbox_name: String::new(),
/// container_image: String::new(),
/// hostname: String::new(),
/// product_version: String::new(),
/// proxy_ip: "127.0.0.1".parse().unwrap(),
/// proxy_port: 3128,
/// origin: EventOrigin::Supervisor,
/// };
/// ProcessActivityBuilder::new(&ctx).build();
/// ProcessActivityBuilder::new(openshell_ocsf::ctx::ctx()).build();
/// ```
#[must_use]
pub fn new(ctx: impl Into<ContextRef<'a>>) -> Self {
Expand Down Expand Up @@ -90,43 +80,12 @@ impl<'a> ProcessActivityBuilder<'a, MissingActor> {
}

impl<ActorState> ProcessActivityBuilder<'_, ActorState> {
/// Set the event activity identifier.
#[must_use]
pub fn activity(mut self, id: ActivityId) -> Self {
self.activity = id;
self
}
/// Set the action taken.
#[must_use]
pub fn action(mut self, id: ActionId) -> Self {
self.action = Some(id);
self
}
/// Set the disposition of the action.
#[must_use]
pub fn disposition(mut self, id: DispositionId) -> Self {
self.disposition = Some(id);
self
}
/// Set the event severity.
#[must_use]
pub fn severity(mut self, id: SeverityId) -> Self {
self.severity = id;
self
}
/// Set the overall event status.
#[must_use]
pub fn status(mut self, id: StatusId) -> Self {
self.status = Some(id);
self
}
/// Set a human-readable event message.
#[must_use]
pub fn message(mut self, msg: impl Into<String>) -> Self {
self.message = Some(msg.into());
self
}
#[must_use]
pub fn process(mut self, proc: Process) -> Self {
self.process = Some(proc);
self
Expand All @@ -143,6 +102,10 @@ impl<ActorState> ProcessActivityBuilder<'_, ActorState> {
}
}

impl_activity_setter!(ProcessActivityBuilder<ActorState>);
impl_action_disposition_setters!(ProcessActivityBuilder<ActorState>);
impl_builder_setters!(ProcessActivityBuilder<ActorState>);

impl ProcessActivityBuilder<'_, HasActor> {
#[must_use]
pub fn build(self) -> OcsfEvent {
Expand Down
Loading
Loading