|
| 1 | +# Stackdog Security - Project Memory |
| 2 | + |
| 3 | +## Project Identity |
| 4 | + |
| 5 | +**Name:** Stackdog Security |
| 6 | +**Version:** 0.1.0 (Security-focused rewrite) |
| 7 | +**Type:** Container and Linux Server Security Platform |
| 8 | +**License:** MIT |
| 9 | + |
| 10 | +## Core Mission |
| 11 | + |
| 12 | +> Provide real-time security monitoring, AI-powered threat detection, and automated response for Docker containers and Linux servers using Rust and eBPF technologies. |
| 13 | +
|
| 14 | +## Key Decisions |
| 15 | + |
| 16 | +### Architecture Decisions |
| 17 | + |
| 18 | +| ID | Decision | Rationale | Date | |
| 19 | +|----|----------|-----------|------| |
| 20 | +| **ARCH-001** | Use eBPF for syscall monitoring | Minimal overhead (<5% CPU), kernel-level visibility, safe (sandboxed) | 2026-03-13 | |
| 21 | +| **ARCH-002** | Use Candle for ML instead of Python | Native Rust, no Python dependencies, fast inference, maintained by HuggingFace | 2026-03-13 | |
| 22 | +| **ARCH-003** | Use nftables over iptables | Modern, faster, better batch support, iptables as fallback | 2026-03-13 | |
| 23 | +| **ARCH-004** | TDD development methodology | Better code quality, maintainability, regression prevention | 2026-03-13 | |
| 24 | +| **ARCH-005** | Functional programming principles | Immutability, fewer bugs, easier reasoning about code | 2026-03-13 | |
| 25 | + |
| 26 | +### Technology Choices |
| 27 | + |
| 28 | +| Component | Technology | Alternatives Considered | |
| 29 | +|-----------|-----------|------------------------| |
| 30 | +| **eBPF Framework** | aya-rs | libbpf (C), bcc (Python) | |
| 31 | +| **ML Framework** | Candle (HuggingFace) | PyTorch (Python), ONNX Runtime, linfa | |
| 32 | +| **Web Framework** | Actix-web 4.x | Axum, Rocket | |
| 33 | +| **Database** | SQLite + rusqlite + r2d2 | PostgreSQL, Redis | |
| 34 | +| **Firewall** | nftables (netlink) | iptables, firewalld | |
| 35 | + |
| 36 | +## Project Structure |
| 37 | + |
| 38 | +``` |
| 39 | +stackdog/ |
| 40 | +├── src/ |
| 41 | +│ ├── collectors/ # Event collection (eBPF, Docker, etc.) |
| 42 | +│ ├── events/ # Event types and structures |
| 43 | +│ ├── ml/ # ML engine (Candle-based) |
| 44 | +│ ├── firewall/ # Firewall management (nftables/iptables) |
| 45 | +│ ├── response/ # Automated response actions |
| 46 | +│ ├── correlator/ # Event correlation |
| 47 | +│ ├── alerting/ # Alert system |
| 48 | +│ ├── api/ # REST API + WebSocket |
| 49 | +│ ├── config/ # Configuration |
| 50 | +│ ├── models/ # Data models |
| 51 | +│ ├── database/ # Database operations |
| 52 | +│ └── utils/ # Utilities |
| 53 | +├── ebpf/ # eBPF programs (separate crate) |
| 54 | +├── web/ # React/TypeScript frontend |
| 55 | +├── tests/ # Integration tests |
| 56 | +├── benches/ # Performance benchmarks |
| 57 | +└── models/ # Pre-trained ML models |
| 58 | +``` |
| 59 | + |
| 60 | +## Development Principles |
| 61 | + |
| 62 | +### Clean Code (Robert C. Martin) |
| 63 | + |
| 64 | +1. **DRY** - Don't Repeat Yourself |
| 65 | +2. **SRP** - Single Responsibility Principle |
| 66 | +3. **OCP** - Open/Closed Principle |
| 67 | +4. **DIP** - Dependency Inversion Principle |
| 68 | +5. **Functional First** - Immutability, From/Into traits, builder pattern |
| 69 | + |
| 70 | +### TDD Workflow |
| 71 | + |
| 72 | +``` |
| 73 | +Red → Green → Refactor |
| 74 | +``` |
| 75 | + |
| 76 | +1. Write failing test |
| 77 | +2. Run test (verify failure) |
| 78 | +3. Implement minimal code to pass |
| 79 | +4. Run test (verify pass) |
| 80 | +5. Refactor (maintain passing tests) |
| 81 | + |
| 82 | +### Code Review Checklist |
| 83 | + |
| 84 | +- [ ] Tests written first (TDD) |
| 85 | +- [ ] All tests pass |
| 86 | +- [ ] Code formatted (`cargo fmt`) |
| 87 | +- [ ] No clippy warnings |
| 88 | +- [ ] DRY principle followed |
| 89 | +- [ ] Functions < 50 lines |
| 90 | +- [ ] Error handling comprehensive |
| 91 | +- [ ] Documentation for public APIs |
| 92 | + |
| 93 | +## Key APIs and Interfaces |
| 94 | + |
| 95 | +### Event Types |
| 96 | + |
| 97 | +```rust |
| 98 | +// Core security event |
| 99 | +pub enum SecurityEvent { |
| 100 | + Syscall(SyscallEvent), |
| 101 | + Network(NetworkEvent), |
| 102 | + Container(ContainerEvent), |
| 103 | + Alert(AlertEvent), |
| 104 | +} |
| 105 | + |
| 106 | +// Syscall event from eBPF |
| 107 | +pub struct SyscallEvent { |
| 108 | + pub pid: u32, |
| 109 | + pub uid: u32, |
| 110 | + pub syscall_type: SyscallType, |
| 111 | + pub timestamp: DateTime<Utc>, |
| 112 | + pub container_id: Option<String>, |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### ML Interface |
| 117 | + |
| 118 | +```rust |
| 119 | +// Feature vector for ML |
| 120 | +pub struct SecurityFeatures { |
| 121 | + pub syscall_rate: f64, |
| 122 | + pub network_rate: f64, |
| 123 | + pub unique_processes: u32, |
| 124 | + pub privileged_calls: u32, |
| 125 | + // ... |
| 126 | +} |
| 127 | + |
| 128 | +// Threat score output |
| 129 | +pub enum ThreatScore { |
| 130 | + Normal, |
| 131 | + Low, |
| 132 | + Medium, |
| 133 | + High, |
| 134 | + Critical, |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### Firewall Interface |
| 139 | + |
| 140 | +```rust |
| 141 | +pub trait FirewallBackend { |
| 142 | + fn add_rule(&self, rule: &Rule) -> Result<()>; |
| 143 | + fn remove_rule(&self, rule: &Rule) -> Result<()>; |
| 144 | + fn batch_update(&self, rules: &[Rule]) -> Result<()>; |
| 145 | + fn block_container(&self, container_id: &str) -> Result<()>; |
| 146 | + fn quarantine_container(&self, container_id: &str) -> Result<()>; |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +## Configuration |
| 151 | + |
| 152 | +### Environment Variables |
| 153 | + |
| 154 | +```bash |
| 155 | +APP_HOST=0.0.0.0 |
| 156 | +APP_PORT=5000 |
| 157 | +DATABASE_URL=stackdog.db |
| 158 | +RUST_LOG=info |
| 159 | +RUST_BACKTRACE=full |
| 160 | + |
| 161 | +# Security-specific |
| 162 | +EBPF_ENABLED=true |
| 163 | +FIREWALL_BACKEND=nftables # or iptables |
| 164 | +ML_ENABLED=true |
| 165 | +ML_MODEL_PATH=models/ |
| 166 | +ALERT_THRESHOLD=0.75 |
| 167 | +``` |
| 168 | + |
| 169 | +### Cargo Features |
| 170 | + |
| 171 | +```toml |
| 172 | +[features] |
| 173 | +default = ["nftables", "ml"] |
| 174 | +nftables = ["netlink-packet-route"] |
| 175 | +iptables = ["iptables"] |
| 176 | +ml = ["candle-core", "candle-nn"] |
| 177 | +ebpf = ["aya"] |
| 178 | +``` |
| 179 | + |
| 180 | +## Testing Strategy |
| 181 | + |
| 182 | +### Test Categories |
| 183 | + |
| 184 | +| Category | Location | Command | Coverage Target | |
| 185 | +|----------|----------|---------|-----------------| |
| 186 | +| Unit | `src/**/*.rs` | `cargo test` | 80%+ | |
| 187 | +| Integration | `tests/integration/` | `cargo test --test integration` | Critical paths | |
| 188 | +| E2E | `tests/e2e/` | `cargo test --test e2e` | Key workflows | |
| 189 | +| Benchmark | `benches/` | `cargo bench` | Performance targets | |
| 190 | + |
| 191 | +### Performance Targets |
| 192 | + |
| 193 | +| Metric | Target | |
| 194 | +|--------|--------| |
| 195 | +| Event throughput | 100K events/sec | |
| 196 | +| ML inference latency | <10ms | |
| 197 | +| Firewall update | <1ms per rule | |
| 198 | +| Memory usage | <256MB baseline | |
| 199 | +| CPU overhead | <5% | |
| 200 | + |
| 201 | +## Dependencies |
| 202 | + |
| 203 | +### Core |
| 204 | + |
| 205 | +- `actix-web` - Web framework |
| 206 | +- `aya` - eBPF framework |
| 207 | +- `candle-core`, `candle-nn` - ML framework |
| 208 | +- `bollard` - Docker API |
| 209 | +- `rusqlite` - SQLite driver |
| 210 | +- `r2d2` - Connection pool |
| 211 | +- `netlink-packet-route` - nftables |
| 212 | +- `tokio` - Async runtime |
| 213 | + |
| 214 | +### Development |
| 215 | + |
| 216 | +- `mockall` - Mocking for tests |
| 217 | +- `criterion` - Benchmarking |
| 218 | +- `cargo-audit` - Security audit |
| 219 | +- `cargo-deny` - Dependency linting |
| 220 | + |
| 221 | +## Milestones |
| 222 | + |
| 223 | +| Version | Target | Features | |
| 224 | +|---------|--------|----------| |
| 225 | +| v0.1.0 | Week 4 | eBPF collectors, basic rules | |
| 226 | +| v0.2.0 | Week 6 | Firewall integration | |
| 227 | +| v0.3.0 | Week 10 | ML anomaly detection | |
| 228 | +| v0.4.0 | Week 12 | Alerting system | |
| 229 | +| v0.5.0 | Week 16 | Web dashboard | |
| 230 | +| v1.0.0 | Week 18 | Production release | |
| 231 | + |
| 232 | +## Risks and Mitigations |
| 233 | + |
| 234 | +| Risk | Impact | Probability | Mitigation | |
| 235 | +|------|--------|-------------|------------| |
| 236 | +| eBPF kernel compatibility | High | Medium | Fallback to auditd | |
| 237 | +| ML model accuracy | High | Medium | Start with rule-based, iterate | |
| 238 | +| Performance overhead | High | Low | Benchmark early, optimize | |
| 239 | +| False positives | Medium | High | Tunable thresholds, learning period | |
| 240 | + |
| 241 | +## Open Questions |
| 242 | + |
| 243 | +1. **Model Training:** How to collect training data for ML models? |
| 244 | + - Decision: Start with synthetic data, then real-world collection |
| 245 | + |
| 246 | +2. **Multi-node Support:** Single node first, cluster later? |
| 247 | + - Decision: Single node for v1.0, cluster in v2.0 |
| 248 | + |
| 249 | +3. **Kubernetes Support:** Include in scope? |
| 250 | + - Decision: Out of scope for v1.0, backlog for v2.0 |
| 251 | + |
| 252 | +## Resources |
| 253 | + |
| 254 | +### Documentation |
| 255 | + |
| 256 | +- [DEVELOPMENT.md](DEVELOPMENT.md) - Full development plan |
| 257 | +- [TODO.md](TODO.md) - Task tracking |
| 258 | +- [BUGS.md](BUGS.md) - Bug tracking |
| 259 | +- [CONTRIBUTING.md](CONTRIBUTING.md) - Contribution guidelines |
| 260 | + |
| 261 | +### External |
| 262 | + |
| 263 | +- [Rust Book](https://doc.rust-lang.org/book/) |
| 264 | +- [Candle Docs](https://docs.rs/candle-core) |
| 265 | +- [aya-rs Docs](https://aya-rs.dev/) |
| 266 | +- [eBPF Documentation](https://ebpf.io/) |
| 267 | + |
| 268 | +## Contact |
| 269 | + |
| 270 | +- **Project Lead:** Vasili Pascal |
| 271 | +- **Email:** info@try.direct |
| 272 | +- **Twitter:** [@VasiliiPascal](https://twitter.com/VasiliiPascal) |
| 273 | +- **Gitter:** [stackdog/community](https://gitter.im/stackdog/community) |
| 274 | + |
| 275 | +--- |
| 276 | + |
| 277 | +*Last updated: 2026-03-13* |
0 commit comments