|
| 1 | +--- |
| 2 | +pcx_content_type: concept |
| 3 | +title: Custom CA and client certificates |
| 4 | +sidebar: |
| 5 | + order: 6 |
| 6 | + badge: |
| 7 | + text: Beta |
| 8 | +--- |
| 9 | + |
| 10 | +import { TabItem, Tabs, Render, WranglerConfig } from "~/components"; |
| 11 | + |
| 12 | +By default, all Hyperdrive configurations are encrypted with SSL/TLS (`sslmode=require`). Databases commonly support 2 additional [SSL modes](https://www.postgresql.org/docs/current/libpq-ssl.html), |
| 13 | +(`verify-ca` and `verify-full`) which allow for a more stringent security between Hyperdrive and the database server. |
| 14 | + |
| 15 | +When using `verify-ca`, Hyperdrive will verify the server certificate when creating a connection, also known as [client verification of server certificates](https://www.postgresql.org/docs/current/libpq-ssl.html#LIBQ-SSL-CERTIFICATES). |
| 16 | +This will configure Hyperdrive to verify that the certificate provided by the database server in the SSL/TLS handshake matches the certificate authority (CA) root certificate. |
| 17 | +This provides an additional security check to prevent man-in-the-middle attacks. |
| 18 | + |
| 19 | +You can also configure Hyperdrive to use `verify-full`. |
| 20 | +In this case, Hyperdrive will do the [client verification of server certificates](https://www.postgresql.org/docs/current/libpq-ssl.html#LIBQ-SSL-CERTIFICATES) and |
| 21 | +provide certificates during the SSL/TLS handshake to allow [server verification of client certificates](https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-CLIENTCERT). |
| 22 | +This is a feature of databases that allow them to use client certificates as an additional factor to authenticate clients (in addition to the username and password). |
| 23 | + |
| 24 | +The table below explains the various SSL modes of SQL databases: |
| 25 | + |
| 26 | +| SSL Mode | Password authentication | Encrypted connections | Server certificate verification[^1] | Client certificates verification[^2] | |
| 27 | +| ------------- | ----------------------- | ----------------------- | ------------------------------------------ | ------------------------------------------ | |
| 28 | +| `require` (default) | ✅ | ✅ | ❌ | ❌ | |
| 29 | +| `verify-ca` | ✅ | ✅ | ✅ | ❌ | |
| 30 | +| `verify-full` | ✅ | ✅ | ✅ | ✅ | |
| 31 | + |
| 32 | +[^1]: Hyperdrive will verify that the certificates provided by the server have been signed by a certificate authority. This can help prevent man-in-the-middle attacks. |
| 33 | +[^2]: The database is configured to verify both the username/password of Hyperdrive and a client certificate. This provides a second factor for authentication. |
| 34 | + |
| 35 | +## Prerequisites |
| 36 | + |
| 37 | +- An external database with SSL/TLS enabled. |
| 38 | +- The root certificate authority (CA) certificate with which the SSL/TLS certificate of the database has been signed with (for `verify-ca` and `verify-full`). |
| 39 | +- If using `verify-full`, you will need client certificates that have been signed by the same certificate authority (CA) as expected by the database server. |
| 40 | + |
| 41 | +## Client verification of server certificates |
| 42 | + |
| 43 | +Client verification of server certificates means that Hyperdrive will verify that the |
| 44 | +certificate provided by the database server in the [SSL/TLS handshake](https://www.postgresql.org/docs/current/ssl-tcp.html) |
| 45 | +matches the root certificate authority (CA) certificate. |
| 46 | + |
| 47 | +To do this, you must provide Hyperdrive with the certificate of the root certificate authority (CA) or an immediate certificate which |
| 48 | +has been used to sign the certificate of your database. |
| 49 | + |
| 50 | +### Step 1: Upload your the root certificate authority (CA) certificate |
| 51 | + |
| 52 | +Using Wrangler, you can upload your root certificate authority (CA) certificate: |
| 53 | + |
| 54 | +```bash |
| 55 | +npx wrangler cert upload certificate-authority --ca-cert \<ROUTE_TO_CA_PEM_FILE\>.pem --name \<CUSTOM_NAME_FOR_CA_CERT\> |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +Uploading CA Certificate tmp-cert... |
| 60 | +Success! Uploaded CA Certificate \<CUSTOM_NAME_FOR_CA_CERT\> |
| 61 | +ID: \<YOUR_ID_FOR_THE_CA_CERTIFICATE\> |
| 62 | +... |
| 63 | +``` |
| 64 | + |
| 65 | +### Step 2: Create your Hyperdrive configuration using the CA certificate |
| 66 | + |
| 67 | +Once your CA certificate has been created, you can create a Hyperdrive configuration with the newly created |
| 68 | +certificates using either the dashboard or Wrangler. |
| 69 | +Using Wrangler, enter the following command in your terminal: |
| 70 | + |
| 71 | +```bash |
| 72 | +npx wrangler hyperdrive create \<NAME_OF_HYPERDRIVE_CONFIG\> --connection-string="postgres://user:password@HOSTNAME_OR_IP_ADDRESS:PORT/database_name" --certificate-authority-id \<YOUR_CA_CERT_ID\> |
| 73 | +``` |
| 74 | + |
| 75 | +When creating the Hyperdrive configuration, Hyperdrive will attempt to connect to the database with the |
| 76 | +provided credentials. If the command provides successful results, you have properly configured your Hyperdrive |
| 77 | +configuration to verify the certificates provided by your database server. |
| 78 | + |
| 79 | +:::note |
| 80 | + |
| 81 | +Hyperdrive will attempt to connect to your database with the provided credentials to verify they are correct before creating a configuration. If you encounter an error when attempting to connect, refer to Hyperdrive's [troubleshooting documentation](/hyperdrive/observability/troubleshooting/) to debug possible causes. |
| 82 | + |
| 83 | +::: |
| 84 | + |
| 85 | +## Server verification of client certificates |
| 86 | + |
| 87 | +For the database server to be able to verify the client certificates, Hyperdrive must be configured to provide a certificate |
| 88 | +file (`client-cert.pem`) and a private key with which the certificate was generated (`private-key.pem`). |
| 89 | + |
| 90 | +### Step 1: Upload your client certificates (mTLS certificates) |
| 91 | + |
| 92 | +Upload your client certificates to be used by Hyperdrive using Wrangler: |
| 93 | + |
| 94 | +```bash |
| 95 | +npx wrangler cert upload mtls-certificate --cert client-cert.pem --key client-key.pem --name <CUSTOM_NAME_FOR_CLIENT_CERTIFICATE> |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +Uploading client certificate <CUSTOM_NAME_FOR_CLIENT_CERTIFICATE>... |
| 100 | +Success! Uploaded client certificate <CUSTOM_NAME_FOR_CLIENT_CERTIFICATE> |
| 101 | +ID: <YOUR_ID_FOR_THE_CLIENT_CERTIFICATE_PAIR> |
| 102 | +... |
| 103 | +``` |
| 104 | + |
| 105 | +### Step 2: Create a Hyperdrive configuration |
| 106 | + |
| 107 | +You can now create a Hyperdrive configuration using the newly created client certificate bundle using the dashboard or Wrangler. |
| 108 | +Using Wrangler, run the following command: |
| 109 | + |
| 110 | +```bash |
| 111 | +npx wrangler hyperdrive create <NAME_OF_HYPERDRIVE_CONFIG> --connection-string="postgres://user:password@HOSTNAME_OR_IP_ADDRESS:PORT/database_name" --certificate-authority-id <YOUR_CA_CERT_ID> --mtls-certificate-uuid <YOUR_CLIENT_CERT_PAIR_ID> |
| 112 | +``` |
0 commit comments