Skip to content

Commit

Permalink
new(event): make serde support optional
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek authored and poiana committed Oct 21, 2024
1 parent c315f94 commit adb0d9d
Show file tree
Hide file tree
Showing 26 changed files with 190 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Docs
on:
push:
branches: [main]
branches: [ main ]
workflow_dispatch:
permissions:
contents: read
Expand All @@ -27,7 +27,7 @@ jobs:
- name: Clean docs folder
run: cargo clean --doc
- name: Build docs
run: cargo doc --no-deps
run: cargo doc --all-features --no-deps
- name: Add redirect
run: echo '<meta http-equiv="refresh" content="0;url=falco_plugin/index.html">' > target/doc/index.html
- name: Remove lock file
Expand Down
7 changes: 5 additions & 2 deletions falco_event/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ readme = "README.md"
keywords = ["falco", "security"]
categories = ["api-bindings"]

[features]
serde = ["dep:serde", "bitflags/serde", "falco_event_derive/serde"]

[dependencies]
byteorder = "1.5.0"
falco_event_derive = { path = "../falco_event_derive", version = "0.4.0" }
memchr = "2.7.1"
num-derive = "0.4.2"
num-traits = "0.2.17"
thiserror = "1.0.58"
bitflags = { version = "2.4.2", features = ["serde"] }
bitflags = { version = "2.4.2" }
anyhow = "1.0.81"
chrono = "0.4.38"
serde = { version = "1.0.210", features = ["derive"] }
serde = { version = "1.0.210", features = ["derive"], optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["signal"] }
Expand Down
1 change: 1 addition & 0 deletions falco_event/src/events/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ where
}
}

#[cfg(feature = "serde")]
mod serde_event {
use super::*;
use crate::events::types::AnyEvent;
Expand Down
4 changes: 2 additions & 2 deletions falco_event/src/events/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::event_derive::Format;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone)]
pub struct EventMetadata {
pub ts: u64,
pub tid: i64,
Expand Down
9 changes: 6 additions & 3 deletions falco_event/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod event;
mod metadata;
pub(crate) mod payload;
mod raw_event;
#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
mod serde_tests;
mod to_bytes;

Expand Down Expand Up @@ -51,12 +51,15 @@ mod to_bytes;
/// |-----------------------------------------------------------|-----------------|--------------|
/// | Loading from a [RawEvent](`crate::events::RawEvent`) | supported | ^1 |
/// | [writing to a byte buffer](`crate::events::EventToBytes`) | supported | supported |
/// | [arbitrary serialization](`serde::Serialize`) | supported | supported |
/// | [arbitrary deserialization](`serde::Deserialize`) | | supported |
/// | [arbitrary serialization](`serde::Serialize`) ^2 | supported | supported |
/// | [arbitrary deserialization](`serde::Deserialize`) ^2 | | supported |
///
/// **Footnotes**:
///
/// 1. Loading an owned event from a raw event is technically possible but has no benefits over
/// loading a borrowed event and incurs extra allocations and copies, so to avoid the confusion
/// it's explicitly not supported.
///
/// 2. Arbitrary serialization and deserialization with [`serde`] is only supported when
/// the `serde` feature of the crate is enabled.
pub mod types;
1 change: 1 addition & 0 deletions falco_event/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ mod event_derive {
pub use crate::types::BorrowDeref;
pub use crate::types::Borrowed;

#[cfg(feature = "serde")]
pub use crate::types::serde;
}
1 change: 1 addition & 0 deletions falco_event/src/types/bytebuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl BorrowDeref for Vec<u8> {
}
}

#[cfg(feature = "serde")]
pub mod serde {
pub mod bytebuf {
use crate::types::utf_chunked::{OwnedUtfChunked, UtfChunked};
Expand Down
9 changes: 7 additions & 2 deletions falco_event/src/types/fd_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::fields::event_flags::PT_FLAGS16_file_flags;
use crate::fields::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
use serde::{Deserialize, Serialize};

/// A list of file descriptors with flags
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct FdList(pub Vec<(u64, PT_FLAGS16_file_flags)>);

impl<F> Format<F> for FdList
Expand Down Expand Up @@ -92,6 +92,11 @@ mod tests {

assert_eq!(fdlist, fdlist2)
}
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use super::*;

#[test]
fn test_serde_fd_list() {
Expand Down
2 changes: 2 additions & 0 deletions falco_event/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ mod path;
mod primitive;
mod string;
mod time;
#[cfg(feature = "serde")]
mod utf_chunked;

#[cfg(feature = "serde")]
pub mod serde {
pub use super::bytebuf::serde::*;
pub use super::string::serde::*;
Expand Down
4 changes: 2 additions & 2 deletions falco_event/src/types/net/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ impl<F> Format<F> for EndpointV6 {
}
}

#[cfg(test)]
mod tests {
#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use super::Port;
use std::net::{Ipv4Addr, Ipv6Addr};

Expand Down
7 changes: 4 additions & 3 deletions falco_event/src/types/net/ipnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::net::IpAddr;
///
/// This is a wrapper around [IpAddr] that makes it a distinct type, suitable for storing
/// IP (v4 or v6) subnets.
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct IpNet(pub IpAddr);

impl FromBytes<'_> for IpNet {
Expand Down Expand Up @@ -40,8 +41,8 @@ where
}
}

#[cfg(test)]
mod tests {
#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

#[test]
Expand Down
12 changes: 6 additions & 6 deletions falco_event/src/types/net/ipv4net.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;
use std::fmt::Formatter;
use std::io::Write;
use std::net::Ipv4Addr;

use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;

/// An IPv4 network
///
/// This is a wrapper around [Ipv4Addr] that makes it a distinct type, suitable for storing
/// IPv4 subnets.
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Ipv4Net(pub Ipv4Addr);

impl FromBytes<'_> for Ipv4Net {
Expand Down Expand Up @@ -38,8 +38,8 @@ impl<F> Format<F> for Ipv4Net {
}
}

#[cfg(test)]
mod tests {
#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use std::net::Ipv4Addr;

#[test]
Expand Down
12 changes: 6 additions & 6 deletions falco_event/src/types/net/ipv6net.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;
use std::fmt::Formatter;
use std::io::Write;
use std::net::Ipv6Addr;

use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;

/// An IPv6 network
///
/// This is a wrapper around [Ipv6Addr] that makes it a distinct type, suitable for storing
/// IPv6 subnets.
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Ipv6Net(pub Ipv6Addr);

impl FromBytes<'_> for Ipv6Net {
Expand Down Expand Up @@ -38,8 +38,8 @@ impl<F> Format<F> for Ipv6Net {
}
}

#[cfg(test)]
mod tests {
#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use std::net::Ipv6Addr;

#[test]
Expand Down
23 changes: 15 additions & 8 deletions falco_event/src/types/net/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use crate::ffi::{PPM_AF_INET, PPM_AF_INET6, PPM_AF_LOCAL, PPM_AF_UNSPEC};
use crate::types::format::Format;
use crate::types::{Borrow, Borrowed, EndpointV4, EndpointV6};
use byteorder::{ReadBytesExt, WriteBytesExt};
use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
use std::fmt::Formatter;
use std::io::Write;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};

/// A socket address
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Debug)]
pub enum SockAddr<'a> {
/// Unix sockets
Unix(&'a Path),
Expand All @@ -24,7 +24,10 @@ pub enum SockAddr<'a> {
V6(EndpointV6),

/// any other address family is represented as the number (`PPM_AF_*` constant) and the raw data
Other(u8, #[serde(with = "crate::types::serde::bytebuf")] &'a [u8]),
Other(
u8,
#[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] &'a [u8],
),
}

impl ToBytes for SockAddr<'_> {
Expand Down Expand Up @@ -106,8 +109,9 @@ where
}

/// A socket address (owned)
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Debug)]
pub enum OwnedSockAddr {
/// Unix sockets
Unix(PathBuf),
Expand All @@ -119,7 +123,10 @@ pub enum OwnedSockAddr {
V6(EndpointV6),

/// any other address family is represented as the number (`PPM_AF_*` constant) and the raw data
Other(u8, #[serde(with = "crate::types::serde::bytebuf")] Vec<u8>),
Other(
u8,
#[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] Vec<u8>,
),
}

impl<'a> Borrowed for SockAddr<'a> {
Expand All @@ -139,7 +146,7 @@ impl Borrow for OwnedSockAddr {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
mod tests {
use crate::types::{OwnedSockAddr, Port, SockAddr};
use std::net::{Ipv4Addr, Ipv6Addr};
Expand Down
29 changes: 22 additions & 7 deletions falco_event/src/types/net/socktuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use crate::types::format::Format;
use crate::types::net::endpoint::{EndpointV4, EndpointV6};
use crate::types::{Borrow, Borrowed};
use byteorder::{ReadBytesExt, WriteBytesExt};
use serde::{Deserialize, Serialize};

/// Socket tuple: describing both endpoints of a connection
#[derive(Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Debug, Eq, PartialEq)]
pub enum SockTuple<'a> {
/// Unix socket connection
Unix {
Expand Down Expand Up @@ -41,7 +41,10 @@ pub enum SockTuple<'a> {
},

/// Unknown/other socket family: `PPM_AF_*` id and a raw byte buffer
Other(u8, #[serde(with = "crate::types::serde::bytebuf")] &'a [u8]),
Other(
u8,
#[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] &'a [u8],
),
}

impl Display for SockTuple<'_> {
Expand Down Expand Up @@ -173,8 +176,9 @@ where
}

/// Socket tuple: describing both endpoints of a connection (owned)
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Debug)]
pub enum OwnedSockTuple {
/// Unix socket connection
Unix {
Expand Down Expand Up @@ -203,7 +207,10 @@ pub enum OwnedSockTuple {
},

/// Unknown/other socket family: `PPM_AF_*` id and a raw byte buffer
Other(u8, #[serde(with = "crate::types::serde::bytebuf")] Vec<u8>),
Other(
u8,
#[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] Vec<u8>,
),
}

impl<'a> Borrowed for SockTuple<'a> {
Expand Down Expand Up @@ -324,6 +331,14 @@ mod tests {

assert_eq!(binary, binary2.as_slice(),);
}
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use crate::types::{OwnedSockTuple, Port, SockTuple};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::path::Path;
use std::str::FromStr;

#[test]
fn test_serde_socktuple_unix() {
Expand Down
Loading

0 comments on commit adb0d9d

Please sign in to comment.