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}
43
44/// How a record becomes a field map.
45#[non_exhaustive]
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
47#[serde(rename_all = "kebab-case")]
48pub enum Decoding {
49 /// A JSON object.
50 Json,
51}
52
53/// One kind of record, and how it maps to an OCSF class.
54#[non_exhaustive]
55#[derive(Debug, Clone, PartialEq, Deserialize)]
56#[serde(deny_unknown_fields)]
57pub struct KindDefinition {
58 /// A name for the kind, used in errors and reports.
59 pub name: String,
60 /// Source paths and the values they must hold for a record to be of this
61 /// kind. Values compare exactly: `1` does not equal `"1"`.
62 pub when: BTreeMap<String, Scalar>,
63 /// The OCSF class and activity of the resulting event.
64 pub class: ClassSpec,
65 /// OCSF attributes and where their values come from.
66 #[serde(default)]
67 pub fields: BTreeMap<String, FieldSpec>,
68 /// Source objects whose other members are all kept under `unmapped`,
69 /// by their own names, so that nothing the source wrote is dropped.
70 #[serde(default)]
71 pub unmapped: Vec<String>,
72}
73
74/// The OCSF class of a kind. The category and type follow from it.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
76#[serde(deny_unknown_fields)]
77pub struct ClassSpec {
78 /// `class_uid`.
79 pub class_uid: u32,
80 /// `activity_id`.
81 pub activity_id: u32,
82}
83
84/// Where an attribute's value comes from.
85#[non_exhaustive]
86#[derive(Debug, Clone, PartialEq, Deserialize)]
87#[serde(untagged)]
88pub enum FieldSpec {
89 /// A source path, copied as it is.
90 Path(String),
91 /// A source path, converted.
92 Converted {
93 /// The source path.
94 from: String,
95 /// The conversion.
96 #[serde(rename = "as")]
97 coercion: Coercion,
98 },
99 /// A source path, translated through a table, as a source's own words
100 /// become the values of an OCSF enumeration.
101 Translated {
102 /// The source path.
103 from: String,
104 /// Source values, as text, and what each becomes. A number in the
105 /// source is looked up by its decimal text.
106 map: BTreeMap<String, Scalar>,
107 /// What a value the table does not list becomes, such as `99`, the
108 /// OCSF `Other`. Without it, such a value is kept under `unmapped`
109 /// and reported as an issue.
110 #[serde(default)]
111 otherwise: Option<Scalar>,
112 },
113 /// A constant.
114 Value {
115 /// The value.
116 value: Scalar,
117 },
118}
119
120/// A conversion of a source value to an OCSF type.
121#[non_exhaustive]
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
123#[serde(rename_all = "kebab-case")]
124pub enum Coercion {
125 /// Text; a number becomes its decimal text.
126 String,
127 /// A whole number, from a number or its decimal text.
128 Integer,
129 /// An RFC 3339 time, such as `2025-12-25T14:30:27.369114Z`, as the
130 /// milliseconds since the Unix epoch OCSF stores.
131 Timestamp,
132}
133
134/// A constant or a value to compare with, as YAML writes it.
135#[non_exhaustive]
136#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
137#[serde(untagged)]
138pub enum Scalar {
139 /// `true` or `false`.
140 Boolean(bool),
141 /// A whole number.
142 Integer(i64),
143 /// Text.
144 String(String),
145}
146
147impl From<&Scalar> for serde_json::Value {
148 fn from(scalar: &Scalar) -> Self {
149 match scalar {
150 Scalar::Boolean(value) => Self::Bool(*value),
151 Scalar::Integer(value) => Self::from(*value),
152 Scalar::String(value) => Self::String(value.clone()),
153 }
154 }
155}