@@ -10,7 +10,7 @@ use crate::events::{NetworkActivityEvent, OcsfEvent};
1010use crate :: objects:: { Actor , ConnectionInfo , Endpoint , FirewallRule } ;
1111
1212/// Builder for Network Activity [4001] events.
13- pub struct NetworkActivityBuilder < ' a > {
13+ pub struct NetworkActivityBuilder < ' a , EndpointState = MissingNetworkEndpoint > {
1414 ctx : & ' a EventContext ,
1515 activity : ActivityId ,
1616 activity_name : Option < String > ,
@@ -28,10 +28,35 @@ pub struct NetworkActivityBuilder<'a> {
2828 status_detail : Option < String > ,
2929 unmapped : Option < serde_json:: Map < String , serde_json:: Value > > ,
3030 log_source : Option < String > ,
31+ endpoint_state : std:: marker:: PhantomData < EndpointState > ,
3132}
3233
33- impl < ' a > NetworkActivityBuilder < ' a > {
34+ /// Marker for a Network Activity builder without an endpoint.
35+ pub struct MissingNetworkEndpoint ;
36+
37+ /// Marker for a Network Activity builder with a source or destination endpoint.
38+ pub struct HasNetworkEndpoint ;
39+
40+ impl < ' a > NetworkActivityBuilder < ' a , MissingNetworkEndpoint > {
3441 /// Start building a Network Activity event.
42+ ///
43+ /// A Network Activity must include a source or destination endpoint before
44+ /// it can be built.
45+ ///
46+ /// ```compile_fail
47+ /// use openshell_ocsf::{EventContext, NetworkActivityBuilder};
48+ ///
49+ /// let ctx = EventContext {
50+ /// sandbox_id: String::new(),
51+ /// sandbox_name: String::new(),
52+ /// container_image: String::new(),
53+ /// hostname: String::new(),
54+ /// product_version: String::new(),
55+ /// proxy_ip: "127.0.0.1".parse().unwrap(),
56+ /// proxy_port: 3128,
57+ /// };
58+ /// NetworkActivityBuilder::new(&ctx).build();
59+ /// ```
3560 #[ must_use]
3661 pub fn new ( ctx : & ' a EventContext ) -> Self {
3762 Self {
@@ -52,6 +77,92 @@ impl<'a> NetworkActivityBuilder<'a> {
5277 status_detail : None ,
5378 unmapped : None ,
5479 log_source : None ,
80+ endpoint_state : std:: marker:: PhantomData ,
81+ }
82+ }
83+
84+ #[ must_use]
85+ pub fn src_endpoint_addr (
86+ self ,
87+ ip : std:: net:: IpAddr ,
88+ port : u16 ,
89+ ) -> NetworkActivityBuilder < ' a , HasNetworkEndpoint > {
90+ self . with_src_endpoint ( Endpoint :: from_ip ( ip, port) )
91+ }
92+
93+ #[ must_use]
94+ pub fn dst_endpoint (
95+ self ,
96+ endpoint : Endpoint ,
97+ ) -> NetworkActivityBuilder < ' a , HasNetworkEndpoint > {
98+ self . with_dst_endpoint ( endpoint)
99+ }
100+ }
101+
102+ impl NetworkActivityBuilder < ' _ , HasNetworkEndpoint > {
103+ #[ must_use]
104+ pub fn src_endpoint_addr ( mut self , ip : std:: net:: IpAddr , port : u16 ) -> Self {
105+ self . src_endpoint = Some ( Endpoint :: from_ip ( ip, port) ) ;
106+ self
107+ }
108+
109+ #[ must_use]
110+ pub fn dst_endpoint ( mut self , endpoint : Endpoint ) -> Self {
111+ self . dst_endpoint = Some ( endpoint) ;
112+ self
113+ }
114+ }
115+
116+ impl < ' a , EndpointState > NetworkActivityBuilder < ' a , EndpointState > {
117+ fn with_src_endpoint (
118+ self ,
119+ endpoint : Endpoint ,
120+ ) -> NetworkActivityBuilder < ' a , HasNetworkEndpoint > {
121+ NetworkActivityBuilder {
122+ ctx : self . ctx ,
123+ activity : self . activity ,
124+ activity_name : self . activity_name ,
125+ action : self . action ,
126+ disposition : self . disposition ,
127+ severity : self . severity ,
128+ status : self . status ,
129+ src_endpoint : Some ( endpoint) ,
130+ dst_endpoint : self . dst_endpoint ,
131+ actor : self . actor ,
132+ firewall_rule : self . firewall_rule ,
133+ connection_info : self . connection_info ,
134+ observation_point_id : self . observation_point_id ,
135+ message : self . message ,
136+ status_detail : self . status_detail ,
137+ unmapped : self . unmapped ,
138+ log_source : self . log_source ,
139+ endpoint_state : std:: marker:: PhantomData ,
140+ }
141+ }
142+
143+ fn with_dst_endpoint (
144+ self ,
145+ endpoint : Endpoint ,
146+ ) -> NetworkActivityBuilder < ' a , HasNetworkEndpoint > {
147+ NetworkActivityBuilder {
148+ ctx : self . ctx ,
149+ activity : self . activity ,
150+ activity_name : self . activity_name ,
151+ action : self . action ,
152+ disposition : self . disposition ,
153+ severity : self . severity ,
154+ status : self . status ,
155+ src_endpoint : self . src_endpoint ,
156+ dst_endpoint : Some ( endpoint) ,
157+ actor : self . actor ,
158+ firewall_rule : self . firewall_rule ,
159+ connection_info : self . connection_info ,
160+ observation_point_id : self . observation_point_id ,
161+ message : self . message ,
162+ status_detail : self . status_detail ,
163+ unmapped : self . unmapped ,
164+ log_source : self . log_source ,
165+ endpoint_state : std:: marker:: PhantomData ,
55166 }
56167 }
57168
@@ -90,6 +201,56 @@ impl<'a> NetworkActivityBuilder<'a> {
90201 self
91202 }
92203
204+ #[ must_use]
205+ pub fn activity ( mut self , id : ActivityId ) -> Self {
206+ self . activity = id;
207+ self
208+ }
209+
210+ #[ must_use]
211+ pub fn action ( mut self , id : ActionId ) -> Self {
212+ self . action = Some ( id) ;
213+ self
214+ }
215+
216+ #[ must_use]
217+ pub fn disposition ( mut self , id : DispositionId ) -> Self {
218+ self . disposition = Some ( id) ;
219+ self
220+ }
221+
222+ #[ must_use]
223+ pub fn actor_process ( mut self , process : crate :: objects:: Process ) -> Self {
224+ self . actor = Some ( Actor { process } ) ;
225+ self
226+ }
227+
228+ #[ must_use]
229+ pub fn firewall_rule ( mut self , name : & str , rule_type : & str ) -> Self {
230+ self . firewall_rule = Some ( FirewallRule :: new ( name, rule_type) ) ;
231+ self
232+ }
233+
234+ #[ must_use]
235+ pub fn severity ( mut self , id : SeverityId ) -> Self {
236+ self . severity = id;
237+ self
238+ }
239+
240+ #[ must_use]
241+ pub fn status ( mut self , id : StatusId ) -> Self {
242+ self . status = Some ( id) ;
243+ self
244+ }
245+
246+ #[ must_use]
247+ pub fn message ( mut self , msg : impl Into < String > ) -> Self {
248+ self . message = Some ( msg. into ( ) ) ;
249+ self
250+ }
251+ }
252+
253+ impl NetworkActivityBuilder < ' _ , HasNetworkEndpoint > {
93254 /// Finalize and return the `OcsfEvent`.
94255 #[ must_use]
95256 pub fn build ( self ) -> OcsfEvent {
@@ -139,14 +300,6 @@ impl<'a> NetworkActivityBuilder<'a> {
139300 }
140301}
141302
142- impl_activity_setter ! ( NetworkActivityBuilder ) ;
143- impl_action_disposition_setters ! ( NetworkActivityBuilder ) ;
144- impl_actor_process_setter ! ( NetworkActivityBuilder ) ;
145- impl_dst_endpoint_setter ! ( NetworkActivityBuilder ) ;
146- impl_src_endpoint_addr_setter ! ( NetworkActivityBuilder ) ;
147- impl_firewall_rule_setter ! ( NetworkActivityBuilder ) ;
148- impl_builder_setters ! ( NetworkActivityBuilder ) ;
149-
150303#[ cfg( test) ]
151304mod tests {
152305 use super :: * ;
0 commit comments