How to Configure Dify Kubernetes Sandbox
You set up a dify kubernetes sandbox by defining strict role rules in one namespace. You then bind those rules to the service account that runs your agent pods. Teams often start by deploying Dify with wide cluster permissions to make setup fast. That quick path creates big risks when an AI workflow runs untrusted code. If a role is too broad, a bad script can read secrets or change other workloads. How a dify kubernetes sandbox works with native roles A safe setup starts with how Kubernetes
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 dify kubernetes sandbox by defining strict role rules in one namespace. You then bind those rules to the service account that runs your agent pods. Teams often start by deploying Dify with wide cluster permissions to make setup fast. That quick path creates big risks when an AI workflow runs untrusted code. If a role is too broad, a bad script can read secrets or change other workloads.
How a dify kubernetes sandbox works with native roles
A safe setup starts with how Kubernetes manages permissions through its role system. In Kubernetes, a Role holds rules that define a set of allowed actions. These permissions are purely additive, which means there are no deny rules at all. You only grant the exact actions the sandbox pod needs to run code. A Role always sets permissions inside a single namespace to keep workloads apart.
In contrast, a ClusterRole is not tied to one single namespace. It can cover cluster-wide resources like nodes or apply across all namespaces at once. For a code sandbox, you should pick a standard Role inside a dedicated namespace. You then create a RoleBinding to link that Role to the Dify service account. A RoleBinding grants those permissions inside that specific namespace only. Avoid using a ClusterRoleBinding, because it grants access across the whole cluster.
You also need to control how Dify talks to pod subresources. Kubernetes RBAC names resources just like they appear in the API URL. Some actions use a subresource, such as reading pod logs or running commands inside a pod. In RBAC rules, you write the subresource with a slash, like pods and log or pods and exec. If Dify needs to run code in a running container, it uses the exec subresource. You should grant exec rights only to the sandbox role in that isolated namespace.
What native setup solves and what it leaves open
Native roles and bindings solve the first layer of cluster security. They make sure the Dify app and its sandbox pods stay in their own namespace. A sandbox pod cannot list nodes or read workloads in other namespaces. It also stops unauthorized users from calling the Kubernetes API to start new pods.
Kubernetes RBAC + AI Sandbox Environments: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Still, native RBAC only checks who can make an API call before it starts. Once a role grants access to the pod exec subresource, Kubernetes steps back. The cluster does not inspect the specific commands sent inside that exec session. It also does not check what queries the sandbox code sends to outside databases. If the sandbox connects to a database, native roles cannot rewrite sensitive fields like emails or phone numbers.
The same gap applies when engineers debug the Dify sandbox pods. If an engineer has a RoleBinding that allows pod exec, they can run any command inside the container. Native RBAC decides who logs in to the pod, not what individual commands do in flight. Teams still need a way to check live commands, approve risky actions, and mask card numbers or national IDs in results.
Govern kubectl and API calls in flight
Identity providers and Kubernetes RBAC decide who logs into a cluster. They cannot redact keys inside returned JSON objects or hold a risky delete call for review. By design, hoop.dev runs as an open-source proxy, a sidecar for your Kubernetes API, checking requests and responses without replacing RBAC.
First, hoop.dev guardrails refuse banned methods, paths, or headers before they run. Next, hoop.dev data masking rewrites sensitive keys in returned Secret, ConfigMap, and log responses in memory, and writes each decision to the audit trail. Explore the repo on GitHub.
Frequently asked questions
Should I use a Role or a ClusterRole for a Dify sandbox?
Use a Role paired with a RoleBinding in a dedicated namespace. A Role limits permissions to one namespace, while a ClusterRoleBinding grants access across the whole cluster.
Can I add a deny rule to block specific pod actions in Kubernetes RBAC?
No, Kubernetes RBAC permissions are purely additive and have no deny rules. You must start with zero permissions and only add the exact rules the sandbox needs.
How does Kubernetes control command execution inside sandbox pods?
Kubernetes uses the pods exec subresource in RBAC rules to allow command execution. Once allowed, native RBAC does not filter which specific shell commands run inside the pod.
Related guides
In this post