Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions docs/RUNTIME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Runtime Sensor Deployment Guide

The Secfoo runtime sensor (`secfoo sensor`) and language probes measure which open-source packages and watched vulnerable functions execute against live traffic—without modifying application code or changing CI/container builds.

---

## 1. Opt-In by Default (Rule 0)

Every probe and deployment artifact is strictly opt-in:
- Installing `secfoo` or copying probe packages never activates instrumentation.
- A probe stays **completely inert** unless `SECFOO_SENSOR=1` is explicitly set (zero hooks, threads, or network connections).
- Cluster-wide manifests and init containers act only when explicitly applied (e.g. `secfoo.com/sensor: "enabled"`).
- Setting `SECFOO_SENSOR=0` (or unsetting it) or removing the sidecar completely disables collection.

---

## 2. Deployment Modes × Runtimes Matrix

| Deployment Mode | Supported Platforms | Probe Transport (`#73`) | Watchlist Source | Best For |
|---|---|---|---|---|
| **Sidecar container** | Kubernetes pods, AWS ECS/Fargate, Google Cloud Run, Azure Container Apps, Nomad, Docker Compose | `unix:///var/run/secfoo/sensor.sock` (shared volume) or `tcp://127.0.0.1:7461` (shared network ns) | Shared volume updated by `secfoo sensor` | Containerized microservices |
| **Host daemon / service** | Linux (`systemd`), macOS (`launchd`), Windows (`sc.exe` / Windows Service) | `unix:///var/run/secfoo/sensor.sock` or `npipe:////./pipe/secfoo-sensor` | Host file written by `secfoo sensor` | VMs, bare metal, multi-worker hosts (`gunicorn`, `puma`, `php-fpm`) |
| **Direct (no sidecar)** | AWS Lambda (direct or Extension layer), Cloud Functions, single-process PaaS | `https://<your-secfoo-server>` (`DirectTransport`, 200 ms cap) or Lambda Extension (`tcp://127.0.0.1:7461`) | Bundled `SECFOO_WATCHLIST` or Extension | Serverless & PaaS without multi-container sidecars |
| **Offline / CI file** | CI test runs, air-gapped environments | `file:///path/events.ndjson` or `SECFOO_EVENTS_OUT` (`{pid}` expanded) | Local `secfoo runtime watchlist` JSON | Test runs ingested later with `secfoo runtime ingest` |

### Supported Language Probes & Zero-Code Hooks

| Runtime | Environment Injection | Implemented / Status |
|---|---|---|
| **Python** | `PYTHONPATH=/opt/secfoo/probe/python` (`sitecustomize.py`) or `secfoo instrument` | Implemented (`secfoo.sensor`, Python 3.10–3.13) |
| **Node.js** | `NODE_OPTIONS="--require /opt/secfoo/probe/node/index.js"` (CJS) or `--import /opt/secfoo/probe/node/register.mjs` (ESM) | Implemented (`npm/secfoo-sensor`, Node 18+ / ESM 20.6+) |
| **JVM (Java/Kotlin/Scala)** | `JAVA_TOOL_OPTIONS="-javaagent:/opt/secfoo/probe/jvm/secfoo-agent.jar"` | Planned (`#74`) |
| **.NET** | `DOTNET_STARTUP_HOOKS` / `CORECLR_ENABLE_PROFILING=1` | Planned (`#75`) |
| **Ruby** | `RUBYOPT="-r/opt/secfoo/probe/ruby/secfoo_probe"` | Planned (`#77`) |
| **PHP** | `auto_prepend_file` / PHP 8 observer extension | Planned (`#78`) |
| **Go / Rust / Native** | Out-of-process build-metadata inventory + Linux eBPF uprobes (`#76`) | Planned (`#76`) |

---

## 3. Generate Ready-to-Review Manifests

```bash
secfoo sensor manifest \
--platform kubernetes \
--project https://github.com/acme/payments-api \
--app payments-api \
--env prod
```

Supported values for `--platform`: `kubernetes`, `ecs`, `cloud-run`, `compose`, `systemd`. Checked-in reference examples live under `examples/runtime/` (`kubernetes/`, `ecs/`, `cloud-run/`, `compose/`, `systemd/`, `windows/`, `lambda/`), and non-root container definitions live in `packaging/docker/Dockerfile.sensor` and `packaging/docker/Dockerfile.probe`.

---

## 4. Environment Variables

| Variable | Target | Purpose |
|---|---|---|
| `SECFOO_SENSOR=1` | App | Master switch. Without `1`, probes do nothing at all. |
| `SECFOO_SENSOR_ADDR` | App | `#73` destination: `unix://...`, `tcp://127.0.0.1:<port>`, `npipe:////./pipe/...`, `https://...`, or `file://...`. |
| `SECFOO_EVENTS_OUT` | App | Fallback NDJSON output file when `SECFOO_SENSOR_ADDR` is unset (`{pid}` expands per process). |
| `SECFOO_WATCHLIST` | App / Sensor | Path to watchlist JSON read by probes and kept updated via `ETag` by `secfoo sensor`. |
| `SECFOO_FLUSH_INTERVAL` | App | Seconds between probe flushes before `welcome` overrides it (default `10`). |
| `SECFOO_SENSOR_KEY` | App & Sensor | Optional shared auth token required on `tcp://` listeners. |
| `SECFOO_API_KEY_FILE` / `SECFOO_API_KEY` | Sensor / Direct | Runtime ingest key (`sfc_...`) minted via `secfoo keys create`. Prefer `SECFOO_API_KEY_FILE`. |
| `SECFOO_SERVER` | Sensor / Direct | URL of `secfoo serve` (`https://...` or loopback `http://`). |
| `SECFOO_PROJECT`, `SECFOO_APP`, `SECFOO_ENV` | Sensor / Direct | Project identifier (`secfoo run` target) plus logical app & environment tags. |

---

## 5. Measured Overhead

- **Python (`secfoo.sensor`):** Verified via `secfoo sensor conformance` under `50 µs` marginal cost per watched call (`sys.monitoring` on Python 3.12+ touches only watched code objects; < 5 s startup/teardown budget).
- **Node.js (`@rakfortltd/secfoo-sensor`):** Measured **`1.88 µs`** marginal overhead per watched call (`node v24.16.0`, `200,000` vs `1,000,000` iterations); both CJS and ESM pass the 6-check `#73` conformance kit well under the 1 ms/request p99 budget.

---

## 6. Privacy Contract (What Is and Is Not Collected)

**Collected:**
- Runtime/language metadata (`session`), loaded package name & version (`load`), and hit counts + trimmed `file:line` app stack frames (`hit`).
- Normalized route templates (e.g. `POST /orders`, `GET /items/{id}`) and `authenticated` / `anonymous` / `unknown` auth state when `#69` middleware is enabled.

**Never collected:**
- Function arguments, return values, local variables, object fields, SQL payloads, or request/response bodies.
- Query strings, cookies, `Authorization` headers, concrete IDs in URL paths (`/users/8675309`), or user identifiers.

---

## 7. Disable & Uninstall

- **Containers (Kubernetes / ECS / Cloud Run / Compose):** Remove `SECFOO_SENSOR=1` or delete the `secfoo-sensor` sidecar / `secfoo-probe-init` init container.
- **Linux systemd:** `sudo systemctl disable --now secfoo-sensor && secfoo sensor uninstall --platform systemd`
- **macOS launchd:** `launchctl unload -w ~/Library/LaunchAgents/com.secfoo.sensor.plist && secfoo sensor uninstall --platform launchd`
- **Windows Service:** Run `examples/runtime/windows/uninstall-service.ps1` or `secfoo sensor uninstall --platform windows`.
- **Lambda:** Remove the `SECFOO_SENSOR` environment variable and detach the Extension layer.
34 changes: 34 additions & 0 deletions examples/runtime/cloud-run/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: payments-api
spec:
template:
spec:
containers:
- name: app
image: us-docker.pkg.dev/project/repo/payments-api:latest
ports:
- containerPort: 8080
env:
- name: SECFOO_SENSOR
value: "1"
- name: SECFOO_SENSOR_ADDR
value: "tcp://127.0.0.1:7461"
- name: secfoo-sensor
image: ghcr.io/secfoo-com/secfoo-sensor:latest
args: ["run", "--listen", "tcp://127.0.0.1:7461"]
env:
- name: SECFOO_SERVER
value: "https://app.secfoo.com"
- name: SECFOO_PROJECT
value: "https://github.com/acme/payments-api"
- name: SECFOO_APP
value: "payments-api"
- name: SECFOO_ENV
value: "prod"
- name: SECFOO_API_KEY
valueFrom:
secretKeyRef:
name: secfoo-api-key
key: latest
54 changes: 54 additions & 0 deletions examples/runtime/compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
services:
secfoo-serve:
image: ghcr.io/secfoo-com/secfoo:latest
command: ["serve", "--host", "0.0.0.0", "--port", "8787", "--allow-insecure-runtime"]
ports:
- "127.0.0.1:8787:8787"
volumes:
- secfoo_data:/root/.secfoo

secfoo-sensor:
build:
context: ../../..
dockerfile: packaging/docker/Dockerfile.sensor
read_only: true
tmpfs:
- /tmp
depends_on:
- secfoo-serve
volumes:
- secfoo_ipc:/var/run/secfoo
- secfoo_spool:/var/spool/secfoo
environment:
SECFOO_SERVER: http://127.0.0.1:8787
SECFOO_PROJECT: https://github.com/acme/payments-api
SECFOO_APP: payments-api
SECFOO_ENV: local
SECFOO_API_KEY: ${SECFOO_RUNTIME_KEY:-replace-with-runtime-key}
command:
- run
- --listen
- unix:///var/run/secfoo/sensor.sock
- --watchlist-out
- /var/run/secfoo/watchlist.json
- --spool-dir
- /var/spool/secfoo

sample-app:
image: python:3.12-slim
depends_on:
- secfoo-sensor
volumes:
- secfoo_ipc:/var/run/secfoo
- ../../../src:/opt/secfoo-src:ro
environment:
PYTHONPATH: /opt/secfoo-src
SECFOO_SENSOR: "1"
SECFOO_SENSOR_ADDR: unix:///var/run/secfoo/sensor.sock
SECFOO_WATCHLIST: /var/run/secfoo/watchlist.json
command: ["python", "-m", "secfoo.sensor", "-m", "secfoo.runtime.conformance.sample_app"]

volumes:
secfoo_data:
secfoo_ipc:
secfoo_spool:
39 changes: 39 additions & 0 deletions examples/runtime/ecs/task-definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"family": "payments-api-secfoo-sensor",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"containerDefinitions": [
{
"name": "app",
"image": "ghcr.io/org/payments-api:latest",
"essential": true,
"portMappings": [{"containerPort": 8000, "protocol": "tcp"}],
"environment": [
{"name": "SECFOO_SENSOR", "value": "1"},
{"name": "SECFOO_SENSOR_ADDR", "value": "tcp://127.0.0.1:7461"}
],
"dependsOn": [{"containerName": "secfoo-sensor", "condition": "START"}]
},
{
"name": "secfoo-sensor",
"image": "ghcr.io/secfoo-com/secfoo-sensor:latest",
"essential": true,
"readonlyRootFilesystem": true,
"command": ["run", "--listen", "tcp://127.0.0.1:7461"],
"environment": [
{"name": "SECFOO_SERVER", "value": "https://app.secfoo.com"},
{"name": "SECFOO_PROJECT", "value": "https://github.com/acme/payments-api"},
{"name": "SECFOO_APP", "value": "payments-api"},
{"name": "SECFOO_ENV", "value": "prod"}
],
"secrets": [
{
"name": "SECFOO_API_KEY",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:secfoo/api-key"
}
]
}
]
}
96 changes: 96 additions & 0 deletions examples/runtime/kubernetes/deployment-sidecar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: payments-api
labels:
app: payments-api
secfoo.com/sensor: "enabled"
spec:
replicas: 1
selector:
matchLabels:
app: payments-api
template:
metadata:
labels:
app: payments-api
secfoo.com/sensor: "enabled"
spec:
securityContext:
runAsNonRoot: true
volumes:
- name: secfoo-ipc
emptyDir: {}
- name: secfoo-probes
emptyDir: {}
- name: secfoo-spool
emptyDir: {}
- name: secfoo-api-key
secret:
secretName: secfoo-runtime-secret
initContainers:
- name: secfoo-probe-init
image: ghcr.io/secfoo-com/secfoo-probe:latest
command: ["sh", "-c", "cp -r /opt/secfoo/probe/. /secfoo-probes/"]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true
volumeMounts:
- name: secfoo-probes
mountPath: /secfoo-probes
containers:
- name: app
image: ghcr.io/org/payments-api:latest
ports:
- containerPort: 8000
env:
- name: SECFOO_SENSOR
value: "1"
- name: SECFOO_SENSOR_ADDR
value: "unix:///var/run/secfoo/sensor.sock"
- name: SECFOO_WATCHLIST
value: "/var/run/secfoo/watchlist.json"
- name: PYTHONPATH
value: "/secfoo-probes/python"
- name: NODE_OPTIONS
value: "--require /secfoo-probes/node/index.js"
volumeMounts:
- name: secfoo-ipc
mountPath: /var/run/secfoo
- name: secfoo-probes
mountPath: /secfoo-probes
readOnly: true
- name: secfoo-sensor
image: ghcr.io/secfoo-com/secfoo-sensor:latest
args:
- run
- --listen
- unix:///var/run/secfoo/sensor.sock
- --watchlist-out
- /var/run/secfoo/watchlist.json
- --spool-dir
- /var/spool/secfoo
env:
- name: SECFOO_API_KEY_FILE
value: "/var/run/secrets/secfoo/api-key"
- name: SECFOO_SERVER
value: "https://app.secfoo.com"
- name: SECFOO_PROJECT
value: "https://github.com/acme/payments-api"
- name: SECFOO_APP
value: "payments-api"
- name: SECFOO_ENV
value: "prod"
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true
volumeMounts:
- name: secfoo-ipc
mountPath: /var/run/secfoo
- name: secfoo-spool
mountPath: /var/spool/secfoo
- name: secfoo-api-key
mountPath: /var/run/secrets/secfoo
readOnly: true
4 changes: 4 additions & 0 deletions examples/runtime/kubernetes/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment-sidecar.yaml
25 changes: 25 additions & 0 deletions examples/runtime/lambda/direct-handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""AWS Lambda / Serverless direct-mode example (#71).

Configure function environment variables directly (no sidecar required):
SECFOO_SENSOR=1
SECFOO_SENSOR_ADDR=https://app.secfoo.com
SECFOO_API_KEY=... # or SECFOO_API_KEY_FILE
SECFOO_PROJECT=https://github.com/acme/payments-api
SECFOO_APP=payments-lambda
SECFOO_ENV=prod
"""

from __future__ import annotations

import json

from secfoo.sensor import start_from_env


def handler(event: dict, context: object) -> dict:
probe = start_from_env()
try:
return {"statusCode": 200, "body": json.dumps({"ok": True, "probed": probe is not None})}
finally:
if probe is not None:
probe.flush()
9 changes: 9 additions & 0 deletions examples/runtime/lambda/extension.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env sh
# Opt-in AWS Lambda Extension launcher (#71).
# Runs `secfoo sensor` in background next to the function runtime using tcp://127.0.0.1:7461
# so batches flush out-of-band without slowing cold starts or request handlers.
set -eu
if [ "${SECFOO_SENSOR:-0}" != "1" ]; then
exec sleep infinity
fi
exec secfoo sensor run --listen "${SECFOO_SENSOR_ADDR:-tcp://127.0.0.1:7461}" --flush-interval "${SECFOO_FLUSH_INTERVAL:-5.0}"
6 changes: 6 additions & 0 deletions examples/runtime/systemd/secfoo-sensor.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copy to /etc/secfoo/sensor.env with mode 0600
SECFOO_SERVER=https://app.secfoo.com
SECFOO_PROJECT=https://github.com/acme/payments-api
SECFOO_APP=payments-api
SECFOO_ENV=prod
SECFOO_API_KEY_FILE=/etc/secfoo/runtime.key
18 changes: 18 additions & 0 deletions examples/runtime/systemd/secfoo-sensor.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Unit]
Description=Secfoo Runtime Sensor
After=network-online.target

[Service]
Type=simple
User=secfoo
Group=secfoo
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/var/run/secfoo /var/spool/secfoo
EnvironmentFile=/etc/secfoo/sensor.env
ExecStart=/usr/local/bin/secfoo sensor run --listen unix:///var/run/secfoo/sensor.sock --watchlist-out /var/run/secfoo/watchlist.json --spool-dir /var/spool/secfoo
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
Loading
Loading