Skip to content

Latest commit

 

History

History
113 lines (85 loc) · 6.26 KB

AGENTS.md

File metadata and controls

113 lines (85 loc) · 6.26 KB

AGENTS.md

Project Overview

fact (File ACTivity) is a BPF-based file integrity monitoring tool for PCI DSS compliance. It attaches to kernel LSM hooks, receives file system events via ring buffers, enriches them, and outputs via gRPC, OTLP, or JSON. Supports hot-reload via SIGHUP and exposes Prometheus metrics.

Requires: BTF symbols, LSM hooks, BPF trampolines. Tested on RHEL 9.6+/10+, RHCOS 4.16+, Fedora CoreOS 43.

Workspace Structure

Cargo workspace with three crates (default-members = ["fact"], so bare cargo build only builds the main crate):

  • fact (edition 2024): Main binary — BPF loading (aya), event processing, config, output, metrics
  • fact-api (edition 2021): gRPC API generated from protos in third_party/stackrox/proto (git submodule)
  • fact-ebpf (edition 2021): BPF C programs (src/bpf/main.c, checks.c) + Rust bindings via bindgen

Build system dependencies

libbpf-dev, protobuf-compiler, clang/libclang (BPF compilation + bindgen), make, git (version string). Proto submodule: git submodule update --init after fresh clone.

Commands

Build & check

cargo build              # builds only `fact` (default member)
cargo build --release
cargo check

Lint

make lint                # cargo clippy --all-targets --all-features -- -D warnings + tests/
cargo clippy --all-targets --all-features -- -D warnings  # Rust only

make -C tests lint runs grpc-gen, so it needs the proto submodule and ruff/pyright installed in the active venv.

Format

make format              # cargo fmt + clang-format (BPF C/H) + ruff format tests/
make format-check        # check only, no modifications

CLANG_FMT defaults to clang-format; CI uses clang-format-18. C/H files use 2-space indent (.editorconfig, .clang-format).

Test

Rust unit tests (no sudo needed):

cargo test

BPF unit tests (requires sudo, avoid in automated workflows):

cargo test --config 'target."cfg(all())".runner="sudo -E"' --features=bpf-test

Integration tests (require Docker, a built image, and proto codegen):

make image                                    # build container image first
python3 -m venv .venv                         # create venv (first time)
source .venv/bin/activate
pip install -r tests/requirements.txt
make -C tests pytest                           # grpc-gen + pytest --image=$(make image-name)
pytest --image="<image-tag>" test_file_open.py  # single file, from tests/
pytest --image="<image-tag>" --output=otlp     # test OTLP output

Integration tests use the Docker Python SDK — they launch fact in a privileged container with --network=host, bind-mount / as /host, and mount the config at /etc/stackrox/fact.yml. They communicate via gRPC/OTLP mock servers + health check endpoint. Missing fact images are pulled automatically; QA helper images use tests/containers/QA_TAG (override with FACT_QA_TAG), and --no-local-builds fails instead of building them. Python linting: ruff check . && pyright . (from tests/).

Run locally (requires sudo)

cargo run --release --config 'target."cfg(all())".runner="sudo -E"' -- -p /etc -p /var/log

--replay <jsonl> reads pre-recorded events and skips BPF loading and HostScanner (fact/src/replay.rs), useful for testing the pipeline without sudo.

Key Architecture Notes

Event flow

  1. Kernel LSM hooks → BPF programs (fact-ebpf/src/bpf/main.c) → ring buffer
  2. Bpf worker (fact/src/bpf/mod.rs) reads ring buffer → mpsc channel
  3. HostScanner (fact/src/host_scanner.rs) enriches host paths, filters events, and does periodic inode scanning
  4. output (fact/src/output/mod.rs) fans out over a broadcast channel → gRPC, OTLP, or JSON stdout
  5. gRPC output rate limiting lives in fact/src/output/grpc.rs (governor); rate_limit: 0 means unlimited

Event type definitions

fact-ebpf/src/bpf/types.h is the single source of truth for event structs and enums. build.rs runs bindgen on it to generate $OUT_DIR/bindings.rs, which lib.rs pulls in via include!. Edit only types.h — Rust bindings are generated automatically. Keep it minimal and do not include vmlinux.h there.

BPF build integration

fact-ebpf/build.rs compiles main.c and checks.c with clang targeting BPF, then runs bindgen on types.h. BPF objects are embedded in the binary. No manual clang invocation needed. fact-ebpf/src/bpf/vmlinux/{x86_64,aarch64}.h are checked-in generated kernel headers; do not hand-edit them.

Config

  • Files loaded low → high precedence: /etc/stackrox/fact.yml, /etc/stackrox/fact.yaml, ./fact.yml, ./fact.yaml; CLI args and FACT_* env vars override them.
  • YAML parsing is strict: unknown keys cause errors.
  • Hot-reload watches those files by mtime every 10s plus SIGHUP (fact/src/config/reloader/mod.rs). Only paths/endpoint/grpc/otel/scan_interval propagate; changing hotreload itself requires a restart.
  • FACT_HOST_MOUNT (default /, tests use /host) controls monitored-path and host lookups.
  • scan_interval: 0 disables periodic scanning.
  • Schema: fact/src/config/mod.rs. Config tests: fact/src/config/tests.rs and fact/src/config/reloader/tests.rs.

Feature flags

  • bpf-test: gates tests that load actual BPF programs (requires sudo)
  • otel: enables OpenTelemetry/OTLP output (fact/Cargo.toml); see docs/otel/otel.md

Gotchas

  • .cargo/config.toml sets build.rustflags = ["-C", "force-frame-pointers=yes"] for all builds and registry.global-min-publish-age = "3 days". Cargo auto-loads .cargo/config.toml in ancestor dirs and $CARGO_HOME/config.toml.
  • fact/build.rs shells out to make -sC .. version to embed the git version string — builds require make; outside a git checkout the embedded version is empty.
  • CLAUDE.md is a symlink to this file; edit only AGENTS.md.
  • Prometheus metrics use prefix stackrox_fact.
  • Don't hand-edit generated artifacts: tests/internalapi/, mock-server/internalapi/, fact-ebpf/src/bpf/vmlinux/*, and compile_commands.json.

Workflow

  • PR checklist is enforced by .github/workflows/pr-checklist.yml: changes need a CHANGELOG.md entry or an explicit note that none is needed, and CI results must be inspected.
  • Release process: docs/release.md (prepare-release.yml / release.yml).