TLS Connections

It is possible to encrypt connections between FerretDB and clients by using TLS. All you need to do is to start the server with the following flags or environment variables:

  • --listen-tls / FERRETDB_LISTEN_TLS specifies the TCP hostname and port that will be used for listening for incoming TLS connections. If empty, TLS listener is disabled;
  • --listen-tls-cert-file / FERRETDB_LISTEN_TLS_CERT_FILE specifies the PEM encoded, TLS certificate file that will be presented to clients;
  • --listen-tls-key-file / FERRETDB_LISTEN_TLS_KEY_FILE specifies the TLS private key file that will be used to decrypt communications;
  • --listen-tls-ca-file / FERRETDB_LISTEN_TLS_CA_FILE specifies the root CA certificate file that will be used to verify client certificates.

Then use tls query parameters in MongoDB URI for the client. You may also need to set tlsCAFile parameter if the system-wide certificate authority did not issue the server’s certificate. See documentation for your client or driver for more details. Example: mongodb://ferretdb:27018/?tls=true&tlsCAFile=companyRootCA.pem.

PostgreSQL backend with TLS

Using TLS is recommended if username and password are transferred in plain text.

In following examples, FerretDB uses TLS certificates to secure the connection. Example certificates are found in build/certs. The ferretdb server uses TLS server certificate file, TLS private key file and root CA certificate file.

  1. server-certs/
  2. ├── rootCA-cert.pem
  3. ├── server-cert.pem
  4. └── server-key.pem

The client uses TLS client certificate file and root CA certificate file.

  1. client-certs/
  2. ├── client.pem
  3. └── rootCA-cert.pem

Using TLS with ferretdb package

The example below connects to localhost PostgreSQL instance using TLS with certificates in server-certs directory. Be sure to check that server-certs directory and files are present.

  1. ferretdb \
  2. --postgresql-url=postgres://localhost:5432/ferretdb \
  3. --listen-tls=:27018 \
  4. --listen-tls-cert-file=./server-certs/server-cert.pem \
  5. --listen-tls-key-file=./server-certs/server-key.pem \
  6. --listen-tls-ca-file=./server-certs/rootCA-cert.pem

Using mongosh, a client connects to ferretdb as user2 using TLS certificates in client-certs directory. Be sure to check that client-certs directory and files are present.

  1. mongosh 'mongodb://user2:pass2@127.0.0.1:27018/ferretdb?authMechanism=PLAIN&tls=true&tlsCertificateKeyFile=./client-certs/client.pem&tlsCaFile=./client-certs/rootCA-cert.pem'

Using TLS with Docker

For using Docker to run ferretdb server, docker-compose.yml example for TLS is provided in below. The Docker host requires certificates server-certs directory, and volume is mounted from ./server-certs of Docker host to /etc/certs of Docker container.

  1. services:
  2. postgres:
  3. image: postgres
  4. restart: on-failure
  5. environment:
  6. - POSTGRES_USER=username
  7. - POSTGRES_PASSWORD=password
  8. - POSTGRES_DB=ferretdb
  9. volumes:
  10. - ./data:/var/lib/postgresql/data
  11. ferretdb:
  12. image: ghcr.io/ferretdb/ferretdb
  13. restart: on-failure
  14. ports:
  15. - 27018:27018
  16. environment:
  17. - FERRETDB_POSTGRESQL_URL=postgres://postgres:5432/ferretdb
  18. - FERRETDB_LISTEN_TLS=:27018
  19. - FERRETDB_LISTEN_TLS_CERT_FILE=/etc/certs/server-cert.pem
  20. - FERRETDB_LISTEN_TLS_KEY_FILE=/etc/certs/server-key.pem
  21. - FERRETDB_LISTEN_TLS_CA_FILE=/etc/certs/rootCA-cert.pem
  22. volumes:
  23. - ./server-certs:/etc/certs
  24. networks:
  25. default:
  26. name: ferretdb

To start ferretdb, use docker compose.

  1. docker compose up

In the following example, a client connects to MongoDB URI using TLS certificates as user2. It uses Docker volume to mount ./clients-certs of Docker host to /clients Docker container.

  1. docker run --rm -it \
  2. --network=ferretdb \
  3. --volume ./client-certs:/clients \
  4. --entrypoint=mongosh \
  5. mongo 'mongodb://user2:pass2@host.docker.internal:27018/ferretdb?authMechanism=PLAIN&tls=true&tlsCertificateKeyFile=/clients/client.pem&tlsCaFile=/clients/rootCA-cert.pem'