> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-users.md).

# Cennso Database User

A Cennso Database User is represented by the `DBUser` Custom Resource (CR). Applying this CR to a Management Cluster creates a Kubernetes Job that provisions a MySQL user account on an existing [Cennso Database Instance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md) and applies the requested `GRANT` statements.

> [!IMPORTANT]
> Before provisioning a user account, the target `DBInstance` must be deployed, and its HAProxy Kubernetes Service must be reachable from the provisioning Job. The Job connects to the MySQL cluster through that Service and authenticates with the root credentials stored in the `ps-db-<dbInstanceName>-secrets` Kubernetes Secret.

The sections below provide information about configuration parameters that you can specify in the `DBUser` CR. For more information on how to provision a new user account with privileges, see the [Create a database user](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/users-creation.md) document.

## Kubernetes cluster secret reference

To deploy the Cennso Database User, you must specify a reference to the [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/) so the Core Cluster is accessible. You can do this using the following configuration parameters:

```yaml
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: {CORE-CLUSTER-SECRET-NAMESPACE}
```

Set **spec.connectionSecretRef.name** to the name of the Core Cluster that contains the MySQL cluster where the user account should be created. The **spec.connectionSecretRef.namespace** configuration parameter defines the Kubernetes Namespace on the Management Cluster where the Kubernetes secret to access this Core Cluster is stored.

## Target Database Instance reference

To select the MySQL cluster where the user account is created, you must specify the `DBInstance` name and the Kubernetes Namespace on the Core Cluster where it is deployed:

```yaml
spec:
  namespace: {MYSQL-CLUSTER-NAMESPACE}
  dbInstanceName: {DB-INSTANCE-NAME}
```

The **spec.dbInstanceName** must match the **metadata.name** of an existing `DBInstance` claim. The user-provisioning Job connects to the `ps-db-<dbInstanceName>-haproxy.<namespace>.svc:3306` Kubernetes Service and uses the credentials from the `ps-db-<dbInstanceName>-secrets` Kubernetes Secret.

## User identity

To define the MySQL user account that should be created, use the following configuration parameters:

```yaml
spec:
  username: {USERNAME}
  host: "%"
  authPlugin: mysql_native_password
```

The **spec.username** must be a valid MySQL identifier (alphanumeric characters and underscore, up to 32 characters).

The **spec.host** configures the host part of the MySQL account identity. Typical values are `%`, `localhost`, or a CIDR-style pattern such as `10.0.0.%`. The default value is `%`.

The **spec.authPlugin** configures the [MySQL authentication plugin](https://dev.mysql.com/doc/refman/8.0/en/authentication-plugins.html) used when creating the account. The supported values are: 

- `mysql_native_password`
- `caching_sha2_password` 

The default value is `mysql_native_password`.

## Password secret reference

> [!IMPORTANT]
> The Kubernetes secret with the user's password must be provisioned before you apply the `DBUser` Crossplane claim on your Management Cluster.

The password for the new MySQL user account is read from a Kubernetes Secret on the Core Cluster, in the same Kubernetes Namespace as the provisioning Job. You can configure it using the following parameters:

```yaml
spec:
  passwordSecretRef:
    name: {PASSWORD-SECRET-NAME}
    key: password
```

The **spec.passwordSecretRef.name** is the name of the Kubernetes Secret that holds the user password. The **spec.passwordSecretRef.key** is the key within the Secret whose value is used as the password. The default value of **spec.passwordSecretRef.key** is `password`.

## Grants

To configure the privileges of the new MySQL user account, you can use the **spec.grants** configuration option. Each item in the list produces a [GRANT](https://dev.mysql.com/doc/refman/8.0/en/grant.html) statement that applies the listed privileges to the specified object:

```yaml
spec:
  grants:
    - privileges:
        - ALL PRIVILEGES
      target: "mydb.*"
    - privileges:
        - REPLICATION CLIENT
        - REPLICATION SLAVE
      target: "*.*"
```

The **spec.grants[].privileges** is the list of MySQL privileges to grant, such as `ALL PRIVILEGES`, `SELECT`, or `REPLICATION CLIENT`. The **spec.grants[].target** is the MySQL object specification for the GRANT, such as `mydb.*` or `*.*`. For the full list of supported privileges, see the [MySQL privileges documentation](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html).

If **spec.grants** is empty or omitted, the user account is created without any additional privileges.

## Job retries, lifetime, and deadline

To control how the user-provisioning [Kubernetes Job](https://kubernetes.io/docs/concepts/workloads/controllers/job/) behaves on failure and after completion, use the following configuration parameters:

```yaml
spec:
  backoffLimit: 1
  ttlSecondsAfterFinished: 3600
  activeDeadlineSeconds: 600
  restartPolicy: OnFailure
```

The **spec.backoffLimit** configures the number of retries before the Job is marked as failed. For more information, see the [pod backoff failure policy](https://kubernetes.io/docs/concepts/workloads/controllers/job/#pod-backoff-failure-policy) documentation.

The **spec.ttlSecondsAfterFinished** configures how long a finished Job (either `Complete` or `Failed`) is retained before it is automatically cleaned up. If omitted, the Job is not deleted automatically. For more information, see the [TTL mechanism for finished Jobs](https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/) documentation.

The **spec.activeDeadlineSeconds** configures a wall-clock deadline, measured from the Job start time, after which the Job is terminated and marked as failed. The default value is `600` seconds.

The **spec.restartPolicy** configures the restart policy of the Job Pod. The supported values are `OnFailure` and `Never`. The default value is `OnFailure`. For more information, see the [pod lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy) documentation.

## Image and pull secret

You can configure the image of the user-provisioning Job and the Kubernetes Secret used to pull it with the following configuration options:

```yaml
spec:
  image:
    registry: registry.cennso.com/cennso/mysql-server-client
    tag: v8.0.0
  imagePullSecretRef:
    name: cennso-pull-secret
```

The **spec.imagePullSecretRef.name** refers to a Kubernetes Secret that must exist on the Core Cluster in the same Kubernetes Namespace as the Job.
