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
297
298
299
300
301
302
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use time::OffsetDateTime;
#[derive(Debug, PartialEq)]
pub enum CefConversionError {
Unexpected(String),
}
impl Error for CefConversionError {}
impl Display for CefConversionError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
CefConversionError::Unexpected(message) => {
write!(f, "CefConversionError::Unexpected {}", message)
}
}
}
}
pub type CefResult = Result<String, CefConversionError>;
pub type CefExtensionsResult = Result<(), CefConversionError>;
pub trait CefHeaderVersion {
fn cef_header_version(&self) -> CefResult;
}
pub trait CefHeaderDeviceVendor {
fn cef_header_device_vendor(&self) -> CefResult;
}
pub trait CefHeaderDeviceProduct {
fn cef_header_device_product(&self) -> CefResult;
}
pub trait CefHeaderDeviceVersion {
fn cef_header_device_version(&self) -> CefResult;
}
pub trait CefHeaderDeviceEventClassID {
fn cef_header_device_event_class_id(&self) -> CefResult;
}
pub trait CefHeaderName {
fn cef_header_name(&self) -> CefResult;
}
pub trait CefHeaderSeverity {
fn cef_header_severity(&self) -> CefResult;
}
pub trait CefExtensions {
fn cef_extensions(&self, collector: &mut HashMap<String, String>) -> CefExtensionsResult;
}
pub trait ToCef:
CefHeaderVersion
+ CefHeaderDeviceVendor
+ CefHeaderDeviceProduct
+ CefHeaderDeviceVersion
+ CefHeaderDeviceEventClassID
+ CefHeaderName
+ CefHeaderSeverity
+ CefExtensions
{
fn to_cef(&self) -> CefResult {
let mut extensions: HashMap<String, String> = HashMap::new();
if let Err(err) = self.cef_extensions(&mut extensions) {
return Err(err);
};
let mut kvstrs: Vec<String> = extensions
.into_iter()
.map(|(key, value)| [key, value].join("="))
.collect();
kvstrs.sort_unstable();
let extensionsstr = kvstrs.join(" ");
let mut cef_entry = String::new();
cef_entry.push_str("CEF:");
cef_entry.push_str(&self.cef_header_version()?);
cef_entry.push('|');
cef_entry.push_str(&self.cef_header_device_vendor()?);
cef_entry.push('|');
cef_entry.push_str(&self.cef_header_device_product()?);
cef_entry.push('|');
cef_entry.push_str(&self.cef_header_device_version()?);
cef_entry.push('|');
cef_entry.push_str(&self.cef_header_device_event_class_id()?);
cef_entry.push('|');
cef_entry.push_str(&self.cef_header_name()?);
cef_entry.push('|');
cef_entry.push_str(&self.cef_header_severity()?);
cef_entry.push('|');
cef_entry.push_str(extensionsstr.as_str());
Ok(cef_entry)
}
}
impl CefExtensions for OffsetDateTime {
fn cef_extensions(&self, collector: &mut HashMap<String, String>) -> CefExtensionsResult {
collector.insert(
"rt".to_owned(),
format!("{}", self.unix_timestamp_nanos() / 1000000),
);
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
struct GoodExample {}
impl ToCef for GoodExample {}
impl CefHeaderVersion for GoodExample {
fn cef_header_version(&self) -> CefResult {
Ok("0".to_owned())
}
}
impl CefHeaderDeviceVendor for GoodExample {
fn cef_header_device_vendor(&self) -> CefResult {
Ok("polyverse".to_owned())
}
}
impl CefHeaderDeviceProduct for GoodExample {
fn cef_header_device_product(&self) -> CefResult {
Ok("zerotect".to_owned())
}
}
impl CefHeaderDeviceVersion for GoodExample {
fn cef_header_device_version(&self) -> CefResult {
Ok("V1".to_owned())
}
}
impl CefHeaderDeviceEventClassID for GoodExample {
fn cef_header_device_event_class_id(&self) -> CefResult {
Ok("LinuxKernelTrap".to_owned())
}
}
impl CefHeaderName for GoodExample {
fn cef_header_name(&self) -> CefResult {
Ok("Linux Kernel Trap".to_owned())
}
}
impl CefHeaderSeverity for GoodExample {
fn cef_header_severity(&self) -> CefResult {
Ok("10".to_owned())
}
}
impl CefExtensions for GoodExample {
fn cef_extensions(&self, collector: &mut HashMap<String, String>) -> CefExtensionsResult {
collector.insert("customField1".to_owned(), "customValue1".to_owned());
collector.insert("customField2".to_owned(), "customValue2".to_owned());
collector.insert("customField3".to_owned(), "customValue2".to_owned());
collector.insert("customField4".to_owned(), "customValue3".to_owned());
Ok(())
}
}
struct BadExample {}
impl ToCef for BadExample {}
impl CefHeaderVersion for BadExample {
fn cef_header_version(&self) -> CefResult {
Ok("0".to_owned())
}
}
impl CefHeaderDeviceVendor for BadExample {
fn cef_header_device_vendor(&self) -> CefResult {
Ok("polyverse".to_owned())
}
}
impl CefHeaderDeviceProduct for BadExample {
fn cef_header_device_product(&self) -> CefResult {
Ok("zerotect".to_owned())
}
}
impl CefHeaderDeviceVersion for BadExample {
fn cef_header_device_version(&self) -> CefResult {
Ok("V1".to_owned())
}
}
impl CefHeaderDeviceEventClassID for BadExample {
fn cef_header_device_event_class_id(&self) -> CefResult {
Err(CefConversionError::Unexpected(
"This error should propagate".to_owned(),
))
}
}
impl CefHeaderName for BadExample {
fn cef_header_name(&self) -> CefResult {
Ok("Linux Kernel Trap".to_owned())
}
}
impl CefHeaderSeverity for BadExample {
fn cef_header_severity(&self) -> CefResult {
Ok("10".to_owned())
}
}
impl CefExtensions for BadExample {
fn cef_extensions(&self, collector: &mut HashMap<String, String>) -> CefExtensionsResult {
collector.insert("customField".to_owned(), "customValue".to_owned());
Ok(())
}
}
#[test]
fn test_impl_works() {
let example = GoodExample {};
let result = example.to_cef();
assert!(result.is_ok());
assert_eq!(result.unwrap(), "CEF:0|polyverse|zerotect|V1|LinuxKernelTrap|Linux Kernel Trap|10|customField1=customValue1 customField2=customValue2 customField3=customValue2 customField4=customValue3");
}
#[test]
fn test_error_propagates() {
let example = BadExample {};
let result = example.to_cef();
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
CefConversionError::Unexpected("This error should propagate".to_owned())
);
}
#[test]
fn test_ext_for_datetime() {
let mut collector = HashMap::<String, String>::new();
let example = OffsetDateTime::from_unix_timestamp_nanos(3435315515325000000);
let result = example.cef_extensions(&mut collector);
assert!(result.is_ok());
let maybe_rt = collector.get("rt");
assert!(maybe_rt.is_some());
let rt = maybe_rt.unwrap();
assert_eq!(rt, "3435315515325");
}
}