Side project

Self-hosted Infra & DB mTLS

Serverless web apps plus one server for long-running processes and databases. Moved serverless-to-database access from passwords to client certificates, and hardened deploys, backups, and reboot recovery.

Period
2025.07 — 2026.01
Role
Plan · Design · Build · Ship · Improve · solo
Stack
nginx, Docker, MariaDB, systemd, cron, rclone, Prometheus

Process

  1. Plan

    Serverless apps were reaching databases behind a password alone; framed that as the risk to fix.

  2. Design

    Private CA, per-account X509, pull-based deploys, a NAS standby.

  3. Build

    Deploy and backup scripts, certificate issuance, daily checks.

  4. Ship

    Built a GitOps-style pull deployer: diff from the last deployed commit, map changed paths to targets, hold a lock, alert once on failure.

  5. Improve

    Audited, then actually rebooted, and fixed what didn’t come back.

System diagram 9 components · 7 connections Play the flow and trace each connection

Overview

Web apps and APIs run serverless. One server runs nginx in front of three services, one MariaDB container per product, systemd units, and Prometheus with node_exporter. A shared mail/SMS library sends through AWS SES and NCloud SENS with server-side cooldowns.

Why I built it

Serverless functions have no fixed egress IP, so database ports were guarded by a password alone. Tailscale, WireGuard, and tunnels need a client-side daemon that serverless cannot run, and a fixed-IP add-on is an enterprise feature. So authentication was fixed instead of transport.

System

A private root CA (10 years), one server certificate, and one client certificate per app. Accounts use REQUIRE X509 one by one—a global require_secure_transport would have cut every existing account at once.

Eleven production databases moved in two days, unused database ports were closed, and every password-only account was retired. Database root accepts only the local socket. A daily check looks for remote accounts without X509. Expiry warnings fire 30 days ahead for client certs and 60 for the CA, because 14 days is too tight to redeploy 13 apps.

See this in the system diagram

Deploy and backup

After CI billing stopped, the server pulls and deploys itself every five minutes. It diffs from the last commit it deployed—HEAD^..HEAD misses earlier PRs when several merge together—holds a lock, stops if its checkout diverges, maps changed paths to targets, and alerts once per failure. The deploy SSH key is pinned to a forced command; sudo allows one service restart.

Local dumps are kept 7 days and copied every 3 hours to a NAS (30 days). The NAS runs a standby MariaDB with the same CA and account hashes, so failover changes only host and port; RPO is up to 3 hours.

Reboot hardening

A pre-reboot audit found 25 containers with no restart policy (20 production databases would have stayed down), two monitoring processes living in a login session for 258 days, and a container IP hard-coded as a database host.

A planned reboot with a kernel upgrade found one more: a console started before its database, stayed Up, and answered 500 to everything, so the restart policy never fired. A deploy script would also have reverted the host fix on the next run. Both were fixed.

Stack

nginx, Docker Compose, MariaDB, systemd, cron, flock, rclone, Prometheus, node_exporter, OpenSSL, Prisma.

Notes

Hard parts: prisma migrate deploy bypasses the app’s client setup; edge bundling needed a dynamic import; macOS LibreSSL’s default p12 cipher (RC2-40) fails under Prisma’s OpenSSL 3 with P1011. Ten delete-then-insert restore jobs exhausted the NAS’s file handles and left ten standby databases at 0MB with only “failed” on screen—replaced with file copies.

What held: zero password-only database accounts, and nothing that has to be started by hand after a reboot.