|
| 1 | +#!/usr/bin/env bash |
| 2 | +############################################################################### |
| 3 | +# Generate ephemeral CA + server certificates for GateSentry test bed. |
| 4 | +# |
| 5 | +# Creates: |
| 6 | +# JVJCA.crt / JVJCA.key — self-signed CA (internal-ca) |
| 7 | +# httpbin.org.crt / httpbin.org.key — server cert signed by the CA |
| 8 | +# |
| 9 | +# All certs are written to the same directory as this script (tests/fixtures/). |
| 10 | +# They are listed in .gitignore and must NOT be committed. |
| 11 | +# |
| 12 | +# Usage: |
| 13 | +# bash tests/fixtures/gen_test_certs.sh # generate once |
| 14 | +# bash tests/fixtures/gen_test_certs.sh --force # regenerate even if exist |
| 15 | +############################################################################### |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 20 | +CA_KEY="${SCRIPT_DIR}/JVJCA.key" |
| 21 | +CA_CERT="${SCRIPT_DIR}/JVJCA.crt" |
| 22 | +SERVER_KEY="${SCRIPT_DIR}/httpbin.org.key" |
| 23 | +SERVER_CERT="${SCRIPT_DIR}/httpbin.org.crt" |
| 24 | +DAYS_VALID=365 |
| 25 | + |
| 26 | +FORCE=false |
| 27 | +[[ "${1:-}" == "--force" ]] && FORCE=true |
| 28 | + |
| 29 | +# Skip if certs already exist (unless --force) |
| 30 | +if [[ "$FORCE" == false && -f "$CA_CERT" && -f "$SERVER_CERT" && -f "$SERVER_KEY" ]]; then |
| 31 | + echo "[gen_test_certs] Certificates already exist — skipping (use --force to regenerate)" |
| 32 | + exit 0 |
| 33 | +fi |
| 34 | + |
| 35 | +echo "[gen_test_certs] Generating ephemeral test certificates in ${SCRIPT_DIR}/" |
| 36 | + |
| 37 | +# ── 1. CA key + self-signed cert ──────────────────────────────────────────── |
| 38 | +openssl genrsa -out "$CA_KEY" 2048 2>/dev/null |
| 39 | + |
| 40 | +openssl req -new -x509 \ |
| 41 | + -key "$CA_KEY" \ |
| 42 | + -out "$CA_CERT" \ |
| 43 | + -days "$DAYS_VALID" \ |
| 44 | + -subj "/CN=internal-ca/C=SG/L=Singapore/O=JVJ 28 Inc." \ |
| 45 | + 2>/dev/null |
| 46 | + |
| 47 | +echo "[gen_test_certs] CA certificate: ${CA_CERT}" |
| 48 | + |
| 49 | +# ── 2. Server key + CSR + CA-signed cert ──────────────────────────────────── |
| 50 | +openssl genrsa -out "$SERVER_KEY" 2048 2>/dev/null |
| 51 | + |
| 52 | +openssl req -new \ |
| 53 | + -key "$SERVER_KEY" \ |
| 54 | + -out "${SCRIPT_DIR}/httpbin.org.csr" \ |
| 55 | + -subj "/CN=httpbin.org/C=SG/L=Singapore/O=JVJ 28 Inc." \ |
| 56 | + 2>/dev/null |
| 57 | + |
| 58 | +# SAN extension for httpbin.org + localhost |
| 59 | +cat > "${SCRIPT_DIR}/_san.cnf" <<EOF |
| 60 | +[v3_req] |
| 61 | +subjectAltName = DNS:httpbin.org, DNS:localhost, IP:127.0.0.1 |
| 62 | +basicConstraints = CA:FALSE |
| 63 | +keyUsage = digitalSignature, keyEncipherment |
| 64 | +extendedKeyUsage = serverAuth |
| 65 | +EOF |
| 66 | + |
| 67 | +openssl x509 -req \ |
| 68 | + -in "${SCRIPT_DIR}/httpbin.org.csr" \ |
| 69 | + -CA "$CA_CERT" \ |
| 70 | + -CAkey "$CA_KEY" \ |
| 71 | + -CAcreateserial \ |
| 72 | + -out "$SERVER_CERT" \ |
| 73 | + -days "$DAYS_VALID" \ |
| 74 | + -extensions v3_req \ |
| 75 | + -extfile "${SCRIPT_DIR}/_san.cnf" \ |
| 76 | + 2>/dev/null |
| 77 | + |
| 78 | +echo "[gen_test_certs] Server certificate: ${SERVER_CERT}" |
| 79 | + |
| 80 | +# ── Cleanup temp files ────────────────────────────────────────────────────── |
| 81 | +rm -f "${SCRIPT_DIR}/httpbin.org.csr" "${SCRIPT_DIR}/_san.cnf" "${SCRIPT_DIR}/JVJCA.srl" |
| 82 | + |
| 83 | +echo "[gen_test_certs] Done — certificates valid for ${DAYS_VALID} days" |
0 commit comments