Databases
By default the server runs using a SQLite database, but it can be configured to use a choice of databases: SQLite encrypted, PostgreSQL, Maria DB, and MySQL, as well as Redis, using the Redis-with-Findex configuration.
Selecting the database¶
All databases but SQLite and SQLite encrypted can be used in a high-availability setup.
The SQLite database can serve high loads and millions of objects, and is very suitable for scenarios that do not demand high availability. To use SQLIte encrypted, see the SQLite encrypted section.
Redis with Findex¶
Redis with Findex offers the ability to use Redis as a database with application-level encryption: all data is encrypted (using AES 256 GCM) by the KMS servers before being sent to Redis. Findex is a Cosmian cryptographic algorithm used to build encrypted indexes on encrypted data, also stored in Redis. This allows the KMS to perform fast encrypted queries on encrypted data. Redis with Findex offers post-quantum resistance on encrypted data and encrypted indexes.
Redis-with-Findex is most useful when:
- KMS servers are run inside a confidential VM or an enclave. In this case, the secret used to encrypt the Redis data and indexes, is protected by the VM or enclave and cannot be recovered at runtime by inspecting the KMS servers’ memory.
- KMS servers are run by a trusted party but the Redis backend is managed by an untrusted third party.
Redis-with-Findex is the database selected to run the Cosmian KMS in the cloud or any other zero-trust environment.
Configuring the database¶
The database parameters may be configured either:
- the TOML configuration file
- or the arguments passed to the server on the command line.
SQLite¶
This is the default configuration. To use SQLite, no additional configuration is needed.
PostgreSQL¶
Setting up a PostgreSQL database
Before running the server, a dedicated database with a dedicated user should be created on the PostgreSQL instance.
These sample instructions create a database called kms
owned by a user kms_user
with password kms_password
:
-
Connect to psql under user
postgres
-
Create user
kms_user
with passwordkms_password
-
Create database
kms
under ownerkms_user
MySQL or MariaDB¶
Using a certificate to authenticate to MySQL or Maria DB
Use a certificate to authenticate to MySQL or Maria DB with the mysql-user-cert-file
option to
specify the certificate file name.
Docker Example: say the certificate is called cert.p12
and is in a directory called /certificate
on the host disk.
Redis with Findex¶
For Redis with Findex, the --redis-master-password
and --redis-findex-label
options must also be specified:
- the
redis-master-password
is the password from which keys will be derived (using Argon 2) to encrypt the Redis data and indexes. - the
redis-findex-label
is a public arbitrary label that can be changed to rotate the Findex ciphertexts without changing the password/key.
- Redis (with-Findex), use:
SQLite encrypted¶
It requires now to install the Cosmian CLI and create a new encrypted database.
Important: encrypted databases must be created first
Before using an encrypted database, you must create it by either using the Cosmian CLI
or calling the POST /new_database
endpoint.
The call will return a secret
Warning:
- This secret is only displayed once and is not stored anywhere on the server.
- Each call to
new_database
will create a new additional database. It will not return the secret of the last created database, and it will not overwrite the last created database.
Once an encrypted database is created, the secret must be passed in every subsequent query to the KMS server. Passing the correct secret “auto-selects” the correct encrypted database: multiple encrypted databases can be used concurrently on the same KMS server.
The secret must be set in database_secret
property of the CLI cosmian.json
configuration file,
and it will be used for all subsequent calls to the KMS server.
The secret must be passed using a DatabaseSecret
HTTP header, e.g.
Clearing the database¶
The KMS server can be configured to automatically clear the database on restart.
Warning: this operation is irreversible
The cleanup operation will delete all objects and keys stored in the database.
Database migration¶
Depending on the KMS database evolution, a migration can happen between 2 versions of the KMS server. It will be clearly written in the CHANGELOG.md. In that case, a generic database upgrade mechanism is run on startup.
At first, the table context
is responsible for storing the version of the software run and the state of the database.
The state can be one of the following:
ready
: the database is ready to be usedupgrading
: the database is being upgraded
On startup, the server checks if the software version is greater than the last version run:
- if no, it simply starts;
-
if yes:
- it looks for all upgrades to apply in order from the last version run to this version;
- if there is any to run, it sets an upgrading flag on the db state field in the context table;
- it runs all the upgrades in order;
- it sets the flag from upgrading to ready;
On every call to the database, a check is performed on the db state field to check if the database is upgrading. If yes, calls fail.
Upgrades resist to being interrupted in the middle and resumed from start if that happens.