Skip to main content

goliath_rule/
error.rs

1//! Error types.
2
3use goliath_ocsf::schema::PathError as SchemaPathError;
4use goliath_sigma::YamlError;
5use thiserror::Error;
6
7use crate::mapping::LogSourceSelector;
8
9/// A field path that cannot be parsed.
10#[non_exhaustive]
11#[derive(Debug, Clone, PartialEq, Eq, Error)]
12pub enum PathError {
13    /// The path is empty.
14    #[error("field path is empty")]
15    Empty,
16
17    /// A segment between dots is empty.
18    #[error("field path `{path}` has an empty segment at position {position}")]
19    EmptySegment {
20        /// The path as written.
21        path: String,
22        /// The zero-based index of the empty segment.
23        position: usize,
24    },
25}
26
27/// Text that is not an IP address or network.
28#[derive(Debug, Clone, PartialEq, Eq, Error)]
29#[error("`{0}` is not an IP address or network")]
30pub struct NetworkError(pub String);
31
32/// A mapping set that cannot be loaded, or cannot resolve a rule's log source.
33#[derive(Debug, Clone, PartialEq, Eq, Error)]
34#[non_exhaustive]
35pub enum MappingError {
36    /// The file is not acceptable YAML, or does not have the shape of a
37    /// mapping set.
38    #[error(transparent)]
39    Yaml(#[from] YamlError),
40
41    /// An entry's selector names no key, so it would apply to every rule.
42    #[error("mapping {index} selects every log source; name at least one key")]
43    EmptySelector {
44        /// The zero-based index of the entry.
45        index: usize,
46    },
47
48    /// A field is mapped to an empty list of paths.
49    #[error("mapping {index} maps field `{field}` to no path")]
50    NoPaths {
51        /// The zero-based index of the entry.
52        index: usize,
53        /// The field name.
54        field: String,
55    },
56
57    /// Two entries have the same selector, so neither can be chosen.
58    #[error("mappings {earlier} and {index} have the same log source selector")]
59    DuplicateSelector {
60        /// The zero-based index of the first entry.
61        earlier: usize,
62        /// The zero-based index of the repeated entry.
63        index: usize,
64    },
65
66    /// An entry's `class_uid` is not a class of the OCSF schema.
67    #[error("mapping {index}: OCSF {version} has no class {class_uid}")]
68    UnknownClass {
69        /// The zero-based index of the entry.
70        index: usize,
71        /// The `class_uid` as written.
72        class_uid: i64,
73        /// The schema version checked against.
74        version: &'static str,
75    },
76
77    /// A path is not an attribute of the entry's class.
78    #[error("mapping {index}: {source}")]
79    Attribute {
80        /// The zero-based index of the entry.
81        index: usize,
82        /// Where the path left the schema.
83        source: SchemaPathError,
84    },
85
86    /// A path names an attribute no value of a rule can be compared with.
87    #[error("mapping {index}: `{path}` holds {holds}, which cannot equal {value}")]
88    Type {
89        /// The zero-based index of the entry.
90        index: usize,
91        /// The path as written.
92        path: String,
93        /// What the attribute holds, such as `` an object `file` ``.
94        holds: String,
95        /// What it would be compared with, such as `a rule's value`.
96        value: String,
97    },
98
99    /// A class condition requires a value its enumerated attribute does
100    /// not define, so no event could satisfy it.
101    #[error("mapping {index}: {value} is not a defined value of `{path}`")]
102    UnknownValue {
103        /// The zero-based index of the entry.
104        index: usize,
105        /// The path as written.
106        path: String,
107        /// The value.
108        value: i64,
109    },
110
111    /// No entry applies to a rule's log source.
112    #[error("no mapping for log source ({logsource})")]
113    NoMapping {
114        /// The log source the rule declared.
115        logsource: LogSourceSelector,
116    },
117
118    /// Two equally specific entries apply to a rule's log source.
119    #[error("more than one mapping applies equally to log source ({logsource})")]
120    Ambiguous {
121        /// The log source the rule declared.
122        logsource: LogSourceSelector,
123    },
124}
125
126/// A rule that cannot be resolved exactly.
127#[derive(Debug, Clone, PartialEq, Eq, Error)]
128#[non_exhaustive]
129pub enum ResolveError {
130    /// No mapping applies to the rule's log source.
131    #[error(transparent)]
132    Mapping(#[from] MappingError),
133
134    /// The mapping for the rule's log source does not map a field it uses.
135    ///
136    /// Refused rather than treated as absent: the rule would load and never
137    /// fire, and the gap would be invisible.
138    #[error("field `{field}` has no mapping for log source ({logsource})")]
139    UnmappedField {
140        /// The field name as the rule wrote it.
141        field: String,
142        /// The rule's log source.
143        logsource: LogSourceSelector,
144    },
145
146    /// A modifier whose meaning depends on configuration this resolution
147    /// does not have.
148    #[error("field `{field}` uses `{modifier}`, which is not supported")]
149    UnsupportedModifier {
150        /// The field name.
151        field: String,
152        /// The modifier.
153        modifier: &'static str,
154    },
155
156    /// A value that cannot mean anything under its modifiers.
157    #[error("field `{field}`: {reason}")]
158    InvalidValue {
159        /// The field name.
160        field: String,
161        /// Why.
162        reason: &'static str,
163    },
164
165    /// A value expands into more variants than a rule may carry.
166    #[error("field `{field}` expands into {count} variants")]
167    TooManyVariants {
168        /// The field name.
169        field: String,
170        /// How many variants it would produce.
171        count: usize,
172    },
173
174    /// A keyword that is not text.
175    #[error("a keyword must be text, found {found}")]
176    UnsupportedKeyword {
177        /// What was found.
178        found: &'static str,
179    },
180}