| name | kubernetes-rbac |
| description | Implements rbac.authorization.k8s.io/v1 Role, ClusterRole, RoleBinding, and ClusterRoleBinding manifests with least privilege access control for Kubernetes resources. |
| license | MIT |
| compatibility | opencode |
| metadata | {"version":"1.0.0","domain":"cncf","triggers":"least privilege, role binding, clusterrole, service account, rbac.authorization.k8s.io, namespace permissions, access control","archetypes":["tactical","enforcement"],"anti_triggers":["http routing","tls termination","persistent volume","pod isolation"],"response_profile":{"verbosity":"low","directive_strength":"high","abstraction_level":"operational"},"role":"implementation","scope":"implementation","output-format":"code","content-types":["code","guidance","config","do-dont"],"related-skills":"cncf/kubernetes-deployment, cncf/kyverno-pod-security-baseline, cncf/open-policy-agent-opa"} |
Kubernetes RBAC Manager
Implements rbac.authorization.k8s.io/v1 Role, ClusterRole, RoleBinding, and ClusterRoleBinding manifests to enforce least-privilege access control for Kubernetes API resources. When loaded, the model generates production-grade RBAC policies with scoped permissions, service account binding, and audit-ready role definitions.
TL;DR Checklist
When to Use
Use this skill when:
- Setting up RBAC policies for CI/CD pipeline service accounts (e.g., Argo CD, Tekton)
- Configuring read-only access for monitoring tools (Prometheus, Grafana)
- Implementing namespace isolation so teams can only manage their own namespaces
- Restricting access to sensitive resources (Secrets, ConfigMaps, Nodes, RBAC resources)
- Creating custom roles for specific application workloads with minimal permissions
When NOT to Use
Avoid this skill for:
- User authentication — that is handled by OIDC, SAML, or LDAP identity providers, not RBAC roles
- Kubernetes API audit logging — that is configured via the apiserver audit policy, not RBAC
- Admission-time policy enforcement — use
cncf/kyverno-pod-security-baseline or cncf/open-policy-agent-opa instead
- Pod security enforcement — use
cncf/kyverno-pod-security-baseline to restrict pod capabilities
Core Workflow
-
Identify the Service Account — Determine which service account needs access and what resources it must interact with. Checkpoint: Each workload should have its own dedicated service account — never use the default service account.
-
Define Namespace-Scoped Role — Create a Role with specific resources, verbs, and subresources. Checkpoint: List every resource and verb explicitly — never use wildcards (*).
-
Create RoleBinding — Bind the Role to the service account within the namespace. Checkpoint: Ensure the roleRef.apiGroup is rbac.authorization.k8s.io, roleRef.kind matches the Role kind, and roleRef.name matches the Role name.
-
Escalate to ClusterRole if Needed — For resources that exist cluster-wide (Nodes, PersistentVolumes, StorageClasses), create a ClusterRole and ClusterRoleBinding. Checkpoint: ClusterRoles should be used sparingly and only when cross-namespace access is genuinely required.
-
Apply and Verify — Apply the RBAC manifests and verify access using kubectl auth can-i. Checkpoint: Test both allowed and denied operations to confirm the policy is precise.
-
Audit Access Periodically — Review RBAC policies quarterly and remove unused permissions. Checkpoint: Run kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa> to audit existing permissions.
Implementation Patterns
Pattern 1: Namespace-Scoped Role and Binding for an Application
A production-grade RBAC setup for a deployment automation service account.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployment-manager
namespace: production
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: deployment-manager-binding
namespace: production
subjects:
- kind: ServiceAccount
name: deployment-manager
namespace: production
roleRef:
kind: Role
name: deployment-manager
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: deployment-manager
namespace: production
Pattern 2: ClusterRole and Least Privilege (BAD vs GOOD)
Overly permissive RBAC is the most common security misconfiguration in Kubernetes clusters.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: overprivileged-role
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: default-binding
namespace: production
subjects:
- kind: ServiceAccount
name: default
namespace: production
roleRef:
kind: Role
name: deployment-manager
apiGroup: rbac.authorization.k8s.io
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: dangerous-role
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list", "watch", "create", "update", "delete"]
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["roles", "rolebindings"]
verbs: ["*"]
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: ci-pipeline-runner
rules:
- apiGroups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["serviceaccounts"]
verbs: ["get", "list"]
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: ci-pipeline-runner-binding
subjects:
- kind: ServiceAccount
name: ci-pipeline
namespace: ci-tools
roleRef:
kind: ClusterRole
name: ci-pipeline-runner
apiGroup: rbac.authorization.k8s.io
Pattern 3: RBAC Access Verification and Audit
def generate_rbac_commands(role_name: str, namespace: str, sa_name: str = "") -> list[str]:
"""Generate kubectl commands to verify and audit an RBAC policy.
Useful for confirming that a RoleBinding grants the expected permissions
and for auditing existing access.
Args:
role_name: The Role or ClusterRole name to audit.
namespace: The namespace context.
sa_name: Optional service account name to check specifically.
Returns:
List of kubectl commands for verification and audit.
"""
commands = [
f"kubectl describe role {role_name} -n {namespace}",
f"kubectl describe rolebinding {role_name}-binding -n {namespace}",
f"kubectl auth can-i create deployments --as=system:serviceaccount:{namespace}:{sa_name or 'default'} -n {namespace}",
f"kubectl auth can-i delete deployments --as=system:serviceaccount:{namespace}:{sa_name or 'default'} -n {namespace}",
]
return commands
def verify_role_binding(binding: dict) -> list[str]:
"""Verify a RoleBinding or ClusterRoleBinding has correct structure.
Checks that roleRef fields are consistent and subjects are properly defined.
Args:
binding: A parsed RoleBinding or ClusterRoleBinding manifest dict.
Returns:
List of validation error messages. Empty means binding is valid.
"""
errors: list[str] = []
metadata = binding.get("metadata", {})
spec = binding.get("spec", {})
api_group = spec.get("roleRef", {}).get("apiGroup", "")
role_kind = spec.get("roleRef", {}).get("kind", "")
role_name = spec.get("roleRef", {}).get("name", "")
if api_group != "rbac.authorization.k8s.io":
errors.append(f"roleRef.apiGroup must be 'rbac.authorization.k8s.io', got '{api_group}'")
if not role_kind:
errors.append("roleRef.kind is required (Role or ClusterRole)")
if not role_name:
errors.append("roleRef.name is required")
subjects = spec.get("subjects", [])
if not subjects:
errors.append("subjects list is empty — no one is granted this role")
for subject in subjects:
if subject.get("kind") not in ("User", "Group", "ServiceAccount"):
errors.append(f"Invalid subject kind: '{subject.get('kind')}' — must be User, Group, or ServiceAccount")
if not subject.get("name"):
errors.append(f"Subject is missing 'name' field")
return errors
Constraints
MUST DO
- Always use
rbac.authorization.k8s.io/v1 API version — never rbac.authorization.k8s.io/v1beta1 (removed in Kubernetes 1.22+)
- Follow the principle of least privilege — list only the specific resources and verbs each service account needs
- Use namespace-scoped
Role + RoleBinding whenever possible — prefer scoping over cluster-wide ClusterRole + ClusterRoleBinding
- Always bind roles to a dedicated
ServiceAccount — never bind to the default service account or to system:anonymous
- Specify exact
apiGroups, resources, and verbs — never use wildcards (*)
- Always include
roleRef.apiGroup: rbac.authorization.k8s.io in every binding — omitting it causes binding failure
- Use
kubectl auth can-i to verify RBAC policies grant the intended permissions after deployment
- Document the justification for each resource/verb pair in the role's metadata annotations
MUST NOT DO
- Never grant
verbs: ["*"] or resources: ["*"] — this is equivalent to cluster-admin and violates least privilege
- Never grant access to
secrets with list and delete verbs — any pod with this can read all secrets in the namespace
- Never bind a ClusterRole to a ServiceAccount without specifying the correct namespace in the binding's subjects
- Never grant access to
rbac.authorization.k8s.io resources (roles, rolebindings, clusterroles) — this enables privilege escalation
- Never use
escalate or bind verbs in any role — these allow modifying RBAC policies
- Never create a RoleBinding without a matching Role — the binding is silently ignored
Output Template
When implementing Kubernetes RBAC policies, produce the following:
- Role or ClusterRole YAML — Complete role definition with specific resources, verbs, and apiGroups.
- RoleBinding or ClusterRoleBinding YAML — Binding that connects the role to the target service account or user.
- ServiceAccount YAML (if needed) — The dedicated service account that receives the role binding.
- Access Verification Commands —
kubectl auth can-i commands to verify the policy grants the expected permissions.
Related Skills
| Skill | Purpose |
|---|
kubernetes-deployment | Reference the RBAC service account in Deployment pod specs via serviceAccountName |
cncf/kyverno-pod-security-baseline | Enforce pod security standards alongside RBAC for defense-in-depth |
cncf/open-policy-agent-opa | Apply admission-time policy enforcement for RBAC compliance validation |
cncf/keycloak | Integrate Kubernetes with external identity providers for user authentication |
Live References
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.