===============
User management
===============

Package Security Manager utilizes Keycloak to manage user identity and access permissions. Keycloak is open source and provides integration with various identity providers, including LDAP and Active Directory.

.. _access_kc:

Accessing the Keycloak administrative console
=============================================

You can access your Keycloak administrative console in one of two ways:

**Method one:**
Navigate directly to the administrative console at ``https://<FQDN>/auth/admin`` in your browser, where ``<FQDN>`` is your Package Security Manager fully qualified domain name, then log in using your Package Security Manager admin credentials.

**Method two:**
To access the administrative console in Package Security Manager UI, complete the following steps:

#. Log in to Package Security Manager.
#. Select |users-gear| **Manage Users** from the left-hand navigation.

   .. thumbnail:: ../../img/as_manage_users.png
      :align: center

#. Click |user| **Manage Users** to be redirected to the Keycloak login screen.

    .. thumbnail:: ../../img/as_user_mgmt.png
      :align: center

#. Log in using admin credentials.

Realms
======

In Keycloak, a realm is an isolated space containing all the necessary information to manage the authentication and authorization of users on a specific domain. Each realm has its own set of users, permissions, and client applications.

The *master* realm contains the admin user profiles. Admin users are able to log in to Keycloak and manage configurations for other users.

The *dev* realm contains all other user profiles. These users can log in to Package Security Manager and download packages from the channels you create.

Further permissions for users on a realm are provided using roles. For more information, see :doc:`Roles and permissions <permissions>`.

Realm selection
===============

Select your realm from the dropdown menu in the upper-left corner.

   .. thumbnail:: ../../img/kc_realm_select.png
      :align: center

Viewing local users in Keycloak
===============================

To view your users at any time, complete the following steps:

#. Access the Keycloak administrative console.
#. Verify you are in the dev realm.
#. Navigate to **Users** in the left-hand navigation. 

   .. thumbnail:: ../../img/kc_view_users.png
      :align: center

.. note:: 
      
   This list of users will not automatically contain users imported from external databases, such as a directory server (LDAP/AD). 

Viewing federated users in Keycloak
===================================

To include users from an external directory server database in the local users list, complete the following steps:

#. Access the Keycloak administrative console.
#. Verify you are in the dev realm.
#. Navigate to **User Federation** in the left-hand navigation. 
#. Select your directory server. 
#. Expand the **Sync Settings** options.
#. Toggle **Periodic Changed Users Sync** to ``ON``.

.. note:: 
   
   The sync period is measured in seconds, and the default setting of 86,400 seconds is equal to 24 hours, or once daily. The servers must synchronize before imported users appear in the local database.

Creating a new user
===================

If you want to provide access to Package Security Manager for a new member of your organization, you need to add their identity information into Keycloak. 

To create a user, complete the following steps:

#. Access the Keycloak administrative console.
#. Verify you are in the dev realm.
#. Navigate to the **Users** in left-hand navigation. 
#. Select **Add user**.
#. Enter the user's information and toggle **Email Verified** to ``ON``.
#. Optionally, if you want to add the user to an existing group, click **Join Groups**, select available groups from the list, and then click **Join**. 

   .. note::
   
      Adding a new user to a group provides the user with all permissions associated with the group. For more information, see :ref:`Group roles <group_roles>`.

#. Click **Save**. More tabs appear. 
#. Select the **Credentials** tab.
#. Click **Set password**.
#. Enter a password for your user. 
#. If you want the user to choose their own password, leave **Temporary** toggled ``ON``. If you want to control their password, toggle **Temporary** to ``OFF``.
#. Click **Save**.

Creating a new admin user
=========================

To add an admin user to the master realm with full permissions, complete the following steps:

#. Access the Keycloak administrative console.
#. Verify you are in the master realm.
#. Navigate to **Users** in the left-hand navigation. 
#. Select **Add user**.
#. Enter the admin user's information and toggle **Email Verified** to ``ON``.
#. Optionally, if you want to add the admin user to an existing group, click**Join Groups**, select available groups from the list, and then click **Join**. 

   .. note:: 
   
      Adding a new admin to a group provides the admin with any permissions associated with the group. For more information, see :ref:`Group roles <group_roles>`.

#. Click **Save**. More tabs appear. 
#. Select the **Credentials** tab.
#. Click **Set password**.
#. Enter a password for your admin user. 
#. If you want them to choose their own password, leave **Temporary** toggled ``ON``. If you want to control their password, toggle **Temporary** to ``OFF``.
#. Click **Save**.
#. Select the **Role Mappings** tab.
#. Click **Assign role**.
#. Select **admin** from the list.
#. Click **Assign**.

Creating an admin user at the command line
==========================================

To add an admin user to the master realm with full permissions from the command line, complete the following steps:

#. Open a terminal and connect to your Package Security Manager instance.
#. Exec into the Keycloak Docker container by running the following command:

   .. code-block:: none 

      # Replace <INSTALLER_DIRECTORY> with your keycloak container ID
      docker exec -it <INSTALLER_DIRECTORY>_keycloak_1 /bin/bash

#. Log in to your Keycloak admin console by running the following command:

   .. code-block:: none 

      # Replace <USERNAME> with your admin account username
      /opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080/auth --realm master --user <USERNAME>

#. Enter your admin account password when prompted.
#. Create your new admin user by running the following command:

   .. code-block:: none
      
      # Replace <USERNAME> with the new admins username
      /opt/keycloak/bin/kcadm.sh create users --server "http://localhost:8080/auth" -r master -s username=<USERNAME> -s enabled=true

#. Set a password for the new admin user by running the following command:

   .. code-block:: none 
      
      # Replace <USERNAME> with the new admins username
      # Replace <PASSWORD> with a password for the new admin user
      /opt/keycloak/bin/kcadm.sh set-password -r master --username <USERNAME> --new-password <PASSWORD>

#. Assign role permissions to the new admin user by running the following command:

   .. code-block:: none
      
      # Replace <USERNAME> with the new admins username
      /opt/keycloak/bin/kcadm.sh add-roles --uusername <USERNAME> --rolename admin -r master

#. Log in to Keycloak using the newly created admin credentials from the following URL:

   .. code-block:: none

      # Replace <FQDN> with your Package Security Manager fully qualified domain name
      <https://<FQDN>/auth/admin/master/console>

Restricting admin rights
========================

Creating an admin user with restricted rights allows you to delegate the responsibility for managing users, channels, and groups on the dev realm to another admin user, without giving them access to manage the master realm. You can create admin roles with restricted rights through the use of composite roles. For more information on composite roles and to view an example of how to provide restricted admin permissions, see :ref:`Composite roles <composite_roles>`. 

Changing your admin account password
====================================

When you update the password for the Keycloak admin account that was created during installation of Package Security Manager, you must also make sure to update the ``KEYCLOAK_ADMIN_PASSWORD`` parameter in your ``.env`` file. 

To update your admin account password:

#. Access the Keycloak administrative console.
#. Verify you are in the master realm.
#. Navigate to **Users** in the left-hand navigation. 
#. Select **admin** from the list of available users.
#. Select the **Credentials** tab.
#. Click **Reset password**. 

   .. thumbnail:: ../../img/kc_reset_admin_password.png
      :align: center

#. Enter and confirm a new password for your admin account.
#. Toggle **Temporary** to ``OFF``.
#. Click **Save**. 
#. Open a terminal and connect to your instance of Package Security Manager. Contact your IT department if you need help with this step.
#. Enter your installer directory by running the following command: 

   .. code-block:: none

      # Replace <INSTALLER_DIRECTORY> with your instance's installer folder 
      cd <INSTALLER_DIRECTORY>

#. Open the ``.env`` file in your preferred file editor. 
#. Locate the ``KEYCLOAK_ADMIN_PASSWORD`` parameter and update the entry to match your new admin account password. 

   .. thumbnail:: ../../img/kc_admin_password_env_file.png
      :align: center

#. Save your changes and close the file. 

Changing your admin account email
=================================

To update the email address that is associated with the Keycloak admin user:

#. Access the Keycloak administrative console.
#. Verify you are on the **master** realm.
#. Select **Users** from the left-hand navigation.
#. Select **admin** from the list of users.
#. Enter the email address, first name, and last name of the admin user in the **Details** tab. 
#. Click **Save**. 

.. thumbnail:: ../../img/kc_admin_email.png       
   :align: center


.. |user| raw:: html

   <i class="fa-regular fa-user"></i>

.. |users-gear| raw:: html

    <svg class="inline" width="1.2em" height="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512">
        <path d="M144 160A80 80 0 1 0 144 0a80 80 0 1 0 0 160zm368 0A80 80 0 1 0 512 0a80 80 0 1 0 0 160zM0 298.7C0 310.4 9.6 320 21.3 320H234.7c.2 0 .4 0 .7 0c-26.6-23.5-43.3-57.8-43.3-96c0-7.6 .7-15 1.9-22.3c-13.6-6.3-28.7-9.7-44.6-9.7H106.7C47.8 192 0 239.8 0 298.7zM320 320c24 0 45.9-8.8 62.7-23.3c2.5-3.7 5.2-7.3 8-10.7c2.7-3.3 5.7-6.1 9-8.3C410 262.3 416 243.9 416 224c0-53-43-96-96-96s-96 43-96 96s43 96 96 96zm65.4 60.2c-10.3-5.9-18.1-16.2-20.8-28.2H261.3C187.7 352 128 411.7 128 485.3c0 14.7 11.9 26.7 26.7 26.7H455.2c-2.1-5.2-3.2-10.9-3.2-16.4v-3c-1.3-.7-2.7-1.5-4-2.3l-2.6 1.5c-16.8 9.7-40.5 8-54.7-9.7c-4.5-5.6-8.6-11.5-12.4-17.6l-.1-.2-.1-.2H178.7c9.5-36.8 42.9-64 82.6-64H371.9c3.6-5 8.2-9.1 13.5-12.2l2.7-1.5c0-.8 0-1.5 0-2.3s0-1.5 0-2.3l-2.7-1.5zM533.3 192H490.7c-15.9 0-31 3.5-44.6 9.7c1.3 7.2 1.9 14.7 1.9 22.3c0 17.4-3.5 33.9-9.7 49c2.5 .9 4.9 2 7.1 3.3l2.6 1.5c1.3-.8 2.6-1.6 4-2.3v-3c0-19.4 13.3-39.1 35.8-42.6c7.9-1.2 16-1.9 24.2-1.9s16.3 .6 24.2 1.9c22.5 3.5 35.8 23.2 35.8 42.6v3c1.3 .7 2.7 1.5 4 2.3l2.6-1.5c16.8-9.7 40.5-8 54.7 9.7c2.3 2.8 4.5 5.8 6.6 8.7c-2.1-57.1-49-102.7-106.6-102.7zM320 176a48 48 0 1 1 0 96 48 48 0 1 1 0-96zM624.6 355.9c6.3-3.6 9.5-11.1 6.8-18c-2.1-5.5-4.6-10.8-7.4-15.9l-2.3-4c-3.1-5.1-6.5-9.9-10.2-14.5c-4.6-5.7-12.7-6.7-19-3L574.4 311c-8.9-7.6-19.1-13.6-30.4-17.6v-21c0-7.3-4.9-13.8-12.1-14.9c-6.5-1-13.1-1.5-19.9-1.5s-13.4 .5-19.9 1.5c-7.2 1.1-12.1 7.6-12.1 14.9v21c-11.2 4-21.5 10-30.4 17.6l-18.2-10.5c-6.3-3.6-14.4-2.6-19 3c-3.7 4.6-7.1 9.5-10.2 14.6l-2.3 3.9c-2.8 5.1-5.3 10.4-7.4 15.9c-2.6 6.8 .5 14.3 6.8 17.9l18.2 10.5c-1 5.7-1.6 11.6-1.6 17.6s.6 11.9 1.6 17.5l-18.2 10.5c-6.3 3.6-9.5 11.1-6.8 17.9c2.1 5.5 4.6 10.7 7.4 15.8l2.4 4.1c3 5.1 6.4 9.9 10.1 14.5c4.6 5.7 12.7 6.7 19 3L449.6 457c8.9 7.6 19.2 13.6 30.4 17.6v21c0 7.3 4.9 13.8 12.1 14.9c6.5 1 13.1 1.5 19.9 1.5s13.4-.5 19.9-1.5c7.2-1.1 12.1-7.6 12.1-14.9v-21c11.2-4 21.5-10 30.4-17.6l18.2 10.5c6.3 3.6 14.4 2.6 19-3c3.7-4.6 7.1-9.4 10.1-14.5l2.4-4.2c2.8-5.1 5.3-10.3 7.4-15.8c2.6-6.8-.5-14.3-6.8-17.9l-18.2-10.5c1-5.7 1.6-11.6 1.6-17.5s-.6-11.9-1.6-17.6l18.2-10.5zM472 384a40 40 0 1 1 80 0 40 40 0 1 1 -80 0z"/>
    </svg>