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