Run an application that requires access to the Kubernetes API of the application's cluster.
With the Kubernetes APIs, a user or an application can query and modify the cluster state. To protect your cluster from malicious interactions, you must grant access to the different Kubernetes APIs.
Role-based access control (RBAC) authorization is preconfigured in OpenShift. An application requires explicit RBAC authorization to access restricted Kubernetes APIs.
A service account is a Kubernetes object within a project. The service account represents the identity of an application that runs in a pod.
To grant an application access to a Kubernetes API, take these actions:
Create an application service account.
Grant the service account access to the Kubernetes API.
Assign the service account to the application pods.
If the pod definition does not specify a service account, then the pod uses the default service account.
OpenShift grants no rights to the default service account, which is expected for business workloads.
It is not recommended to grant additional permissions to the default service account, because it grants those additional permissions to all pods in the project, which might not be intended.
Regular business applications can successfully use the default service account, without requiring access to the Kubernetes APIs.
On the contrary, infrastructure applications need access to monitor or to modify the cluster resources.
These infrastructure applications might be classified into the following use cases:
Applications in this category need read access to watch cluster resources or to verify cluster health. For example, a service such as Red Hat Advanced Cluster Security (ACS) needs read access to scan your cluster containers for vulnerabilities.
Controllers are applications that constantly watch and try to reach the intended state of a resource.
For example, GitOps tools, such as ArgoCD, have controllers that watch cluster resources that are stored in a repository, and update the cluster to react to changes in that repository.
Operators automate creating, configuring, and managing instances of Kubernetes-native applications. Therefore, operators need permissions for configuration and maintenance tasks.
For example, a database operator might create a deployment when it detects a CR that defines a new database.
To provide the application with the needed permissions only, you can create roles or cluster roles that describe the application requirements. Roles grant permissions to Kubernetes API resources within a single namespace. Cluster roles grant permissions, either within one or more namespaces, or to all the cluster.
For example, you can create a cluster role for an application to read secrets.
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: secret-reader rules: - apiGroups: [""]resources: ["secrets"]
verbs: ["get", "watch", "list"]
The API groups, where an empty string represents the core API | |
The resources that the role refers to | |
The verbs or actions that the role allows the application to perform on the resource |
You can also use the default cluster roles that OpenShift defines, which have wider permissions.
For example, you can use the edit cluster role to get read access on secrets, as in the previous secret-reader cluster role.
The edit cluster role is less restrictive, and allows the application to create or update most objects.
For an application to use the role permissions, you must bind the role or cluster role to the application service account.
To bind a role or cluster role to a service account in a namespace, you can use the oc adm policy command with the add-role-to-user subcommand.
This command assigns a cluster role to a service account that exists in the current project:
[user@host ~]$ oc adm policy add-role-to-user cluster-role -z service-accountYou can optionally use -z to avoid specifying the system:serviceaccount: prefix when you assign the role to a service account that exists in the current project.project
To create a cluster role binding, you can use the oc adm policy command with the add-cluster-role-to-user subcommand.
The following command assigns a cluster role to a service account with a cluster scope:
[user@host ~]$ oc adm policy add-cluster-role-to-user cluster-role service-accountOpenShift uses RBAC authorization by using the roles that are associated to the service account to grant or deny access to the resource.
You specify the service account name in the spec.serviceAccountName pod definition field.
Applications must use the service account token internally when accessing a Kubernetes API. In earlier OpenShift versions than 4.11, OpenShift generated a secret with a token when creating a service account. Starting from OpenShift 4.11, tokens are no longer generated automatically. You must use the TokenRequest API to generate the service account token. You must mount the token as a pod volume for the application to access it.
An application might require access to a resource in the same namespace, or in a different namespace, or in all namespaces.
To grant an application access to resources in the same namespace, you need a role or a cluster role and a service account in that namespace. You then create a role binding that associates to the service account the actions that the role grants on the resource. Using a role binding with a cluster role grants access only to the resource within the namespace.
To give an application access to a resource in a different namespace, you must create the role binding in the project with the resource. The subject for the binding references the application service account that is in a different namespace from the binding.
You can use the following syntax to refer to service accounts from other projects:
system:serviceaccount:project:service-account
For example, if you have an application pod in the project-1 project that requires access to project-2 secrets, then you must take these actions:
Create an app-sa service account in the project-1 project.
Assign the app-sa service account to your application pod.
Create a role binding on the project-2 project that references the app-sa service account and the secret-reader role or cluster role.
In this way, you restrict an application's access to a Kubernetes API to specified namespaces.
Grant your application service account the cluster role by using a cluster role binding. The cluster role binding grants the application cluster access to the API.
For more information, refer to the Using RBAC to Define and Apply Permissions chapter in the Red Hat OpenShift Container Platform 4.14 Authentication and Authorization documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/authentication_and_authorization/index#using-rbac
For more information, refer to the Understanding and Creating Service Accounts chapter in the Red Hat OpenShift Container Platform 4.14 Authentication and Authorization documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/authentication_and_authorization/index#understanding-and-creating-service-accounts
For more information, refer to the Using Service Accounts in Applications chapter in the Red Hat OpenShift Container Platform 4.14 Authentication and Authorization documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/authentication_and_authorization/index#using-service-accounts
For more information, refer to the About Automatically-generated Service Account Token Secrets section in the Red Hat OpenShift Container Platform 4.14 Authentication and Authorization documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/authentication_and_authorization/index#auto-generated-sa-token-secrets_using-service-accounts