How to Configure Pydantic AI Kubernetes Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai kubernetes audit log by defining API server log rules. You also bind narrow cluster roles to the service account your agent uses. Many teams start by giving an AI agent a broad admin role during testing. That quick setup lets the agent read or change every resource in the cluster. If a prompt goes wrong, the agent can run bad commands across all namespaces. How a pydantic ai kubernetes audit log works with cluster roles A Pydantic AI agent talks to your cluster throug

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up a pydantic ai kubernetes audit log by defining API server log rules. You also bind narrow cluster roles to the service account your agent uses. Many teams start by giving an AI agent a broad admin role during testing. That quick setup lets the agent read or change every resource in the cluster. If a prompt goes wrong, the agent can run bad commands across all namespaces.

How a pydantic ai kubernetes audit log works with cluster roles

A Pydantic AI agent talks to your cluster through standard API requests. When you give the agent a tool to check pods, it sends requests to the API server. The API server checks role rules first and then writes an entry to the audit log.

In Kubernetes, a Role sets permissions inside one single namespace. A ClusterRole sets permissions across the whole cluster, including nodes and all namespaces. Both role types hold rules that are purely additive, with no deny rules. A RoleBinding grants those permissions inside one namespace. A ClusterRoleBinding grants permissions across the entire cluster.

Some API calls hit subresources, such as pod logs or pod exec calls. RBAC rules write these subresources with a slash, like pods and log or pods and exec. When your Pydantic AI tool calls these endpoints, the audit log records the API call event. This gives your team a trail of which service account hit which API path.

What native roles and audit logs solve and what they miss

Native roles and audit logs handle basic cluster entry and API tracking very well. You can limit a Pydantic AI service account to one namespace with a RoleBinding. You can also see when that service account opens an exec subresource or reads pod logs.

Still, RBAC only decides who can call an API endpoint. It does not inspect the exact shell command run inside a pod exec session. Once the API server allows the subresource call, the agent can run any command inside that container. Also, the API audit log records the API request, not the full output stream from the container.

This leaves two big gaps when AI agents work with live clusters. First, an agent with exec rights can still run dangerous commands inside an allowed pod. Second, pod logs or command outputs can return sensitive data like emails, phone numbers, or card numbers. Native cluster roles and API audit logs do not rewrite those matched fields in results before the model reads them.

AI Audit Trails + Audit Log Integrity: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Scoping permissions for Pydantic AI cluster tools

Start by creating a dedicated service account just for your Pydantic AI app. Never let the agent share a service account with human users or other backend jobs. Next, create a Role in the target namespace instead of a ClusterRole.

Grant read verbs only on the exact resources the agent needs to check. Avoid granting access to subresources like pod exec unless the task truly requires shell commands. If the agent only checks app status, allow pod status and pod log subresources only.

Review your audit log rules on the API server to make sure they capture all calls from that service account. Check both read and write requests in the log output. This helps your team spot unusual tool calls from the agent right away.

Add request guardrails and response masking beside Kubernetes

Cluster roles and service accounts govern who reaches the Kubernetes API. Still, a broad role or agent can delete workloads or print sensitive values from ConfigMaps and logs. In practice, hoop.dev runs beside the API server as a sidecar for your Kubernetes API and checks every request and response alongside RBAC.

With hoop.dev Kubernetes controls, guardrails can allow listing Secret names while refusing reads of Secret data before execution. Data masking redacts matched keys in returned JSON responses and log streams in memory. Furthermore, hoop.dev holds SOC 2 Type II and is MIT licensed on GitHub.

Frequently asked questions

Can Kubernetes RBAC block specific shell commands from a Pydantic AI agent?

No, Kubernetes RBAC only checks if the service account can call the pod exec subresource. It cannot read or filter the individual shell commands run inside that session.

Should I use a Role or a ClusterRole for a Pydantic AI agent?

Use a Role with a RoleBinding whenever the agent works in one namespace. Only use a ClusterRole if the agent must check cluster-wide nodes or many namespaces.

Does the Kubernetes audit log mask sensitive data in pod outputs?

No, the native audit log only records API server events and metadata. It does not rewrite emails, phone numbers, or national IDs in results sent back to the agent.

The query runs. The emails stay hidden.