Skip to content

Commit 1bbe5c5

Browse files
author
Devdutt Shenoi
committed
fix: ensure deployment_id is set at send and improve codeflow
1 parent 39732aa commit 1bbe5c5

File tree

1 file changed

+39
-64
lines changed

1 file changed

+39
-64
lines changed

src/audit.rs

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::{
2121
fmt::{Debug, Display},
2222
};
2323

24-
use crate::about::current;
24+
use crate::{about::current, handlers::http::modal::utils::rbac_utils::get_metadata};
2525

2626
use super::option::CONFIG;
2727
use chrono::{DateTime, Utc};
@@ -107,7 +107,7 @@ pub struct AuditDetails {
107107
pub generated_at: DateTime<Utc>,
108108
}
109109

110-
#[derive(Serialize)]
110+
#[derive(Serialize, Default)]
111111
pub struct ServerDetails {
112112
pub version: String,
113113
pub deployment_id: Ulid,
@@ -155,18 +155,27 @@ pub struct AuditLog {
155155
pub struct AuditLogBuilder {
156156
// Used to ensure that log is only constructed if the logger is enabled
157157
enabled: bool,
158-
pub actor: Option<ActorDetails>,
159-
pub request: Option<RequestDetails>,
160-
pub response: Option<ResponseDetails>,
158+
inner: AuditLog,
161159
}
162160

163161
impl Default for AuditLogBuilder {
164162
fn default() -> Self {
165163
AuditLogBuilder {
166164
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+
},
170179
}
171180
}
172181
}
@@ -175,109 +184,89 @@ impl AuditLogBuilder {
175184
/// Sets the remote host for the audit log
176185
pub fn with_host(mut self, host: impl Into<String>) -> Self {
177186
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();
181188
}
182189
self
183190
}
184191

185192
/// Sets the username for the audit log
186193
pub fn with_username(mut self, username: impl Into<String>) -> Self {
187194
if self.enabled {
188-
self.actor
189-
.get_or_insert_with(ActorDetails::default)
190-
.username = username.into();
195+
self.inner.actor.username = username.into();
191196
}
192197
self
193198
}
194199

195200
/// Sets the user agent for the audit log
196201
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
197202
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();
201204
}
202205
self
203206
}
204207

205208
/// Sets the authorization method for the audit log
206209
pub fn with_auth_method(mut self, auth_method: impl Into<String>) -> Self {
207210
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();
211212
}
212213
self
213214
}
214215

215216
/// Sets the stream for the request details
216217
pub fn with_stream(mut self, stream: impl Into<String>) -> Self {
217218
if self.enabled {
218-
self.request
219-
.get_or_insert_with(RequestDetails::default)
220-
.stream = stream.into();
219+
self.inner.request.stream = stream.into();
221220
}
222221
self
223222
}
224223

225224
/// Sets the request timing details
226225
pub fn with_timing(mut self, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
227226
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;
231230
}
232231
self
233232
}
234233

235234
/// Sets the request method details
236235
pub fn with_method(mut self, method: impl Into<String>) -> Self {
237236
if self.enabled {
238-
self.request
239-
.get_or_insert_with(RequestDetails::default)
240-
.method = method.into();
237+
self.inner.request.method = method.into();
241238
}
242239
self
243240
}
244241

245242
/// Sets the request path
246243
pub fn with_path(mut self, path: impl Into<String>) -> Self {
247244
if self.enabled {
248-
self.request
249-
.get_or_insert_with(RequestDetails::default)
250-
.path = path.into();
245+
self.inner.request.path = path.into();
251246
}
252247
self
253248
}
254249

255250
/// Sets the request protocol
256251
pub fn with_protocol(mut self, protocol: impl Into<String>) -> Self {
257252
if self.enabled {
258-
self.request
259-
.get_or_insert_with(RequestDetails::default)
260-
.protocol = protocol.into();
253+
self.inner.request.protocol = protocol.into();
261254
}
262255
self
263256
}
264257

265258
/// Sets the request headers
266259
pub fn with_headers(mut self, headers: impl IntoIterator<Item = (String, String)>) -> Self {
267260
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();
271262
}
272263
self
273264
}
274265

275266
/// Sets the response status code
276267
pub fn with_status(mut self, status_code: u16) -> Self {
277268
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;
281270
}
282271
self
283272
}
@@ -287,9 +276,7 @@ impl AuditLogBuilder {
287276
if self.enabled {
288277
let error = err.to_string();
289278
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);
293280
}
294281
}
295282
self
@@ -304,32 +291,20 @@ impl AuditLogBuilder {
304291

305292
// build the audit log
306293
let AuditLogBuilder {
307-
actor,
308-
request,
309-
response,
294+
inner: mut audit_log,
310295
..
311296
} = self;
312297

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+
313305
// get the logger
314306
let logger = AUDIT_LOGGER.as_ref().unwrap();
315307

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-
333308
logger.send_log(json!(audit_log)).await
334309
}
335310
}

0 commit comments

Comments
 (0)