# Security

> Secure your Druid cluster with TLS encryption, LDAP, or OIDC authentication. Connect with OPA for policy-based authorization.

Secure your Apache Druid Stacklet by enabling TLS encryption, user authentication and authorization.

## Encryption

TLS encryption is supported for internal cluster communication (e.g. between Broker and Coordinator) as well as for external communication (e.g. between the browser and the Router web UI).

```yaml
spec:
  clusterConfig:
    tls:
      serverAndInternalSecretClass: tls (1)
```

(1) Name of the SecretClass that is used to encrypt internal and external communication.

> **Important:** A Druid Stacklet is always encrypted per default i.e. `spec.clusterConfig.tls.serverAndInternalSecretClass: tls` in the above example does not need to be specified as it is applied by default: in order to disable this default behavior you can set `spec.clusterConfig.tls.serverAndInternalSecretClass: null`.

## Authentication

### TLS

The access to the Druid cluster can be limited by configuring client authentication (mutual TLS) for all participants. This means that processes acting as internal clients (e.g. a Broker) or external clients (e.g. a Browser) have to authenticate themselves with valid certificates in order to communicate with the Druid cluster.

```yaml
spec:
  clusterConfig:
    authentication:
      - authenticationClass: druid-tls-auth (1)
```

(1) Name of the AuthenticationClass that is used to encrypt and authenticate communication.

The AuthenticationClass may or may not have a SecretClass configured:

```yaml
---
apiVersion: authentication.stackable.tech/v1alpha1
kind: AuthenticationClass
metadata:
  name: druid-mtls-authentication-class
spec:
  provider:
    # Option 1
    tls:
      clientCertSecretClass: druid-mtls (1)
    # Option 2
    tls: {} (2)
```

(1) If a client SecretClass is provided in the AuthenticationClass (here `druid-mtls`), these certificates are used for encryption and authentication.
(2) If no client SecretClass is provided in the AuthenticationClass, the `spec.clusterConfig.tls.serverAndInternalSecretClass` is used for encryption and authentication. It cannot be explicitly set to null in this case.

### LDAP

Druid supports [authentication](https://docs.stackable.tech/home/stable/concepts/authentication/index.md) of users via an LDAP server. This requires setting up an [AuthenticationClass](https://docs.stackable.tech/home/stable/concepts/authentication/index.md) for the LDAP server:

```yaml
apiVersion: authentication.stackable.tech/v1alpha1
kind: AuthenticationClass
metadata:
  name: ldap-auth
spec:
  [...]
```

> **Tip:** You can follow the [Authentication with OpenLDAP](https://docs.stackable.tech/home/stable/tutorials/authentication_with_openldap/index.md) tutorial to learn how to create an AuthenticationClass for an LDAP server.

Reference the AuthenticationClass in your DruidCluster resource:

```yaml
apiVersion: druid.stackable.tech/v1alpha1
kind: DruidCluster
metadata:
  name: druid
spec:
  clusterConfig:
    authentication:
      - authenticationClass: ldap-auth
  [...]
```

Check out the [Authentication with OpenLDAP](https://docs.stackable.tech/home/stable/tutorials/authentication_with_openldap/index.md) tutorial to see a complete example of how to set LDAP authentication for another Stackable operator. You can also consult the [AuthenticationClass](https://docs.stackable.tech/home/stable/concepts/authentication/index.md) reference, or [the LDAP test](https://github.com/stackabletech/druid-operator/tree/main/tests/templates/kuttl/ldap-authentication) suite.

### OIDC

Druid supports [authentication](https://docs.stackable.tech/home/stable/concepts/authentication/index.md) of users of the web console against a OIDC provider. This requires setting up a [AuthenticationClass](https://docs.stackable.tech/home/stable/concepts/authentication/index.md) for the OIDC provider:

```yaml
apiVersion: authentication.stackable.tech/v1alpha1
kind: AuthenticationClass
metadata:
  name: oidc-auth
spec:
  provider:
    oidc:
  [...]
```

Reference the AuthenticationClass and a secret containing OIDC client credentials in your DruidCluster resource:

```yaml
apiVersion: druid.stackable.tech/v1alpha1
kind: DruidCluster
metadata:
  name: druid
spec:
  clusterConfig:
    authentication:
      - authenticationClass: oidc-auth
        oidc:
          clientCredentialsSecret: druid-oidc-client
          # Optional: Override the client authentication method (default: client_secret_basic).
          # Supported values: client_secret_basic, client_secret_post, client_secret_jwt, private_key_jwt, none.
          clientAuthenticationMethod: client_secret_basic
  [...]
```

The secret containing the OIDC client credentials should be structured like this:

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: druid-oidc-client
stringData:
  clientId: <client-id>
  clientSecret: <client-secret>
```

#### Client authentication method

The `clientAuthenticationMethod` field controls which method Druid uses when authenticating with the OIDC provider’s token endpoint. Supported values are `client_secret_basic` (default), `client_secret_post`, `client_secret_jwt`, `private_key_jwt`, and `none`.

> **Note:** Since Druid versions `35.x.x` and `36.x.x`, the underlying pac4j library may auto-detect `private_key_jwt` from the OIDC provider’s discovery document, causing authentication to fail. If you encounter this, set `clientAuthenticationMethod` explicitly to `client_secret_post` (or whichever method your provider expects).

### Current Limitations and Upcoming Work

At the moment you can either use TLS, LDAP or OIDC authentication but not a combination of authentication methods.

Druid doesn’t support LDAP authentication **without** bind credentials. See [our issue](https://github.com/stackabletech/druid-operator/issues/383) for details.

Authorization is done using the `allowAll` authorizer or OPA (see below).

## Authorization with Open Policy Agent (OPA)

Druid can connect to an Open Policy Agent (OPA) instance for authorization policy decisions. You need to run an OPA instance to connect to: for this, please refer to the [OPA Operator docs](https://docs.stackable.tech/home/stable/opa/index.md). A short explanation of how to write RegoRules for Druid is given [below](https://docs.stackable.tech/home/stable/druid/usage-guide/security/index.md).

Once you have defined your rules, you need to configure the OPA cluster name and endpoint to use for Druid authorization requests. Add a section to the `spec` for OPA:

```yaml
spec:
  clusterConfig:
    authorization:
      opa:
        configMapName: simple-opa (1)
        package: my-druid-rules (2)
```

(1) The name of your OPA Stacklet (`simple-opa` in this case)
(2) The RegoRule package to use for policy decisions. The package needs to contain an `allow` rule. This is optional and defaults to the name of the Druid Stacklet.

### Defining RegoRules

For a general explanation of how rules are written, please refer to the [OPA documentation](https://www.openpolicyagent.org/docs/latest/#rego). Inside your rule you have access to input from Druid. Druid provides this data to you to base your policy decisions on:

```json
{
  "authenticationResult": {
    "identity": <String>, (1)
    "authorizerName": <String>,
    "authenticatedBy": <String>,
    "context": Map<String, Object>,
  }
  "action": <String: READ|WRITE> (2)
  "resource": {
    "name": <String>, (3)
    "type": <String> (4)
  }
}
```

(1) The authenticated identity of the user that wants to perform the action
(2) The action type, can be either `READ` or `WRITE`.
(3) In case of a datasource this is the table name, for `STATE` this is simply `STATE`, the same for `CONFIG`.
(4) The resource type, one of `STATE`, `CONFIG` and `DATASOURCE`.

For more details consult the [Druid Authentication and Authorization Model](https://druid.apache.org/docs/latest/operations/security-user-auth/#authentication-and-authorization-model).
