|
| 1 | +--- |
| 2 | +title: SSH Tunnel |
| 3 | +slug: ssh-tunnel |
| 4 | +--- |
| 5 | + |
| 6 | +SSH tunnels provide a secure way to connect to your database by routing traffic through an intermediary SSH server. This ensures that sensitive data is encrypted and protected during transit. |
| 7 | + |
| 8 | +It's an easy way to connect QueryCanary to your production database servers, without exposing them to the public web. |
| 9 | + |
| 10 | +### What is an SSH Tunnel? |
| 11 | + |
| 12 | +An SSH tunnel creates a secure connection between QueryCanary.com's runner machines and your remote server. It forwards a local port to a target database port on the remote server, allowing you to securely access the database as if it were running locally. |
| 13 | + |
| 14 | +### When Should You Use an SSH Tunnel? |
| 15 | + |
| 16 | +- **Secure Connections**: When your database is not directly accessible over the internet. |
| 17 | +- **Firewall Restrictions**: When the database is only accessible from a specific SSH server. |
| 18 | +- **Extra Security**: To encrypt database traffic and prevent unauthorized access. |
| 19 | + |
| 20 | +### Configuration |
| 21 | + |
| 22 | +#### 1. **Gather Required Information** |
| 23 | + |
| 24 | +Before setting up an SSH tunnel, ensure you have the following details: |
| 25 | + |
| 26 | +- **SSH Server Information**: |
| 27 | + |
| 28 | + - Hostname or IP address of the SSH server. |
| 29 | + - Port number (default is `22`). |
| 30 | + - Username for SSH authentication. |
| 31 | + |
| 32 | +- **Database Information**: |
| 33 | + |
| 34 | + - Hostname or IP address of the database (from the SSH server's perspective). |
| 35 | + - Port number (e.g., `5432` for PostgreSQL, `3306` for MySQL). |
| 36 | + |
| 37 | +#### 2. **Enable SSH Tunnel in QueryCanary** |
| 38 | + |
| 39 | +When adding or editing a server in QueryCanary, enable the SSH tunnel and provide the required details: |
| 40 | + |
| 41 | +| Field Name | Description | |
| 42 | +| --- | --- | |
| 43 | +| **SSH Hostname** | The hostname or IP address of the SSH server. | |
| 44 | +| **SSH Port** | The port of the SSH server (default: `22`). | |
| 45 | +| **SSH Username** | The username to authenticate with the SSH server. | |
| 46 | + |
| 47 | +We'll save those details and provide you a SSH public key to add to your server. |
| 48 | + |
| 49 | +Here's a configuration example: |
| 50 | + |
| 51 | +- **SSH Server**: |
| 52 | + |
| 53 | + - Hostname: `ssh.example.com` |
| 54 | + - Port: `22` |
| 55 | + - Username: `ssh_user` |
| 56 | + |
| 57 | +#### 3. Add the generated public key to your server |
| 58 | +QueryCanary will generate each server a unique SSH public key to authorize it's connection to your SSH server. This makes it easy to track & manage the permissions QueryCanary has, which should be extremely limited. |
| 59 | + |
| 60 | +On your SSH server: |
| 61 | +```bash |
| 62 | +# Create the SSH folder if necessary |
| 63 | +mkdir -p ~/.ssh |
| 64 | +chmod 700 ~/.ssh |
| 65 | +touch ~/.ssh/authorized_keys |
| 66 | +chmod 600 ~/.ssh/authorized_keys |
| 67 | + |
| 68 | +# Edit the authorized keys file to add our public key |
| 69 | +nano ~/.ssh/authorized_keys |
| 70 | +``` |
| 71 | +Add the QueryCanary SSH key, it should look something like this: |
| 72 | +```bash |
| 73 | +ecdsa-sha2-nistp256 SoMElOnGVALue== querycanary.com |
| 74 | +``` |
| 75 | + |
| 76 | +You can then test the connection to ensure it's properly setup. |
| 77 | + |
| 78 | +### Troubleshooting Common Issues |
| 79 | + |
| 80 | +1. **Failed to Connect to SSH Server**: |
| 81 | + |
| 82 | + - Verify the SSH hostname, port, and username. |
| 83 | + - Ensure the SSH server is reachable from the QueryCanary server. |
| 84 | + |
| 85 | +2. **Invalid SSH Key**: |
| 86 | + |
| 87 | + - Verify that your servers uniquely generated public key is added to the SSH server's `authorized_keys` file. |
| 88 | + |
| 89 | +3. **Database Connection Issues**: |
| 90 | + |
| 91 | + - Ensure the database hostname and port are correct from the SSH server's perspective. |
| 92 | + - Verify that the database allows connections from the SSH server. |
| 93 | + |
| 94 | +By following this guide, you can securely connect to your database using SSH tunnels in QueryCanary. |
0 commit comments