> For the complete documentation index, see [llms.txt](https://docs.enginsight.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.enginsight.com/docs/manual/english/installation-and-configuration/installation-and-configuration/enginsight-platform/manual-installation/tls-encryption-for-database.md). Agents can also search and read this documentation through its [MCP server](https://docs.enginsight.com/docs/~gitbook/mcp).

# TLS Encryption for Database

To ensure that communication between the Enginsight Application Server and the database is encrypted, follow these steps:

{% stepper %}
{% step %}

### Stop the Application Server

1. Log in to the **Enginsight Application Server**.
2. Navigate to the Enginsight installation directory using the following command:

```
cd /opt/enginsight/enterprise
```

3. Stop the Application Server using the following command:

```
sudo docker-compose down
```

{% hint style="danger" %}
**Please note:** This step is mandatory before making any changes to the database configuration.
{% endhint %}
{% endstep %}

{% step %}

### Create the configuration file and certificate directory

1. Log in to the **Enginsight Database Server**.
2. Create a directory for the certificate files using the following command:

```
mkdir -p /etc/enginsight/ssl
```

3. Navigate to the directory you just created using the following command:

```
cd /etc/enginsight/ssl
```

4. Create the `csr.cnf` configuration file for a Certificate Signing Request (CSR) using the following command:

```
sudo nano csr.cnf
```

5. Use the following template for the configuration and replace the placeholders in `<>` accordingly:

<pre><code>[req]
default_bits = 4096
default_md = sha256
distinguished_name = req_dn
req_extensions = v3_req
 
[v3_req]
subjectKeyIdentifier = hash
basicConstraints = CA:FALSE
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
 
[alt_names]
<strong>DNS.1 = &#x3C;DomainName>
</strong><strong>IP.1 = &#x3C;DatabaseServerIPAddress>
</strong> 
[req_dn]
countryName = DE
organizationName = Enginsight
<strong>commonName = &#x3C;DomainName>
</strong></code></pre>

6. Save the configuration file (**Ctrl** + **o**) and confirm the save process. Close the file (**Ctrl** + **x**).
   {% endstep %}

{% step %}

### Generate the SSL/TLS certificate

1. Run the following commands to generate an SSL/TLS certificate:

```
if [ ! -f ./passwd ]
then
echo $(openssl rand -base64 16) > ./passwd
fi

passwd=$(cat ./passwd)
openssl genrsa -des3 -out mongodbCA.key -passout pass:$(echo ${passwd}) 4096
openssl req -x509 -new -nodes -key mongodbCA.key -sha256 -days 3650 -subj "/C=DE/ST=CA/O=Enginsight/CN=enginsight.com" -passin pass:$(echo ${passwd}) -out mongodbCA.crt
openssl genrsa -out mongodb.key -passout pass:$(echo ${passwd}) 2048
openssl req -new -sha256 -key mongodb.key -out mongodb.csr -config csr.cnf -subj "/C=DE/O=Enginsight/CN=enginsight.com"
openssl x509 -sha256 -req -days 3650 -passin pass:$(echo ${passwd}) -in mongodb.csr -CA mongodbCA.crt -CAkey mongodbCA.key -CAcreateserial -out mongodb.crt -extfile csr.cnf -extensions v3_req
cat mongodb.key mongodb.crt > mongodb.pem
```

2. Exit the root session using the following command:

```
exit
```

{% endstep %}

{% step %}

### Adjust the MongoDB configuration

You must now configure the generated SSL/TLS certificate in the MongoDB configuration file.

1. Open the `mongod.conf` MongoDB configuration file using the following command:

```
sudo nano /etc/mongod.conf
```

2. Navigate to the `net` section and adjust it as follows:

```
net:
  port: 27017
  bindIp: 0.0.0.0
  tls:
    mode: requireTLS
    certificateKeyFile: /etc/enginsight/ssl/mongodb.pem
    CAFile: /etc/enginsight/ssl/mongodbCA.crt
```

3. Save the changes to the configuration file (**Ctrl** + **o**) and confirm the save process. Close the file (**Ctrl** + **x**).
4. Restart MongoDB using the following command to apply the changes:

```
sudo systemctl restart mongod
```

{% endstep %}

{% step %}

### Check the configuration changes

Now check if the database is available and that TLS encryption is working correctly.

Open the MongoDB shell `mongosh` using the following command:

```
mongosh --tls --host <DatabaseServerIPAddress>:<DatabasePort> --tlsCAFile /etc/enginsight/ssl/mongodbCA.crt --tlsCertificateKeyFile /etc/enginsight/ssl/mongodb.pem
```

{% hint style="info" %}
Remember to replace `<DatabaseServerIPAddress>` and `<DatabasePort>` accordingly.
{% endhint %}
{% endstep %}

{% step %}

### Transfer the certificates to the Application Server (Linux)

{% hint style="warning" %}
**Please note:** The following instructions are intended for Linux systems. If you are using Windows, you can use a tool such as **WinSCP** to transfer the files.
{% endhint %}

1. Navigate back to the **Application Server**.
2. Create a directory for the certificate files using the following command:

```
sudo mkdir -p /opt/enginsight/enterprise/conf/ssl
```

3. Create a temporary directory in the user's home directory to receive the certificate files:

```
cd ~
mkdir db_tls
```

4. Navigate back to the **Database Server**.
5. Temporarily copy the certificate files to the home directory of the default database user using the following command:

```
cd ~
mkdir db_tls
sudo cp /etc/enginsight/ssl/* db_tls/
```

6. Switch to a machine from which you can connect to both the Database Server and the Application Server via **SSH** (Secure Shell).
7. Copy the certificate files **from the Database Server to the Application Server** using the following command:

<pre><code>scp -3 
<strong>&#x3C;DatabaseServerUsername>@&#x3C;DatabaseServerIPAddress>:“db_tls/*“ 
</strong><strong>&#x3C;ApplicationServerUsername>@&#x3C;ApplicationServerIPAddress>:db_tls/
</strong></code></pre>

{% hint style="info" %}
Remember to replace the following placeholders in `<>` accordingly:

* `<DatabaseServerUsername>`
* `<DatabaseServerIPAddress>`
* `<ApplicationServerUsername>`
* `<ApplicationServerIPAddress>`
  {% endhint %}

8. Navigate to the **Database Server**.
9. Delete the temporary directory from the home directory of the default database user using the following command:

```
rm -r ~/db_tls
```

10. Navigate to the **Application Server**.
11. Copy the certificate files from the temporary directory to the actual target directory:

```
sudo cp ~/db_tls/* /opt/enginsight/enterprise/conf/ssl/
```

12. Delete the temporary directory:

```
rm -r ~/db_tls
```

{% endstep %}

{% step %}

### Adjust the Docker configuration file

1. Navigate to the Enginsight installation directory on the **Application Server** using the following command:

```
cd /opt/enginsight/enterprise
```

2. Open the Docker configuration file using the following command:

```
sudo nano docker-compose.yml
```

3. Make sure that the following volume is present and enabled for each container:

```
volumes:
    - "./conf/ssl/:/etc/enginsight/ssl/"
```

{% hint style="warning" %}
**Please note:** In most cases, these lines already exist but are commented out with a **#** character. If so, simply remove the **#** characters to enable them. If the lines do not exist, add them manually.
{% endhint %}

4. Save the changes to the configuration file (**Ctrl** + **o**) and confirm the save process. Close the file (**Ctrl** + **x**).
5. Restart the database service using the following command to apply the configuration changes:

```
sudo systemctl restart mongod
```

{% endstep %}

{% step %}

### Apply the changes on the Application Server

You must now run the Application Server setup script again to apply the configuration changes.

Run the setup script again using the following command and confirm all prompts by pressing **Enter**:

```
sudo ./setup.sh
```

{% endstep %}
{% endstepper %}

***
