-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathaudit.rs
296 lines (261 loc) · 8.44 KB
/
audit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use std::{
collections::HashMap,
fmt::{Debug, Display},
};
use crate::{about::current, parseable::PARSEABLE, storage::StorageMetadata, HTTP_CLIENT};
use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use serde::Serialize;
use serde_json::{json, Value};
use tracing::error;
use ulid::Ulid;
use url::Url;
static AUDIT_LOGGER: Lazy<Option<AuditLogger>> = Lazy::new(AuditLogger::new);
// AuditLogger handles sending audit logs to a remote logging system
pub struct AuditLogger {
log_endpoint: Url,
}
impl AuditLogger {
/// Create an audit logger that can be used to capture and push
/// audit logs to the appropriate logging system over HTTP
pub fn new() -> Option<AuditLogger> {
// Try to construct the log endpoint URL by joining the base URL
// with the ingest path, This can fail if the URL is not valid,
// when the base URL is not set or the ingest path is not valid
let log_endpoint = match PARSEABLE
.options
.audit_logger
.as_ref()?
.join("/api/v1/ingest")
{
Ok(url) => url,
Err(err) => {
eprintln!("Couldn't setup audit logger: {err}");
return None;
}
};
Some(AuditLogger { log_endpoint })
}
// Sends the audit log to the configured endpoint with proper authentication
async fn send_log(&self, json: Value) {
let mut req = HTTP_CLIENT
.post(self.log_endpoint.as_str())
.json(&json)
.header("x-p-stream", "audit_log");
// Use basic auth if credentials are configured
if let Some(username) = PARSEABLE.options.audit_username.as_ref() {
req = req.basic_auth(username, PARSEABLE.options.audit_password.as_ref())
}
match req.send().await {
Ok(r) => {
if let Err(e) = r.error_for_status() {
error!("{e}")
}
}
Err(e) => error!("Failed to send audit event: {}", e),
}
}
}
// Represents the version of the audit log format
#[non_exhaustive]
#[repr(u8)]
#[derive(Debug, Clone, Copy, Serialize, Default)]
pub enum AuditLogVersion {
// NOTE: default should be latest version
#[default]
V1 = 1,
}
#[derive(Serialize, Default)]
pub struct AuditDetails {
pub version: AuditLogVersion,
pub id: Ulid,
pub generated_at: DateTime<Utc>,
}
#[derive(Serialize, Default)]
pub struct ServerDetails {
pub version: String,
pub deployment_id: Ulid,
}
// Contains information about the actor (user) who performed the action
#[derive(Serialize, Default)]
pub struct ActorDetails {
pub remote_host: String,
pub user_agent: String,
pub username: String,
pub authorization_method: String,
}
// Contains details about the HTTP request that was made
#[derive(Serialize, Default)]
pub struct RequestDetails {
pub stream: String,
pub start_time: DateTime<Utc>,
pub end_time: DateTime<Utc>,
pub method: String,
pub path: String,
pub protocol: String,
pub headers: HashMap<String, String>,
}
/// Contains information about the response sent back to the client
#[derive(Default, Serialize)]
pub struct ResponseDetails {
pub status_code: u16,
pub error: Option<String>,
}
/// The main audit log structure that combines all audit information
#[derive(Serialize)]
pub struct AuditLog {
pub audit: AuditDetails,
pub parseable_server: ServerDetails,
pub actor: ActorDetails,
pub request: RequestDetails,
pub response: ResponseDetails,
}
/// Builder pattern implementation for constructing audit logs
pub struct AuditLogBuilder {
// Used to ensure that log is only constructed if the logger is enabled
enabled: bool,
inner: AuditLog,
}
impl Default for AuditLogBuilder {
fn default() -> Self {
AuditLogBuilder {
enabled: AUDIT_LOGGER.is_some(),
inner: AuditLog {
audit: AuditDetails {
version: AuditLogVersion::V1,
id: Ulid::new(),
..Default::default()
},
parseable_server: ServerDetails {
version: current().released_version.to_string(),
deployment_id: StorageMetadata::global().deployment_id,
},
request: RequestDetails {
start_time: Utc::now(),
..Default::default()
},
actor: ActorDetails::default(),
response: ResponseDetails::default(),
},
}
}
}
impl AuditLogBuilder {
/// Sets the remote host for the audit log
pub fn with_host(mut self, host: impl Into<String>) -> Self {
if self.enabled {
self.inner.actor.remote_host = host.into();
}
self
}
/// Sets the username for the audit log
pub fn with_username(mut self, username: impl Into<String>) -> Self {
if self.enabled {
self.inner.actor.username = username.into();
}
self
}
/// Sets the user agent for the audit log
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
if self.enabled {
self.inner.actor.user_agent = user_agent.into();
}
self
}
/// Sets the authorization method for the audit log
pub fn with_auth_method(mut self, auth_method: impl Into<String>) -> Self {
if self.enabled {
self.inner.actor.authorization_method = auth_method.into();
}
self
}
/// Sets the stream for the request details
pub fn with_stream(mut self, stream: impl Into<String>) -> Self {
if self.enabled {
self.inner.request.stream = stream.into();
}
self
}
/// Sets the request method details
pub fn with_method(mut self, method: impl Into<String>) -> Self {
if self.enabled {
self.inner.request.method = method.into();
}
self
}
/// Sets the request path
pub fn with_path(mut self, path: impl Into<String>) -> Self {
if self.enabled {
self.inner.request.path = path.into();
}
self
}
/// Sets the request protocol
pub fn with_protocol(mut self, protocol: impl Into<String>) -> Self {
if self.enabled {
self.inner.request.protocol = protocol.into();
}
self
}
/// Sets the request headers
pub fn with_headers(mut self, headers: impl IntoIterator<Item = (String, String)>) -> Self {
if self.enabled {
self.inner.request.headers = headers.into_iter().collect();
}
self
}
/// Sets the response status code
pub fn with_status(mut self, status_code: u16) -> Self {
if self.enabled {
self.inner.response.status_code = status_code;
}
self
}
/// Sets the response error if any
pub fn with_error(mut self, err: impl Display) -> Self {
if self.enabled {
let error = err.to_string();
if !error.is_empty() {
self.inner.response.error = Some(error);
}
}
self
}
/// Sends the audit log to the logging server if configured
pub async fn send(self) {
// ensures that we don't progress if logger is not enabled
if !self.enabled {
return;
}
// build the audit log
let AuditLogBuilder {
inner: mut audit_log,
..
} = self;
let now = Utc::now();
audit_log.audit.generated_at = now;
audit_log.request.end_time = now;
AUDIT_LOGGER
.as_ref()
.unwrap()
.send_log(json!(audit_log))
.await
}
}