# Authentication with OpenLDAP

> Learn how to set up LDAP authentication for Stackable's Superset and Trino products, including installing OpenLDAP and configuring authentication classes.

The Stackable platform supports user authentication with LDAP in multiple products. This page guides you through setting up the configuration necessary to use an existing LDAP installation with Stackable supported products. You can learn more about authentication in the Stackable Platform on the [concepts page](https://docs.stackable.tech/home/stable/concepts/authentication/index.md).

Prerequisites:

-   a k8s cluster available, or [kind](https://kind.sigs.k8s.io/) installed

-   [stackablectl](https://docs.stackable.tech/management/stable/stackablectl/index.md) installed

-   basic knowledge of how to create resources in Kubernetes (i.e. `kubectl apply -f <filename>.yaml`) and inspect them (`kubectl get` or a tool like [k9s](https://k9scli.io/))


## Setup

Before configuring LDAP you need to set up some services to configure, as well as an LDAP server to use for authentication. This tutorial is about learning how to configure LDAP, so there won’t be detailed instructions on how to set up all of this, instead the tutorial uses the [`stackablectl stacks`](https://docs.stackable.tech/management/stable/stackablectl/commands/stack/index.md) command.

### Superset and Trino Stack

This command will install the latest Stackable release for you, and set up the `trino-superset-s3` Stack. It contains a Superset instance connected to a Trino instance, and all their dependencies (Minio S3, PostgreSQL). Later in this guide, a Superset and Trino instance will be configured to use LDAP.

If you do not have a Kubernetes cluster already, add the `--cluster kind` (or `-c kind`) flag to let stackablectl deploy one for you.

```bash
stackablectl stack install trino-superset-s3
```

This command will take a few minutes to complete.

> **Note:** The stack installed here is used in the [`trino-taxi-data` demo](https://docs.stackable.tech/home/stable/demos/trino-taxi-data/index.md). Click the link to learn more.

#### Inspect

Before starting to add configuration to your Stackable cluster, inspect what the command above has set up in your Kubernetes cluster.

Use `stackablectl stacklet list` to find the endpoints of Superset and Trino and open their web interfaces in the browser.

You can log into both Superset and Trino with user `admin` and password `adminadmin`.

These _admin_ users are defined in Superset’s and Trino’s internal user management. Later you will see that these users cannot be used for authentication anymore after LDAP authentication has been enabled.

### OpenLDAP Stack

Install another Stack, the `tutorial-openldap`.

```bash
stackablectl stack install tutorial-openldap
```

#### Inspect

Look at the Pod definition of the `openldap-0` Pod, it contains the environment settings

```
LDAP_ADMIN_USERNAME:      ldapadmin
LDAP_ADMIN_PASSWORD:      ldapadminpassword
LDAP_USERS:               alice,bob
LDAP_PASSWORDS:           alice,bob
```

`ldapadmin` is the admin or bind user, and `ldapadminpassword` is the password that belongs to that user. `alice` and `bob` (and their respective passwords) are the only two users defined in the LDAP instance. You will use this information later to configure LDAP.

## Steps

Now that you have a couple of data products as well as the LDAP server installed, you can start configuring the products to use LDAP. The following image shows the parts that are already there in blue and the parts you will add in green:

![image$openldap tutorial.drawio](https://docs.stackable.tech/home/stable/tutorials/_images/openldap-tutorial.drawio.svg)

The tutorial has 3 steps:

1.  Configure an AuthenticationClass (as well as SecretClass and Secret) with this LDAP installation

2.  Update the SupersetCluster to use the AuthenticationClass to authenticate users

3.  Update the TrinoCluster to use the AuthenticationClass to authenticate users


### Configure LDAP

The AuthenticationClass is the main resource required to configure the products, but it depends on some other resources. Below you will create a Secret, SecretClass and the AuthenticationClass. Use `kubectl apply` to deploy the manifests shown below.

> **Note:** If you’re having problems here, install the `openldap` stack instead of `tutorial-openldap` which comes with an already configured AuthenticationClass ready to use. You can then skip to [configuring superset](https://docs.stackable.tech/home/stable/tutorials/authentication_with_openldap/index.md).

First, create a secret that contains the LDAP bind credentials which products can use to authenticate with LDAP:

```yaml
---
apiVersion: v1
kind: Secret
metadata:
  name: openldap-bind-credentials
  labels:
    secrets.stackable.tech/class: openldap-bind-credentials  (1)
stringData:
  user: cn=ldapadmin,dc=example,dc=org  (2)
  password: ldapadminpassword           (3)
```

(1) The annotation which SecretClass this secret belongs to
(2) The LDAP bind user that was provided by the `tutorial-ldap` Stack
(3) The corresponding password

Notice the SecretClass annotation. Create the SecretClass next:

```yaml
---
apiVersion: secrets.stackable.tech/v1alpha1
kind: SecretClass
metadata:
  name: openldap-bind-credentials  (1)
spec:
  backend:
    k8sSearch:
      searchNamespace:
        pod: {}  (2)
```

(1) The name of the [SecretClass](https://docs.stackable.tech/home/stable/secret-operator/secretclass/index.md) we are creating that is referred to by the Secret
(2) This determines the namespace in which the referenced `Secret` will be looked for. In this case it searches for a `Secret` in the same namespace as the product runs in. See [the documentation of SecretClass](https://docs.stackable.tech/home/stable/secret-operator/secretclass/index.md)

This level of indirection is necessary, because the AuthenticationClass is cluster-scoped but Secrets are not.

Now you can create the AuthenticationClass `openldap` which references the SecretClass:

```yaml
---
apiVersion: authentication.stackable.tech/v1alpha1
kind: AuthenticationClass
metadata:
  name: openldap  (1)
spec:
  provider:
    ldap:
      hostname: openldap.default.svc.cluster.local  (2)
      searchBase: ou=users,dc=example,dc=org
      bindCredentials:
        secretClass: openldap-bind-credentials  (3)
      port: 1636
      tls:
        verification:
          server:
            caCert:
              secretClass: openldap-tls  (4)
```

(1) The name of the AuthenticationClass, which needs to be referenced later
(2) The hostname in this case is the Service at which the OpenLDAP is running, inside of Kubernetes.
(3) Here the SecretClass name is referenced
(4) A SecretClass used to verify the LDAP server’s TLS certificate, see below.

The AuthenticationClass connects to LDAP over TLS (port `1636`) and verifies the server’s certificate against the CA provided by the `openldap-tls` SecretClass. You did not have to create that SecretClass: the `tutorial-openldap` stack created it for you. It uses the `autoTls` backend with `autoGenerate: true`, so the Secret Operator generates its own certificate authority and uses it both to issue OpenLDAP’s server certificate and to let the products verify that certificate. This works because the Secret Operator owns the CA, including its private key, from end to end.

Connecting to a **real** LDAP server is different: you do not control the server’s certificate and usually only have its CA certificate, not the CA’s private key. In that case you create the SecretClass yourself using the `k8sSearch` backend, which simply makes the existing CA certificate available to the products. A SecretClass used only for server verification needs nothing but the CA certificate; it does not need a key. See [TLS server verification](https://docs.stackable.tech/home/stable/concepts/tls-server-verification/index.md) for a complete, copy-pasteable example.

Remember the name of the AuthenticationClass (`openldap`), you will use it in the next steps when configuring the products.

### Add LDAP authentication to Superset

To make Superset use your new LDAP AuthenticationClass, you have to update the SupersetCluster definition. A SupersetCluster named `superset` is already installed by the stack.

Fetch the existing SupersetCluster defintion from the Kubernetes API server and save it into a `superset.yaml` file:

```bash
kubectl get superset superset -o yaml > superset.yaml
```

The `superset.yaml` file should look similar to this

```yaml
---
apiVersion: superset.stackable.tech/v1alpha1
kind: SupersetCluster
metadata:
  name: superset
  ...
spec:
  image:
    productVersion: ...
  clusterConfig:
    listenerClass: ...
    credentialsSecret: superset-credentials
    mapboxSecret: ...
  nodes:
    roleGroups:
      default:
        replicas: 1
  ...
```

You can now delete the SupersetCluster, you recreate it later with the new configuration:

```bash
kubectl delete superset superset
```

Modify your `superset.yaml` to include this new `authentication` property under the `spec.clusterConfig`:

```yaml
spec:
  clusterConfig:
    authentication:                   (1)
      - authenticationClass: openldap (2)
        userRegistrationRole: Admin   (3)
```

(1) The new `authentication` configuration section which configures how Superset is authenticating users
(2) The `authenticationClass` property is referencing the AuthenticationClass `openldap` you created earlier
(3) The default Superset role that users should be assigned to when they log in. Any user will be an Admin

Your `superset.yaml` should now look similar to this

```yaml
---
apiVersion: superset.stackable.tech/v1alpha1
kind: SupersetCluster
metadata:
  name: superset
  ...
spec:
  image:
    productVersion: ...
  clusterConfig:
    authentication:
      - authenticationClass: openldap
        userRegistrationRole: Admin
    listenerClass: ...
    credentialsSecret: superset-credentials
    mapboxSecret: ...
  nodes:
    roleGroups:
      default:
        config:
  ...
```

Now deploy the updated superset cluster:

```bash
kubectl apply -f superset.yaml
```

Connect to superset as before (logout if you still had a valid session from earlier), and try logging in again with username _admin_ and password _adminadmin_, Superset will not accept these credentials anymore. You now have to use LDAP credentials to log in. The OpenLDAP you installed earlier comes with two users, _alice_ (password _alice_) and _bob_ (password _bob_). Log in with any of these users and Superset will accept.

### Add LDAP configuration to Trino

Trino is configured very similarly to Superset.

Fetch the existing TrinoCluster definition from the Kubernetes API server and save it into a `trino.yaml` file:

```bash
kubectl get trino trino -o yaml > trino.yaml
```

The `trino.yaml` file should look similar to this

```yaml
---
apiVersion: trino.stackable.tech/v1alpha1
kind: TrinoCluster
metadata:
  name: trino
  ...
spec:
  image:
    productVersion: ...
  clusterConfig:
    authentication:
      - authenticationClass: trino-users
    authorization:
      opa:
        configMapName: opa
        package: trino
    catalogLabelSelector:
    ...
  workers:
    ...
  coordinators:
    ...
  ...
```

You can now delete the TrinoCluster. you recreate it later with the new configuration:

```bash
kubectl delete trino trino
```

Replace the `trino-users` AuthenticationClass in your `trino.yaml` with the `openldap` AuthenticationClass:

```yaml
spec:
  clusterConfig:
    authentication:
      - authenticationClass: openldap (1)
```

(1) Switch the `authenticationClass` to the AuthenticationClass `openldap` you created earlier

Your `trino.yaml` should now look similar to this

```yaml
---
apiVersion: trino.stackable.tech/v1alpha1
kind: TrinoCluster
metadata:
  name: trino
  ...
spec:
  image:
    productVersion: ...
  clusterConfig:
    authentication:
      - authenticationClass: openldap
    authorization:
      opa:
        configMapName: opa
        package: trino
    catalogLabelSelector:
    ...
  workers:
    ...
  coordinators:
    ...
  ...
```

Now deploy the updated Trino cluster:

```bash
kubectl apply -f trino.yaml
```

Again, like with Superset, connect to Trino now (make sure that the StatefulSets are running). You will notice that the _admin_ user cannot be used anymore, but the LDAP users _alice_ and _bob_ work!

### Bonus: Reconfigure OPA to use the new LDAP users

This is a bonus step, and if you want you can skip straight to the next section: [Log in with the new LDAP credentials](https://docs.stackable.tech/home/stable/tutorials/authentication_with_openldap/index.md)

This step is not required for _authentication_ by itself. But the demo stack you installed comes with an _authorization_ configuration for Trino as well. Authorization on the platform is done using the [Stackable Operator for OPA (OpenPolicyAgent)](https://docs.stackable.tech/home/stable/opa/index.md).

Fetch the snippet as before:

```bash
kubectl get cm trino-opa-bundle -o yaml > trino-opa-bundle.yaml
```

Update the ConfigMap to look like the below:

```yaml
data:
  trino.rego: |
    package trino

    default allow = false

    allow if {
      is_alice
    }

    batch contains i if {
      some i
      input.action.filterResources[i]
      is_alice
    }

    batch contains i if {
      some i
      input.action.operation == "FilterColumns"
      count(input.action.filterResources) == 1
      input.action.filterResources[0].table.columns[i]
      is_alice
    }

    is_alice() if {
      input.context.identity.user == "alice"
    }
```

And apply the new bundle that lets `alice` do everything and denies `bob` everything:

```bash
kubectl apply -f trino-opa-bundle.yaml
```

The OPA Operator will automatically detect the change and update the Trino authorization bundle.

### Log in with the new LDAP credentials

Congratulations! You have configured Superset and Trino to use LDAP for authentication.

Log in with the LDAP user credentials `alice:alice` or `bob:bob` into Superset and Trino. Note also that the previously used `admin` credentials no longer work.

If you skipped the OPA step, you will be able to log into Trino, but running queries will not work, as the LDAP users are not authorized to do so. If you _did_ do this step, running queries with the LDAP users will also work.

That concludes the tutorial!

## Summary

To summarize, you have done the following:

-   Written an AuthenticationClass for an existing LDAP server. The credentials are stored in a Secret with a SecretClass that is referenced in the AuthenticationClass.

-   Adapted the SupersetCluster spec to include the reference to the LDAP AuthenticationClass.

-   Adapted the TrinoCluster spec to include the reference to the AuthenticationClass.


The LDAP connection details only need to be written down once, in the AuthenticationClass. Making a product use this AuthenticationClass is then done by referencing the AuthenticationClass, so the configuration for LDAP is all in a single resource.

## Further Reading

-   [Authentication concepts page](https://docs.stackable.tech/home/stable/concepts/authentication/index.md)

    -   [AuthenticationClass CRD reference](https://hub.stackable.tech/crds/authentication.stackable.tech/v1alpha1/authenticationclass?release=26.7)


-   [Getting started with the Stackable Operator for Apache Superset](https://docs.stackable.tech/home/stable/superset/getting_started/index.md)

-   [Getting started with the Stackable Operator for Trino](https://docs.stackable.tech/home/stable/trino/getting_started/index.md)
