@@ -10,7 +10,7 @@ use crate::events::{HttpActivityEvent, OcsfEvent};
1010use crate :: objects:: { Actor , Endpoint , FirewallRule , HttpRequest , HttpResponse } ;
1111
1212/// Builder for HTTP Activity [4002] events.
13- pub struct HttpActivityBuilder < ' a > {
13+ pub struct HttpActivityBuilder < ' a , Context = MissingHttpContext > {
1414 ctx : & ' a EventContext ,
1515 activity : ActivityId ,
1616 action : Option < ActionId > ,
@@ -26,9 +26,35 @@ pub struct HttpActivityBuilder<'a> {
2626 message : Option < String > ,
2727 status_detail : Option < String > ,
2828 unmapped : serde_json:: Map < String , serde_json:: Value > ,
29+ context : std:: marker:: PhantomData < Context > ,
2930}
3031
31- impl < ' a > HttpActivityBuilder < ' a > {
32+ /// Marker for an HTTP Activity builder that has neither request nor response.
33+ pub struct MissingHttpContext ;
34+
35+ /// Marker for an HTTP Activity builder that has a request or response.
36+ pub struct HasHttpContext ;
37+
38+ impl < ' a > HttpActivityBuilder < ' a , MissingHttpContext > {
39+ /// Start building an HTTP Activity event.
40+ ///
41+ /// An HTTP Activity must include an HTTP request or response before it can
42+ /// be built.
43+ ///
44+ /// ```compile_fail
45+ /// use openshell_ocsf::{EventContext, HttpActivityBuilder};
46+ ///
47+ /// let ctx = EventContext {
48+ /// sandbox_id: String::new(),
49+ /// sandbox_name: String::new(),
50+ /// container_image: String::new(),
51+ /// hostname: String::new(),
52+ /// product_version: String::new(),
53+ /// proxy_ip: "127.0.0.1".parse().unwrap(),
54+ /// proxy_port: 3128,
55+ /// };
56+ /// HttpActivityBuilder::new(&ctx).build();
57+ /// ```
3258 #[ must_use]
3359 pub fn new ( ctx : & ' a EventContext ) -> Self {
3460 Self {
@@ -47,19 +73,78 @@ impl<'a> HttpActivityBuilder<'a> {
4773 message : None ,
4874 status_detail : None ,
4975 unmapped : serde_json:: Map :: new ( ) ,
76+ context : std:: marker:: PhantomData ,
5077 }
5178 }
5279
80+ #[ must_use]
81+ pub fn http_request ( self , req : HttpRequest ) -> HttpActivityBuilder < ' a , HasHttpContext > {
82+ self . with_http_request ( req)
83+ }
84+
85+ #[ must_use]
86+ pub fn http_response ( self , resp : HttpResponse ) -> HttpActivityBuilder < ' a , HasHttpContext > {
87+ self . with_http_response ( resp)
88+ }
89+ }
90+
91+ impl HttpActivityBuilder < ' _ , HasHttpContext > {
5392 #[ must_use]
5493 pub fn http_request ( mut self , req : HttpRequest ) -> Self {
5594 self . http_request = Some ( req) ;
5695 self
5796 }
97+
5898 #[ must_use]
5999 pub fn http_response ( mut self , resp : HttpResponse ) -> Self {
60100 self . http_response = Some ( resp) ;
61101 self
62102 }
103+ }
104+
105+ impl < ' a , Context > HttpActivityBuilder < ' a , Context > {
106+ fn with_http_request ( self , req : HttpRequest ) -> HttpActivityBuilder < ' a , HasHttpContext > {
107+ HttpActivityBuilder {
108+ ctx : self . ctx ,
109+ activity : self . activity ,
110+ action : self . action ,
111+ disposition : self . disposition ,
112+ severity : self . severity ,
113+ status : self . status ,
114+ http_request : Some ( req) ,
115+ http_response : self . http_response ,
116+ src_endpoint : self . src_endpoint ,
117+ dst_endpoint : self . dst_endpoint ,
118+ actor : self . actor ,
119+ firewall_rule : self . firewall_rule ,
120+ message : self . message ,
121+ status_detail : self . status_detail ,
122+ unmapped : self . unmapped ,
123+ context : std:: marker:: PhantomData ,
124+ }
125+ }
126+
127+ fn with_http_response ( self , resp : HttpResponse ) -> HttpActivityBuilder < ' a , HasHttpContext > {
128+ HttpActivityBuilder {
129+ ctx : self . ctx ,
130+ activity : self . activity ,
131+ action : self . action ,
132+ disposition : self . disposition ,
133+ severity : self . severity ,
134+ status : self . status ,
135+ http_request : self . http_request ,
136+ http_response : Some ( resp) ,
137+ src_endpoint : self . src_endpoint ,
138+ dst_endpoint : self . dst_endpoint ,
139+ actor : self . actor ,
140+ firewall_rule : self . firewall_rule ,
141+ message : self . message ,
142+ status_detail : self . status_detail ,
143+ unmapped : self . unmapped ,
144+ context : std:: marker:: PhantomData ,
145+ }
146+ }
147+
63148 #[ must_use]
64149 pub fn src_endpoint ( mut self , ep : Endpoint ) -> Self {
65150 self . src_endpoint = Some ( ep) ;
@@ -78,6 +163,62 @@ impl<'a> HttpActivityBuilder<'a> {
78163 self
79164 }
80165
166+ #[ must_use]
167+ pub fn activity ( mut self , id : ActivityId ) -> Self {
168+ self . activity = id;
169+ self
170+ }
171+
172+ #[ must_use]
173+ pub fn action ( mut self , id : ActionId ) -> Self {
174+ self . action = Some ( id) ;
175+ self
176+ }
177+
178+ #[ must_use]
179+ pub fn disposition ( mut self , id : DispositionId ) -> Self {
180+ self . disposition = Some ( id) ;
181+ self
182+ }
183+
184+ #[ must_use]
185+ pub fn actor_process ( mut self , process : crate :: objects:: Process ) -> Self {
186+ self . actor = Some ( Actor { process } ) ;
187+ self
188+ }
189+
190+ #[ must_use]
191+ pub fn dst_endpoint ( mut self , endpoint : Endpoint ) -> Self {
192+ self . dst_endpoint = Some ( endpoint) ;
193+ self
194+ }
195+
196+ #[ must_use]
197+ pub fn firewall_rule ( mut self , name : & str , rule_type : & str ) -> Self {
198+ self . firewall_rule = Some ( FirewallRule :: new ( name, rule_type) ) ;
199+ self
200+ }
201+
202+ #[ must_use]
203+ pub fn severity ( mut self , id : SeverityId ) -> Self {
204+ self . severity = id;
205+ self
206+ }
207+
208+ #[ must_use]
209+ pub fn status ( mut self , id : StatusId ) -> Self {
210+ self . status = Some ( id) ;
211+ self
212+ }
213+
214+ #[ must_use]
215+ pub fn message ( mut self , msg : impl Into < String > ) -> Self {
216+ self . message = Some ( msg. into ( ) ) ;
217+ self
218+ }
219+ }
220+
221+ impl HttpActivityBuilder < ' _ , HasHttpContext > {
81222 #[ must_use]
82223 pub fn build ( self ) -> OcsfEvent {
83224 let activity_name = self . activity . http_label ( ) . to_string ( ) ;
@@ -118,13 +259,6 @@ impl<'a> HttpActivityBuilder<'a> {
118259 }
119260}
120261
121- impl_activity_setter ! ( HttpActivityBuilder ) ;
122- impl_action_disposition_setters ! ( HttpActivityBuilder ) ;
123- impl_actor_process_setter ! ( HttpActivityBuilder ) ;
124- impl_dst_endpoint_setter ! ( HttpActivityBuilder ) ;
125- impl_firewall_rule_setter ! ( HttpActivityBuilder ) ;
126- impl_builder_setters ! ( HttpActivityBuilder ) ;
127-
128262#[ cfg( test) ]
129263mod tests {
130264 use super :: * ;
0 commit comments