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    /// 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    #[serde(default)]
78    pub unmapped: Vec<String>,
79}
80
81/// The OCSF class of a kind. The category and type follow from it.
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
83#[serde(deny_unknown_fields)]
84pub struct ClassSpec {
85    /// `class_uid`.
86    pub class_uid: u32,
87    /// `activity_id`.
88    pub activity_id: u32,
89}
90
91/// Where an attribute's value comes from.
92#[non_exhaustive]
93#[derive(Debug, Clone, PartialEq, Deserialize)]
94#[serde(untagged)]
95pub enum FieldSpec {
96    /// A source path, copied as it is.
97    Path(String),
98    /// A source path, converted.
99    Converted {
100        /// The source path.
101        from: String,
102        /// The conversion.
103        #[serde(rename = "as")]
104        coercion: Coercion,
105    },
106    /// A source path, translated through a table, as a source's own words
107    /// become the values of an OCSF enumeration.
108    Translated {
109        /// The source path.
110        from: String,
111        /// Source values, as text, and what each becomes. A number in the
112        /// source is looked up by its decimal text.
113        map: BTreeMap<String, Scalar>,
114        /// What a value the table does not list becomes, such as `99`, the
115        /// OCSF `Other`. Without it, such a value is kept under `unmapped`
116        /// and reported as an issue.
117        #[serde(default)]
118        otherwise: Option<Scalar>,
119    },
120    /// A constant.
121    Value {
122        /// The value.
123        value: Scalar,
124    },
125}
126
127/// A conversion of a source value to an OCSF type.
128#[non_exhaustive]
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
130#[serde(rename_all = "kebab-case")]
131pub enum Coercion {
132    /// Text; a number becomes its decimal text.
133    String,
134    /// A whole number, from a number or its decimal text.
135    Integer,
136    /// An RFC 3339 time, such as `2025-12-25T14:30:27.369114Z`, as the
137    /// milliseconds since the Unix epoch OCSF stores.
138    Timestamp,
139    /// Seconds since the Unix epoch, with a fraction if any, such as
140    /// auditd's `1727251200.123`, as milliseconds.
141    UnixSeconds,
142}
143
144/// A constant or a value to compare with, as YAML writes it.
145#[non_exhaustive]
146#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
147#[serde(untagged)]
148pub enum Scalar {
149    /// `true` or `false`.
150    Boolean(bool),
151    /// A whole number.
152    Integer(i64),
153    /// Text.
154    String(String),
155}
156
157impl From<&Scalar> for serde_json::Value {
158    fn from(scalar: &Scalar) -> Self {
159        match scalar {
160            Scalar::Boolean(value) => Self::Bool(*value),
161            Scalar::Integer(value) => Self::from(*value),
162            Scalar::String(value) => Self::String(value.clone()),
163        }
164    }
165}