Configure the HTPasswd identity provider and create users for cluster administrators.
Outcomes
Create users and passwords for HTPasswd authentication.
Configure the Identity Provider for HTPasswd authentication.
Assign cluster administration rights to users.
[student@workstation ~]$ lab start auth-providers
The command ensures that the cluster API is reachable, the httpd-utils package is installed, and that the authentication settings are configured to the installation defaults.
Instructions
Add an entry for two users, new_admin and new_developer.
Assign the new_admin user the redhat password, and assign the new_developer user the developer password.
Create an HTPasswd authentication file named htpasswd in the ~/DO280/labs/auth-providers/ directory.
Add the new_admin user with the redhat password.
The file name is arbitrary; this exercise uses the ~/DO280/labs/auth-providers/htpasswd file.
Use the htpasswd command to populate the HTPasswd authentication file with the usernames and encrypted passwords.
The -B option uses bcrypt encryption.
By default, the htpasswd command uses the MD5 hashing algorithm if you do not specify another algorithm.
[student@workstation ~]$ htpasswd -c -B -b ~/DO280/labs/auth-providers/htpasswd \
new_admin redhat
Adding password for user new_adminAdd the new_developer user with the developer password to the ~/DO280/labs/auth-providers/htpasswd file.
The password for the new_developer user is hashed with the MD5 algorithm, because no algorithm was specified and MD5 is the default hashing algorithm.
[student@workstation ~]$ htpasswd -b ~/DO280/labs/auth-providers/htpasswd \
new_developer developer
Adding password for user new_developerReview the contents of the ~/DO280/labs/auth-providers/htpasswd file and verify that it includes two entries with hashed passwords: one for the new_admin user and another for the new_developer user.
[student@workstation ~]$ cat ~/DO280/labs/auth-providers/htpasswd
new_admin:$2y$05$qQaFbpx4hbf4uZe.SMLSduTN8uN4DNJMJ4jE5zXDA57WrTRlpu2QS
new_developer:$apr1$S0TxtLXl$QSRfBIufYP39pKNsIg/nD1Log in to OpenShift and create a secret that contains the HTPasswd users file.
Log in to the cluster as the admin user.
[student@workstation ~]$ oc login -u admin -p redhatocp \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Create a secret from the ~/DO280/labs/auth-providers/htpasswd file.
To use the HTPasswd identity provider, you must define a secret with a key named htpasswd that contains the HTPasswd user file ~/DO280/labs/auth-providers/htpasswd.
[student@workstation ~]$ oc create secret generic localusers \
--from-file htpasswd=~/DO280/labs/auth-providers/htpasswd \
-n openshift-config
secret/localusers createdAssign the new_admin user the cluster-admin role.
[student@workstation ~]$ oc adm policy add-cluster-role-to-user \
cluster-admin new_admin
Warning: User 'new_admin' not found
clusterrole.rbac.authorization.k8s.io/cluster-admin added: "new_admin"The output indicates that the new_admin user is not found. You can safely ignore this warning.
Update the HTPasswd identity provider for the cluster so that your users can authenticate. Configure the custom resource file and update the cluster.
Export the existing OAuth resource to a file named oauth.yaml in the ~/DO280/labs/auth-providers directory.
[student@workstation ~]$ oc get oauth cluster \
-o yaml > ~/DO280/labs/auth-providers/oauth.yamlFor convenience, an oauth.yaml file that contains the completed custom resource file is downloaded to ~/DO280/solutions/auth-providers.
Edit the ~/DO280/labs/auth-providers/oauth.yaml file with your preferred text editor.
You can choose the names of the identityProviders and fileData structures.
For this exercise, use the myusers and localusers values, respectively.
The completed custom resource should match the following structure.
Ensure that the htpasswd, mappingMethod, name, and type strings are at the same indentation level.
apiVersion: config.openshift.io/v1 kind: OAuth ...output omitted...spec:identityProviders:- ldap: ...output omitted... type: LDAP- htpasswd:fileData:name: localusersmappingMethod: claimname: myuserstype: HTPasswd
Apply the custom resource that was defined in the previous step.
[student@workstation ~]$ oc replace -f ~/DO280/labs/auth-providers/oauth.yaml
oauth.config.openshift.io/cluster replacedAuthentication changes require redeploying pods in the openshift-authentication namespace.
Use the watch command to examine the status of workloads in the openshift-authentication namespace.
[student@workstation ~]$ watch oc get all -n openshift-authentication
NAME READY STATUS RESTARTS AGE
pod/oauth-openshift-6d68ffb9dc-6f8dr 1/1 Running 3 2m
...output omitted...A few minutes after you ran the oc replace command, the redeployment starts.
Wait until new pods are running.
Press Ctrl+C to exit the watch command.
Provided that the previously created secret was created correctly, you can log in by using the HTPasswd identity provider.
Log in as the new_admin and as the new_developer user to verify the HTPasswd user configuration.
Log in to the cluster as the new_admin user to verify that the HTPasswd authentication is configured correctly.
The authentication operator takes some time to load the configuration changes from the previous step.
If the authentication fails, then wait a few moments and try again.
[student@workstation ~]$ oc login -u new_admin -p redhat
Login successful.
...output omitted...Use the oc get nodes command to verify that the new_admin user has the cluster-admin role.
[student@workstation ~]$ oc get nodes
NAME STATUS ROLES AGE VERSION
master01 Ready control-plane,master,worker 13d v1.27.6+f67aeb3Log in to the cluster as the new_developer user to verify that the HTPasswd authentication is configured correctly.
[student@workstation ~]$ oc login -u new_developer -p developer
Login successful.
...output omitted...Use the oc get nodes command to verify that the new_developer and new_admin users do not have the same level of access.
[student@workstation ~]$ oc get nodes
Error from server (Forbidden): nodes is forbidden: User "new_developer" cannot list resource "nodes" in API group "" at the cluster scopeLog in as the new_admin user.
[student@workstation ~]$ oc login -u new_admin -p redhat
Login successful.
...output omitted...List the current users.
[student@workstation ~]$ oc get users
NAME UID ... IDENTITIES
admin 6126c5a9-4d18-4cdf-95f7-b16c3d3e7f24 ... ...
new_admin 489c7402-d318-4805-b91d-44d786a92fc1 ... myusers:new_admin
new_developer 8dbae772-1dd4-4242-b2b4-955b005d9022 ... myusers:new_developerYou might see additional users from previously completed exercises.
Display the list of current identities.
[student@workstation ~]$ oc get identity
NAME IDP NAME IDP USER NAME USER NAME
USER UID
... ... ... admin
6126c5a9-4d18-4cdf-95f7-b16c3d3e7f24
myusers:new_admin myusers new_admin new_admin
489c7402-d318-4805-b91d-44d786a92fc1
myusers:new_developer myusers new_developer new_developer
8dbae772-1dd4-4242-b2b4-955b005d9022You might see additional identities from previously completed exercises.
As the new_admin user, create a HTPasswd user named manager with a password of redhat.
Extract the file data from the secret to the ~/DO280/labs/auth-providers/htpasswd file.
[student@workstation ~]$ oc extract secret/localusers -n openshift-config \
--to ~/DO280/labs/auth-providers/ --confirm
/home/student/DO280/labs/auth-providers/htpasswdAdd an entry to your ~/DO280/labs/auth-providers/htpasswd file for the additional manager user with the redhat password.
[student@workstation ~]$ htpasswd -b ~/DO280/labs/auth-providers/htpasswd \
manager redhat
Adding password for user managerReview the contents of your ~/DO280/labs/auth-providers/htpasswd file and verify that it includes three entries with hashed passwords: one each for the new_admin, new_developer, and manager users.
[student@workstation ~]$ cat ~/DO280/labs/auth-providers/htpasswd
new_admin:$2y$05$qQaFbpx4hbf4uZe.SMLSduTN8uN4DNJMJ4jE5zXDA57WrTRlpu2QS
new_developer:$apr1$S0TxtLXl$QSRfBIufYP39pKNsIg/nD1
manager:$apr1$HZ/9tC6b$j2OcHHg2GO2SSu1wyGOge.You must update the secret after adding additional users.
Use the oc set data secret command to update the secret.
If the command fails, then wait a few moments for the oauth operator to finish reloading, and rerun the command.
[student@workstation ~]$ oc set data secret/localusers \
--from-file htpasswd=~/DO280/labs/auth-providers/htpasswd \
-n openshift-config
secret/localusers data updatedUse the watch command to examine the status of workloads in the openshift-authentication namespace.
[student@workstation ~]$ watch oc get all -n openshift-authentication
NAME READY STATUS RESTARTS AGE
pod/oauth-openshift-6d68ffb9dc-6f8dr 1/1 Running 3 2m
...output omitted...A few minutes after you ran the oc set data command, the redeployment starts.
Wait until new pods are running.
Press Ctrl+C to exit the watch command.
Log in to the cluster as the manager user.
If the authentication fails, then wait a few moments and try again.
[student@workstation ~]$ oc login -u manager -p redhat
Login successful.
...output omitted...Create an auth-providers project, and then verify that the new_developer user cannot access the project.
As the manager user, create an auth-providers project.
[student@workstation ~]$ oc new-project auth-providers
Now using project "auth-providers" on server https://api.ocp4.example.com:6443".
...output omitted...Log in as the new_developer user.
[student@workstation ~]$ oc login -u new_developer -p developer
Login successful.
...output omitted...Attempt to delete the auth-providers project.
[student@workstation ~]$ oc delete project auth-providers
Error from server (Forbidden): projects.project.openshift.io "auth-providers" is forbidden: User "new_developer" cannot delete resource "projects" in API group "project.openshift.io" in the namespace "auth-providers"Change the password for the manager user.
Log in as the new_admin user.
[student@workstation ~]$ oc login -u new_admin -p redhat
Login successful.
...output omitted...Extract the file data from the secret to the ~/DO280/labs/auth-providers/htpasswd file.
[student@workstation ~]$ oc extract secret/localusers -n openshift-config \
--to ~/DO280/labs/auth-providers/ --confirm
/home/student/DO280/labs/auth-providers/htpasswdGenerate a random user password and assign it to the MANAGER_PASSWD variable.
[student@workstation ~]$ MANAGER_PASSWD="$(openssl rand -hex 15)"Update the manager user to use the stored password in the MANAGER_PASSWD variable.
[student@workstation ~]$ htpasswd -b ~/DO280/labs/auth-providers/htpasswd \
manager ${MANAGER_PASSWD}
Updating password for user managerUpdate the secret.
[student@workstation ~]$ oc set data secret/localusers \
--from-file htpasswd=~/DO280/labs/auth-providers/htpasswd \
-n openshift-config
secret/localusers data updatedUse the watch command to examine the status of workloads in the openshift-authentication namespace.
[student@workstation ~]$ watch oc get all -n openshift-authentication
NAME READY STATUS RESTARTS AGE
pod/oauth-openshift-6d68ffb9dc-6f8dr 1/1 Running 3 2m
...output omitted...A few minutes after you ran the oc set data command, the redeployment starts.
Wait until new pods are running.
Press Ctrl+C to exit the watch command.
Log in as the manager user to verify the updated password.
[student@workstation ~]$ oc login -u manager -p ${MANAGER_PASSWD}
Login successful.
...output omitted...If the authentication fails, then wait a few moments and try again.
Remove the manager user.
Log in as the new_admin user.
[student@workstation ~]$ oc login -u new_admin -p redhat
Login successful.
...output omitted...Extract the file data from the secret to the ~/DO280/labs/auth-providers/htpasswd file.
[student@workstation ~]$ oc extract secret/localusers -n openshift-config \
--to ~/DO280/labs/auth-providers/ --confirm
/home/student/DO280/labs/auth-providers/htpasswdDelete the manager user from the ~/DO280/labs/auth-providers/htpasswd file.
[student@workstation ~]$ htpasswd -D ~/DO280/labs/auth-providers/htpasswd manager
Deleting password for user managerUpdate the secret.
[student@workstation ~]$ oc set data secret/localusers \
--from-file htpasswd=~/DO280/labs/auth-providers/htpasswd \
-n openshift-config
secret/localusers data updatedUse the watch command to examine the status of workloads in the openshift-authentication namespace.
[student@workstation ~]$ watch oc get all -n openshift-authentication
NAME READY STATUS RESTARTS AGE
pod/oauth-openshift-6d68ffb9dc-6f8dr 1/1 Running 3 2m
...output omitted...A few minutes after you ran the oc set data command, the redeployment starts.
Wait until new pods are running.
Press Ctrl+C to exit the watch command.
Log in as the manager user. If the login succeeds, then try again until the login fails.
[student@workstation ~]$ oc login -u manager -p ${MANAGER_PASSWD}
Login failed (401 Unauthorized)
Verify you have provided correct credentials.Log in as the new_admin user.
[student@workstation ~]$ oc login -u new_admin -p redhat
Login successful.
...output omitted...Delete the identity resource for the manager user.
[student@workstation ~]$ oc delete identity "myusers:manager"
identity.user.openshift.io "myusers:manager" deletedDelete the user resource for the manager user.
[student@workstation ~]$ oc delete user manager
user.user.openshift.io manager deletedList the current users to verify that you deleted the manager user.
[student@workstation ~]$ oc get users
NAME UID ... IDENTITIES
admin 6126c5a9-4d18-4cdf-95f7-b16c3d3e7f24 ... ...
new_admin 489c7402-d318-4805-b91d-44d786a92fc1 ... myusers:new_admin
new_developer 8dbae772-1dd4-4242-b2b4-955b005d9022 ... myusers:new_developerDisplay the list of current identities to verify that you deleted the manager identity.
[student@workstation ~]$ oc get identity
NAME IDP NAME IDP USER NAME USER NAME
USER UID
... ... ... admin
6126c5a9-4d18-4cdf-95f7-b16c3d3e7f24
myusers:new_admin myusers new_admin new_admin
489c7402-d318-4805-b91d-44d786a92fc1
myusers:new_developer myusers new_developer new_developer
8dbae772-1dd4-4242-b2b4-955b005d9022Extract the secret and verify that only the new_admin and new_developer users are displayed.
Using --to - sends the secret to STDOUT rather than saving it to a file.
[student@workstation ~]$ oc extract secret/localusers -n openshift-config --to -
# htpasswd
new_admin:$2y$05$qQaFbpx4hbf4uZe.SMLSduTN8uN4DNJMJ4jE5zXDA57WrTRlpu2QS
new_developer:$apr1$S0TxtLXl$QSRfBIufYP39pKNsIg/nD1Remove the identity provider and clean up all users.
Log in as the admin user.
[student@workstation ~]$ oc login -u admin -p redhatocp
Login successful.
...output omitted...Delete the auth-providers project.
[student@workstation ~]$ oc delete project auth-providers
project.project.openshift.io "auth-providers" deletedEdit the resource in place to remove the identity provider from OAuth:
[student@workstation ~]$ oc edit oauthDelete all the lines under the ldap identity provider definition.
Your file should match the following example:
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
name: cluster
spec:
identityProviders:
- ldap:
...output omitted...
type: LDAP
# Delete all lines below
- htpasswd:
fileData:
name: localusers
mappingMethod: claim
name: myusers
type: HTPasswdSave your changes, and then verify that the oc edit command applied those changes:
oauth.config.openshift.io/cluster edited
Use the watch command to examine the status of workloads in the openshift-authentication namespace.
[student@workstation ~]$ watch oc get all -n openshift-authentication
NAME READY STATUS RESTARTS AGE
pod/oauth-openshift-6d68ffb9dc-6f8dr 1/1 Running 3 2m
...output omitted...A few minutes after you ran the oc edit command, the redeployment starts.
Wait until new pods are running.
Press Ctrl+C to exit the watch command.
Delete the localusers secret from the openshift-config namespace.
[student@workstation ~]$ oc delete secret localusers -n openshift-config
secret "localusers" deletedDelete all identity resources.
[student@workstation ~]$ oc delete identity --all
identity.user.openshift.io "Red Hat Identity Management:dWlk...jb20" deleted
identity.user.openshift.io "myusers:new_admin" deleted
identity.user.openshift.io "myusers:new_developer" deletedYou might see additional identities from previously completed exercises.
Delete all user resources.
[student@workstation ~]$ oc delete user --all
user.user.openshift.io "admin" deleted
user.user.openshift.io "developer" deleted
user.user.openshift.io "new_admin" deleted
user.user.openshift.io "new_developer" deletedYou might see additional users from previously completed exercises.