How to Configure Mastra Kubernetes Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up mastra kubernetes least privilege by binding a narrow role to your agent identity. This role only grants the exact resource actions that the agent needs to run. Most teams start by giving their AI agents broad cluster roles during testing. That fast setup works early on, but it creates big risks in production. An agent with broad rights can read secrets or delete workloads across the whole cluster. Scoping down those rights keeps one bad prompt or bug from harming other apps. How ma

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 mastra kubernetes least privilege by binding a narrow role to your agent identity. This role only grants the exact resource actions that the agent needs to run. Most teams start by giving their AI agents broad cluster roles during testing. That fast setup works early on, but it creates big risks in production. An agent with broad rights can read secrets or delete workloads across the whole cluster. Scoping down those rights keeps one bad prompt or bug from harming other apps.

How mastra kubernetes least privilege works in practice

Kubernetes uses role-based rules to control who can call the cluster API. All rules in this system are additive, so there are no deny rules. You start with zero rights and add only what the Mastra workflow needs. A standard Role sets rules inside one single namespace. A ClusterRole sets rules across the whole cluster or for nodes.

For a Mastra agent, you should almost always pick a single namespace Role. You list the exact resources the agent must read or change, such as pods. You also pick the allowed verbs, such as get, list, or watch. Next, you create a RoleBinding inside that same namespace. The RoleBinding links your Role to the service account that Mastra uses. Even if you reference a shared ClusterRole, a RoleBinding keeps rights inside one namespace. By contrast, a ClusterRoleBinding grants those rights across every namespace in the cluster.

Handling subresources and runtime actions

Many Mastra tools need to read pod logs or run commands in containers. Kubernetes treats these targets as subresources under the main pod resource. In your role rules, you write them with a slash, like pods and log. If an agent only needs log data, grant access to the log subresource alone. Do not grant full rights to the parent pod or the exec subresource.

Wait before you grant the exec subresource to any automated agent or user. That permission lets the caller run shell commands inside a live container. Once inside, the agent can read local files or call internal network services. You can limit a role to specific resource names for most read verbs. Still, name limits on create actions only apply to top-level resources, not subresources. Keep exec rights out of standard agent roles whenever you can.

Least Privilege Principle + Kubernetes RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native roles solve and what they miss

Native cluster roles do a great job of locking down the API server. They decide which service account or user can call a specific API endpoint. They also stop a Mastra agent in one namespace from touching other namespaces. This boundary is the core foundation of cluster safety.

Still, native roles only check who logs in and which endpoint they hit. Once the API server allows an exec request, Kubernetes stops inspecting the traffic. The cluster does not check the exact shell commands that run inside the pod. It also cannot redact sensitive fields like emails or phone numbers in command output. If a Mastra agent queries a pod and prints card numbers, native roles allow it. Teams also struggle to approve single risky commands without changing role bindings by hand. Static roles alone cannot govern what happens inside an active session in flight.

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

Should I use a Role or a ClusterRole for a Mastra agent?

Use a Role with a RoleBinding so the agent stays inside one namespace. Only use a ClusterRole if the agent must inspect cluster-wide items like nodes.

How do I let Mastra read pod logs without giving shell access?

Grant get and list verbs on the pods log subresource in your Role. Leave out the pods exec subresource so the agent cannot open a shell.

Can Kubernetes RBAC block specific commands inside a pod exec session?

No, native RBAC only checks if the identity can start the exec call. It does not inspect or block individual shell commands once the session starts.

The query runs. The emails stay hidden.