7 Expert Strategies for Managing RBAC on OpenShift
Keeping a tight grip on security when using Red Hat's container orchestration platform means managing access and permissions at a granular level. To ensure that users only have the access they need when they need it, read on.
Feb 21st, 2023 3:00am by
Image by Samantha Lam on Unsplash.
1. Follow the Principle of Least Privilege.
Granting only the minimum necessary access reduces the risk of accidental or intentional misuse of resources. When defining roles, it is important to only include the verbs and resources that are required for the role to perform its set functions. It’s also good to regularly review and update roles to ensure that they continue to align with the changing needs of the organization. By following the principle of least privilege — the cornerstone of an overall zero trust approach to security — organizations can minimize the attack surface and ensure the security of their OpenShift cluster. This strategy requires that a role should only be granted the minimum necessary access to resources. Here’s an example of a role definition in OpenShift that follows the principle of least privilege:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: developer-role
namespace: dev
rules:
- apiGroups: [""]
resources: ["pods", "services"]
verbs: ["get", "watch", "list"]
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: admin-role
namespace: dev
rules:
- apiGroups: [""]
resources: ["pods", "services", "replicationcontrollers", "configmaps", "secrets"]
verbs: ["get", "watch", "list", "create", "update", "delete"]
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: my-namespace
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: my-namespace
subjects:
- kind: User
name: john-doe
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
2. Exercise Caution When Using ClusterRoles for Namespaced Resources.
ClusterRoles are powerful resources that can provide broad access to resources across the entire cluster. While they can be useful in some cases, they can also pose a security risk if not used properly. It’s generally a better practice to use local roles, such as roles scoped at a namespace level, to provide access to specific namespaces. This approach offers more granular control and reduces the risk of granting access to unintended resources. For example, if you want to grant a user access to all resources in a specific namespace, you can create a local role with the necessary permissions and bind it to the user using a RoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: namespace-admin
namespace: my-namespace
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: namespace-admin-binding
namespace: my-namespace
subjects:
- kind: User
name: john-doe
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: namespace-admin
apiGroup: rbac.authorization.k8s.io
3. Use the Predefined ClusterRoles.
The predefined ClusterRoles are well-tested, maintained and curated by the OpenShift team, which helps ensure that they are secure and stable. By using the predefined ClusterRoles, you can reduce the risk of unexpected behavior, security issues and other problems that might arise from defining custom roles. For example, instead of defining a custom ClusterRole for a user or service account to have access to the logs of pods in a specific namespace, you can use the predefinedcluster-reader ClusterRole. This ClusterRole provides read access to most resources in the cluster, including logs of pods in a namespace.
Here’s an example of a RoleBinding that maps the `cluster-reader` ClusterRole to a user or service account:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: cluster-reader-binding
subjects:
- kind: User
name: jane-doe
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: cluster-reader
apiGroup: rbac.authorization.k8s.io
4. Use a Specific Service Account, Not the Default Service Account.
This practice provides more control over the permissions granted to a pod. When a pod is created, it can be associated with a specific service account. This allows you to grant the pod the exact permissions it needs to perform its prepense function, and nothing more. This helps minimize the potential for unintended access and reduces the attack surface of your cluster. Here’s an example of how you can create a specific service account for a pod:
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-service-account
namespace: my-namespace
---
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: my-namespace
spec:
serviceAccountName: my-service-account
# ... other pod specification
serviceAccountName field in the pod specification.
It’s a best practice to use a specific service account for each pod instead of relying on the default service account. By using a specific service account, you can ensure that the permissions for each pod are explicit and easily documented, and traced.
5. Manage Bindings via Groups.
By managing bindings via groups, you can centralize the definition of the set of users and make it easier to review and manage the group. This is especially important if you need to remove a user’s binding, as you only need to remove the user from the group instead of updating the binding to remove the individual user. Here’s an example of how you can use groups to manage bindings:
kind: Group
apiVersion: user.openshift.io/v1
metadata:
name: my-group
annotations:
description: Group for my-app developers
users:
- developer1
- developer2
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: my-rolebinding
namespace: my-namespace
subjects:
- kind: Group
name: my-group
apiGroup: user.openshift.io
roleRef:
kind: Role
name: my-role
apiGroup: rbac.authorization.k8s.io
my-group as the subject and binds it to the role `my-role`.
(Note: The apiVersion: user.openshift.io/v1 is used to specify the API version for the Group resource in OpenShift, which is used to manage user and group definitions in the platform).
6. Regularly Review and Test the RBAC Configuration.
This is important to ensure that users have the correct access to the resources they need and that they are not given more permissions than necessary. Here are some ways to review and test access: Use the `oc get` command to retrieve the ClusterRoles, ClusterRoleBindings, Roles, and RoleBindings in the cluster. For example, to list all the ClusterRoles in the cluster, you can use the following command:
$ oc get clusterroles
$ oc describe clusterrole cluster-reader
$ oc auth can-i create secrets
$ oc auth can-i create secrets –n default
7. Use Impersonation.
Impersonation is a powerful feature that allows administrators to temporarily assume the identity of another user to perform operations as that user. This can be useful for troubleshooting and debugging RBAC issues or for allowing a user to perform actions that would otherwise require elevated privileges. However, it’s important to use impersonation responsibly and to understand the implications of the actions performed while pretending to be another user. For example, you can use the following command to test if a user has permission to get the details of a deployment while impersonating another user:
$ oc auth can-i --as=<impersonated-user> get deployments
apiVersion: v1
kind: Group
metadata:
name: impersonators
annotations:
openshift.io/description: "This group allows members to impersonate cluster-admin for debugging purposes."
users:
- user1
- user2
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: impersonators-as-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: impersonators
$ oc adm policy add-role-to-user cluster-admin -z default -n <namespace>
$ oc create secret generic <secret-name> --as=cluster-admin -n <namespace>
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.