Skip to content

Commit 8759564

Browse files
author
vsilent
committed
refactoring, ebpf / containers
1 parent e72c96b commit 8759564

7 files changed

Lines changed: 48 additions & 37 deletions

File tree

src/collectors/ebpf/container.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,16 @@ mod tests {
196196

197197
#[test]
198198
fn test_parse_docker_cgroup() {
199-
let cgroup = "12:memory:/docker/abc123def456789012345678901234567890";
199+
let cgroup = "12:memory:/docker/abc123def456abc123def456abc123def456abc123def456abc123def456abcd";
200200
let result = ContainerDetector::parse_container_from_cgroup(cgroup);
201-
assert_eq!(result, Some("abc123def456789012345678901234567890".to_string()));
201+
assert_eq!(result, Some("abc123def456abc123def456abc123def456abc123def456abc123def456abcd".to_string()));
202202
}
203-
203+
204204
#[test]
205205
fn test_parse_kubernetes_cgroup() {
206-
let cgroup = "11:cpu:/kubepods/pod123/def456abc789012345678901234567890";
206+
let cgroup = "11:cpu:/kubepods/pod123/def456abc123def456abc123def456abc123def456abc123def456abc123def4";
207207
let result = ContainerDetector::parse_container_from_cgroup(cgroup);
208-
assert_eq!(result, Some("def456abc789012345678901234567890".to_string()));
208+
assert_eq!(result, Some("def456abc123def456abc123def456abc123def456abc123def456abc123def4".to_string()));
209209
}
210210

211211
#[test]
@@ -215,6 +215,7 @@ mod tests {
215215
assert_eq!(result, None);
216216
}
217217

218+
#[cfg(target_os = "linux")]
218219
#[test]
219220
fn test_validate_valid_container_id() {
220221
let detector = ContainerDetector::new().unwrap();
@@ -226,6 +227,7 @@ mod tests {
226227
assert!(detector.validate_container_id("abc123def456"));
227228
}
228229

230+
#[cfg(target_os = "linux")]
229231
#[test]
230232
fn test_validate_invalid_container_id() {
231233
let detector = ContainerDetector::new().unwrap();

src/collectors/ebpf/programs/mod.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/collectors/ebpf/types.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct EbpfSyscallEvent {
2727

2828
/// Event data union
2929
#[repr(C)]
30-
#[derive(Debug, Clone, Copy)]
30+
#[derive(Clone, Copy)]
3131
pub union EbpfEventData {
3232
/// execve data
3333
pub execve: ExecveData,
@@ -41,6 +41,14 @@ pub union EbpfEventData {
4141
pub raw: [u8; 128],
4242
}
4343

44+
impl std::fmt::Debug for EbpfEventData {
45+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46+
// SAFETY: raw is always a valid field in any union variant
47+
let raw = unsafe { self.raw };
48+
write!(f, "EbpfEventData {{ raw: {:?} }}", &raw[..])
49+
}
50+
}
51+
4452
impl Default for EbpfEventData {
4553
fn default() -> Self {
4654
Self {
@@ -51,7 +59,7 @@ impl Default for EbpfEventData {
5159

5260
/// execve-specific data
5361
#[repr(C)]
54-
#[derive(Debug, Clone, Copy, Default)]
62+
#[derive(Debug, Clone, Copy)]
5563
pub struct ExecveData {
5664
/// Filename length
5765
pub filename_len: u32,
@@ -61,6 +69,12 @@ pub struct ExecveData {
6169
pub argc: u32,
6270
}
6371

72+
impl Default for ExecveData {
73+
fn default() -> Self {
74+
Self { filename_len: 0, filename: [0u8; 128], argc: 0 }
75+
}
76+
}
77+
6478
/// connect-specific data
6579
#[repr(C)]
6680
#[derive(Debug, Clone, Copy, Default)]
@@ -75,7 +89,7 @@ pub struct ConnectData {
7589

7690
/// openat-specific data
7791
#[repr(C)]
78-
#[derive(Debug, Clone, Copy, Default)]
92+
#[derive(Debug, Clone, Copy)]
7993
pub struct OpenatData {
8094
/// File path length
8195
pub path_len: u32,
@@ -85,6 +99,12 @@ pub struct OpenatData {
8599
pub flags: u32,
86100
}
87101

102+
impl Default for OpenatData {
103+
fn default() -> Self {
104+
Self { path_len: 0, path: [0u8; 256], flags: 0 }
105+
}
106+
}
107+
88108
/// ptrace-specific data
89109
#[repr(C)]
90110
#[derive(Debug, Clone, Copy, Default)]

src/database/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ pub mod repositories;
77
pub use connection::{create_pool, init_database, DbPool};
88
pub use models::*;
99
pub use repositories::alerts::*;
10+
11+
/// Marker struct for module tests
12+
pub struct DatabaseMarker;

src/docker/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl DockerClient {
2929

3030
/// List all containers
3131
pub async fn list_containers(&self, all: bool) -> Result<Vec<ContainerInfo>> {
32-
let options = Some(ListContainersOptions {
32+
let options: Option<ListContainersOptions<String>> = Some(ListContainersOptions {
3333
all,
3434
size: false,
3535
..Default::default()
@@ -85,7 +85,7 @@ impl DockerClient {
8585
/// Quarantine a container (disconnect from all networks)
8686
pub async fn quarantine_container(&self, container_id: &str) -> Result<()> {
8787
// List all networks
88-
let networks: Vec<bollard::models::NetworkResource> = self.client
88+
let networks: Vec<bollard::models::Network> = self.client
8989
.list_networks(None::<ListNetworksOptions<String>>)
9090
.await
9191
.context("Failed to list networks")?;
@@ -104,7 +104,7 @@ impl DockerClient {
104104
};
105105

106106
let _ = self.client
107-
.disconnect_network(&name, Some(options))
107+
.disconnect_network(&name, options)
108108
.await;
109109
}
110110
}

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ pub mod models;
4646
#[cfg(target_os = "linux")]
4747
pub mod firewall;
4848

49-
// Security modules - Collectors
50-
#[cfg(target_os = "linux")]
49+
// Security modules - Collectors (cross-platform; Linux-specific internals are gated within)
5150
pub mod collectors;
5251

5352
// Optional modules
@@ -74,7 +73,6 @@ pub use alerting::{NotificationChannel, NotificationConfig};
7473
pub use firewall::{QuarantineManager, QuarantineState};
7574
#[cfg(target_os = "linux")]
7675
pub use firewall::{ResponseAction, ResponseChain, ResponseExecutor, ResponseType};
77-
#[cfg(target_os = "linux")]
7876
pub use collectors::{EbpfLoader, SyscallMonitor};
7977

8078
// Rules

tests/structure/mod_test.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,61 @@
55
66
#[test]
77
fn test_collectors_module_imports() {
8-
// Verify collectors module exists and can be imported
9-
// This test will compile only if the module structure is correct
10-
use crate::collectors;
11-
12-
// Suppress unused import warning
8+
use stackdog::collectors;
139
let _ = std::marker::PhantomData::<collectors::CollectorsMarker>;
1410
}
1511

1612
#[test]
1713
fn test_events_module_imports() {
18-
use crate::events;
14+
use stackdog::events;
1915
let _ = std::marker::PhantomData::<events::EventsMarker>;
2016
}
2117

2218
#[test]
2319
fn test_rules_module_imports() {
24-
use crate::rules;
20+
use stackdog::rules;
2521
let _ = std::marker::PhantomData::<rules::RulesMarker>;
2622
}
2723

2824
#[test]
2925
fn test_ml_module_imports() {
30-
use crate::ml;
26+
use stackdog::ml;
3127
let _ = std::marker::PhantomData::<ml::MlMarker>;
3228
}
3329

30+
#[cfg(target_os = "linux")]
3431
#[test]
3532
fn test_firewall_module_imports() {
36-
use crate::firewall;
33+
use stackdog::firewall;
3734
let _ = std::marker::PhantomData::<firewall::FirewallMarker>;
3835
}
3936

4037
#[test]
4138
fn test_response_module_imports() {
42-
use crate::response;
39+
use stackdog::response;
4340
let _ = std::marker::PhantomData::<response::ResponseMarker>;
4441
}
4542

4643
#[test]
4744
fn test_correlator_module_imports() {
48-
use crate::correlator;
45+
use stackdog::correlator;
4946
let _ = std::marker::PhantomData::<correlator::CorrelatorMarker>;
5047
}
5148

5249
#[test]
5350
fn test_alerting_module_imports() {
54-
use crate::alerting;
51+
use stackdog::alerting;
5552
let _ = std::marker::PhantomData::<alerting::AlertingMarker>;
5653
}
5754

5855
#[test]
5956
fn test_baselines_module_imports() {
60-
use crate::baselines;
57+
use stackdog::baselines;
6158
let _ = std::marker::PhantomData::<baselines::BaselinesMarker>;
6259
}
6360

6461
#[test]
6562
fn test_database_module_imports() {
66-
use crate::database;
63+
use stackdog::database;
6764
let _ = std::marker::PhantomData::<database::DatabaseMarker>;
6865
}

0 commit comments

Comments
 (0)