> 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/operation-guides/certificates.md).

# Manage TLS certificates

> [!IMPORTANT]
> Any update or renewal of the TLS certificates leads to the graceful restart of the MySQL database cluster, starting from the secondary instances and ending with the primary.

By default, each MySQL cluster has pre-configured TLS certificates and keys. The Cennso Database extension provides two ways to obtain these certificates:

- Automatic TLS certificates (default) – Cennso Database Operator automatically creates a [Certificate](https://cert-manager.io/docs/usage/certificate/) Custom Resource (managed by [cert-manager](https://cert-manager.io/)) for every cluster deployed with a [DBInstance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md) Custom Resource. These certificates are valid for `90` days.
- Customised TLS certificates – if you need TLS certificates with a custom validity period, use the [DBTLSCertificate](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-certificate.md) Custom Resource. In this case, in your `DBInstance` claim, you must set the **spec.tlsSecretName** configuration option to an empty string, or skip this configuration parameter.

> [!NOTE]
> If you want to create a custom TLS certificate for the new database, you must install the `DBTLSCertificate` before the database deployment, and configure your MySQL cluster to use the Kubernetes Secret with TLS certificates generated by it. For more information on how to do it, read the [Deploy TLS certificates for the MySQL cluster](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-database.md#deploy-tls-certificates-for-the-mysql-cluster) section.

This document describes how to update the TLS certificates in the already existing MySQL cluster deployment.

## Create TLS certificates for existing database

To create the custom TLS certificates for your MySQL cluster, use the `DBTLSCertificate` Custom Resource. After you install the `DBTLSCertificate` Custom Resource in your Management Cluster, it triggers the generation of new TLS certificates and keys in your Core Cluster. The newly generated TLS certificates and keys are stored in a Kubernetes Secret. The secret is located in the Kubernetes Namespace specified by the **spec.namespace** configuration option of the `DBTLSCertificate` CR. The name of the generated secret is `cennso-db-{MYSQL-CLUSTER-TLS-CERTIFICATE}-ssl` where the `MYSQL-CLUSTER-TLS-CERTIFICATE` is the value of the **metadata.name** from the `DBTLSCertificate`.

Since the secret with the TLS certificates and keys was successfully created, you should put this secret name in the **spec.tlsSecretName** of your `DBInstance` claim and deploy the MySQL cluster using it.

For example, using the following YAML manifest, you can deploy the TLS certificates to the `database` namespace of the `database-cluster` Core Cluster:

```yaml
apiVersion: db.contrib.cennso.com/v1alpha1
kind: DBTLSCertificate
metadata:
  name: tls-certificates
  namespace: cennso-infra
spec:
  connectionSecretRef:
    name: database-cluster
    namespace: cennso-infra
  namespace: database
  duration: 26280h # 3 years
  renewBefore: 24h
```

Use the following command to apply the Custom Resource to your Core Cluster:

```bash
kubectl apply -f database-tls-certificates.yaml -n cennso-infra
```

This generates TLS certificates and stores them in the `database` namespace with the name `cennso-db-tls-certificates-ssl`.

To use these TLS certificates with the already deployed MySQL cluster, update the `DBInstance` Custom Resource which you used to deploy your MySQL cluster. To do this, open the file with the `DBInstance` Custom Resource that you used to deploy your MySQL cluster and update or set the **spec.tlsSecretName**. For example:

```yaml
apiVersion: db.contrib.cennso.com/v1alpha1
kind: DBInstance
metadata:
  name: database-cluster
  namespace: cennso-infra
spec:
  tlsSecretName: cennso-db-tls-certificates-ssl
  connectionSecretRef:
    name: database-cluster
    namespace: cennso-infra
  namespace: database
  storage:
    capacity: 20Gi
    className: mysql-cluster-storage
  instances:
    0:
      storage:
        node: {NODE-0-NAME}
        path: /opt/local-path-provisioner/mysql-cluster/mysql-instance-0
    1:
      storage:
        node: {NODE-1-NAME}
        path: /opt/local-path-provisioner/mysql-cluster/mysql-instance-1
```

After this, apply the updated claim to the Management Cluster, so the MySQL cluster is updated with the new TLS certificates. Use the following command to apply the updated `DBInstance` Custom Resource on your Management Cluster:

```bash
kubectl apply -f database-instance.yaml -n cennso-infra
```
