Creating a gRPC client for Tetragon in Rust using Tonic
Post card from the SubCom engineering blog.
The code for this project can be found here https://github.com/SubconsciousCompute/tetragon-grpc-client
Tetragon is a powerful tool that offers eBPF-based Security Observability and Runtime Enforcement, providing real-time security event detection and reaction capabilities, especially in Kubernetes environments.
Tetragon has a gRPC API and we’re interested in the getevents function. This function returns a stream which will send system process events. We’ll begin by setting up Tetragon and reading this stream through the tetra cli application:
- Download the Tetragon package
- Extract the tarball and run the install.sh script
- Check its status:
- Run the tetra cli to check if you’re getting events
- You should get some JSON output
The API will be running on a Unix Domain Socket /var/run/tetragon/tetragon.sock. Our gRPC client will connect to this socket & get the events.
First we’ll need to get the proto files used for the Tetragon API. Luckily it’s mentioned in the documentation. We’ll add it as a submodule to our repository & symlink the relevant folder to a proto directory.
Then our dependencies:
and dev dependencies. You may also need protoc (a proto compiler) if it is not installed.
# sudo apt install -y protobuf-compiler
We’ll create a bulid.rs file to compile our proto files, but before we do that, make sure to install the the protoc Protocol Buffers compiler. The tonic examples readme might be of some help on setting this up. Once we have that create the build.rs at the root of your directory:
build.rs
In build.rs file, we use tonic-build to compile the proto files and generate Rust code we can include in our program. Additionally we extract the error to print it with new lines.
Then in our main.rs we can add the following:
main.rs
You’ll want to allow some clippy warnings as the generated code cannot be changed from our side. The include_proto! macro gives us access to the generated rust code inside our program. We can just import the types and create a program like so:
getevents.rsuse UnixStream;
use ;
use service_fn;
use crateFineGuidanceSensorsClient;
use crate;
pub async
async
Once you run this program, it’ll ask for a sudo access if it isn’t root and start returning events. It’ll look like this:
getevents via tetragon-grpc-client — asciinema.org/a/646726 Running the getevents function via the tetragon-grpc-client written in rustInterested in work like this, apply at our Job Board.
Corrected a missing brace in the getevents.rs listing from the original post.