Bookmark this page

Guided Exercise: Configure Identity Providers

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

  1. 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.

    1. 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_admin
    2. Add 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_developer
    3. Review 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/nD1
  2. Log in to OpenShift and create a secret that contains the HTPasswd users file.

    1. 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...
    2. 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 created
    3. Assign 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"

      Note

      The output indicates that the new_admin user is not found. You can safely ignore this warning.

  3. Update the HTPasswd identity provider for the cluster so that your users can authenticate. Configure the custom resource file and update the cluster.

    1. 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.yaml

      Note

      For convenience, an oauth.yaml file that contains the completed custom resource file is downloaded to ~/DO280/solutions/auth-providers.

    2. 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: localusers
          mappingMethod: claim
          name: myusers
          type: HTPasswd
    3. 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 replaced

      Note

      Authentication 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.

  4. Log in as the new_admin and as the new_developer user to verify the HTPasswd user configuration.

    1. 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.

      Note

      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...
    2. 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+f67aeb3
    3. Log 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...
    4. 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 scope
    5. Log in as the new_admin user.

      [student@workstation ~]$ oc login -u new_admin -p redhat
      Login successful.
      
      ...output omitted...
    6. 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_developer

      Note

      You might see additional users from previously completed exercises.

    7. 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-955b005d9022

      Note

      You might see additional identities from previously completed exercises.

  5. As the new_admin user, create a HTPasswd user named manager with a password of redhat.

    1. 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/htpasswd
    2. Add 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 manager
    3. Review 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.
    4. 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 updated
    5. 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 set data command, the redeployment starts. Wait until new pods are running. Press Ctrl+C to exit the watch command.

    6. Log in to the cluster as the manager user.

      Note

      If the authentication fails, then wait a few moments and try again.

      [student@workstation ~]$ oc login -u manager -p redhat
      Login successful.
      
      ...output omitted...
  6. Create an auth-providers project, and then verify that the new_developer user cannot access the project.

    1. 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...
    2. Log in as the new_developer user.

      [student@workstation ~]$ oc login -u new_developer -p developer
      Login successful.
      
      ...output omitted...
    3. 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"
  7. Change the password for the manager user.

    1. Log in as the new_admin user.

      [student@workstation ~]$ oc login -u new_admin -p redhat
      Login successful.
      
      ...output omitted...
    2. 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/htpasswd
    3. Generate a random user password and assign it to the MANAGER_PASSWD variable.

      [student@workstation ~]$ MANAGER_PASSWD="$(openssl rand -hex 15)"
    4. 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 manager
    5. Update the secret.

      [student@workstation ~]$ oc set data secret/localusers \
          --from-file htpasswd=~/DO280/labs/auth-providers/htpasswd \
          -n openshift-config
      secret/localusers data updated
    6. 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 set data command, the redeployment starts. Wait until new pods are running. Press Ctrl+C to exit the watch command.

    7. Log in as the manager user to verify the updated password.

      [student@workstation ~]$ oc login -u manager -p ${MANAGER_PASSWD}
      Login successful.
      
      ...output omitted...

      Note

      If the authentication fails, then wait a few moments and try again.

  8. Remove the manager user.

    1. Log in as the new_admin user.

      [student@workstation ~]$ oc login -u new_admin -p redhat
      Login successful.
      
      ...output omitted...
    2. 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/htpasswd
    3. Delete 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 manager
    4. Update the secret.

      [student@workstation ~]$ oc set data secret/localusers \
          --from-file htpasswd=~/DO280/labs/auth-providers/htpasswd \
          -n openshift-config
      secret/localusers data updated
    5. 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 set data command, the redeployment starts. Wait until new pods are running. Press Ctrl+C to exit the watch command.

    6. 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.
    7. Log in as the new_admin user.

      [student@workstation ~]$ oc login -u new_admin -p redhat
      Login successful.
      
      ...output omitted...
    8. Delete the identity resource for the manager user.

      [student@workstation ~]$ oc delete identity "myusers:manager"
      identity.user.openshift.io "myusers:manager" deleted
    9. Delete the user resource for the manager user.

      [student@workstation ~]$ oc delete user manager
      user.user.openshift.io manager deleted
    10. List 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_developer
    11. Display 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-955b005d9022
    12. Extract 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/nD1
  9. Remove the identity provider and clean up all users.

    1. Log in as the admin user.

      [student@workstation ~]$ oc login -u admin -p redhatocp
      Login successful.
      
      ...output omitted...
    2. Delete the auth-providers project.

      [student@workstation ~]$ oc delete project auth-providers
      project.project.openshift.io "auth-providers" deleted
    3. Edit the resource in place to remove the identity provider from OAuth:

      [student@workstation ~]$ oc edit oauth

      Delete 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: HTPasswd

      Save your changes, and then verify that the oc edit command applied those changes:

      oauth.config.openshift.io/cluster edited
    4. 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.

    5. Delete the localusers secret from the openshift-config namespace.

      [student@workstation ~]$ oc delete secret localusers -n openshift-config
      secret "localusers" deleted
    6. Delete 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" deleted

      Note

      You might see additional identities from previously completed exercises.

    7. 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" deleted

      Note

      You might see additional users from previously completed exercises.

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish auth-providers

Revision: do280-4.14-08d11e1