sumarsono.com
Take it with a grain of salt


Setup Bastion Host Pakai Haproxy Dan Wireguard

Posted on

Apa itu Bastion host?

Bastion host adalah:

A bastion host is a special-purpose computer on a network specifically designed and configured to withstand attacks. The computer generally hosts a single application, for example a proxy server, and all other services are removed or limited to reduce the threat to the computer. It is hardened in this manner primarily due to its location and purpose, which is either on the outside of a firewall or in a demilitarized zone (DMZ) and usually involves access from untrusted networks or computers. -- wikipedia

Kenapa aku butuh bastion host?

Untuk meminimalkan pengeluaran uang. Haha. Jadi ceritanya begini:

Aku belajar banyak hal, dan hal-hal tersebut harus di hosting-kan. Contohnya, aku belajar kubernetes cluster dengan 3 node, masing-masing node memiliki spek 2 CPU dan 2 GB RAM. Kalau pakai cloud mahal dong tarifnya dengan spek segitu, 3 node pula. Solusi murah ya aku belajar di local, pakai VM (libvrit). PC local ini ada dibelakang NAT dan tidak boleh ada port forwarding dari router.

Bagaimana caranya aku expose service ke internet?. Misalnya aku deploy wordpress diatas kubernetes cluster, setup service wordpress, dan setup ingress untuk wordpress. Nah, kalau akses dari LAN mah gak msalah, kalau aku akses dari internet gimana? Ya pakai bastion host.

Aku butuh SSL dari lets encrypt, di local pc ada certbot, di kubernetes cluster ada cert-manage, bagaimana caranya aku menerima validasi dari Lets Encrypt kalau aku pilih challenge HTTP dan TLS/Alpn? Ya pakai bastion host.

Kok bastion host dibilang solusi murah?

Murah karena aku cuma butuh spek kecil, cukup VPS dengan 1 CPU dan 512MB RAM. Harga sewa cuma 30rb rupiah. Aku sewa di virtua.host. Bandingkan dengan harga sewa (2 CPU + 2 GB RAM) x 3 node.

Apa yang aku perlukan dari bastion host?

  1. Aku butuh Wireguard

    Wireguard ini VPN tercepat yang pernah ku coba. Aku butuh VPN krn pc local ada di belakang NAT dan tidak boleh ada port forward dari router. Jadinya mau bikin koneksi peer to peer pakai wireguard.

  2. Aku butuh HAProxy

    HAProxy ini bertugas sebagia proxy dengan mode full passtrhough. Misal aku buka port 80 dan 443, maka incoming request akan diteruskan ke peer wireguard di PC local/VM. Mode full passtrough ini berfungsi supaya HAProxy tidak terminate SSL. Urursan SSL diserahkan ke belakang, alias di PC local/VM. Dengan begitu certbot atau cert-manager bisa berfungsi ketika memilih challenge HTTP atau TLS/ALPN.

  3. Buang semua service di bastion host kecuali ssh, wireguard, dan HAProxy

    Service yang ada di bastion host aku set seminimal mungkin, menyisakan ssh untuk remote shell, wireguard untuk vpn, dan HAProxy untuk proxy.

  4. Firewall whitelist mode

    Block semua kecuali yang diijinkan.

Setup Wireguard

Instalasi silahkan ikuti dokumentasi mereka. Contoh konfigurasi wireguard yang kupakai:

bash [Interface] Address = 192.168.23.1/24 ListenPort = 51194 PrivateKey = isi-dengan-private-key-server

[Peer] PublicKey = isi-dengan-pubkey-peer PresharedKey = samakan-preshared-key-dengan-peer AllowedIPs = 192.168.23.2/32, 192.168.100.0/24


Yang perlu diperhatikan adalah `AllowedIPs = 192.168.23.2/32, 192.168.100.0/24`:

- 192.168.23.2/32 adalah ip address peer
- 192.168.100.0/24 adalah ip address ingress yang dimasukin ke metallb layer2. Tujuan dari memasukan ini adalah supaya bastion host bisa komunikasi dengan ingress di kubernetes cluster. Dengan begini haproxy bisa komunikasi langsung ke ingress yang ada di kubernetes cluster.

## Setup HAProxy

Install aja sesuai dokumentasi mereka. Contoh konfigurasi yang kupaka:

global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners stats timeout 30s user haproxy group haproxy daemon

# Default SSL material locations
#ca-base /etc/ssl/certs
#crt-base /etc/ssl/private

#ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
#ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
#tune.ssl.cachesize 500000

defaults mode http log global option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000

errorfile 400 /etc/haproxy/errors-custom/400.http
errorfile 403 /etc/haproxy/errors-custom/403.http
errorfile 408 /etc/haproxy/errors-custom/408.http
errorfile 500 /etc/haproxy/errors-custom/500.http
errorfile 502 /etc/haproxy/errors-custom/502.http
errorfile 503 /etc/haproxy/errors-custom/503.http
errorfile 504 /etc/haproxy/errors-custom/504.http

frontend http_front bind 185.10.17.182:80 mode tcp option tcplog default_backend ingress_80

frontend https_front bind 185.10.17.182:443 mode tcp option tcplog default_backend ingress_443

backend ingress_80 mode tcp server server1 192.168.100.30:80 check send-proxy-v2

backend ingress_443 mode tcp server server1 192.168.100.30:443 check send-proxy-v2


Disini haproxy mode full passtrough dan di set untuk kirim data send-proxy-v2, kubernetes ingress harus di set untuk accept proxy, kebetulan disini pakai nginx ingress controller, jadi edit configmap nya dan tambahkan:

```yaml
apiVersion: v1
kind: ConfigMap
data:
  proxy-real-ip-cidr: 192.168.100.0/24, 192.168.23.0/24
  use-proxy-protocol: "true"
  use-forwarded-headers: "true"
  compute-full-forwarded-for: "true"
...
...

Inti dari semua hal diatas adalah meneruskan incoming request ke VM local lewat wireguard:

bash Internet -> Haproxy -> wiregurad -> VM lokal