> Source: https://docs.cyberelements.io/bastion/4.7/sv/Usage/Reference/ConfigurationFiles/MediationController/DatabaseSettings/

# databasesettings.ini

`databasesettings.ini` holds the connection of the Mediation Controller to its main database. The connections to the database of each organisation and to the database of its vault each have their own file in `/etc/ipdiva/care/databases.d/`, which the product reads together with this one. It is the file you look at when a connection fails, when you set up a cluster, and when you restore a backup.

| Property | Value |
|---|---|
| **Component** | Mediation Controller |
| **Path** | `/etc/ipdiva/care/databasesettings.ini` |
| **Files per database** | `/etc/ipdiva/care/databases.d/<section>.ini`, one per organisation and one per vault of an organisation |
| **Owner and permissions** | `www-data:ipdiva-synchro`, `660` |
| **Package** | none, written at installation |
| **Format** | INI file: sections in brackets, one `key = value` per line |

> **The file holds passwords**
>
> Each connection carries the password of its database user, in clear text. Its permissions are what protects it: do not widen them, do not copy the file outside the server, and do not paste its content into a ticket.

## Structure

One section per connection. The section `[database]` of this file is the main connection. **Every other connection is a section of the same form, in its own file of `databases.d/`**: the section of an organisation bears the name of that organisation (`databases.d/<organization>.ini`), the section of its vault is named `basic_vault.<organization>` (`databases.d/basic_vault.<organization>.ini`).

Example of content, without the values that are yours:

```
1
2
3
4
5
6
7
```

> **Organisations and vaults have their own files**
>
> The product reads this file, then every `*.ini` file of `/etc/ipdiva/care/databases.d/`, in alphabetical order, and merges them. A connection written there behaves exactly as if it were in this file. Those files are written by the console when an organisation is created or when the database of its vault is changed: do not edit them by hand.

## Keys of a connection

| Key | Role |
|---|---|
| `ENGINE` | The database back end: `django.db.backends.postgresql` for PostgreSQL, `mssql` for Microsoft SQL Server. Any other value is refused: the consoles do not start, and a system backup carrying one is not restored with its connection settings. |
| `NAME` | Name of the database. For an organisation, it is the name of the organisation. |
| `USER`, `PASSWORD` | The account used to connect. On an organisation section, both are read only if `USER` is present. |
| `HOST`, `PORT` | Address and port of the server. |
| `sslmode` | TLS mode of the connection. See [TLS modes](#tls-modes) below. |
| `ca` | **Absolute path** of the certificate of the authority used to verify the server, a file in **PEM** format. PostgreSQL only, and only with `verify-full` or `verify-ca`. The console copies the authority chosen at creation to `/var/lib/ipdiva/care/pg_cas/<organization>.pem` and writes that path here. |
| `REAL_HOST` | Name expected in the certificate of the server, when `HOST` is an IP address. The connection then goes to the address of `HOST` while checking the name of `REAL_HOST`. |
| `DRIVER` | ODBC driver, for a SQL Server database: `ODBC Driver 18 for SQL Server`, or `ODBC Driver 17 for SQL Server` when version 18 is not installed. The console writes the one it finds on the server. |
| `gateway` | Names of the Edge Gateways through which the connection is tunnelled, when the database is not reachable directly, separated by commas. On the `[database]` section, they are gateways of the `cleanroom_infra` virtual organisation: see [Main database through Edge Gateways](#main-database-through-edge-gateways). |
| `gateway_org` | Organisation section only: organisation the Edge Gateways of `gateway` belong to. Defaults to `cleanroom_infra`. |

> **A missing key stops the consoles, and the message is short**
>
> `ENGINE`, `NAME`, `USER`, `PASSWORD`, `HOST` and `PORT` are read without a default on the `[database]` section: one of them missing leaves the product without any database at all. Check the file after editing it.

### TLS modes

For a **PostgreSQL** database, the console offers three values when the organisation is created:

| Value | Label in the console | Effect |
|---|---|---|
| `verify-full` | **Verify server certificate and its name (sslmode verify-full)** | The certificate of the server is verified, and so is the name it carries. |
| `verify-ca` | **Verify server certificate (sslmode verify-ca)** | The certificate of the server is verified, not the name. |
| `prefer` | **Use SSL if available whithout verifying the certificate (sslmode prefer) (insecure)** | TLS is used when the server offers it, without any verification. |

With `verify-full` or `verify-ca`, the console also asks for the PKI and the certification authority that `ca` will point to. When the key is absent, the value used is `prefer`.

For a **Microsoft SQL Server** database, the console asks for neither the TLS mode nor the authority, and writes no `sslmode` key: the value used is then `verify-full`.

> **`prefer` encrypts without authenticating**
>
> The connection is encrypted, but nothing guarantees that the server answering is yours: the console labels this mode insecure. Keep it for a test platform.

## Main database through Edge Gateways

The main database (`[database]` section) can be reached through one or more Edge Gateways, when the Mediation Controller has no direct access to it, without a dedicated Gate organisation. These Edge Gateways belong to `cleanroom_infra`, a virtual organisation reserved for the infrastructure of the platform: no organisation can bear that name.

On each new connection to the database, the Mediation Controller opens a tunnel through one of the Edge Gateways named by `gateway` that is connected, picked at random. If none is connected, the connection fails: the product never tries to reach the database directly. With PostgreSQL, the certificate of the server is still checked against its real name (`HOST`), not the tunnel address.

### Setting up

1. **Make the SSL Router operational.** An Edge Gateway only connects to an SSL Router that has its licence and certificates, and those are installed from the system console, which requires a reachable main database. Start therefore with a **local** main database (the PostgreSQL database installed by default on the Mediation Controller), install the licence, the certificates and, if needed, the PKIs, and check that the SSL Router starts.
2. **Prepare the certificate of each Edge Gateway.** Its name (CN) must be `<name>@cleanroom_infra`, where `<name>` is the name chosen for the Edge Gateway. The certificate and its key are provided in a PKCS#12 file (`.p12`).
3. **Generate the pairing token**, on the Mediation Controller, as `root`:

   ```
   1
   ```

   The script checks the name of the certificate, copies the file into `/etc/ipdiva/care/defaultgw/`, writes the Edge Gateway into `/etc/ipdiva/care/defaultgateway.ini`, registers it with the SSL router and prints the token. It refuses a name already present; `--force` then replaces the certificate and generates a new token. The token remains valid until it is replaced.
4. **Pair the Edge Gateway** with that token, with the [`--cleanroom-infra`](https://docs.cyberelements.io/bastion/4.7/sv/Usage/Configuration/EdgeGateway/ConnectAnEdgeGatewayWithPairingToken/#cleanroom-infra) option of the pairing script:

   ```
   1
   ```
5. **Name the Edge Gateways** in the `[database]` section, which now targets the real database:

   ```
   1
   2
   3
   4
   5
   6
   7
   8
   ```
6. **Restart the web server of the Mediation Controller**:

   ```
   1
   ```

## In a cluster

Two things change.

**The file and the `databases.d/` directory are synchronised between the nodes.** The first line of the delivered model says it: every address written here must be reachable **from both servers**, and every host name must resolve on both. An address that is valid on one node only works until the day the other one takes over.

**An organisation can name a different server per node.** On a section of organisation, the product reads `HOST_MASTER` and `PORT_MASTER` on the primary node, `HOST_SLAVE` and `PORT_SLAVE` on the secondary one, and falls back on `HOST` and `PORT` when they are absent. This is what lets each node reach the closest instance of a replicated database.

| Node | Keys read | Fallback |
|---|---|---|
| primary | `HOST_MASTER`, `PORT_MASTER` | `HOST`, `PORT` |
| secondary | `HOST_SLAVE`, `PORT_SLAVE` | `HOST`, `PORT` |
| Standalone | `HOST`, `PORT` | — |

## Backup and restoration

The file is part of the backup, together with the `databases.d/` directory: both are added to the archive, and the restoration **refuses an archive that does not hold the file**. The system console offers to restore them or not, with the option **Restore database connection settings (databasesettings.ini)**. When it is ticked, the restoration first copies the current settings to `/var/lib/ipdiva/care/backup/dbsettings-pre-restore/`; the sections of an archive without `databases.d/` (backup of an earlier version) are written to one file per database in `databases.d/`.

> **Restoring the file restores the addresses it held**
>
> Keep the option unticked when you restore onto a platform whose database servers are not the ones of the backup (a restoration on a new infrastructure, typically). Otherwise the restored file points at the former servers, and the consoles come back on a platform they cannot reach.

## Applying a change

The consoles read the file when they start. Renewing their processes is enough, and does not interrupt the service:

```
1
```

## Upgrade

An upgrade does not replace this file, but moves its connections to `databases.d/`. When the package is configured, `/usr/local/ipdiva/bin/migratedatabasesettings.py` writes each section other than `[database]` (organisations and vaults) to `/etc/ipdiva/care/databases.d/<section>.ini` (`www-data:ipdiva-synchro`, `660`), then leaves only `[database]` in this file. It first keeps a copy of the file as `databasesettings.ini.pre-migration`. A section whose file already exists in `databases.d/` is not moved and stays in this file. On a file that holds only `[database]`, the script changes nothing.
