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