Skip to content

Commit c4d222e

Browse files
authored
Merge pull request #1 from vsilent/dev
Dev
2 parents f750dce + d364801 commit c4d222e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5440
-4810
lines changed

.env.sample

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@ APP_HOST=0.0.0.0
55
APP_PORT=5000
66
DATABASE_URL=stackdog.db
77
RUST_BACKTRACE=full
8+
9+
# Log Sniff Configuration
10+
#STACKDOG_LOG_SOURCES=/var/log/syslog,/var/log/auth.log
11+
#STACKDOG_SNIFF_INTERVAL=30
12+
#STACKDOG_SNIFF_OUTPUT_DIR=./stackdog-logs/
13+
14+
# AI Provider Configuration
15+
# Supports OpenAI, Ollama (http://localhost:11434/v1), or any OpenAI-compatible API
16+
#STACKDOG_AI_PROVIDER=openai
17+
#STACKDOG_AI_API_URL=http://localhost:11434/v1
18+
#STACKDOG_AI_API_KEY=
19+
#STACKDOG_AI_MODEL=llama3
20+
21+
# Notification Channels
22+
# Slack: create an incoming webhook at https://api.slack.com/messaging/webhooks
23+
#STACKDOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T.../B.../xxxxx
24+
# Generic webhook endpoint for alert notifications
25+
#STACKDOG_WEBHOOK_URL=https://example.com/webhook

.github/copilot-instructions.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Stackdog Security — Copilot Instructions
2+
3+
## What This Project Is
4+
5+
Stackdog is a Rust-based security platform for Docker containers and Linux servers. It collects events via eBPF syscall monitoring, runs them through a rule/signature engine and optional ML anomaly detection, manages firewall responses (nftables/iptables + container quarantine), and exposes a REST + WebSocket API consumed by a React/TypeScript dashboard.
6+
7+
## Workspace Structure
8+
9+
This is a Cargo workspace with two crates:
10+
- `.` — Main crate (`stackdog`): HTTP server, all security logic
11+
- `ebpf/` — Separate crate (`stackdog-ebpf`): eBPF programs compiled for the kernel (uses `aya-ebpf`)
12+
13+
## Build, Test, and Lint Commands
14+
15+
```bash
16+
# Build
17+
cargo build
18+
cargo build --release
19+
20+
# Tests
21+
cargo test --lib # Unit tests only (in-source)
22+
cargo test --all # All tests including integration
23+
cargo test --lib -- events:: # Run tests for a specific module
24+
cargo test --lib -- rules::scorer # Run a single test by name prefix
25+
26+
# Code quality
27+
cargo fmt --all
28+
cargo clippy --all
29+
cargo audit # Dependency vulnerability scan
30+
31+
# Benchmarks
32+
cargo bench
33+
34+
# Frontend (in web/)
35+
npm test
36+
npm run lint
37+
npm run build
38+
```
39+
40+
## Environment Setup
41+
42+
Requires a `.env` file (copy `.env.sample`). Key variables:
43+
```
44+
APP_HOST=0.0.0.0
45+
APP_PORT=5000
46+
DATABASE_URL=stackdog.db
47+
RUST_BACKTRACE=full
48+
```
49+
50+
System dependencies (Linux): `libsqlite3-dev libssl-dev clang llvm pkg-config`
51+
52+
## Architecture
53+
54+
```
55+
Collectors (Linux only) Rule Engine Response
56+
eBPF syscall events → Signatures → nftables/iptables
57+
Docker daemon events → Threat scoring → Container quarantine
58+
Network events → ML anomaly det. → Alerting
59+
60+
REST + WebSocket API
61+
React/TypeScript UI
62+
```
63+
64+
**Key src/ modules:**
65+
66+
| Module | Purpose |
67+
|---|---|
68+
| `events/` | Core event types: `SyscallEvent`, `SecurityEvent`, `NetworkEvent`, `ContainerEvent` |
69+
| `rules/` | Rule engine, signature database, threat scorer |
70+
| `alerting/` | `AlertManager`, notification channels (Slack/email/webhook) |
71+
| `collectors/` | eBPF loader, Docker daemon events, network collector (Linux only) |
72+
| `firewall/` | nftables management, iptables fallback, `QuarantineManager` (Linux only) |
73+
| `ml/` | Candle-based anomaly detection (optional `ml` feature) |
74+
| `correlator/` | Event correlation engine |
75+
| `baselines/` | Baseline learning for anomaly detection |
76+
| `database/` | SQLite connection pool (`r2d2` + raw `rusqlite`), repositories |
77+
| `api/` | actix-web REST endpoints + WebSocket |
78+
| `response/` | Automated response action pipeline |
79+
80+
## Key Conventions
81+
82+
### Platform-Gating
83+
Linux-only modules (`collectors`, `firewall`) and deps (aya, netlink) are gated:
84+
```rust
85+
#[cfg(target_os = "linux")]
86+
pub mod firewall;
87+
```
88+
The `ebpf` and `ml` features are opt-in and must be enabled explicitly:
89+
```bash
90+
cargo build --features ebpf
91+
cargo build --features ml
92+
```
93+
94+
### Error Handling
95+
- Use `anyhow::{Result, Context}` for application/binary code
96+
- Use `thiserror` for library error types
97+
- Never use `.unwrap()` in production code; use `?` with `.context("...")`
98+
99+
### Database
100+
The project uses raw `rusqlite` with `r2d2` connection pooling. `DbPool` is `r2d2::Pool<SqliteConnectionManager>`. Tables are created with `CREATE TABLE IF NOT EXISTS` in `database::connection::init_database`. Repositories are in `src/database/repositories/` and receive a `&DbPool`.
101+
102+
### API Routes
103+
Each API sub-module exports a `configure_routes(cfg: &mut web::ServiceConfig)` function. All routes are composed in `api::configure_all_routes`, which is the single call site in `main.rs`.
104+
105+
### Test Location
106+
- **Unit tests**: `#[cfg(test)] mod tests { ... }` inside source files
107+
- **Integration tests**: `tests/` directory at workspace root
108+
109+
### eBPF Programs
110+
The `ebpf/` crate is compiled separately for the Linux kernel. User-space loading is handled by `src/collectors/ebpf/` using the `aya` library. Kernel-side programs use `aya-ebpf`.
111+
112+
### Async Runtime
113+
The main binary uses `#[actix_rt::main]`. Library code uses `tokio`. Avoid mixing runtimes.

.github/workflows/codacy-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
steps:
2222
# Checkout the repository to the GitHub Actions runner
2323
- name: Checkout code
24-
uses: actions/checkout@v2
24+
uses: actions/checkout@v4
2525

2626
# Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis
2727
- name: Run Codacy Analysis CLI
@@ -41,6 +41,6 @@ jobs:
4141

4242
# Upload the SARIF file generated in the previous step
4343
- name: Upload SARIF results file
44-
uses: github/codeql-action/upload-sarif@v1
44+
uses: github/codeql-action/upload-sarif@v3
4545
with:
4646
sarif_file: results.sarif

.github/workflows/docker.yml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ on:
1212
jobs:
1313
cicd-linux-docker:
1414
name: Cargo and npm build
15-
runs-on: ubuntu-latest
15+
#runs-on: ubuntu-latest
16+
runs-on: [self-hosted, linux]
1617
steps:
1718
- name: Checkout sources
18-
uses: actions/checkout@v2
19+
uses: actions/checkout@v4
1920

2021
- name: Install stable toolchain
2122
uses: actions-rs/toolchain@v1
@@ -26,7 +27,7 @@ jobs:
2627
components: rustfmt, clippy
2728

2829
- name: Cache cargo registry
29-
uses: actions/cache@v2.1.6
30+
uses: actions/cache@v4
3031
with:
3132
path: ~/.cargo/registry
3233
key: docker-registry-${{ hashFiles('**/Cargo.lock') }}
@@ -35,7 +36,7 @@ jobs:
3536
docker-
3637
3738
- name: Cache cargo index
38-
uses: actions/cache@v2.1.6
39+
uses: actions/cache@v4
3940
with:
4041
path: ~/.cargo/git
4142
key: docker-index-${{ hashFiles('**/Cargo.lock') }}
@@ -48,7 +49,7 @@ jobs:
4849
head -c16 /dev/urandom > src/secret.key
4950
5051
- name: Cache cargo build
51-
uses: actions/cache@v2.1.6
52+
uses: actions/cache@v4
5253
with:
5354
path: target
5455
key: docker-build-${{ hashFiles('**/Cargo.lock') }}
@@ -101,15 +102,15 @@ jobs:
101102
# npm test
102103

103104
- name: Archive production artifacts
104-
uses: actions/upload-artifact@v2
105+
uses: actions/upload-artifact@v4
105106
with:
106107
name: dist-without-markdown
107108
path: |
108109
web/dist
109110
!web/dist/**/*.md
110111
111112
# - name: Archive code coverage results
112-
# uses: actions/upload-artifact@v2
113+
# uses: actions/upload-artifact@v4
113114
# with:
114115
# name: code-coverage-report
115116
# path: output/test/code-coverage.html
@@ -128,18 +129,19 @@ jobs:
128129
cd ..
129130
130131
- name: Upload app archive for Docker job
131-
uses: actions/upload-artifact@v2.2.2
132+
uses: actions/upload-artifact@v4
132133
with:
133134
name: artifact-linux-docker
134135
path: app.tar.gz
135136

136137
cicd-docker:
137138
name: CICD Docker
138-
runs-on: ubuntu-latest
139+
#runs-on: ubuntu-latest
140+
runs-on: [self-hosted, linux]
139141
needs: cicd-linux-docker
140142
steps:
141143
- name: Download app archive
142-
uses: actions/download-artifact@v2
144+
uses: actions/download-artifact@v4
143145
with:
144146
name: artifact-linux-docker
145147

@@ -149,12 +151,14 @@ jobs:
149151
- name: Display structure of downloaded files
150152
run: ls -R
151153

152-
- name: Docker build and publish
153-
uses: docker/build-push-action@v1
154+
- name: Login to Docker Hub
155+
uses: docker/login-action@v3
154156
with:
155157
username: ${{ secrets.DOCKER_USERNAME }}
156158
password: ${{ secrets.DOCKER_PASSWORD }}
157-
repository: trydirect/stackdog
158-
add_git_labels: true
159-
tag_with_ref: true
160-
#no-cache: true
159+
160+
- name: Docker build and publish
161+
uses: docker/build-push-action@v6
162+
with:
163+
push: true
164+
tags: trydirect/stackdog:latest

.github/workflows/release.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
permissions:
9+
contents: write
10+
11+
env:
12+
CARGO_TERM_COLOR: always
13+
14+
jobs:
15+
build:
16+
name: Build ${{ matrix.target }}
17+
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
include:
21+
- target: x86_64-unknown-linux-gnu
22+
artifact: stackdog-linux-x86_64
23+
- target: aarch64-unknown-linux-gnu
24+
artifact: stackdog-linux-aarch64
25+
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v4
29+
30+
- name: Install Rust toolchain
31+
uses: dtolnay/rust-toolchain@stable
32+
with:
33+
targets: ${{ matrix.target }}
34+
35+
- name: Install cross
36+
run: cargo install cross --git https://github.com/cross-rs/cross
37+
38+
- name: Build release binary
39+
run: cross build --release --target ${{ matrix.target }}
40+
41+
- name: Package
42+
run: |
43+
mkdir -p dist
44+
cp target/${{ matrix.target }}/release/stackdog dist/stackdog
45+
cd dist
46+
tar czf ${{ matrix.artifact }}.tar.gz stackdog
47+
sha256sum ${{ matrix.artifact }}.tar.gz > ${{ matrix.artifact }}.tar.gz.sha256
48+
49+
- name: Upload artifact
50+
uses: actions/upload-artifact@v4
51+
with:
52+
name: ${{ matrix.artifact }}
53+
path: |
54+
dist/${{ matrix.artifact }}.tar.gz
55+
dist/${{ matrix.artifact }}.tar.gz.sha256
56+
57+
release:
58+
name: Create GitHub Release
59+
needs: build
60+
runs-on: ubuntu-latest
61+
steps:
62+
- name: Checkout
63+
uses: actions/checkout@v4
64+
65+
- name: Download all artifacts
66+
uses: actions/download-artifact@v4
67+
with:
68+
path: artifacts
69+
merge-multiple: true
70+
71+
- name: Create release
72+
uses: softprops/action-gh-release@v2
73+
with:
74+
generate_release_notes: true
75+
files: |
76+
artifacts/*.tar.gz
77+
artifacts/*.sha256

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ Cargo.lock
3737
=======
3838
*.db
3939
>>>>>>> testing
40+
docs/tasks/

0 commit comments

Comments
 (0)