19
19
use std:: {
20
20
collections:: HashMap ,
21
21
fmt:: { Debug , Display } ,
22
- sync:: Arc ,
23
22
} ;
24
23
25
24
use crate :: about:: current;
@@ -38,15 +37,19 @@ use url::Url;
38
37
39
38
static AUDIT_LOGGER : Lazy < Option < AuditLogger > > = Lazy :: new ( AuditLogger :: new) ;
40
39
40
+ // AuditLogger handles sending audit logs to a remote logging system
41
41
pub struct AuditLogger {
42
42
client : Client ,
43
43
log_endpoint : Url ,
44
44
}
45
45
46
46
impl AuditLogger {
47
- /// Create an audit logger that can be used to capture
48
- /// and push audit logs to the appropriate logging system over HTTP
47
+ /// Create an audit logger that can be used to capture and push
48
+ /// audit logs to the appropriate logging system over HTTP
49
49
pub fn new ( ) -> Option < AuditLogger > {
50
+ // Try to construct the log endpoint URL by joining the base URL
51
+ // with the ingest path, This can fail if the URL is not valid,
52
+ // when the base URL is not set or the ingest path is not valid
50
53
let log_endpoint = match CONFIG
51
54
. parseable
52
55
. audit_logger
@@ -66,6 +69,7 @@ impl AuditLogger {
66
69
} )
67
70
}
68
71
72
+ // Sends the audit log to the configured endpoint with proper authentication
69
73
async fn send_log ( & self , json : Value ) {
70
74
let mut req = self
71
75
. client
@@ -89,13 +93,15 @@ impl AuditLogger {
89
93
}
90
94
}
91
95
96
+ // Represents the version of the audit log format
92
97
#[ non_exhaustive]
93
98
#[ repr( u8 ) ]
94
99
#[ derive( Debug , Clone , Copy , Serialize ) ]
95
100
pub enum AuditLogVersion {
96
101
V1 = 1 ,
97
102
}
98
103
104
+ // Contains information about the actor (user) who performed the action
99
105
#[ derive( Serialize , Default ) ]
100
106
#[ serde( rename_all = "camelCase" ) ]
101
107
pub struct ActorLog {
@@ -105,6 +111,7 @@ pub struct ActorLog {
105
111
pub authorization_method : String ,
106
112
}
107
113
114
+ // Contains details about the HTTP request that was made
108
115
#[ derive( Serialize , Default ) ]
109
116
pub struct RequestLog {
110
117
pub method : String ,
@@ -113,23 +120,15 @@ pub struct RequestLog {
113
120
pub headers : HashMap < String , String > ,
114
121
}
115
122
116
- #[ derive( Serialize ) ]
123
+ /// Contains information about the response sent back to the client
124
+ #[ derive( Default , Serialize ) ]
117
125
#[ serde( rename_all = "camelCase" ) ]
118
126
pub struct ResponseLog {
119
127
pub status_code : u16 ,
120
128
pub error : Option < String > ,
121
129
}
122
130
123
- impl Default for ResponseLog {
124
- fn default ( ) -> Self {
125
- // Server failed to respond
126
- ResponseLog {
127
- status_code : 500 ,
128
- error : None ,
129
- }
130
- }
131
- }
132
-
131
+ /// The main audit log structure that combines all audit information
133
132
#[ derive( Serialize ) ]
134
133
#[ serde( rename_all = "camelCase" ) ]
135
134
pub struct AuditLog {
@@ -145,6 +144,7 @@ pub struct AuditLog {
145
144
pub response : ResponseLog ,
146
145
}
147
146
147
+ /// Builder pattern implementation for constructing audit logs
148
148
pub struct AuditLogBuilder {
149
149
// Used to ensure that log is only constructed if the logger is enabled
150
150
enabled : bool ,
@@ -169,6 +169,7 @@ impl Default for AuditLogBuilder {
169
169
}
170
170
171
171
impl AuditLogBuilder {
172
+ /// Sets the stream name for the audit log if logger is set
172
173
pub fn set_stream_name ( mut self , stream : impl Into < String > ) -> Self {
173
174
if !self . enabled {
174
175
return self ;
@@ -178,6 +179,7 @@ impl AuditLogBuilder {
178
179
self
179
180
}
180
181
182
+ /// Sets the actor details for the audit log if logger is set
181
183
pub fn set_actor (
182
184
mut self ,
183
185
host : impl Into < String > ,
@@ -198,6 +200,7 @@ impl AuditLogBuilder {
198
200
self
199
201
}
200
202
203
+ /// Sets the request details for the audit log if logger is set
201
204
pub fn set_request (
202
205
mut self ,
203
206
method : impl Into < String > ,
@@ -218,6 +221,7 @@ impl AuditLogBuilder {
218
221
self
219
222
}
220
223
224
+ /// Sets the response details for the audit log if logger is set
221
225
pub fn set_response ( mut self , status_code : u16 , err : impl Display ) -> Self {
222
226
if !self . enabled {
223
227
return self ;
@@ -229,7 +233,7 @@ impl AuditLogBuilder {
229
233
self
230
234
}
231
235
232
- // NOTE: Ensure that the logger has been constructed by Default
236
+ /// Sends the audit log to the logging server if configured
233
237
pub async fn send ( self ) {
234
238
let AuditLogBuilder {
235
239
start_time,
0 commit comments