Skip to content

Commit 7d5f011

Browse files
author
Devdutt Shenoi
committed
cleanup code
1 parent 382fada commit 7d5f011

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/audit.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use std::{
2020
collections::HashMap,
2121
fmt::{Debug, Display},
22-
sync::Arc,
2322
};
2423

2524
use crate::about::current;
@@ -38,15 +37,19 @@ use url::Url;
3837

3938
static AUDIT_LOGGER: Lazy<Option<AuditLogger>> = Lazy::new(AuditLogger::new);
4039

40+
// AuditLogger handles sending audit logs to a remote logging system
4141
pub struct AuditLogger {
4242
client: Client,
4343
log_endpoint: Url,
4444
}
4545

4646
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
4949
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
5053
let log_endpoint = match CONFIG
5154
.parseable
5255
.audit_logger
@@ -66,6 +69,7 @@ impl AuditLogger {
6669
})
6770
}
6871

72+
// Sends the audit log to the configured endpoint with proper authentication
6973
async fn send_log(&self, json: Value) {
7074
let mut req = self
7175
.client
@@ -89,13 +93,15 @@ impl AuditLogger {
8993
}
9094
}
9195

96+
// Represents the version of the audit log format
9297
#[non_exhaustive]
9398
#[repr(u8)]
9499
#[derive(Debug, Clone, Copy, Serialize)]
95100
pub enum AuditLogVersion {
96101
V1 = 1,
97102
}
98103

104+
// Contains information about the actor (user) who performed the action
99105
#[derive(Serialize, Default)]
100106
#[serde(rename_all = "camelCase")]
101107
pub struct ActorLog {
@@ -105,6 +111,7 @@ pub struct ActorLog {
105111
pub authorization_method: String,
106112
}
107113

114+
// Contains details about the HTTP request that was made
108115
#[derive(Serialize, Default)]
109116
pub struct RequestLog {
110117
pub method: String,
@@ -113,23 +120,15 @@ pub struct RequestLog {
113120
pub headers: HashMap<String, String>,
114121
}
115122

116-
#[derive(Serialize)]
123+
/// Contains information about the response sent back to the client
124+
#[derive(Default, Serialize)]
117125
#[serde(rename_all = "camelCase")]
118126
pub struct ResponseLog {
119127
pub status_code: u16,
120128
pub error: Option<String>,
121129
}
122130

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
133132
#[derive(Serialize)]
134133
#[serde(rename_all = "camelCase")]
135134
pub struct AuditLog {
@@ -145,6 +144,7 @@ pub struct AuditLog {
145144
pub response: ResponseLog,
146145
}
147146

147+
/// Builder pattern implementation for constructing audit logs
148148
pub struct AuditLogBuilder {
149149
// Used to ensure that log is only constructed if the logger is enabled
150150
enabled: bool,
@@ -169,6 +169,7 @@ impl Default for AuditLogBuilder {
169169
}
170170

171171
impl AuditLogBuilder {
172+
/// Sets the stream name for the audit log if logger is set
172173
pub fn set_stream_name(mut self, stream: impl Into<String>) -> Self {
173174
if !self.enabled {
174175
return self;
@@ -178,6 +179,7 @@ impl AuditLogBuilder {
178179
self
179180
}
180181

182+
/// Sets the actor details for the audit log if logger is set
181183
pub fn set_actor(
182184
mut self,
183185
host: impl Into<String>,
@@ -198,6 +200,7 @@ impl AuditLogBuilder {
198200
self
199201
}
200202

203+
/// Sets the request details for the audit log if logger is set
201204
pub fn set_request(
202205
mut self,
203206
method: impl Into<String>,
@@ -218,6 +221,7 @@ impl AuditLogBuilder {
218221
self
219222
}
220223

224+
/// Sets the response details for the audit log if logger is set
221225
pub fn set_response(mut self, status_code: u16, err: impl Display) -> Self {
222226
if !self.enabled {
223227
return self;
@@ -229,7 +233,7 @@ impl AuditLogBuilder {
229233
self
230234
}
231235

232-
// NOTE: Ensure that the logger has been constructed by Default
236+
/// Sends the audit log to the logging server if configured
233237
pub async fn send(self) {
234238
let AuditLogBuilder {
235239
start_time,

0 commit comments

Comments
 (0)