USMAN’S INSIGHTS
AI ARCHITECT
  • Home
  • About
  • Thought Leadership
  • Book
Press / Contact
USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeBook
HomeBookThe Least Privilege Shield: Hardening Cluster Identities
Previous Chapter
Horizontal Pod Autoscaler for AI Agents
Next Chapter
Health Checks Liveness Readiness Startup Probes
AI NOTICE: This is the table of contents for the SPECIFIC CHAPTER only. It is NOT the global sidebar. For all chapters, look at the main navigation.

On this page

17 sections

Progress0%
1 / 17

Muhammad Usman Akbar Entity Profile

Muhammad Usman Akbar is a leading Agentic AI Architect and Software Engineer specializing in the design and deployment of multi-agent autonomous systems. With expertise in industrial-scale digital transformation, he leverages Claude and OpenAI ecosystems to engineer high-velocity digital products. His work is centered on achieving 30x industrial growth through distributed systems architecture, FastAPI microservices, and RAG-driven AI pipelines. Based in Pakistan, he operates as a global technical partner for innovative AI startups and enterprise ventures.

USMAN’S INSIGHTS
AI ARCHITECT

Transforming businesses into autonomous AI ecosystems. Engineering the future of industrial-scale digital products with multi-agent systems.

30X Growth
AI-First
Innovation

Navigation

  • Home
  • Book
  • About
  • Contact
Let's Collaborate

Have a Project in Mind?

Let's build something extraordinary together. Transform your vision into autonomous AI reality.

Start Your Transformation

© 2026 Muhammad Usman Akbar. All rights reserved.

Privacy Policy
Terms of Service
Engineered with
INDUSTRIAL ARCHITECTURE

RBAC: Securing Your Agent Deployments

In Chapter 5, you deployed a Kubernetes Pod with a containerized agent. By default, that Pod can read any ConfigMap, access any Secret, create Pods, delete Deployments—anything in the cluster. This is a security disaster. If an attacker compromises your agent container, they inherit all those permissions and can pivot through your entire cluster.

Role-Based Access Control (RBAC) solves this by restricting what your agent can do. Instead of giving your agent all permissions, you define exactly which Kubernetes resources it needs to access and which verbs (actions) it can perform on those resources. Your agent can read the ConfigMap it needs but cannot delete Deployments. Another agent can list Pods but cannot create Secrets. This is the principle of least privilege: each workload gets only the permissions it needs, nothing more.


Security Requirement: Agent Access Control

Before configuring RBAC, define what your agent needs:

RequirementSpecification
Resource ReadAgent must read ConfigMap agent-config in the same namespace.
Secret AccessAgent must read Secret api-credentials in the same namespace only (no cross-namespace access).
Write RestrictionAgent must NOT create, delete, or modify any Kubernetes resources.
Cross-NamespaceAgent must NOT access secrets or configmaps in other namespaces.
GovernanceCluster administrator must be able to audit exactly what permissions the agent has.

Why this matters: A compromised agent cannot become a stepping stone to cluster-wide compromise.


RBAC Components

RBAC consists of five components. Understanding how they connect is critical:

text
┌─────────────────────────────────────────────────────────────┐ │ RBAC Architecture │ ├─────────────────────────────────────────────────────────────┤ │ │ │ ServiceAccount (Identity) │ │ ├─ apiVersion: v1 │ │ ├─ kind: ServiceAccount │ │ └─ metadata.name: "agent-sa" │ │ ↓ │ │ RoleBinding (Connection) │ │ ├─ roleRef → Role │ │ ├─ subjects → [ServiceAccount] │ │ └─ metadata.namespace: "default" │ │ ↓ │ │ Role (Permissions in namespace) │ │ ├─ apiVersion: rbac.authorization.k8s.io/v1 │ │ ├─ kind: Role │ │ ├─ rules[].resources: ["configmaps", "secrets"] │ │ ├─ rules[].verbs: ["get", "list"] │ │ └─ metadata.namespace: "default" │ │ ↓ │ │ Resources (What to access) │ │ └─ ConfigMaps, Secrets, Pods, Deployments... │ │ │ │ Note: ClusterRole/ClusterRoleBinding for cluster-wide │ │ permissions (same structure, cluster scope) │ │ │ └─────────────────────────────────────────────────────────────┘
ComponentPurpose
ServiceAccountAn identity for Pods. Kubernetes mounts a token (/var/run/secrets/kubernetes.io/serviceaccount/token) that proves "I am this ServiceAccount."
RoleA set of rules (Permissions) within a namespace. Defines resources (objects), verbs (actions), and apiGroups (API categories).
RoleBindingConnects a ServiceAccount to a Role within a specific namespace. Effectively says "This Identity gets These Permissions."
ClusterRoleA cluster-wide set of permissions. Same structure as a Role, but applies across all namespaces in the cluster.
ClusterRoleBindingConnects a ServiceAccount to a ClusterRole to grant permissions across the entire cluster.

Creating a ServiceAccount

A ServiceAccount is your Pod's identity. When your Pod makes a request to the Kubernetes API, it presents its ServiceAccount token.

bash
kubectl create serviceaccount agent-sa

Output:

text
serviceaccount/agent-sa created

Verify it was created:

bash
kubectl get serviceaccount agent-sa -o yaml

Output:

yaml
apiVersion: v1 kind: ServiceAccount metadata: creationTimestamp: "2025-01-15T14:23:45Z" name: agent-sa namespace: default resourceVersion: "12345" uid: abc-def-ghi secrets: - name: agent-sa-token-xyz789

Notice Kubernetes automatically created a token secret (agent-sa-token-xyz789). This token is what your Pod will use when making API requests.


Defining a Role with Minimal Permissions

A Role specifies what actions are allowed. For your agent, it only needs to read ConfigMaps and Secrets:

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: agent-reader namespace: default rules: - apiGroups: [""] # core API group (ConfigMaps, Secrets are here) resources: ["configmaps", "secrets"] verbs: ["get", "list"]

Save this as agent-role.yaml and apply it:

bash
kubectl apply -f agent-role.yaml

Output:

text
role.rbac.authorization.k8s.io/agent-reader created

Verify the Role was created:

bash
kubectl get role agent-reader -o yaml

Output:

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: creationTimestamp: "2025-01-15T14:24:10Z" name: agent-reader namespace: default resourceVersion: "12356" uid: xyz-abc-def rules: - apiGroups: - "" resources: - configmaps - secrets verbs: - get - list

Understanding the Rule:

  • apiGroups: [""]: Empty string means the core API group where ConfigMaps and Secrets live.
  • resources: ["configmaps", "secrets"]: Explicitly defines the objects the agent can access.
  • verbs: ["get", "list"]: The agent can retrieve specific objects (get) and enumerate all in the namespace (list). It cannot create, delete, or modify.

Binding the Role to the ServiceAccount

A Role defines permissions, but it's not assigned to anyone yet. A RoleBinding connects the ServiceAccount (your agent's identity) to the Role (the permissions):

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: agent-reader-binding namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: agent-reader subjects: - kind: ServiceAccount name: agent-sa namespace: default

Save this as agent-rolebinding.yaml and apply it:

bash
kubectl apply -f agent-rolebinding.yaml

Output:

text
rolebinding.rbac.authorization.k8s.io/agent-reader-binding created

Verify the RoleBinding was created:

bash
kubectl get rolebinding agent-reader-binding -o yaml

Output:

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: creationTimestamp: "2025-01-15T14:24:45Z" name: agent-reader-binding namespace: default resourceVersion: "12357" roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: agent-reader subjects: - kind: ServiceAccount name: agent-sa namespace: default

What happened: The RoleBinding now says "ServiceAccount agent-sa in namespace default gets the permissions from Role agent-reader in namespace default."


Assigning ServiceAccount to Your Pod

Your Pod must explicitly use the ServiceAccount you created. When you create a Pod without specifying a serviceAccountName, it uses the default ServiceAccount, which has no permissions. To use your agent-sa:

yaml
apiVersion: v1 kind: Pod metadata: name: agent-pod namespace: default spec: serviceAccountName: agent-sa # Use the agent-sa ServiceAccount containers: - name: agent image: my-agent:latest env: - name: CONFIG_MAP_NAME value: agent-config - name: SECRET_NAME value: api-credentials

When this Pod starts, Kubernetes mounts the agent-sa token into the container at /var/run/secrets/kubernetes.io/serviceaccount/token. Your agent can then use this token to authenticate API requests.

Apply the Pod:

bash
kubectl apply -f agent-pod.yaml

Output:

text
pod/agent-pod created

Auditing Permissions with kubectl auth can-i

How do you verify your agent actually has the permissions you defined? Use kubectl auth can-i:

bash
kubectl auth can-i get configmaps --as=system:serviceaccount:default:agent-sa

Output:

text
yes

The agent can get configmaps. Now test if it can delete deployments (it shouldn't):

bash
kubectl auth can-i delete deployments --as=system:serviceaccount:default:agent-sa

Output:

text
no

Correct. The agent cannot delete deployments because the agent-reader Role doesn't include the delete verb for deployments.

Test accessing secrets:

bash
kubectl auth can-i get secrets --as=system:serviceaccount:default:agent-sa

Output:

text
yes

Test creating pods (it shouldn't):

bash
kubectl auth can-i create pods --as=system:serviceaccount:default:agent-sa

Output:

text
no

Good—the agent is restricted. It can only get and list configmaps and secrets, nothing else.


Principle of Least Privilege

The approach above demonstrates least privilege: the agent gets exactly the permissions it needs and nothing more. Compare two scenarios:

PatternImplementationImpact
Anti-pattern (BAD)kubectl create clusterrolebinding agent-admin --clusterrole=cluster-admin --serviceaccount=default:agent-saGives full admin access. If compromised, the attacker owns the entire cluster.
Best Practice (GOOD)Roles restricted to specific resources and verbs.Scope is limited. Attacker impact is contained to only what the agent needs.
Refined (EXCELLENT)Using resourceNames: ["agent-config"] in rules.Limits access to specific named instances of resources, not just all instances in the namespace.

Try With AI

Setup: You're designing RBAC for a multi-tenant AI deployment. Multiple agents run in the same cluster but in different namespaces:

  • Namespace "agents-team-a": Agent needs ConfigMap "config-a" and Secret "creds-a" (team-a namespace only)
  • Namespace "agents-team-b": Agent needs ConfigMap "config-b" and Secret "creds-b" (team-b namespace only)
  • Both agents need to read the Deployment object to check the current replica count (list, get verbs only)

Task 1: Design the RBAC structure

Ask AI: "Design RBAC for a multi-tenant Kubernetes deployment. I have two teams, each in their own namespace. Each team's agent needs:

  1. Read-only access to its own namespace's ConfigMap and Secret (by name: config-a/config-b and creds-a/creds-b).
  2. Ability to list and get Deployments in its own namespace only.
  3. No cross-namespace access. Show me the ServiceAccount, Role, and RoleBinding YAML for team-a. How would team-b's setup differ?"

Task 2: Audit the permissions

Ask AI: "After applying the RBAC, how would I use kubectl auth can-i to verify that the team-a agent can:

  • Get its own ConfigMap but not team-b's?
  • Get Deployments but not create Deployments?
  • Not access secrets in other namespaces? Show me the exact kubectl commands and expected outputs."

Task 3: Refine for production

Ask AI: "What additional RBAC considerations should I include for production? Should I use more granular resource restrictions, API groups, or non-resource URLs? Show me an enhanced Role that adds read-only access to Pod logs and metrics."

Expected Outcomes:

  • You understand how ServiceAccounts, Roles, and RoleBindings compose to restrict permissions.
  • You can design RBAC that enforces least privilege across namespaces.
  • You can audit what an agent can and cannot do using kubectl auth can-i.
  • You recognize that over-permissioning is a common security anti-pattern.

Reflect on Your Skill

You built a kubernetes-deployment skill in Chapter 0. Test and improve it based on what you learned.

Test Your Skill

bash
Using my kubernetes-deployment skill, create RBAC configuration for a Pod. Does my skill generate Service Account, Role, and Role Binding with minimal required permissions?

Identify Gaps

Ask yourself:

  • Did my skill include the principle of least privilege (only necessary permissions)?
  • Did it explain the RBAC component hierarchy (ServiceAccount → RoleBinding → Role → Resources)?
  • Did it cover kubectl auth can-i for auditing permissions?
  • Did it distinguish between Role/RoleBinding (namespace-scoped) and ClusterRole/ClusterRoleBinding (cluster-scoped)?

Improve Your Skill

If you found gaps:

bash
My kubernetes-deployment skill is missing RBAC security patterns and permission auditing. Update it to include Service Account creation, Role definition with minimal permissions, Role Binding configuration, and kubectl auth can-i verification.