@@ -559,11 +559,13 @@ where
559559 }
560560 } ;
561561 let Some ( config) = select_l7_config_for_path ( configs, & route_target) else {
562+ let reason = "no L7 endpoint path matched request" ;
563+ emit_l7_request_log ( ctx, & req. action , & route_target, "deny" , "l7" , reason, None ) ;
562564 crate :: l7:: rest:: RestProvider :: default ( )
563565 . deny_with_redacted_target (
564566 & req,
565567 & ctx. policy_name ,
566- "no L7 endpoint path matched request" ,
568+ reason ,
567569 client,
568570 None ,
569571 Some ( crate :: l7:: rest:: DenyResponseContext :: from_l7_context ( ctx) ) ,
@@ -724,12 +726,12 @@ where
724726 l7_protocol_log_summary ( graphql_info. as_ref ( ) , jsonrpc_info. as_ref ( ) ) ;
725727 emit_l7_request_log (
726728 ctx,
727- & request_info,
729+ & request_info. action ,
728730 & redacted_target,
729731 decision_str,
730732 engine_type,
731733 & reason,
732- & protocol_summary,
734+ protocol_summary. as_deref ( ) ,
733735 ) ;
734736
735737 if allowed || ( config. enforcement == EnforcementMode :: Audit && !force_deny) {
@@ -935,13 +937,35 @@ fn select_l7_config_for_path<'a>(
935937
936938fn emit_l7_request_log (
937939 ctx : & L7EvalContext ,
938- request_info : & L7RequestInfo ,
940+ action : & str ,
939941 redacted_target : & str ,
940942 decision_str : & str ,
941943 engine_type : & str ,
942944 reason : & str ,
943- protocol_summary : & str ,
945+ protocol_summary : Option < & str > ,
944946) {
947+ let event = build_l7_request_event (
948+ ctx,
949+ action,
950+ redacted_target,
951+ decision_str,
952+ engine_type,
953+ reason,
954+ protocol_summary,
955+ ) ;
956+ ocsf_emit ! ( event) ;
957+ emit_activity ( ctx, decision_str == "deny" , "l7_policy" ) ;
958+ }
959+
960+ fn build_l7_request_event (
961+ ctx : & L7EvalContext ,
962+ action : & str ,
963+ redacted_target : & str ,
964+ decision_str : & str ,
965+ engine_type : & str ,
966+ reason : & str ,
967+ protocol_summary : Option < & str > ,
968+ ) -> openshell_ocsf:: OcsfEvent {
945969 let ( action_id, disposition_id, severity) = match decision_str {
946970 "deny" => ( ActionId :: Denied , DispositionId :: Blocked , SeverityId :: Medium ) ,
947971 "allow" | "audit" => (
@@ -955,43 +979,44 @@ fn emit_l7_request_log(
955979 SeverityId :: Informational ,
956980 ) ,
957981 } ;
958- let event = HttpActivityBuilder :: new ( openshell_ocsf:: ctx:: ctx ( ) )
982+ let protocol_suffix =
983+ protocol_summary. map_or_else ( String :: new, |summary| format ! ( " {summary}" ) ) ;
984+ let message = format ! (
985+ "L7_REQUEST {decision_str} {action} {}:{}{}{protocol_suffix} reason={reason}" ,
986+ ctx. host, ctx. port, redacted_target,
987+ ) ;
988+ HttpActivityBuilder :: new ( openshell_ocsf:: ctx:: ctx ( ) )
959989 . activity ( ActivityId :: Other )
960990 . action ( action_id)
961991 . disposition ( disposition_id)
962992 . severity ( severity)
963993 . http_request ( HttpRequest :: new (
964- & request_info . action ,
994+ action,
965995 OcsfUrl :: new ( "http" , & ctx. host , redacted_target, ctx. port ) ,
966996 ) )
967997 . dst_endpoint ( Endpoint :: from_domain ( & ctx. host , ctx. port ) )
968998 . firewall_rule ( & ctx. policy_name , engine_type)
969- . message ( format ! (
970- "L7_REQUEST {decision_str} {} {}:{}{}{} reason={}" ,
971- request_info. action, ctx. host, ctx. port, redacted_target, protocol_summary, reason,
972- ) )
973- . build ( ) ;
974- ocsf_emit ! ( event) ;
975- emit_activity ( ctx, decision_str == "deny" , "l7_policy" ) ;
999+ . message ( message)
1000+ . build ( )
9761001}
9771002
9781003fn l7_protocol_log_summary (
9791004 graphql_info : Option < & crate :: l7:: graphql:: GraphqlRequestInfo > ,
9801005 jsonrpc_info : Option < & crate :: l7:: jsonrpc:: JsonRpcRequestInfo > ,
981- ) -> String {
1006+ ) -> Option < String > {
9821007 if let Some ( info) = graphql_info {
983- return format ! ( " {}" , crate :: l7:: graphql:: log_summary( info) ) ;
1008+ return Some ( crate :: l7:: graphql:: log_summary ( info) ) ;
9841009 }
9851010
9861011 if let Some ( info) = jsonrpc_info {
987- return format ! (
988- " rule_methods={} tools={}" ,
1012+ return Some ( format ! (
1013+ "rule_methods={} tools={}" ,
9891014 rule_method_names_for_log( info) ,
9901015 tool_names_for_log( info)
991- ) ;
1016+ ) ) ;
9921017 }
9931018
994- String :: new ( )
1019+ None
9951020}
9961021
9971022fn emit_activity ( ctx : & L7EvalContext , denied : bool , deny_group : & ' static str ) {
@@ -7360,6 +7385,95 @@ network_policies:
73607385 }
73617386 }
73627387
7388+ #[ test]
7389+ fn unmatched_route_path_builds_denied_http_activity_event ( ) {
7390+ let ctx = L7EvalContext {
7391+ host : "gateway.example.test" . into ( ) ,
7392+ port : 443 ,
7393+ policy_name : "route_api" . into ( ) ,
7394+ ..Default :: default ( )
7395+ } ;
7396+
7397+ let event = build_l7_request_event (
7398+ & ctx,
7399+ "GET" ,
7400+ "/other" ,
7401+ "deny" ,
7402+ "l7" ,
7403+ "no L7 endpoint path matched request" ,
7404+ None ,
7405+ ) ;
7406+
7407+ assert_eq ! ( event. class_uid( ) , 4002 ) ;
7408+ assert_eq ! ( event. base( ) . severity, SeverityId :: Medium ) ;
7409+ assert_eq ! (
7410+ event. format_shorthand( ) ,
7411+ "HTTP:GET [MED] DENIED GET http://gateway.example.test:443/other [policy:route_api engine:l7] [reason:L7_REQUEST deny GET gateway.example.test:443/other reason=no L7 endpoint path matched request]"
7412+ ) ;
7413+ }
7414+
7415+ #[ tokio:: test]
7416+ async fn route_selected_unmatched_path_emits_denied_policy_activity ( ) {
7417+ let engine = OpaEngine :: from_strings ( TEST_POLICY , ENCODED_SLASH_SCOPING_POLICY ) . unwrap ( ) ;
7418+ let tunnel_engine = engine
7419+ . clone_engine_for_tunnel ( engine. current_generation ( ) )
7420+ . unwrap ( ) ;
7421+ let configs = encoded_slash_scoping_configs ( ) ;
7422+ let ( activity_tx, mut activity_rx) = tokio:: sync:: mpsc:: channel ( 1 ) ;
7423+ let ctx = L7EvalContext {
7424+ activity_tx : Some ( activity_tx) ,
7425+ ..encoded_slash_scoping_ctx ( )
7426+ } ;
7427+ let ( mut app, mut relay_client) = tokio:: io:: duplex ( 8192 ) ;
7428+ let ( mut relay_upstream, mut upstream) = tokio:: io:: duplex ( 8192 ) ;
7429+ let relay = tokio:: spawn ( async move {
7430+ relay_with_route_selection (
7431+ & configs,
7432+ tunnel_engine,
7433+ & mut relay_client,
7434+ & mut relay_upstream,
7435+ & ctx,
7436+ )
7437+ . await
7438+ } ) ;
7439+
7440+ app. write_all (
7441+ b"GET /other HTTP/1.1\r \n Host: gateway.example.test\r \n Connection: close\r \n \r \n " ,
7442+ )
7443+ . await
7444+ . unwrap ( ) ;
7445+
7446+ let mut response = [ 0u8 ; 1024 ] ;
7447+ let n = tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 1 ) , app. read ( & mut response) )
7448+ . await
7449+ . expect ( "denial should reach client" )
7450+ . unwrap ( ) ;
7451+ assert ! ( String :: from_utf8_lossy( & response[ ..n] ) . contains( "403 Forbidden" ) ) ;
7452+
7453+ let mut upstream_bytes = [ 0u8 ; 16 ] ;
7454+ assert ! ( matches!(
7455+ tokio:: time:: timeout(
7456+ std:: time:: Duration :: from_millis( 100 ) ,
7457+ upstream. read( & mut upstream_bytes)
7458+ )
7459+ . await ,
7460+ Err ( _) | Ok ( Ok ( 0 ) )
7461+ ) ) ;
7462+ let activity = tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 1 ) , activity_rx. recv ( ) )
7463+ . await
7464+ . expect ( "policy activity should be emitted" )
7465+ . expect ( "activity channel should remain open" ) ;
7466+ assert ! ( activity. denied) ;
7467+ assert_eq ! ( activity. deny_group, "l7_policy" ) ;
7468+
7469+ drop ( app) ;
7470+ tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 1 ) , relay)
7471+ . await
7472+ . expect ( "relay should finish" )
7473+ . unwrap ( )
7474+ . unwrap ( ) ;
7475+ }
7476+
73637477 /// Canonicalization runs before the matching config is known, so
73647478 /// `allow_encoded_slash` is taken permissively across the whole
73657479 /// host:port. The endpoint that did *not* opt in must still reject a
0 commit comments