99
1010use openshell_ocsf:: {
1111 ActionId , ActivityId , ApiActivityBuilder , AuthProtocolId , AuthenticationBuilder , ConfidenceId ,
12- DetectionFindingBuilder , DispositionId , Endpoint , FindingInfo , HttpMethod , HttpRequest ,
13- ResourceDetails , Service , SeverityId , StatusId , Url , User , UserTypeId , ocsf_emit,
12+ DetectionFindingBuilder , DispositionId , Endpoint , EventContext , FindingInfo , HttpMethod ,
13+ HttpRequest , ResourceDetails , Service , SeverityId , StatusId , Url , User , UserTypeId , ocsf_emit,
1414} ;
1515use std:: collections:: { HashSet , VecDeque } ;
1616use std:: future:: Future ;
1717use std:: net:: SocketAddr ;
1818use std:: sync:: { Arc , Mutex , PoisonError } ;
1919use std:: time:: { Duration , Instant } ;
20+ use tonic:: Status ;
2021
2122use super :: identity:: IdentityProvider ;
2223use super :: principal:: { Principal , SandboxIdentitySource } ;
@@ -60,21 +61,7 @@ impl RequestOrigin {
6061 }
6162}
6263
63- use super :: authenticator:: { AuthFailureReason , AuthMechanism , bearer_credential} ;
64-
65- fn category ( reason : AuthFailureReason ) -> & ' static str {
66- match reason {
67- AuthFailureReason :: MissingCredential => "missing_credential" ,
68- AuthFailureReason :: ConflictingCredentials => "conflicting_credentials" ,
69- AuthFailureReason :: MalformedCredential => "malformed_credential" ,
70- AuthFailureReason :: InvalidCredential => "invalid_credential" ,
71- AuthFailureReason :: ExpiredCredential => "expired_credential" ,
72- AuthFailureReason :: AudienceOrIssuerMismatch => "audience_or_issuer_mismatch" ,
73- AuthFailureReason :: UnknownIdentity => "unknown_identity" ,
74- AuthFailureReason :: StaleIdentity => "stale_identity" ,
75- AuthFailureReason :: IdentityProviderUnavailable => "identity_provider_unavailable" ,
76- }
77- }
64+ use super :: authenticator:: { AuthFailureReason , AuthMechanism , AuthRejection , bearer_credential} ;
7865
7966fn severity ( reason : AuthFailureReason ) -> SeverityId {
8067 match reason {
@@ -91,18 +78,37 @@ fn severity(reason: AuthFailureReason) -> SeverityId {
9178 }
9279}
9380
94- fn with_protocol (
95- builder : AuthenticationBuilder < ' _ > ,
96- mechanism : AuthMechanism ,
97- ) -> AuthenticationBuilder < ' _ > {
81+ /// The request to `path` as an OCSF HTTP request; gRPC calls are POSTs.
82+ fn post ( path : & str ) -> HttpRequest {
83+ HttpRequest {
84+ http_method : HttpMethod :: Post ,
85+ url : Some ( Url :: from_path ( path) ) ,
86+ }
87+ }
88+
89+ /// An Authentication event for `user`'s request to `path` from `origin`,
90+ /// naming the protocol of `mechanism` when known.
91+ fn authentication < ' a > (
92+ ctx : & ' a EventContext ,
93+ path : & str ,
94+ origin : & RequestOrigin ,
95+ user : User ,
96+ mechanism : Option < AuthMechanism > ,
97+ ) -> AuthenticationBuilder < ' a > {
98+ let mut builder = AuthenticationBuilder :: new ( ctx, user, Service :: new ( "openshell-gateway" ) )
99+ . http_request ( post ( path) ) ;
98100 match mechanism {
99- AuthMechanism :: Oidc => builder. auth_protocol ( AuthProtocolId :: OpenId ) ,
100- AuthMechanism :: Mtls => builder. auth_protocol_other ( "mtls" ) ,
101- AuthMechanism :: SandboxJwt => builder . auth_protocol_other ( "sandbox_jwt" ) ,
102- AuthMechanism :: ComputeDriver => builder . auth_protocol_other ( "compute_driver" ) ,
103- AuthMechanism :: GatewayPeer => builder . auth_protocol_other ( "k8s_service_account" ) ,
104- AuthMechanism :: SshSessionToken => builder. auth_protocol_other ( "ssh_session_token" ) ,
101+ Some ( AuthMechanism :: Oidc ) => builder = builder. auth_protocol ( AuthProtocolId :: OpenId ) ,
102+ Some ( mechanism ) => builder = builder . auth_protocol_other ( mechanism . as_str ( ) ) ,
103+ None => { }
104+ }
105+ if let Some ( source ) = origin . source {
106+ builder = builder. src_endpoint ( source . endpoint ( ) ) ;
105107 }
108+ if let Some ( request_id) = & origin. request_id {
109+ builder = builder. correlation_uid ( request_id) ;
110+ }
111+ builder
106112}
107113
108114/// Emit an Authentication failure event for a gRPC request to `path`.
@@ -116,20 +122,21 @@ pub fn emit_authentication_failure(
116122}
117123
118124/// Emit an Authentication failure event for a credential that a handler
119- /// verifies itself, after the router established `principal` .
120- pub fn emit_credential_failure (
121- principal : & Principal ,
122- mechanism : AuthMechanism ,
123- reason : AuthFailureReason ,
124- ) {
125+ /// verifies itself, returning the status that refuses the current request .
126+ ///
127+ /// The event names ` principal`, the caller the router established from a
128+ /// separate credential, or leaves the user unidentified when the rejected
129+ /// credential is the one that named the caller.
130+ pub fn reject_credential ( principal : Option < & Principal > , rejection : AuthRejection ) -> Status {
125131 let ( path, origin) = current_request ( ) ;
126132 authentication_failure (
127133 & path,
128134 & origin,
129- principal_user ( principal) ,
130- Some ( mechanism) ,
131- reason,
135+ principal. map_or_else ( User :: unidentified , principal_user ) ,
136+ Some ( rejection . mechanism ) ,
137+ rejection . reason ,
132138 ) ;
139+ rejection. status
133140}
134141
135142fn authentication_failure (
@@ -140,26 +147,13 @@ fn authentication_failure(
140147 reason : AuthFailureReason ,
141148) {
142149 let ctx = crate :: gateway_ocsf:: context ( "" , "" ) ;
143- let mut builder = AuthenticationBuilder :: new ( & ctx, user, Service :: new ( "openshell-gateway" ) )
150+ let builder = authentication ( & ctx, path , origin , user, mechanism )
144151 . action ( ActionId :: Denied )
145152 . disposition ( DispositionId :: Blocked )
146153 . severity ( severity ( reason) )
147154 . status ( StatusId :: Failure )
148- . status_detail ( category ( reason) )
149- . http_request ( HttpRequest {
150- http_method : HttpMethod :: Post ,
151- url : Some ( Url :: from_path ( path) ) ,
152- } )
153- . message ( format ! ( "authentication failed: {}" , category( reason) ) ) ;
154- if let Some ( mechanism) = mechanism {
155- builder = with_protocol ( builder, mechanism) ;
156- }
157- if let Some ( source) = origin. source {
158- builder = builder. src_endpoint ( source. endpoint ( ) ) ;
159- }
160- if let Some ( request_id) = & origin. request_id {
161- builder = builder. correlation_uid ( request_id) ;
162- }
155+ . status_detail ( reason. as_str ( ) )
156+ . message ( format ! ( "authentication failed: {}" , reason. as_str( ) ) ) ;
163157 ocsf_emit ! ( builder. build( ) ) ;
164158}
165159
@@ -271,7 +265,7 @@ fn digest(
271265 let mut material = principal. audit_actor ( ) . into_bytes ( ) ;
272266 material. push ( 0 ) ;
273267 if let Some ( mechanism) = mechanism {
274- material. extend_from_slice ( format ! ( "{ mechanism:?}" ) . as_bytes ( ) ) ;
268+ material. extend_from_slice ( mechanism. as_str ( ) . as_bytes ( ) ) ;
275269 material. push ( 0 ) ;
276270 }
277271 material. extend_from_slice ( credential. as_bytes ( ) ) ;
@@ -355,29 +349,12 @@ fn logon(
355349 mechanism : Option < AuthMechanism > ,
356350) {
357351 let ctx = crate :: gateway_ocsf:: context ( "" , "" ) ;
358- let mut builder = AuthenticationBuilder :: new (
359- & ctx,
360- principal_user ( principal) ,
361- Service :: new ( "openshell-gateway" ) ,
362- )
363- . action ( ActionId :: Allowed )
364- . disposition ( DispositionId :: Allowed )
365- . severity ( SeverityId :: Informational )
366- . status ( StatusId :: Success )
367- . http_request ( HttpRequest {
368- http_method : HttpMethod :: Post ,
369- url : Some ( Url :: from_path ( path) ) ,
370- } )
371- . message ( "authentication succeeded" ) ;
372- if let Some ( mechanism) = mechanism {
373- builder = with_protocol ( builder, mechanism) ;
374- }
375- if let Some ( source) = origin. source {
376- builder = builder. src_endpoint ( source. endpoint ( ) ) ;
377- }
378- if let Some ( request_id) = & origin. request_id {
379- builder = builder. correlation_uid ( request_id) ;
380- }
352+ let builder = authentication ( & ctx, path, origin, principal_user ( principal) , mechanism)
353+ . action ( ActionId :: Allowed )
354+ . disposition ( DispositionId :: Allowed )
355+ . severity ( SeverityId :: Informational )
356+ . status ( StatusId :: Success )
357+ . message ( "authentication succeeded" ) ;
381358 ocsf_emit ! ( builder. build( ) ) ;
382359}
383360
@@ -536,6 +513,13 @@ pub fn emit_authorization_denial(principal: &Principal, denial: AuthzDenial<'_>)
536513 emit_authorization_denial_for ( & path, & origin, principal, denial) ;
537514}
538515
516+ /// Emit an authorization denial raised by a handler for the current request,
517+ /// returning the status that refuses it with `message`.
518+ pub fn deny ( principal : & Principal , denial : AuthzDenial < ' _ > , message : impl Into < String > ) -> Status {
519+ emit_authorization_denial ( principal, denial) ;
520+ Status :: permission_denied ( message)
521+ }
522+
539523/// Emit an API Activity denial for a gRPC request to `path`, paired with a
540524/// Detection Finding when a sandbox principal crossed its boundary.
541525pub fn emit_authorization_denial_for (
@@ -573,10 +557,7 @@ pub fn emit_authorization_denial_for(
573557 if !service. is_empty ( ) {
574558 builder = builder
575559 . api_service ( Service :: new ( service) )
576- . http_request ( HttpRequest {
577- http_method : HttpMethod :: Post ,
578- url : Some ( Url :: from_path ( path) ) ,
579- } ) ;
560+ . http_request ( post ( path) ) ;
580561 }
581562 if let Some ( resource) = denial. resource ( ) {
582563 builder = builder. resource ( resource) ;
0 commit comments