Metadata-Version: 2.4
Name: tetragon-client
Version: 0.1.0
Summary: Typed Python client for Cilium Tetragon events, over gRPC or from JSON export files
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: tetragon,cilium,ebpf,security,observability,grpc
Author: Miguel Jacq
Author-email: mig@mig5.net
Requires-Python: >=3.11,<3.15
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Security
Classifier: Topic :: System :: Monitoring
Classifier: Typing :: Typed
Requires-Dist: grpcio (>=1.71.2)
Requires-Dist: protobuf (>=5.29,<8)
Project-URL: Documentation, https://git.mig5.net/mig5/tetragon-client/src/branch/main/docs
Project-URL: Repository, https://git.mig5.net/mig5/tetragon-client
Description-Content-Type: text/markdown

# tetragon-client

A typed Python client for [Cilium Tetragon](https://tetragon.io), the eBPF
security observability and runtime enforcement tool. It works with Tetragon
on Kubernetes or, as here, on a plain Docker host.

- **The whole gRPC API.** Stream events live; load, list, configure and
  delete tracing policies; check health; change log levels; dump the process
  cache; tune export rotation. Sync and asyncio clients.
- **Export files without gaps.** `ExportFileReader` reads Tetragon's JSON
  export (`--export-filename`), follows its rotation (including gzipped
  backups) and resumes from a checkpoint after a restart or crash, so your
  program misses nothing while it's down.
- **One event type.** Both sources give you the same `GetEventsResponse`
  protobuf messages (or plain dicts), so your handling code doesn't care
  where events came from.
- **Typed and packaged.** Ships type stubs (`py.typed`), is checked with
  `mypy --strict`, and is available from PyPI and as Debian, Ubuntu and
  Fedora packages.

It's a general-purpose library: anything that wants Tetragon events (a SIEM
forwarder, an alerting script, a compliance tool's ingester) can use it.

## Install

From PyPI with `pip` or `pipx`:

```bash
pip install tetragon-client
```

On Debian 12/13 and Ubuntu 24.04 / 26.04, from the mig5 apt repository:

```bash
sudo mkdir -p /usr/share/keyrings
curl -fsSL https://mig5.net/static/mig5.asc | sudo gpg --dearmor -o /usr/share/keyrings/mig5.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/mig5.gpg] https://apt.mig5.net $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mig5.list
sudo apt update
sudo apt install python3-tetragon-client
```

On Fedora 43 / 44, from the mig5 rpm repository:

```bash
sudo rpm --import https://mig5.net/static/mig5.asc
sudo tee /etc/yum.repos.d/mig5.repo > /dev/null << 'EOF'
[mig5]
name=mig5 Repository
baseurl=https://rpm.mig5.net/$releasever/rpm/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mig5.net/static/mig5.asc
EOF
sudo dnf install python3-tetragon-client
```

The release key's fingerprint is `54A9 1143 AE0A B4F7 743B  01FE 888E D1B4 23A3 BC99`.
PyPI releases come with detached signatures and a signed `SHA256SUMS`.

## Quick start

### Live events over gRPC

```python
from tetragon_client import TetragonClient, event_filter, event_kind

with TetragonClient() as client:  # unix:///var/run/tetragon/tetragon.sock
    print("Tetragon", client.version())
    for event in client.events(allow=[event_filter(kinds=["process_exec"])]):
        proc = event.process_exec.process
        print(proc.binary, proc.arguments)
```

The stream reconnects by itself when Tetragon restarts. Events emitted
while it's disconnected are **not** replayed; read the export file when
every event matters.

### Every event, from the export file

```python
from tetragon_client import ExportFileReader, FileCheckpoint

reader = ExportFileReader(
    "/var/log/tetragon/tetragon.log",
    FileCheckpoint("/var/lib/my-ingester/tetragon.pos"),
)
with reader:
    for event in reader:  # follows rotation; resumes where it stopped
        handle(event)
```

Delivery is at-least-once: after a crash, the last few events may be
delivered again. (Under extreme load Tetragon can drop events before they
reach the file at all; see [export files](docs/export-files.md#delivery-guarantees).)

To store each event exactly once, keep the position in your own database
and commit it with the events ([examples/sqlite_checkpoint.py](examples/sqlite_checkpoint.py)).

### Managing policies

```python
with TetragonClient() as client:
    for policy in client.list_tracing_policies():
        print(policy.name, policy.state, policy.mode)
    client.configure_tracing_policy("file-monitoring-filtered", mode="monitor")
```

### From the shell

```bash
tetragon-events grpc --kind process_exec
tetragon-events file /var/log/tetragon/tetragon.log --checkpoint ~/.tetragon.pos
tetragon-events version
```

Each event is printed as one line of JSON, in the same shape as
`tetra getevents -o json`.

## Running Tetragon for this library

With Tetragon in Docker, the client needs the gRPC socket and, for
`ExportFileReader`, the export directory mounted on the host:

```bash
docker run --name tetragon --rm --pull always \
  --pid=host --cgroupns=host --privileged \
  -v /sys/kernel/btf/vmlinux:/var/lib/tetragon/btf \
  -v /etc/tetragon/tetragon.tp.d:/etc/tetragon/tetragon.tp.d:ro \
  -v /var/run/tetragon:/var/run/tetragon \
  -v /var/log/tetragon:/var/log/tetragon \
  quay.io/cilium/tetragon:v1.8.0 \
  --export-filename /var/log/tetragon/tetragon.log \
  --export-file-max-size-mb 100 --export-file-max-backups 20 --export-file-compress
```

See [docs/deployment.md](docs/deployment.md) for permissions, retention and security.

## Documentation

- [Deployment](docs/deployment.md): Tetragon flags, socket and file access, retention, security.
- [gRPC client](docs/grpc.md): events and filters, reconnection, every API method, asyncio, TLS.
- [Export files](docs/export-files.md): rotation, checkpoints, delivery guarantees, custom stores.
- [Writing an ingester](docs/ingesters.md): choosing a source, error handling, deduplication.
- [Command line](docs/cli.md): `tetragon-events`.
- [Protobuf compatibility](docs/protobuf-compatibility.md): supported versions, distro packages, updating Tetragon's API.
- [Examples](examples/): streaming, enforcement alerts, checkpointed ingestion, exactly-once storage, policy management.
- [Contributing](CONTRIBUTING.md) and [releasing](RELEASING.md).

## Compatibility

| | |
|---|---|
| Python | 3.11 to 3.14 |
| Tetragon API | generated from v1.8.0 ([TETRAGON_VERSION](TETRAGON_VERSION)); newer servers work, and unknown fields and event kinds are tolerated |
| PyPI wheel | grpcio ≥ 1.71.2, protobuf ≥ 5.29 and < 8 |
| Debian / Ubuntu / Fedora packages | the distribution's own python3-grpcio and python3-protobuf |

## Licence

Apache-2.0. The vendored Tetragon API definitions in `proto/` and the code
generated from them are Apache-2.0, © Authors of Hubble/Tetragon.

