ration/infra/dev-ca/generate-dev-ca.sh
caelum0x 521d6176c4 feat(ration): REST/JSON facade over gateway, final phase-2 doc updates
Phase 2 section 7 (the other half, alongside sdk/go/ration and
sdk/rust/ration-client already pushed in c5297e4/8215fb8): a hand-rolled
internal/httpapi on services/gateway translating JSON <-> the gRPC source
of truth (cards, deposits, bridge funding, activity), documented as a
convenience facade over gRPC, not a parallel implementation. Also folds in
the remaining phase-2 doc updates (ARCHITECTURE/RUNBOOK/THREAT-MODEL/
AIRWALLEX-SETUP/SUBMISSION, ADR 0003/0004) and docker-compose/Makefile/
.env.example wiring from the resumed phase-2 workflow's final agents.

Verified: go build/vet/test clean across all 10 services + both new SDKs,
`make build && make test` clean end to end (C WAL, Rust ledger-core + the
new Rust SDK, every Go module including sdk/go/ration).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-19 12:56:58 +03:00

115 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Generates a throwaway, dev-only internal CA plus one identity
# (cert+key) per Ration service, for exercising mTLS locally or in the
# demo without any real PKI.
#
# ************************************************************************
# DEV-ONLY. Never use anything this script produces in a real deployment.
# The CA private key is written to disk in plaintext next to everything
# it signs — fine for a laptop/demo, a non-starter for production. A
# real deployment should issue service identities from a managed CA
# (e.g. AWS Private CA / ACM PCA, HashiCorp Vault PKI, or a service mesh
# like Istio/Linkerd's own mTLS) with short-lived certs and automatic
# rotation instead of this script's 1-year leaves and manual re-run.
# ************************************************************************
#
# Usage:
# infra/dev-ca/generate-dev-ca.sh [output-dir]
#
# Output (default output-dir: infra/dev-ca/out/):
# ca.pem, ca.key — the dev CA itself
# <service>-cert.pem/-key.pem — one identity per service, signed
# by ca.pem, usable as BOTH a server
# cert (mTLS server listeners) and a
# client cert (mTLS dial options),
# since every internal service here
# both serves and calls other
# services' RPCs.
# dev-ca.env — env var assignments matching
# pkg/mtls's LoadFromEnv convention
# (and ledger-core's Rust equivalent),
# ready to `source` or paste into a
# .env file to turn mTLS on locally.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
OUT_DIR="${1:-$ROOT/infra/dev-ca/out}"
mkdir -p "$OUT_DIR"
# One identity per internal service that either serves or dials a gRPC
# endpoint directly. Kept in sync with docs/SCALE-EXPANSION.md section 8
# and the services this repo actually ships (see services/).
SERVICES=(ledger-core gateway audit-log agent cli tui auth tenant-registry card-issuance webhook-ingestion)
CA_KEY="$OUT_DIR/ca.key"
CA_CERT="$OUT_DIR/ca.pem"
CA_SUBJECT="/C=US/O=Ration Dev/CN=Ration Dev CA - DO NOT USE IN PRODUCTION"
CA_DAYS=3650
echo "==> generating dev CA at $CA_CERT (NOT for production use)"
openssl genrsa -out "$CA_KEY" 4096 >/dev/null 2>&1
openssl req -x509 -new -nodes \
-key "$CA_KEY" \
-sha256 \
-days "$CA_DAYS" \
-subj "$CA_SUBJECT" \
-addext "basicConstraints=critical,CA:true" \
-addext "keyUsage=critical,keyCertSign,cRLSign" \
-out "$CA_CERT"
chmod 600 "$CA_KEY"
ENV_FILE="$OUT_DIR/dev-ca.env"
: > "$ENV_FILE"
echo "# Generated by infra/dev-ca/generate-dev-ca.sh — source this file" >> "$ENV_FILE"
echo "# (or copy into .env) to make every service require mTLS locally." >> "$ENV_FILE"
echo "# Dev-only: see this script's header before using any of this" >> "$ENV_FILE"
echo "# outside a laptop/demo environment." >> "$ENV_FILE"
for svc in "${SERVICES[@]}"; do
key="$OUT_DIR/${svc}-key.pem"
csr="$OUT_DIR/${svc}.csr"
cert="$OUT_DIR/${svc}-cert.pem"
ext="$OUT_DIR/${svc}.ext"
days=365
echo "==> issuing identity for ${svc}"
openssl genrsa -out "$key" 2048 >/dev/null 2>&1
cat > "$ext" <<EOF
basicConstraints=CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=DNS:${svc},DNS:localhost,IP:127.0.0.1
EOF
openssl req -new \
-key "$key" \
-subj "/C=US/O=Ration Dev/CN=${svc}" \
-out "$csr" >/dev/null 2>&1
openssl x509 -req \
-in "$csr" \
-CA "$CA_CERT" -CAkey "$CA_KEY" -CAcreateserial \
-days "$days" -sha256 \
-extfile "$ext" \
-out "$cert" >/dev/null 2>&1
rm -f "$csr" "$ext"
chmod 600 "$key"
env_prefix="$(echo "$svc" | tr '[:lower:]-' '[:upper:]_')"
{
echo "export ${env_prefix}_MTLS_ENABLED=true"
echo "export ${env_prefix}_MTLS_CERT_FILE=${cert}"
echo "export ${env_prefix}_MTLS_KEY_FILE=${key}"
echo "export ${env_prefix}_MTLS_CA_FILE=${CA_CERT}"
} >> "$ENV_FILE"
done
rm -f "$OUT_DIR/ca.srl"
echo
echo "==> done. CA + $(echo "${SERVICES[@]}" | wc -w | tr -d ' ') service identities written to $OUT_DIR"
echo " source $ENV_FILE to enable mTLS for a local run, e.g.:"
echo " set -a; source $ENV_FILE; set +a; make build && ./bin/ledger-core"