Skip to content

Commit

Permalink
fix(user): avoid using serde(flatten) in events (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
qjerome authored Nov 7, 2024
1 parent 7869a85 commit 05c483a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
22 changes: 10 additions & 12 deletions kunai/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,22 +753,20 @@ impl<'s> EventConsumer<'s> {
if let Some(ns) = ns {
match self.cache.get_or_cache_in_ns(ns, p) {
Ok(h) => h,
Err(e) => Hashes {
path: p.to_path_buf().clone(),
meta: FileMeta {
Err(e) => {
let meta = FileMeta {
error: Some(format!("{e}")),
..Default::default()
},
},
};
Hashes::with_meta(p.to_path_buf().clone(), meta)
}
}
} else {
Hashes {
path: p.to_path_buf().clone(),
meta: FileMeta {
error: Some("unknown namespace".into()),
..Default::default()
},
}
let meta = FileMeta {
error: Some("unknown namespace".into()),
..Default::default()
};
Hashes::with_meta(p.to_path_buf().clone(), meta)
}
}

Expand Down
53 changes: 43 additions & 10 deletions kunai/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ pub struct FileMeta {
pub error: Option<String>,
}

impl From<Hashes> for FileMeta {
fn from(value: Hashes) -> Self {
Self {
md5: value.md5,
sha1: value.sha1,
sha256: value.sha256,
sha512: value.sha512,
size: value.size,
error: value.error,
}
}
}

impl FileMeta {
#[inline]
pub(crate) fn iocs(&self) -> Vec<Cow<'_, str>> {
Expand All @@ -67,11 +80,27 @@ impl FileMeta {
#[derive(Debug, Default, Clone, FieldGetter, Serialize, Deserialize)]
pub struct Hashes {
pub path: PathBuf,
#[serde(flatten)]
pub meta: FileMeta,
pub md5: String,
pub sha1: String,
pub sha256: String,
pub sha512: String,
pub size: usize,
pub error: Option<String>,
}

impl Hashes {
pub fn with_meta(p: PathBuf, meta: FileMeta) -> Self {
Self {
path: p,
md5: meta.md5,
sha1: meta.sha1,
sha256: meta.sha256,
sha512: meta.sha512,
size: meta.size,
error: meta.error,
}
}

#[inline(always)]
pub fn from_path_ref<T: AsRef<std::path::Path>>(p: T) -> Self {
let path = p.as_ref();
Expand All @@ -95,23 +124,27 @@ impl Hashes {
sha1.update(&buf[..n]);
sha256.update(&buf[..n]);
sha512.update(&buf[..n]);
h.meta.size += n;
h.size += n;
}

h.meta.md5 = hex::encode(md5.finalize());
h.meta.sha1 = hex::encode(sha1.finalize());
h.meta.sha256 = hex::encode(sha256.finalize());
h.meta.sha512 = hex::encode(sha512.finalize());
h.md5 = hex::encode(md5.finalize());
h.sha1 = hex::encode(sha1.finalize());
h.sha256 = hex::encode(sha256.finalize());
h.sha512 = hex::encode(sha512.finalize());
}

h
}

#[inline(always)]
pub(crate) fn iocs(&self) -> Vec<Cow<'_, str>> {
let mut v = vec![self.path.to_string_lossy()];
v.extend(self.meta.iocs());
v
vec![
self.path.to_string_lossy(),
self.md5.as_str().into(),
self.sha1.as_str().into(),
self.sha256.as_str().into(),
self.sha512.as_str().into(),
]
}
}

Expand Down
5 changes: 3 additions & 2 deletions kunai/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,9 +1015,10 @@ pub struct FileScanData {

impl FileScanData {
pub fn from_hashes(h: Hashes) -> Self {
let p = h.path.clone();
Self {
path: h.path,
meta: h.meta,
path: p,
meta: h.into(),
..Default::default()
}
}
Expand Down

0 comments on commit 05c483a

Please sign in to comment.