Abstract
| Goal |
Configure authentication with the HTPasswd identity provider and assign roles to users and groups. |
| Objectives |
|
| Sections |
|
| Lab |
|
Several OpenShift resources relate to authentication and authorization. The following list shows the primary resource types and their definitions:
In the OpenShift Container Platform architecture, users are entities that interact with the API server. The user resource represents an actor within the system. Assign permissions by adding roles to the user directly or to the groups that the user is a member of.
The identity resource keeps a record of successful authentication attempts from a specific user and identity provider. Any data about the source of the authentication is stored on the identity.
In OpenShift, applications can communicate with the API independently when user credentials cannot be acquired. To preserve the integrity of the credentials for a regular user, credentials are not shared and service accounts are used instead. With service accounts, you can control API access without the need to borrow a regular user's credentials.
Groups represent a specific set of users. Users are assigned to groups. Authorization policies use groups to assign permissions to multiple users at the same time. For example, to grant 20 users access to objects within a project, it is better to use a group instead of granting access to each user individually. OpenShift Container Platform also provides system groups or virtual groups that are provisioned automatically by the cluster.
A role defines the API operations that a user has permissions to perform on specified resource types. You grant permissions to users, groups, and service accounts by assigning roles to them.
User and identity resources are usually not created in advance. OpenShift usually creates these resources automatically after a successful interactive login with OAuth.
The authentication and authorization security layers enable user interaction with the cluster. When a user makes a request to the API, the API associates the user with the request. The authentication layer authenticates the user. On successful authentication, the authorization layer either accepts or rejects the API request. The authorization layer uses role-based access control (RBAC) policies to determine user privileges.
The OpenShift API has two methods for authenticating requests:
OAuth access tokens
X.509 client certificates
If the request does not present an access token or certificate, then the authentication layer assigns it the system:anonymous virtual user and the system:unauthenticated virtual group.
The OpenShift Container Platform provides the Authentication operator, which runs an OAuth server. The OAuth server provides OAuth access tokens to users when they attempt to authenticate to the API. An identity provider must be configured and available to the OAuth server. The OAuth server uses an identity provider to validate the identity of the requester. The server reconciles the user with the identity and creates the OAuth access token for the user. OpenShift automatically creates identity and user resources after a successful login.
The OpenShift OAuth server can be configured to use many identity providers. The following lists includes the more common identity providers:
Validates usernames and passwords against a secret that stores credentials that are generated by using the htpasswd command.
Enables shared authentication with an OpenStack Keystone v3 server.
Configures the LDAP identity provider to validate usernames and passwords against an LDAPv3 server, by using simple bind authentication.
Configures a GitHub identity provider to validate usernames and passwords against GitHub or the GitHub Enterprise OAuth authentication server.
Integrates with an OpenID Connect identity provider by using an Authorization Code Flow.
The OAuth custom resource must be updated with your chosen identity provider. You can define multiple identity providers, of the same or different kinds, on the same OAuth custom resource.
Before you can configure an identity provider and manage users, you must access your OpenShift cluster as a cluster administrator.
A newly installed OpenShift cluster provides two ways to authenticate API requests with cluster administrator privileges.
One way is to use the kubeconfig file, which embeds an X.509 client certificate that never expires.
Another way is to authenticate as the kubeadmin virtual user.
Successful authentication grants an OAuth access token.
To create additional users and grant them different access levels, you must configure an identity provider and assign roles to your users.
During installation, the OpenShift installer creates a unique kubeconfig file in the auth directory.
The kubeconfig file contains specific details and parameters for the CLI to connect a client to the correct API server, including an X.509 certificate.
The installation logs provide the location of the kubeconfig file:
INFO Run 'export KUBECONFIG=root/auth/kubeconfig' to manage the cluster with 'oc'.
In the classroom environment, the utility machine stores the kubeconfig file at /home/lab/ocp4/auth/kubeconfig.
To use the kubeconfig file to authenticate oc commands, you must copy the file to your workstation and set the absolute or relative path to the KUBECONFIG environment variable.
Then, you can run any oc command that requires cluster administrator privileges without logging in to OpenShift.
[user@host ~]$export KUBECONFIG=/home/user/auth/kubeconfig[user@host ~]$oc get nodes
As an alternative, you can use the --kubeconfig option of the oc command.
[user@host ~]$ oc --kubeconfig /home/user/auth/kubeconfig get nodesAfter installation completes, OpenShift creates the kubeadmin virtual user.
The kubeadmin secret in the kube-system namespace contains the hashed password for the kubeadmin user.
The kubeadmin user has cluster administrator privileges.
The OpenShift installer dynamically generates a unique kubeadmin password for the cluster.
The installation logs provide the kubeadmin credentials to log in to the cluster.
The cluster installation logs also provide the login, password, and the URL for console access.
...output omitted...
INFO The cluster is ready when 'oc login -u kubeadmin -p shdU_trbi_6ucX_edbu_aqop'
...output omitted...
INFO Access the OpenShift web-console here:
https://console-openshift-console.apps.ocp4.example.com
INFO Login to the console with user: kubeadmin, password: shdU_trbi_6ucX_edbu_aqopIn the classroom environment, the utility machine stores the password for the kubeadmin user in the /home/lab/ocp4/auth/kubeadmin-password file.
After you define an identity provider, create a user, and assign that user the cluster-admin role, you can remove the kubeadmin user credentials to improve cluster security.
[user@host ~]$ oc delete secret kubeadmin -n kube-systemIf you delete the kubeadmin secret before you configure another user with cluster admin privileges, then you can administer your cluster only by using the kubeconfig file.
If you do not have a copy of this file in a safe location, then you cannot recover administrative access to your cluster.
The only alternative is to destroy and reinstall your cluster.
Do not delete the kubeadmin user at any time during this course.
The kubeadmin user is essential to the course lab architecture.
If you deleted this user, you would have to delete the lab environment and re-create it.
The HTPasswd identity provider validates users against a secret that contains usernames and passwords that are generated with the htpasswd command from the Apache HTTP Server project.
Only a cluster administrator can change the data inside the HTPasswd secret.
Regular users cannot change their own passwords.
Managing users with the HTPasswd identity provider might suffice for a proof-of-concept environment with a small set of users. However, most production environments require a more powerful identity provider that integrates with the organization's identity management system.
To use the HTPasswd identity provider, the OAuth custom resource must be edited to add an entry to the .spec.identityProviders array:
apiVersion: config.openshift.io/v1 kind: OAuth metadata: name: clusterspec:identityProviders: - name:my_htpasswd_providermappingMethod:
claimtype: HTPasswd htpasswd: fileData: name:
htpasswd-secret
This provider name is prefixed to provider user names to form an identity name. | |
Controls how mappings are established between provider identities and user objects.
With the default | |
An existing secret that contains data that is generated by using the |
To update the OAuth custom resource, use the oc get command to export the existing OAuth cluster resource to a file in YAML format.
[user@host ~]$ oc get oauth cluster -o yaml > oauth.yamlThen, open the resulting file in a text editor and make the needed changes to the embedded identity provider settings.
After completing modifications and saving the file, you must apply the new custom resource by using the oc replace command.
[user@host ~]$ oc replace -f oauth.yamlManaging user credentials with the HTPasswd Identity Provider requires creating a temporary htpasswd file, changing the file, and applying these changes to the secret.
The httpd-tools package provides the htpasswd utility, which must be installed and available on your system.
Create the htpasswd file.
[user@host ~]$ htpasswd -c -B -b /tmp/htpasswd student redhat123Use the -c option only when creating a file.
The -c option replaces all file content if the file already exists.
Add or update credentials.
[user@host ~]$ htpasswd -b /tmp/htpasswd student redhat1234Delete credentials.
[user@host ~]$ htpasswd -D /tmp/htpasswd studentTo use the HTPasswd provider, you must create a secret that contains the htpasswd file data.
The following example uses a secret named htpasswd-secret.
[user@host ~]$ oc create secret generic htpasswd-secret \
--from-file htpasswd=/tmp/htpasswd -n openshift-configA secret that the HTPasswd identity provider uses requires adding the htpasswd= prefix before specifying the path to the file.
When adding or removing users, use the oc extract command to retrieve the secret.
Extracting the secret ensures that you work on the current set of users.
By default, the oc extract command saves each key within a configuration map or secret as a separate file.
Alternatively, you can then redirect all data to a file or display it as standard output.
To extract data from the htpasswd-secret secret to the /tmp/ directory, use the following command.
The --confirm option replaces the file if it exists.
[user@host ~]$ oc extract secret/htpasswd-secret -n openshift-config \
--to /tmp/ --confirm
/tmp/htpasswdThe secret must be updated after adding, changing, or deleting users.
Use the oc set data secret command to update a secret.
Unless the file name is htpasswd, you must specify htpasswd= to update the htpasswd key within the secret.
The following command updates the htpasswd-secret secret in the openshift-config namespace by using the content of the /tmp/htpasswd file.
[user@host ~]$ oc set data secret/htpasswd-secret \
--from-file htpasswd=/tmp/htpasswd -n openshift-configAfter updating the secret, the OAuth operator redeploys pods in the openshift-authentication namespace.
Monitor the redeployment of the new OAuth pods by running the following command:
[user@host ~]$ watch oc get pods -n openshift-authenticationTest additions, changes, or deletions to the secret after the new pods finish deploying.
When a scenario occurs that requires you to delete a user, it is not sufficient to delete the user from the identity provider. The user and identity resources must also be deleted.
You must remove the password from the htpasswd secret, remove the user from the local htpasswd file, and then update the secret.
To delete the user from htpasswd, run the following command:
[user@host ~]$ htpasswd -D /tmp/htpasswd managerUpdate the secret to remove all remnants of the user's password.
[user@host ~]$ oc set data secret/htpasswd-secret \
--from-file htpasswd=/tmp/htpasswd -n openshift-configRemove the user resource with the following command:
[user@host ~]$ oc delete user manager
user.user.openshift.io "manager" deletedIdentity resources include the name of the identity provider.
To delete the identity resource for the manager user, find the resource and then delete it.
[user@host ~]$oc get identities | grep managermy_htpasswd_provider:manager my_htpasswd_provider manager manager ... [user@host ~]$oc delete identity my_htpasswd_provider:manageridentity.user.openshift.io "my_htpasswd_provider:manager" deleted
The cluster-wide cluster-admin role grants cluster administration privileges to users and groups.
With this role, the user can perform any action on any resources within the cluster.
The following example assigns the cluster-admin role to the student user.
[user@host ~]$ oc adm policy add-cluster-role-to-user cluster-admin studentFor more information about identity providers, refer to the Understanding Identity Provider Configuration 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-identity-provider