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