@@ -21,7 +21,7 @@ use std::{
21
21
fmt:: { Debug , Display } ,
22
22
} ;
23
23
24
- use crate :: about:: current;
24
+ use crate :: { about:: current, handlers :: http :: modal :: utils :: rbac_utils :: get_metadata } ;
25
25
26
26
use super :: option:: CONFIG ;
27
27
use chrono:: { DateTime , Utc } ;
@@ -107,7 +107,7 @@ pub struct AuditDetails {
107
107
pub generated_at : DateTime < Utc > ,
108
108
}
109
109
110
- #[ derive( Serialize ) ]
110
+ #[ derive( Serialize , Default ) ]
111
111
pub struct ServerDetails {
112
112
pub version : String ,
113
113
pub deployment_id : Ulid ,
@@ -155,18 +155,27 @@ pub struct AuditLog {
155
155
pub struct AuditLogBuilder {
156
156
// Used to ensure that log is only constructed if the logger is enabled
157
157
enabled : bool ,
158
- pub actor : Option < ActorDetails > ,
159
- pub request : Option < RequestDetails > ,
160
- pub response : Option < ResponseDetails > ,
158
+ inner : AuditLog ,
161
159
}
162
160
163
161
impl Default for AuditLogBuilder {
164
162
fn default ( ) -> Self {
165
163
AuditLogBuilder {
166
164
enabled : AUDIT_LOGGER . is_some ( ) ,
167
- actor : None ,
168
- request : None ,
169
- response : None ,
165
+ inner : AuditLog {
166
+ audit : AuditDetails {
167
+ version : AuditLogVersion :: V1 ,
168
+ id : Ulid :: new ( ) ,
169
+ generated_at : Utc :: now ( ) ,
170
+ } ,
171
+ parseable_server : ServerDetails {
172
+ version : current ( ) . released_version . to_string ( ) ,
173
+ deployment_id : Ulid :: nil ( ) ,
174
+ } ,
175
+ actor : ActorDetails :: default ( ) ,
176
+ request : RequestDetails :: default ( ) ,
177
+ response : ResponseDetails :: default ( ) ,
178
+ } ,
170
179
}
171
180
}
172
181
}
@@ -175,109 +184,89 @@ impl AuditLogBuilder {
175
184
/// Sets the remote host for the audit log
176
185
pub fn with_host ( mut self , host : impl Into < String > ) -> Self {
177
186
if self . enabled {
178
- self . actor
179
- . get_or_insert_with ( ActorDetails :: default)
180
- . remote_host = host. into ( ) ;
187
+ self . inner . actor . remote_host = host. into ( ) ;
181
188
}
182
189
self
183
190
}
184
191
185
192
/// Sets the username for the audit log
186
193
pub fn with_username ( mut self , username : impl Into < String > ) -> Self {
187
194
if self . enabled {
188
- self . actor
189
- . get_or_insert_with ( ActorDetails :: default)
190
- . username = username. into ( ) ;
195
+ self . inner . actor . username = username. into ( ) ;
191
196
}
192
197
self
193
198
}
194
199
195
200
/// Sets the user agent for the audit log
196
201
pub fn with_user_agent ( mut self , user_agent : impl Into < String > ) -> Self {
197
202
if self . enabled {
198
- self . actor
199
- . get_or_insert_with ( ActorDetails :: default)
200
- . user_agent = user_agent. into ( ) ;
203
+ self . inner . actor . user_agent = user_agent. into ( ) ;
201
204
}
202
205
self
203
206
}
204
207
205
208
/// Sets the authorization method for the audit log
206
209
pub fn with_auth_method ( mut self , auth_method : impl Into < String > ) -> Self {
207
210
if self . enabled {
208
- self . actor
209
- . get_or_insert_with ( ActorDetails :: default)
210
- . authorization_method = auth_method. into ( ) ;
211
+ self . inner . actor . authorization_method = auth_method. into ( ) ;
211
212
}
212
213
self
213
214
}
214
215
215
216
/// Sets the stream for the request details
216
217
pub fn with_stream ( mut self , stream : impl Into < String > ) -> Self {
217
218
if self . enabled {
218
- self . request
219
- . get_or_insert_with ( RequestDetails :: default)
220
- . stream = stream. into ( ) ;
219
+ self . inner . request . stream = stream. into ( ) ;
221
220
}
222
221
self
223
222
}
224
223
225
224
/// Sets the request timing details
226
225
pub fn with_timing ( mut self , start_time : DateTime < Utc > , end_time : DateTime < Utc > ) -> Self {
227
226
if self . enabled {
228
- let request = self . request . get_or_insert_with ( RequestDetails :: default ) ;
229
- request. start_time = start_time ;
230
- request . end_time = end_time ;
227
+ self . inner . request . start_time = start_time ;
228
+ self . inner . request . end_time = end_time ;
229
+ self . inner . audit . generated_at = start_time ;
231
230
}
232
231
self
233
232
}
234
233
235
234
/// Sets the request method details
236
235
pub fn with_method ( mut self , method : impl Into < String > ) -> Self {
237
236
if self . enabled {
238
- self . request
239
- . get_or_insert_with ( RequestDetails :: default)
240
- . method = method. into ( ) ;
237
+ self . inner . request . method = method. into ( ) ;
241
238
}
242
239
self
243
240
}
244
241
245
242
/// Sets the request path
246
243
pub fn with_path ( mut self , path : impl Into < String > ) -> Self {
247
244
if self . enabled {
248
- self . request
249
- . get_or_insert_with ( RequestDetails :: default)
250
- . path = path. into ( ) ;
245
+ self . inner . request . path = path. into ( ) ;
251
246
}
252
247
self
253
248
}
254
249
255
250
/// Sets the request protocol
256
251
pub fn with_protocol ( mut self , protocol : impl Into < String > ) -> Self {
257
252
if self . enabled {
258
- self . request
259
- . get_or_insert_with ( RequestDetails :: default)
260
- . protocol = protocol. into ( ) ;
253
+ self . inner . request . protocol = protocol. into ( ) ;
261
254
}
262
255
self
263
256
}
264
257
265
258
/// Sets the request headers
266
259
pub fn with_headers ( mut self , headers : impl IntoIterator < Item = ( String , String ) > ) -> Self {
267
260
if self . enabled {
268
- self . request
269
- . get_or_insert_with ( RequestDetails :: default)
270
- . headers = headers. into_iter ( ) . collect ( ) ;
261
+ self . inner . request . headers = headers. into_iter ( ) . collect ( ) ;
271
262
}
272
263
self
273
264
}
274
265
275
266
/// Sets the response status code
276
267
pub fn with_status ( mut self , status_code : u16 ) -> Self {
277
268
if self . enabled {
278
- self . response
279
- . get_or_insert_with ( ResponseDetails :: default)
280
- . status_code = status_code;
269
+ self . inner . response . status_code = status_code;
281
270
}
282
271
self
283
272
}
@@ -287,9 +276,7 @@ impl AuditLogBuilder {
287
276
if self . enabled {
288
277
let error = err. to_string ( ) ;
289
278
if !error. is_empty ( ) {
290
- self . response
291
- . get_or_insert_with ( ResponseDetails :: default)
292
- . error = Some ( error) ;
279
+ self . inner . response . error = Some ( error) ;
293
280
}
294
281
}
295
282
self
@@ -304,32 +291,20 @@ impl AuditLogBuilder {
304
291
305
292
// build the audit log
306
293
let AuditLogBuilder {
307
- actor,
308
- request,
309
- response,
294
+ inner : mut audit_log,
310
295
..
311
296
} = self ;
312
297
298
+ // get the deployment id from metadata
299
+ // NOTE: this fails if the metadata couldn't be loaded due to network issue, etc.
300
+ audit_log. parseable_server . deployment_id = get_metadata ( )
301
+ . await
302
+ . expect ( "Metadata should have been loaded" )
303
+ . deployment_id ;
304
+
313
305
// get the logger
314
306
let logger = AUDIT_LOGGER . as_ref ( ) . unwrap ( ) ;
315
307
316
- // build the audit log
317
- let now = Utc :: now ( ) ;
318
- let audit_log = AuditLog {
319
- audit : AuditDetails {
320
- version : AuditLogVersion :: V1 ,
321
- id : Ulid :: new ( ) ,
322
- generated_at : now,
323
- } ,
324
- parseable_server : ServerDetails {
325
- version : current ( ) . released_version . to_string ( ) ,
326
- deployment_id : Ulid :: new ( ) ,
327
- } ,
328
- actor : actor. unwrap_or_default ( ) ,
329
- request : request. unwrap_or_default ( ) ,
330
- response : response. unwrap_or_default ( ) ,
331
- } ;
332
-
333
308
logger. send_log ( json ! ( audit_log) ) . await
334
309
}
335
310
}
0 commit comments