Kahibaro
Discord Login Register

13.2 Role-Based Access Control

Core RBAC Concepts in OpenShift

OpenShift’s Role-Based Access Control (RBAC) builds on Kubernetes RBAC but integrates tightly with OpenShift-specific objects (projects, routes, image streams, builds, etc.) and its authentication model.

At a high level, RBAC in OpenShift answers:

RBAC in OpenShift is primarily expressed through four core resource types:

OpenShift ships with many predefined roles and bindings to support its platform features (builders, deployers, routers, web console, etc.), in addition to the standard Kubernetes roles.

Roles and ClusterRoles

A Role defines permissions within a single namespace (OpenShift “Project”).

Basic structure:

yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pod-reader
  namespace: myproject
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

Key points:

A ClusterRole is similar but not bound to a namespace:

yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: view-pods-everywhere
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

ClusterRoles are used for:

Common patterns:

RoleBindings and ClusterRoleBindings

Roles and ClusterRoles define what is allowed. Bindings connect those definitions to who is allowed.

A RoleBinding gives permissions in one specific namespace:

yaml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: read-pods
  namespace: myproject
subjects:
- kind: User
  name: alice
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

Key details:

Example binding a ClusterRole in a project:

yaml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: devs-can-edit
  namespace: team-a
subjects:
- kind: Group
  name: devs-team-a
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: edit
  apiGroup: rbac.authorization.k8s.io

A ClusterRoleBinding is cluster-scoped:

yaml
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: platform-admins
subjects:
- kind: Group
  name: platform-admins
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

ClusterRoleBindings should be used sparingly because they grant permissions across all projects.

Subject Types in OpenShift RBAC

OpenShift RBAC subjects include standard Kubernetes types plus specifics important in OpenShift:

Service accounts are critical for controlling what applications can do in the cluster.

Example binding a service account to a role:

yaml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: ci-job-can-deploy
  namespace: app-prod
subjects:
- kind: ServiceAccount
  name: deployer
  namespace: app-prod
roleRef:
  kind: ClusterRole
  name: system:deployer
  apiGroup: rbac.authorization.k8s.io

Built-in Roles in OpenShift

OpenShift ships with a rich set of predefined ClusterRoles. Some of the most commonly used high-level roles:

These can be used directly with oc adm policy helpers or via standard RBAC objects.

Example: Grant a user project admin rights:

bash
oc adm policy add-role-to-user admin alice -n myproject

Example: Allow a group to view a project:

bash
oc adm policy add-role-to-group view auditors -n finance-app

OpenShift also defines system roles such as:

Generally, you don’t assign these directly to humans; they are used for platform and automation identities.

RBAC and Projects (Namespaces)

In OpenShift, a Project is a Kubernetes namespace with additional metadata and access control conventions.

Project-level access is typically managed by:

Example: create a new project and add collaborators:

bash
# As a cluster-admin:
oc new-project team-a
# Make alice the project admin:
oc adm policy add-role-to-user admin alice -n team-a
# Give devs group edit access:
oc adm policy add-role-to-group edit devs-team-a -n team-a

Behind the scenes, these commands create or update RoleBinding objects in that project.

Key behavior:

Service Accounts, Pods, and RBAC

Each pod in OpenShift runs as a service account, which determines what the pod can do via RBAC.

Basics:

Example: Give an application permission to manage ConfigMaps in its own project:

bash
oc create serviceaccount app-config-manager -n myproject

Create a Role:

yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: config-manager
  namespace: myproject
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["get", "list", "watch", "create", "update", "patch"]

Bind the role:

bash
oc create rolebinding app-config-manager-binding \
  --role=config-manager \
  --serviceaccount=myproject:app-config-manager \
  -n myproject

In the pod spec:

yaml
spec:
  serviceAccountName: app-config-manager

This pattern enforces least privilege for each workload.

Managing RBAC with the `oc` CLI

OpenShift provides conveniences around RBAC management beyond direct YAML editing.

Common inspection commands:

bash
# View roles in a namespace
oc get roles -n myproject
# View cluster roles
oc get clusterroles
# Describe a role/clusterrole
oc describe role pod-reader -n myproject
oc describe clusterrole edit
# See who has which roles in a project
oc get rolebindings -n myproject
oc describe rolebinding devs-can-edit -n myproject

Common modification commands:

bash
# Add/remove roles to/from users or groups
oc adm policy add-role-to-user edit alice -n myproject
oc adm policy remove-role-from-user edit alice -n myproject
oc adm policy add-role-to-group view auditors -n myproject
oc adm policy remove-role-from-group view auditors -n myproject
# Add a cluster role to a user or group cluster-wide
oc adm policy add-cluster-role-to-user cluster-admin ops-user
oc adm policy add-cluster-role-to-group cluster-reader auditors

These oc adm policy commands create or modify the underlying RoleBinding/ClusterRoleBinding objects.

Common RBAC Design Patterns in OpenShift

Per-Project Role Assignment

Typical structure:

Example policy for a team:

Centralized Platform Admins

A small group of platform admins receives:

Custom Fine-Grained Roles

When the built-in roles are too broad, you can:

  1. Define a ClusterRole with exactly the needed rules.
  2. Bind it at cluster or project level.

Example: a role that only allows managing routes:

yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: manage-routes
rules:
- apiGroups: ["route.openshift.io"]
  resources: ["routes"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

Bind it in a project:

bash
oc create rolebinding routes-admins \
  --clusterrole=manage-routes \
  --group=web-ops \
  -n frontend-app

RBAC Best Practices in OpenShift

bash
    oc get clusterrolebindings
    oc get rolebindings -A

By designing RBAC carefully in OpenShift, you can ensure that both humans and workloads have only the access they need, aligned with the broader security policies of your organization.

Views: 61

Comments

Please login to add a comment.

Don't have an account? Register now!