Skip to content

Commit 4418002

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

File tree

5 files changed

+167
-3
lines changed

5 files changed

+167
-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: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,181 @@ 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+
![](/img/new-source-db-ssh-arch.png)
80+
81+
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:
8082

8183
![dashboard](/img/new-source-db-ssh.png)
8284

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

8691
```sh
8792
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
8893
echo 'ssh-ed25519 AAAAC3Nz...' >> ~/.ssh/authorized_keys
8994
```
9095

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

93255
## Bemi Static IPs
94256

76.3 KB
Loading
131 KB
Loading
96.1 KB
Loading

0 commit comments

Comments
 (0)