Skip to main content

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 constant.
100    Value {
101        /// The value.
102        value: Scalar,
103    },
104}
105
106/// A conversion of a source value to an OCSF type.
107#[non_exhaustive]
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
109#[serde(rename_all = "kebab-case")]
110pub enum Coercion {
111    /// Text; a number becomes its decimal text.
112    String,
113    /// A whole number, from a number or its decimal text.
114    Integer,
115    /// An RFC 3339 time, such as `2025-12-25T14:30:27.369114Z`, as the
116    /// milliseconds since the Unix epoch OCSF stores.
117    Timestamp,
118}
119
120/// A constant or a value to compare with, as YAML writes it.
121#[non_exhaustive]
122#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
123#[serde(untagged)]
124pub enum Scalar {
125    /// `true` or `false`.
126    Boolean(bool),
127    /// A whole number.
128    Integer(i64),
129    /// Text.
130    String(String),
131}
132
133impl From<&Scalar> for serde_json::Value {
134    fn from(scalar: &Scalar) -> Self {
135        match scalar {
136            Scalar::Boolean(value) => Self::Bool(*value),
137            Scalar::Integer(value) => Self::from(*value),
138            Scalar::String(value) => Self::String(value.clone()),
139        }
140    }
141}