Skip to content

Commit 54e7cfe

Browse files
committed
Enable connecting to PG within private VPCs using a VPN tunnel
1 parent b1cbd0f commit 54e7cfe

File tree

4 files changed

+166
-3
lines changed

4 files changed

+166
-3
lines changed

docs/docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ keywords: ['Bemi Changelog', 'Bemi New Features', 'Postgres Audit Trails', 'Chan
1111

1212
## 2024-12
1313

14+
* Platform
15+
* Enable connecting to PostgreSQL database within private VPCs using a [VPN tunnel](https://docs.bemi.io/postgresql/source-database#vpn-tunnel)
1416
* [Bemi Core](https://github.com/BemiHQ/bemi)
1517
* Stitch context with out of order LSN positions
1618

docs/docs/postgresql/source-database.md

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,180 @@ Note that this column will always be recorded if there were updated values in ot
7676

7777
## SSH Tunnel
7878

79-
If your PostgreSQL source database is not accessible over the internet, you can specify SSH credentials to enable an SSH tunnel via a jump host.
79+
If your PostgreSQL source database is not accessible over the internet, you can specify SSH credentials to enable an SSH tunnel via your public jump host:
8080

8181
![dashboard](/img/new-source-db-ssh.png)
8282

83-
Once the source database connection settings are submitted, we'll generate a public SSH key.
83+
Once the source database connection settings are submitted, we'll generate a public SSH key:
84+
85+
![](/img/new-source-db-ssh-key.png)
86+
8487
Add this public SSH key to your SSH host to allow Bemi workers to connect and SSH-tunnel to the PostgreSQL database:
8588

8689
```sh
8790
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
8891
echo 'ssh-ed25519 AAAAC3Nz...' >> ~/.ssh/authorized_keys
8992
```
9093

91-
If you need a public SSH Key before you know the SSH host address, just specify any address and later reach out to us to update it.
94+
Note: if you need a public SSH Key before you know the SSH host address, just specify any address and later reach out to us to update it.
95+
96+
## VPN Tunnel
97+
98+
If your PostgreSQL source database and SSH jump host can't be accessible over the internet, you can establish a secure VPN tunnel to our VPN server.
99+
100+
![](/img/new-source-db-vpn.png)
101+
102+
You need to provision a VPN node and configure VPN using [WireGuard](https://www.wireguard.com/) that combines strong encryption with fast speeds:
103+
104+
```sh
105+
# Install WireGuard
106+
sudo apt update && sudo apt install -y resolvconf wireguard
107+
108+
# Generate a private and public keys
109+
wg genkey | tee wg-privatekey | wg pubkey > wg-publickey
110+
111+
# Get your network interface name (e.g., ens5)
112+
NETWORK_INTERFACE=$(ip route get 8.8.8.8 | grep 8.8.8.8 | awk '{print $5}')
113+
114+
# Reach out to us to get the VPN server public key and endpoint
115+
BEMI_PUBLIC_KEY=
116+
BEMI_ENDPOINT=
117+
118+
# Send us your public key and PostgreSQL private IP address
119+
cat wg-publickey
120+
dig your-prod-postgres.abcdef5ghij.us-west-1.rds.amazonaws.com A +short | tail -n 1
121+
122+
# Enable IP forwarding
123+
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
124+
sudo sysctl -p
125+
126+
# Create a WireGuard configuration file with the following content
127+
cat << EOF > wg0.conf
128+
[Interface]
129+
PrivateKey = $(cat ./wg-privatekey)
130+
Address = 10.0.0.1/24
131+
PostUp = iptables -t nat -A POSTROUTING -o $NETWORK_INTERFACE -j MASQUERADE
132+
PostDown = iptables -t nat -D POSTROUTING -o $NETWORK_INTERFACE -j MASQUERAD
133+
134+
[Peer]
135+
PublicKey = $BEMI_PUBLIC_KEY
136+
AllowedIPs = 10.0.0.2/32
137+
Endpoint = $BEMI_ENDPOINT:51820
138+
PersistentKeepalive = 25
139+
EOF
140+
sudo mv wg0.conf /etc/wireguard/wg0.conf
141+
142+
sudo systemctl enable [email protected]
143+
sudo systemctl start [email protected]
144+
systemctl status [email protected]
145+
```
146+
147+
Please make sure to have the port 5432 (Postgres) open on your VPN node.
148+
Once you have the WireGuard running, we'll be able to connect to your PostgreSQL database over the VPN tunnel.
149+
150+
Here is a Terraform snippet to run all the commands above on AWS:
151+
152+
```tf
153+
locals {
154+
# For example, Ubuntu 24.04 LTS - arm64
155+
ami = "ami-..."
156+
instance_type = "t4g.micro"
157+
158+
# Your private subnet ID
159+
private_subnet_id = "subnet-..."
160+
161+
# Bemi VPN server information
162+
bemi_public_key = "..."
163+
bemi_endpoint = "..."
164+
165+
# Optionally if you want to SSH to the VPN node
166+
ssh_key_name = "..."
167+
ssh_cidr_blocks = [".../32"]
168+
}
169+
170+
resource "aws_security_group" "bemi-vpn-node" {
171+
name = "bemi-vpn-node"
172+
description = "Security group for bemi-vpn-node"
173+
174+
ingress {
175+
description = "SSH"
176+
from_port = 22
177+
to_port = 22
178+
protocol = "tcp"
179+
cidr_blocks = local.ssh_cidr_blocks
180+
}
181+
182+
egress {
183+
from_port = 0
184+
to_port = 0
185+
protocol = "-1"
186+
cidr_blocks = ["0.0.0.0/0"]
187+
ipv6_cidr_blocks = ["::/0"]
188+
}
189+
190+
tags = {
191+
Name = "bemi-vpn-node"
192+
}
193+
}
194+
195+
resource "aws_instance" "bemi-vpn-node" {
196+
ami = local.ami
197+
instance_type = local.instance_type
198+
key_name = local.ssh_key_name
199+
associate_public_ip_address = false
200+
subnet_id = local.private_subnet_id
201+
security_groups = [aws_security_group.bemi-vpn-node.id]
202+
203+
tags = {
204+
Name = "bemi-vpn-node"
205+
}
206+
207+
user_data = <<-EOF
208+
#!/bin/bash
209+
210+
sudo apt update
211+
sudo apt install -y resolvconf wireguard
212+
213+
cd /home/ubuntu
214+
wg genkey | tee wg-privatekey | wg pubkey > wg-publickey
215+
216+
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
217+
sudo sysctl -p
218+
219+
cat << EOF_WG_CONF > wg0.conf
220+
[Interface]
221+
PrivateKey = $(cat ./wg-privatekey)
222+
Address = 10.0.0.1/24
223+
PostUp = iptables -t nat -A POSTROUTING -o ens5 -j MASQUERADE
224+
PostDown = iptables -t nat -D POSTROUTING -o ens5 -j MASQUERAD
225+
226+
[Peer]
227+
PublicKey = ${local.bemi_public_key}
228+
AllowedIPs = 10.0.0.2/32
229+
Endpoint = ${local.bemi_endpoint}:51820
230+
PersistentKeepalive = 25
231+
EOF_WG_CONF
232+
sudo mv wg0.conf /etc/wireguard/wg0.conf
233+
234+
sudo systemctl enable [email protected]
235+
sudo systemctl start [email protected]
236+
EOF
237+
}
238+
```
239+
240+
To check the WireGuard status, SSH to the VPN node and run:
241+
242+
```sh
243+
# Check the WireGuard status
244+
systemctl status [email protected]
245+
sudo wg show
246+
247+
# Check the WireGuard configuration file
248+
sudo cat /etc/wireguard/wg0.conf
249+
250+
# Check your public WireGuard key and share it with us
251+
cat ./wg-publickey
252+
```
92253

93254
## Bemi Static IPs
94255

131 KB
Loading

docs/static/img/new-source-db-vpn.png

102 KB
Loading

0 commit comments

Comments
 (0)