Setting Up Jumphost with Wireguard
The note contains step by step to setup VPN machine with Wireguard as jumphost for connecting to services within private network in Cloud.
Configuration
------------- ------------- -------------
| | | | | |
| MacOS | --> | Wireguard | --> | Nginx |
| | | VM | | VM |
------------- ------------- -------------
- MacOS: Local network
- Wireguard VM: VPC
payment-vpcsubnetpayment-private-us-central1-public - Nginx VM: VPC
payment-vpcsubnetpayment-private-us-central1
Notes
Setting up new GCP Project
Setting up new custom VPC with 2 subnets:
payment-private-us-central1-public. CIDR:10.1.2.0/24payment-private-us-central1. CIDR:10.1.1.0/24
Setting up following firewall rules:
payment-private-allow-httpallowstcp:80withhttp-servernetwork tagspayment-allow-user-vpnallowsudp:51820withwireguardnetwork tagspayment-private-allow-sshallowstcp:22payment-private-allow-customallows all ports between subnets
Setting up NAT Gateway for private
Create 1 Nginx VM without public IP address.
sudo apt update sudo apt install nginx curl localhostCreate 1 VPN VM and install wireguard with public IP address. Follow this blog to configure wireguard on the jumphost
sudo apt update sudo apt install wireguard sudo su cd /etc/wireguard wg genkey > jumphost.key wg pubkey < jumphost.key > jumphost.pubCreate jumphost configuration
# local settings for the jumphost [Interface] PrivateKey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE= Address = 10.0.0.1/24 ListenPort = 51820 PreUp = sysctl -w net.ipv4.conf.all.forwarding=1Run jumphost as systemd service
sudo systemctl start wg-quick@wg0.service sudo systemctl enable wg-quick@wg0.serviceCheck wireguard
sudo wgTesting access to nginx vm from jumphost box
curl <nginx_private_ip_address>Install and configure wireguard client in macos.
[Interface] PrivateKey = <CLIENT_PRIVATE_KEY> Address = 10.0.0.11 #client wireguard ip address # remote settings for the jumphost [Peer] PublicKey = <SERVER_PUBLIC_KEY> Endpoint = <WIREGUARD_VM_PUBLIC_IP_ADDRESS>:51820 AllowedIPs = 10.1.1.0/24, 10.1.2.0/24, 10.0.0.1/3210.1.1.0/24and10.1.2.0/24are CIDR from the subnets inpaymentvpc.10.0.0.1/32should be the wireguard server ip address inwg0interface
Configure peer in jumphost
[Interface] PrivateKey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE= Address = 10.0.0.1/24 ListenPort = 51820 PreUp = sysctl -w net.ipv4.conf.all.forwarding=1 [Peer] PublicKey = <CLIENT_PUBLIC_KEY> AllowedIPs = 10.0.0.11/32 # CLIENT WIREGUARD IP ADDRESSCLIENT_WIREGUARD_IP_ADDRESSis not the same as gcp vm private ip address. Look like this is ip address assigned to the virtual network interface.
Start VPN client on the macos
Testing ssh to jumpbox by using its private ip address
ssh -i ~/.ssh/google_compute_engine imre.nagi@<wireguard_private_ip>Setting up traffic forwarding
sudo nano /etc/sysctl.confuncomment
net.ipv4.ip_forward=1sudo sysctl -pFind the network interface that can route the traffic to internal subnet
ip route list default # default via 10.x.x.x dev ens4 proto dhcp src 10.y.y.y metric 100 # ens4 is the network interface nameUpdate
wg0.confPreUp = sysctl -w net.ipv4.conf.all.forwarding=1 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens4 -j MASQUERADE PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens4 -j MASQUERADEsystemctl restart wg-quick@wg0.serviceTest access to nginx webserver by using its ip address from macos
ssh -i ~/.ssh/google_compute_engine imre.nagi@<nginx_private_ip>
Open Questions
- How to dynamically configure peer in both server and client? We need to assign the wireguard ip address to each client, how to do this dynamically?