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 /// Fields every kind writes, such as the time and the device.
26 #[serde(default)]
27 pub common: BTreeMap<String, FieldSpec>,
28 /// The kinds of record the source produces, tried in order.
29 pub kinds: Vec<KindDefinition>,
30}
31
32/// How a byte stream splits into records.
33#[non_exhaustive]
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
35#[serde(rename_all = "kebab-case")]
36pub enum Framing {
37 /// One record per line. Blank lines are skipped.
38 Lines,
39 /// JSON values one after another, separated by any whitespace, as
40 /// `evtx_dump` writes them.
41 JsonValues,
42 /// Linux audit lines, grouped into one record per event by their
43 /// `msg=audit(time:serial)`, whether or not they are adjacent. Needs
44 /// `auditd` decoding.
45 AuditEvents,
46}
47
48/// How a record becomes a field map.
49#[non_exhaustive]
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
51#[serde(rename_all = "kebab-case")]
52pub enum Decoding {
53 /// A JSON object.
54 Json,
55 /// Linux audit records, `key=value` fields per line, as one object with
56 /// each record under its type, such as `SYSCALL.exe` or `PATH.0.name`.
57 Auditd,
58}
59
60/// One kind of record, and how it maps to an OCSF class.
61#[non_exhaustive]
62#[derive(Debug, Clone, PartialEq, Deserialize)]
63#[serde(deny_unknown_fields)]
64pub struct KindDefinition {
65 /// A name for the kind, used in errors and reports.
66 pub name: String,
67 /// Source paths and the values they must hold for a record to be of this
68 /// kind. Values compare exactly: `1` does not equal `"1"`.
69 pub when: BTreeMap<String, Scalar>,
70 /// The OCSF class and activity of the resulting event.
71 pub class: ClassSpec,
72 /// OCSF attributes and where their values come from.
73 #[serde(default)]
74 pub fields: BTreeMap<String, FieldSpec>,
75 /// Source objects whose other members are all kept under `unmapped`,
76 /// by their own names, so that nothing the source wrote is dropped. `.`
77 /// names the record itself, for sources that write every field at its
78 /// top level.
79 #[serde(default)]
80 pub unmapped: Vec<String>,
81}
82
83/// The OCSF class of a kind. The category and type follow from it.
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
85#[serde(deny_unknown_fields)]
86pub struct ClassSpec {
87 /// `class_uid`.
88 pub class_uid: u32,
89 /// `activity_id`.
90 pub activity_id: u32,
91}
92
93/// Where an attribute's value comes from.
94#[non_exhaustive]
95#[derive(Debug, Clone, PartialEq, Deserialize)]
96#[serde(untagged)]
97pub enum FieldSpec {
98 /// A source path, copied as it is.
99 Path(String),
100 /// A source path, converted.
101 Converted {
102 /// The source path.
103 from: String,
104 /// The conversion.
105 #[serde(rename = "as")]
106 coercion: Coercion,
107 },
108 /// A source path, translated through a table, as a source's own words
109 /// become the values of an OCSF enumeration.
110 Translated {
111 /// The source path.
112 from: String,
113 /// Source values, as text, and what each becomes. A number in the
114 /// source is looked up by its decimal text.
115 map: BTreeMap<String, Scalar>,
116 /// What a value the table does not list becomes, such as `99`, the
117 /// OCSF `Other`. Without it, such a value is kept under `unmapped`
118 /// and reported as an issue.
119 #[serde(default)]
120 otherwise: Option<Scalar>,
121 },
122 /// A constant.
123 Value {
124 /// The value.
125 value: Scalar,
126 },
127}
128
129/// A conversion of a source value to an OCSF type.
130#[non_exhaustive]
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
132#[serde(rename_all = "kebab-case")]
133pub enum Coercion {
134 /// Text; a number becomes its decimal text.
135 String,
136 /// A whole number, from a number or its decimal text.
137 Integer,
138 /// An RFC 3339 time, such as `2025-12-25T14:30:27.369114Z`, as the
139 /// milliseconds since the Unix epoch OCSF stores.
140 Timestamp,
141 /// Seconds since the Unix epoch, with a fraction if any, such as
142 /// auditd's `1727251200.123`, as milliseconds.
143 UnixSeconds,
144}
145
146/// A constant or a value to compare with, as YAML writes it.
147#[non_exhaustive]
148#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
149#[serde(untagged)]
150pub enum Scalar {
151 /// `true` or `false`.
152 Boolean(bool),
153 /// A whole number.
154 Integer(i64),
155 /// Text.
156 String(String),
157}
158
159impl From<&Scalar> for serde_json::Value {
160 fn from(scalar: &Scalar) -> Self {
161 match scalar {
162 Scalar::Boolean(value) => Self::Bool(*value),
163 Scalar::Integer(value) => Self::from(*value),
164 Scalar::String(value) => Self::String(value.clone()),
165 }
166 }
167}