goliath_normalize/definition.rs
1//! The source definition file format.
2//!
3//! A definition is written by people who know a log source, not Rust, and is
4//! loaded as untrusted input: see `docs/adr/0007-source-and-parser-model.md`
5//! for the model and `sources/sysmon.yaml` for a complete example.
6
7use std::collections::BTreeMap;
8
9use serde::Deserialize;
10
11/// A source definition as written.
12#[non_exhaustive]
13#[derive(Debug, Clone, PartialEq, Deserialize)]
14#[serde(deny_unknown_fields)]
15pub struct SourceDefinition {
16 /// The source's name, such as `sysmon`.
17 pub name: String,
18 /// Increased whenever the output for some input changes, so a stored
19 /// event can be traced to the definition that produced it.
20 pub version: u32,
21 /// How a byte stream splits into records.
22 pub framing: Framing,
23 /// How a record becomes a field map.
24 pub decoding: Decoding,
25 /// Values a source writes for a field it has no value for, such as
26 /// Windows' `-`. A field whose source holds one is not written, as if
27 /// the source had left it out; the value is still kept under
28 /// `unmapped` where the kind keeps the object it is in.
29 #[serde(default)]
30 pub nil: Vec<String>,
31 /// Fields every kind writes, such as the time and the device.
32 #[serde(default)]
33 pub common: BTreeMap<String, FieldSpec>,
34 /// The kinds of record the source produces, tried in order.
35 pub kinds: Vec<KindDefinition>,
36}
37
38/// How a byte stream splits into records.
39#[non_exhaustive]
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
41#[serde(rename_all = "kebab-case")]
42pub enum Framing {
43 /// One record per line. Blank lines are skipped.
44 Lines,
45 /// JSON values one after another, separated by any whitespace, as
46 /// `evtx_dump` writes them.
47 JsonValues,
48 /// Linux audit lines, grouped into one record per event by their
49 /// `msg=audit(time:serial)`, whether or not they are adjacent. Needs
50 /// `auditd` decoding.
51 AuditEvents,
52 /// Syslog messages framed as RFC 6587 describes, each either preceded by
53 /// its length in bytes and a space or ending at its line's end, as
54 /// senders over TCP write them. Needs a `syslog` decoding.
55 Syslog,
56}
57
58/// How a record becomes a field map.
59#[non_exhaustive]
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
61#[serde(rename_all = "kebab-case")]
62pub enum Decoding {
63 /// A JSON object.
64 Json,
65 /// Linux audit records, `key=value` fields per line, as one object with
66 /// each record under its type, such as `SYSCALL.exe` or `PATH.0.name`.
67 Auditd,
68 /// A syslog message, RFC 5424 or RFC 3164, as one object with its
69 /// header's fields, such as `hostname` and `app_name`, and the text
70 /// after the header as `message`.
71 Syslog,
72 /// A syslog message whose text after the header is JSON, decoded under
73 /// `message`, for applications that log JSON over syslog.
74 SyslogJson,
75}
76
77/// One kind of record, and how it maps to an OCSF class.
78#[non_exhaustive]
79#[derive(Debug, Clone, PartialEq, Deserialize)]
80#[serde(deny_unknown_fields)]
81pub struct KindDefinition {
82 /// A name for the kind, used in errors and reports.
83 pub name: String,
84 /// Source paths and the values they must hold for a record to be of this
85 /// kind. Values compare exactly: `1` does not equal `"1"`.
86 pub when: BTreeMap<String, Scalar>,
87 /// The OCSF class and activity of the resulting event.
88 pub class: ClassSpec,
89 /// OCSF attributes and where their values come from.
90 #[serde(default)]
91 pub fields: BTreeMap<String, FieldSpec>,
92 /// Source objects whose other members are all kept under `unmapped`,
93 /// by their own names, so that nothing the source wrote is dropped. `.`
94 /// names the record itself, for sources that write every field at its
95 /// top level.
96 #[serde(default)]
97 pub unmapped: Vec<String>,
98}
99
100/// The OCSF class of a kind. The category and type follow from it.
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
102#[serde(deny_unknown_fields)]
103pub struct ClassSpec {
104 /// `class_uid`.
105 pub class_uid: u32,
106 /// `activity_id`.
107 pub activity_id: u32,
108}
109
110/// Where an attribute's value comes from.
111#[non_exhaustive]
112#[derive(Debug, Clone, PartialEq, Deserialize)]
113#[serde(untagged)]
114pub enum FieldSpec {
115 /// A source path, copied as it is.
116 Path(String),
117 /// A source path, converted.
118 Converted {
119 /// The source path.
120 from: String,
121 /// The conversion.
122 #[serde(rename = "as")]
123 coercion: Coercion,
124 },
125 /// A source path, translated through a table, as a source's own words
126 /// become the values of an OCSF enumeration.
127 Translated {
128 /// The source path.
129 from: String,
130 /// Source values, as text, and what each becomes. A number in the
131 /// source is looked up by its decimal text.
132 map: BTreeMap<String, Scalar>,
133 /// What a value the table does not list becomes, such as `99`, the
134 /// OCSF `Other`. Without it, such a value is kept under `unmapped`
135 /// and reported as an issue.
136 #[serde(default)]
137 otherwise: Option<Scalar>,
138 },
139 /// A constant.
140 Value {
141 /// The value.
142 value: Scalar,
143 },
144}
145
146/// A conversion of a source value to an OCSF type.
147#[non_exhaustive]
148#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
149#[serde(rename_all = "kebab-case")]
150pub enum Coercion {
151 /// Text; a number becomes its decimal text.
152 String,
153 /// A whole number, from a number, its decimal text, or its hexadecimal
154 /// text after `0x`, as Windows writes process and logon identifiers.
155 Integer,
156 /// An RFC 3339 time, such as `2025-12-25T14:30:27.369114Z`, as the
157 /// milliseconds since the Unix epoch OCSF stores.
158 Timestamp,
159 /// Seconds since the Unix epoch, with a fraction if any, such as
160 /// auditd's `1727251200.123`, as milliseconds.
161 UnixSeconds,
162}
163
164/// A constant or a value to compare with, as YAML writes it.
165#[non_exhaustive]
166#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
167#[serde(untagged)]
168pub enum Scalar {
169 /// `true` or `false`.
170 Boolean(bool),
171 /// A whole number.
172 Integer(i64),
173 /// Text.
174 String(String),
175}
176
177impl From<&Scalar> for serde_json::Value {
178 fn from(scalar: &Scalar) -> Self {
179 match scalar {
180 Scalar::Boolean(value) => Self::Bool(*value),
181 Scalar::Integer(value) => Self::from(*value),
182 Scalar::String(value) => Self::String(value.clone()),
183 }
184 }
185}