=======
Secrets
=======

Secrets are a core component of Kubernetes that enable you to securely store sensitive information such as usernames, passwords, API keys, or authentication tokens for external resource authentication. When you create a secret, it is stored as a base64 encoded file within the Kubernetes cluster, with the name of the secret corresponding to the name of the file. 

These encoded secrets can be accessed by mounting the file into your project sessions and deployments, allowing your applications to securely retrieve and use this sensitive information without exposing it in your source code. 

.. note::

   Anaconda *strongly* recommends using secrets as opposed to including credentials in your project, due to the security risk associated with storing them in version control.

Creating a secret
=================

#. Open the |circle-user| **My account** dropdown menu in the top navigation, then select *Settings*.
#. Under **Secrets**, click |plus| **Add**.
#. Enter a name and value for the secret you want to store, then click **Add**.  

    .. note::

        Because secret names are file names, they can only contain alphanumeric characters and underscores.

Secrets you create are listed here, and are stored in the ``/var/run/secrets/user_credentials/`` directory. 

Using secrets
=============

To use a secret in a project, you must install the ``ae5-tools`` package in your project's environment. 

    .. note:: 

        * The ``ae5-tools`` package is available from Workbench's internal repository. 
        * If you are using an external package repository, you can pull the ``ae5-tools`` package from `anaconda.org <https://anaconda.org/ae5-admin/ae5-tools>`_. 
        * If you are working in an air-gapped environment, you can `download the package here <https://airgap.svc.anaconda.com/?prefix=misc/>`_, then transfer it to your cluster. 

Then, from within your project, add the following code:

    .. code-block:: none

        import os
        from ae5_tools import load_ae5_user_secrets

This function reads all secrets stored in the ``/var/run/secrets/user_credentials/`` directory and creates environment variables for each defined secret. 

For example, let's say you have defined the following secrets:

    .. code-block:: none

        MLFLOW_ACCESS_SECRET = "Pa55w0rd"
        REDIS_ACCESS_TOKEN = "N0Haxh3r3"

From within one of your projects, you could do the following: 

    .. code-block:: none

        import os
        from ae5_tools import load_ae5_user_secrets

        mlflow_secret: str = os.environ["MLFLOW_ACCESS_SECRET"]
        redis_secret: str = os.environ["REDIS_ACCESS_TOKEN"]

        print(mlflow_secret)
        print(redis_secret)

    .. tip::

        There are multiple ways to call environment variables! For example, you could also import the ``demand_env_var`` function from the ``ae5-tools`` package and call environment variables like this:

            .. code-block:: none

                import os
                from ae5_tools import load_ae5_user_secrets, demand_env_var

                mlflow_secret: str = os.environ["MLFLOW_ACCESS_SECRET"]
                redis_secret: str = demand_env_var("REDIS_ACCESS_TOKEN")

                print(mlflow_secret)
                print(redis_secret)

Editing a secret
================

#. Open the |circle-user| **My account** dropdown menu, then select *Settings*.
#. Open the |ellipsis-vertical| actions dropdown menu for your secret and select *Edit*.
#. Update the Value for your secret, then click **Save**.

   .. note::

      If you edit the name of a secret, a new secret is created instead.

Deleting a secret
=================

#. Open the |circle-user| **My account** dropdown menu, then select *Settings*.
#. Open the |ellipsis-vertical| actions dropdown menu for your secret and select *Delete*.
#. Click **Delete**. 

.. |circle-user| raw:: html
   
   <svg class="inline" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M256 496c-54 0-103.9-17.9-144-48v0c0-61.9 50.1-112 112-112h64c61.9 0 112 50.1 112 112v0c5.3-4 10.4-8.2 15.4-12.6C409.1 370.6 354.5 320 288 320H224c-66.5 0-121.1 50.6-127.4 115.4C47.2 391.5 16 327.4 16 256C16 123.5 123.5 16 256 16s240 107.5 240 240s-107.5 240-240 240zm0 16A256 256 0 1 0 256 0a256 256 0 1 0 0 512zm80-304a80 80 0 1 0 -160 0 80 80 0 1 0 160 0zm-80-64a64 64 0 1 1 0 128 64 64 0 1 1 0-128z"/></svg>

.. |ellipsis-vertical| raw:: html

   <svg class="inline" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 512"><path d="M64 360a56 56 0 1 0 0 112 56 56 0 1 0 0-112zm0-160a56 56 0 1 0 0 112 56 56 0 1 0 0-112zM120 96A56 56 0 1 0 8 96a56 56 0 1 0 112 0z"/></svg>

.. |plus| raw:: html

    <i class="fa-light fa-plus"></i>