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
105 changes: 105 additions & 0 deletions src/secfoo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,111 @@ def sensor_conformance(
raise typer.Exit(code=1)


@sensor_app.command(name="run")
def sensor_run(
listen: Annotated[
list[str] | None,
typer.Option(
"--listen",
help="Local IPC/loopback listener (unix://, tcp://127.0.0.1:7461, npipe:////./pipe/secfoo-sensor). Repeatable.",
),
] = None,
flush_interval: Annotated[float, typer.Option("--flush-interval", min=0.05)] = 5.0,
heartbeat_interval: Annotated[float, typer.Option("--heartbeat-interval", min=0.5)] = 30.0,
watchlist_interval: Annotated[float, typer.Option("--watchlist-interval", min=1.0)] = 300.0,
max_spool_mb: Annotated[int, typer.Option("--max-spool-mb", min=1, help="Max disk spool size in MiB.")] = 50,
spool_dir: Annotated[Path | None, typer.Option("--spool-dir", help="Directory for offline spool batches.")] = None,
watchlist_out: Annotated[Path | None, typer.Option("--watchlist-out", help="Where fetched SECFOO_WATCHLIST JSON is written.")] = None,
sensor_key: Annotated[str | None, typer.Option("--sensor-key", help="Optional shared auth key for tcp:// listeners.")] = None,
) -> None:
"""Run the standalone runtime sensor sidecar / host daemon (#67)."""
from secfoo.runtime.client import SensorConfigError, load_sensor_config
from secfoo.sensor.daemon import SensorDaemon, SensorListenError

try:
cfg = load_sensor_config()
daemon = SensorDaemon(
cfg,
listen=listen,
flush_interval=flush_interval,
heartbeat_interval=heartbeat_interval,
watchlist_interval=watchlist_interval,
max_spool_bytes=max_spool_mb * 1024 * 1024,
spool_dir=spool_dir,
watchlist_path=watchlist_out,
sensor_key=sensor_key,
)
addrs = daemon.start_listeners()
except (SensorConfigError, SensorListenError) as exc:
err_console.print(f"[red]Error:[/] {exc}")
raise typer.Exit(code=2) from None
console.print(f"Secfoo sensor {daemon.sensor_id} listening on {', '.join(addrs)} -> {cfg.server}")
daemon.run_forever()


@sensor_app.command(name="status")
def sensor_status(
json_output: Annotated[bool, typer.Option("--json", help="Emit JSON liveness state.")] = False,
) -> None:
"""Inspect standalone runtime sensor health for liveness probes (#67)."""
import json as json_lib

from secfoo.sensor.service import inspect_sensor_status

info = inspect_sensor_status()
if json_output:
typer.echo(json_lib.dumps(info, indent=2))
else:
state_label = "[green]running[/]" if info["running"] else "[yellow]stopped[/]"
reason = f" ({info['reason']})" if info.get("reason") else ""
console.print(f"Sensor status: {state_label}{reason}")
if info.get("pid"):
console.print(
f"PID {info['pid']} | listen={info.get('listen')} | "
f"spool_bytes={info.get('spool_bytes', 0)} | dropped={info.get('dropped_total', 0)}"
)
if not info["running"]:
raise typer.Exit(code=1)


@sensor_app.command(name="install")
def sensor_install(
platform: Annotated[str | None, typer.Option("--platform", help="systemd, launchd, or windows.")] = None,
env_file: Annotated[Path | None, typer.Option("--env-file", help="EnvironmentFile path for systemd.")] = None,
listen: Annotated[str | None, typer.Option("--listen", help="Override --listen address.")] = None,
dry_run: Annotated[bool, typer.Option("--dry-run", help="Render config/command without installing.")] = False,
) -> None:
"""Register `secfoo sensor` explicitly as a systemd unit, launchd agent, or Windows Service (#67)."""
import json as json_lib

from secfoo.sensor.service import install_service

try:
res = install_service(platform=platform, env_file=env_file, listen=listen, dry_run=dry_run)
except ValueError as exc:
err_console.print(f"[red]Error:[/] {exc}")
raise typer.Exit(code=2) from None
typer.echo(json_lib.dumps(res, indent=2))


@sensor_app.command(name="uninstall")
def sensor_uninstall(
platform: Annotated[str | None, typer.Option("--platform", help="systemd, launchd, or windows.")] = None,
dry_run: Annotated[bool, typer.Option("--dry-run", help="Render uninstall action without changing OS.")] = False,
) -> None:
"""Remove `secfoo sensor` service registration (#67)."""
import json as json_lib

from secfoo.sensor.service import uninstall_service

try:
res = uninstall_service(platform=platform, dry_run=dry_run)
except ValueError as exc:
err_console.print(f"[red]Error:[/] {exc}")
raise typer.Exit(code=2) from None
typer.echo(json_lib.dumps(res, indent=2))


_SITECUSTOMIZE = """\
# Written by `secfoo instrument` (#55): starts the secfoo runtime probe in
# every Python process of the wrapped command. Inert unless SECFOO_SENSOR=1.
Expand Down
Loading
Loading