use goliath_ocsf::schema::{self, Base};
use goliath_rule::FieldPath;
use serde_json::{Map, Value};
use crate::definition::{Coercion, Decoding, FieldSpec, Framing, SourceDefinition};
use crate::error::DefinitionError;
#[derive(Debug, Clone)]
pub struct Normalizer {
name: String,
version: u32,
framing: Framing,
kinds: Vec<Kind>,
}
#[derive(Debug, Clone)]
struct Kind {
name: String,
when: Vec<(SourcePath, Value)>,
class: Vec<(&'static str, Value)>,
fields: Vec<Field>,
unmapped: Vec<SourcePath>,
}
#[derive(Debug, Clone)]
struct Field {
target: FieldPath,
source: Source,
}
#[derive(Debug, Clone)]
enum Source {
Constant(Value),
Path {
path: SourcePath,
coercion: Option<Coercion>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct SourcePath {
text: String,
segments: Vec<String>,
}
impl SourcePath {
fn parse(text: &str) -> Option<Self> {
let segments: Vec<String> = text.split('.').map(str::to_owned).collect();
segments
.iter()
.all(|segment| !segment.is_empty())
.then(|| Self {
text: text.to_owned(),
segments,
})
}
fn lookup<'a>(&self, record: &'a Value) -> Option<&'a Value> {
self.segments
.iter()
.try_fold(record, |value, segment| value.get(segment.as_str()))
}
fn is_member(&self, object: &Self, key: &str) -> bool {
self.segments.len() == object.segments.len() + 1
&& self.segments.starts_with(&object.segments)
&& self.segments.last().is_some_and(|last| last == key)
}
fn last(&self) -> &str {
self.segments.last().map_or("", String::as_str)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum Outcome {
Event(Normalized),
DeadLetter(DeadLetter),
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Normalized {
pub event: Value,
pub kind: String,
pub issues: Vec<Issue>,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Issue {
pub target: String,
pub source: String,
pub reason: String,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeadLetter {
pub stage: Stage,
pub error: String,
pub raw: Vec<u8>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stage {
Framing,
Decoding,
Mapping,
}
impl Stage {
pub fn as_str(self) -> &'static str {
match self {
Self::Framing => "framing",
Self::Decoding => "decoding",
Self::Mapping => "mapping",
}
}
}
impl Normalizer {
pub fn from_yaml(text: &str) -> Result<Self, DefinitionError> {
let definition: SourceDefinition = goliath_sigma::yaml::from_str(text)?;
Self::new(&definition)
}
pub fn new(definition: &SourceDefinition) -> Result<Self, DefinitionError> {
if definition.kinds.is_empty() {
return Err(DefinitionError::NoKinds);
}
let common = compile_fields("common", &definition.common)?;
let mut kinds = Vec::with_capacity(definition.kinds.len());
for kind in &definition.kinds {
if kind.when.is_empty() {
return Err(DefinitionError::Unconditional {
kind: kind.name.clone(),
});
}
let when = kind
.when
.iter()
.map(|(path, value)| Ok((source_path(&kind.name, path)?, Value::from(value))))
.collect::<Result<_, DefinitionError>>()?;
let mut fields = common.clone();
fields.extend(compile_fields(&kind.name, &kind.fields)?);
check_overlaps(&kind.name, &fields)?;
check_schema(
&kind.name,
kind.class.class_uid,
kind.class.activity_id,
&fields,
)?;
let unmapped = kind
.unmapped
.iter()
.map(|path| source_path(&kind.name, path))
.collect::<Result<_, _>>()?;
let class_uid = kind.class.class_uid;
let activity_id = kind.class.activity_id;
let category_uid = goliath_ocsf::Event::derive_category_uid(class_uid);
let type_uid = goliath_ocsf::Event::derive_type_uid(class_uid, activity_id);
kinds.push(Kind {
name: kind.name.clone(),
when,
class: vec![
("class_uid", Value::from(class_uid)),
("category_uid", Value::from(category_uid)),
("type_uid", Value::from(type_uid)),
("activity_id", Value::from(activity_id)),
],
fields,
unmapped,
});
}
match definition.decoding {
Decoding::Json => {}
}
Ok(Self {
name: definition.name.clone(),
version: definition.version,
framing: definition.framing,
kinds,
})
}
pub fn name(&self) -> &str {
&self.name
}
pub fn version(&self) -> u32 {
self.version
}
pub fn normalize(&self, bytes: &[u8], mut out: impl FnMut(Outcome)) {
match self.framing {
Framing::Lines => {
for line in bytes.split(|&byte| byte == b'\n') {
let line = line.strip_suffix(b"\r").unwrap_or(line);
if line.iter().all(u8::is_ascii_whitespace) {
continue;
}
out(match serde_json::from_slice::<Value>(line) {
Ok(record) => self.record(&record, line),
Err(error) => dead(Stage::Decoding, error.to_string(), line),
});
}
}
Framing::JsonValues => {
let mut stream = serde_json::Deserializer::from_slice(bytes).into_iter::<Value>();
let mut start = 0;
while let Some(next) = stream.next() {
match next {
Ok(record) => {
let end = stream.byte_offset();
out(self.record(&record, trim(&bytes[start..end])));
start = end;
}
Err(error) => {
out(dead(
Stage::Framing,
error.to_string(),
trim(&bytes[start..]),
));
return;
}
}
}
}
}
}
fn record(&self, record: &Value, raw: &[u8]) -> Outcome {
if !record.is_object() {
return dead(
Stage::Decoding,
"the record is not a JSON object".to_owned(),
raw,
);
}
let Some(kind) = self.kinds.iter().find(|kind| {
kind.when
.iter()
.all(|(path, expected)| path.lookup(record) == Some(expected))
}) else {
return dead(
Stage::Mapping,
format!("no kind of source `{}` accepts the record", self.name),
raw,
);
};
let mut event = Map::new();
let mut unmapped = Map::new();
let mut issues = Vec::new();
let mut consumed: Vec<&SourcePath> = Vec::new();
for (name, value) in &kind.class {
event.insert((*name).to_owned(), value.clone());
}
for field in &kind.fields {
match &field.source {
Source::Constant(value) => set(&mut event, field.target.segments(), value.clone()),
Source::Path { path, coercion } => {
let Some(found) = path.lookup(record).filter(|value| !value.is_null()) else {
continue;
};
consumed.push(path);
match coerce(found, *coercion) {
Ok(value) => set(&mut event, field.target.segments(), value),
Err(reason) => {
issues.push(Issue {
target: field.target.as_str().to_owned(),
source: path.text.clone(),
reason,
});
let listed = kind
.unmapped
.iter()
.any(|object| path.is_member(object, path.last()));
let name = if listed { path.last() } else { &path.text };
unmapped.insert(name.to_owned(), found.clone());
}
}
}
}
}
for object in &kind.unmapped {
let Some(Value::Object(members)) = object.lookup(record) else {
continue;
};
for (key, member) in members {
if member.is_null() || consumed.iter().any(|path| path.is_member(object, key)) {
continue;
}
unmapped
.entry(key.clone())
.or_insert_with(|| member.clone());
}
}
if !unmapped.is_empty() {
event.insert("unmapped".to_owned(), Value::Object(unmapped));
}
Outcome::Event(Normalized {
event: Value::Object(event),
kind: kind.name.clone(),
issues,
})
}
}
fn dead(stage: Stage, error: String, raw: &[u8]) -> Outcome {
Outcome::DeadLetter(DeadLetter {
stage,
error,
raw: raw.to_vec(),
})
}
fn trim(bytes: &[u8]) -> &[u8] {
let start = bytes
.iter()
.position(|byte| !byte.is_ascii_whitespace())
.unwrap_or(bytes.len());
let end = bytes
.iter()
.rposition(|byte| !byte.is_ascii_whitespace())
.map_or(start, |position| position + 1);
&bytes[start..end]
}
fn source_path(kind: &str, text: &str) -> Result<SourcePath, DefinitionError> {
SourcePath::parse(text).ok_or_else(|| DefinitionError::SourcePath {
kind: kind.to_owned(),
path: text.to_owned(),
})
}
fn compile_fields(
kind: &str,
specs: &std::collections::BTreeMap<String, FieldSpec>,
) -> Result<Vec<Field>, DefinitionError> {
specs
.iter()
.map(|(target, spec)| {
let path = FieldPath::parse(target).map_err(|source| DefinitionError::Target {
kind: kind.to_owned(),
target: target.clone(),
source,
})?;
let reserved = match path.segments()[0].as_str() {
"class_uid" | "category_uid" | "type_uid" | "activity_id" => {
Some("the kind's `class`")
}
"unmapped" => Some("the kind's `unmapped` list"),
_ => None,
};
if let Some(reason) = reserved {
return Err(DefinitionError::Reserved {
kind: kind.to_owned(),
target: target.clone(),
reason,
});
}
let source = match spec {
FieldSpec::Path(from) => Source::Path {
path: source_path(kind, from)?,
coercion: None,
},
FieldSpec::Converted { from, coercion } => Source::Path {
path: source_path(kind, from)?,
coercion: Some(*coercion),
},
FieldSpec::Value { value } => Source::Constant(Value::from(value)),
};
Ok(Field {
target: path,
source,
})
})
.collect()
}
fn check_overlaps(kind: &str, fields: &[Field]) -> Result<(), DefinitionError> {
for (index, first) in fields.iter().enumerate() {
for second in &fields[index + 1..] {
let (a, b) = (first.target.segments(), second.target.segments());
if a.starts_with(b) || b.starts_with(a) {
return Err(DefinitionError::Overlap {
kind: kind.to_owned(),
first: first.target.as_str().to_owned(),
second: second.target.as_str().to_owned(),
});
}
}
}
Ok(())
}
fn check_schema(
kind: &str,
class_uid: u32,
activity_id: u32,
fields: &[Field],
) -> Result<(), DefinitionError> {
let class = schema::class(class_uid).ok_or_else(|| DefinitionError::UnknownClass {
kind: kind.to_owned(),
class_uid,
version: goliath_ocsf::SCHEMA_VERSION,
})?;
let activities = class
.attribute("activity_id")
.map(schema::Attribute::enum_values)
.unwrap_or_default();
if !activities.contains(&i64::from(activity_id)) {
return Err(DefinitionError::UnknownActivity {
kind: kind.to_owned(),
class: class.name(),
activity_id,
});
}
for field in fields {
let target = field.target.as_str();
let found = class
.resolve(target)
.map_err(|source| DefinitionError::Attribute {
kind: kind.to_owned(),
source,
})?;
if found.within_array {
return Err(DefinitionError::WithinArray {
kind: kind.to_owned(),
target: target.to_owned(),
});
}
let Some(writes) = field.source.writes() else {
continue;
};
if found.free_form {
continue;
}
let attribute = found.attribute;
let fits = !attribute.is_array()
&& match writes {
Writes::Boolean => attribute.base() == Base::Boolean,
Writes::Integer => matches!(attribute.base(), Base::Integer | Base::Long),
Writes::Text => attribute.base() == Base::String,
Writes::Timestamp => attribute.type_name() == "timestamp_t",
};
if !fits {
let holds = match (attribute.is_array(), attribute.base()) {
(true, _) => format!("an array of `{}`", attribute.type_name()),
(false, Base::Object) => format!("an object `{}`", attribute.type_name()),
(false, _) => format!("`{}`", attribute.type_name()),
};
return Err(DefinitionError::Type {
kind: kind.to_owned(),
target: target.to_owned(),
holds,
writes: writes.describe(),
});
}
let values = attribute.enum_values();
if let Source::Constant(Value::Number(number)) = &field.source
&& let Some(value) = number.as_i64()
&& !values.is_empty()
&& !values.contains(&value)
{
return Err(DefinitionError::UnknownValue {
kind: kind.to_owned(),
target: target.to_owned(),
value,
});
}
}
Ok(())
}
#[derive(Debug, Clone, Copy)]
enum Writes {
Boolean,
Integer,
Text,
Timestamp,
}
impl Writes {
fn describe(self) -> &'static str {
match self {
Self::Boolean => "a boolean",
Self::Integer => "an integer",
Self::Text => "text",
Self::Timestamp => "a timestamp",
}
}
}
impl Source {
fn writes(&self) -> Option<Writes> {
match self {
Self::Constant(Value::Bool(_)) => Some(Writes::Boolean),
Self::Constant(Value::Number(_)) => Some(Writes::Integer),
Self::Constant(Value::String(_)) => Some(Writes::Text),
Self::Constant(_) | Self::Path { coercion: None, .. } => None,
Self::Path {
coercion: Some(coercion),
..
} => Some(match coercion {
Coercion::String => Writes::Text,
Coercion::Integer => Writes::Integer,
Coercion::Timestamp => Writes::Timestamp,
}),
}
}
}
fn set(object: &mut Map<String, Value>, segments: &[String], value: Value) {
let Some((last, parents)) = segments.split_last() else {
return;
};
let mut object = object;
for segment in parents {
let child = object
.entry(segment.clone())
.or_insert_with(|| Value::Object(Map::new()));
let Value::Object(child) = child else {
return;
};
object = child;
}
object.insert(last.clone(), value);
}
fn coerce(value: &Value, coercion: Option<Coercion>) -> Result<Value, String> {
let Some(coercion) = coercion else {
return Ok(value.clone());
};
match (coercion, value) {
(Coercion::String, Value::String(_)) => Ok(value.clone()),
(Coercion::String, Value::Number(number)) => Ok(Value::String(number.to_string())),
(Coercion::Integer, Value::Number(number)) => number
.as_i64()
.map(Value::from)
.ok_or_else(|| format!("{number} is not a whole number")),
(Coercion::Integer, Value::String(text)) => text
.trim()
.parse::<i64>()
.map(Value::from)
.map_err(|_| format!("`{text}` is not a whole number")),
(Coercion::Timestamp, Value::String(text)) => text
.parse::<jiff::Timestamp>()
.map(|time| Value::from(time.as_millisecond()))
.map_err(|error| format!("`{text}` is not an RFC 3339 time: {error}")),
(coercion, other) => Err(format!(
"cannot convert {} to {coercion:?}",
describe(other)
)),
}
}
fn describe(value: &Value) -> &'static str {
match value {
Value::Null => "null",
Value::Bool(_) => "a boolean",
Value::Number(_) => "a number",
Value::String(_) => "text",
Value::Array(_) => "a list",
Value::Object(_) => "an object",
}
}