|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Generate TLS certificates for Flower SuperLink and create Kubernetes Secrets. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# ./hack/generate-certs.sh [--hub-ip <IP>] [--namespace <ns>] [--days <validity>] |
| 6 | +# |
| 7 | +# This creates two Secrets: |
| 8 | +# flower-tls-ca - CA cert + key (used to verify server identity) |
| 9 | +# flower-superlink-tls - SuperLink server cert + key + CA cert |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +NAMESPACE="${NAMESPACE:-flower-system}" |
| 14 | +DAYS=365 |
| 15 | +HUB_IPS=() |
| 16 | +CERT_DIR="" |
| 17 | + |
| 18 | +usage() { |
| 19 | + echo "Usage: $0 [--hub-ip <IP>]... [--namespace <ns>] [--days <n>]" |
| 20 | + echo "" |
| 21 | + echo "Options:" |
| 22 | + echo " --hub-ip <IP> Hub node IP to include in server cert SANs (repeatable)" |
| 23 | + echo " --namespace <ns> Kubernetes namespace (default: flower-system)" |
| 24 | + echo " --days <n> Certificate validity in days (default: 365)" |
| 25 | + exit 1 |
| 26 | +} |
| 27 | + |
| 28 | +while [[ $# -gt 0 ]]; do |
| 29 | + case "$1" in |
| 30 | + --hub-ip) |
| 31 | + [[ -z "${2:-}" ]] && { echo "Error: --hub-ip requires a value"; exit 1; } |
| 32 | + HUB_IPS+=("$2") |
| 33 | + shift 2 |
| 34 | + ;; |
| 35 | + --namespace) |
| 36 | + [[ -z "${2:-}" ]] && { echo "Error: --namespace requires a value"; exit 1; } |
| 37 | + NAMESPACE="$2" |
| 38 | + shift 2 |
| 39 | + ;; |
| 40 | + --days) |
| 41 | + [[ -z "${2:-}" ]] && { echo "Error: --days requires a value"; exit 1; } |
| 42 | + DAYS="$2" |
| 43 | + shift 2 |
| 44 | + ;; |
| 45 | + -h|--help) |
| 46 | + usage |
| 47 | + ;; |
| 48 | + *) |
| 49 | + echo "Unknown option: $1" |
| 50 | + usage |
| 51 | + ;; |
| 52 | + esac |
| 53 | +done |
| 54 | + |
| 55 | +cleanup() { |
| 56 | + if [[ -n "${CERT_DIR}" && -d "${CERT_DIR}" ]]; then |
| 57 | + rm -rf "${CERT_DIR}" |
| 58 | + fi |
| 59 | +} |
| 60 | +trap cleanup EXIT |
| 61 | + |
| 62 | +CERT_DIR=$(mktemp -d) |
| 63 | + |
| 64 | +echo "==> Generating CA certificate..." |
| 65 | +openssl req -x509 -newkey rsa:2048 -nodes \ |
| 66 | + -keyout "${CERT_DIR}/ca.key" \ |
| 67 | + -out "${CERT_DIR}/ca.crt" \ |
| 68 | + -days "${DAYS}" \ |
| 69 | + -subj "/CN=Flower CA/O=flower-addon" |
| 70 | + |
| 71 | +echo "==> Generating SuperLink server certificate..." |
| 72 | + |
| 73 | +# Build SAN extension |
| 74 | +SAN="DNS:superlink,DNS:superlink.${NAMESPACE},DNS:superlink.${NAMESPACE}.svc,DNS:superlink.${NAMESPACE}.svc.cluster.local" |
| 75 | +for ip in "${HUB_IPS[@]}"; do |
| 76 | + SAN="${SAN},IP:${ip}" |
| 77 | +done |
| 78 | + |
| 79 | +cat > "${CERT_DIR}/server-ext.cnf" <<EOF |
| 80 | +[req] |
| 81 | +req_extensions = v3_req |
| 82 | +distinguished_name = req_distinguished_name |
| 83 | +
|
| 84 | +[req_distinguished_name] |
| 85 | +
|
| 86 | +[v3_req] |
| 87 | +basicConstraints = CA:FALSE |
| 88 | +keyUsage = digitalSignature, keyEncipherment |
| 89 | +extendedKeyUsage = serverAuth |
| 90 | +subjectAltName = ${SAN} |
| 91 | +EOF |
| 92 | + |
| 93 | +openssl req -newkey rsa:2048 -nodes \ |
| 94 | + -keyout "${CERT_DIR}/server.key" \ |
| 95 | + -out "${CERT_DIR}/server.csr" \ |
| 96 | + -subj "/CN=superlink/O=flower-addon" |
| 97 | + |
| 98 | +openssl x509 -req \ |
| 99 | + -in "${CERT_DIR}/server.csr" \ |
| 100 | + -CA "${CERT_DIR}/ca.crt" \ |
| 101 | + -CAkey "${CERT_DIR}/ca.key" \ |
| 102 | + -CAcreateserial \ |
| 103 | + -out "${CERT_DIR}/server.pem" \ |
| 104 | + -days "${DAYS}" \ |
| 105 | + -extensions v3_req \ |
| 106 | + -extfile "${CERT_DIR}/server-ext.cnf" |
| 107 | + |
| 108 | +echo "==> Creating namespace ${NAMESPACE} (if not exists)..." |
| 109 | +kubectl create namespace "${NAMESPACE}" --dry-run=client -o yaml | kubectl apply -f - |
| 110 | + |
| 111 | +echo "==> Creating Secret flower-tls-ca..." |
| 112 | +kubectl create secret generic flower-tls-ca \ |
| 113 | + --namespace="${NAMESPACE}" \ |
| 114 | + --from-file=ca.crt="${CERT_DIR}/ca.crt" \ |
| 115 | + --from-file=ca.key="${CERT_DIR}/ca.key" \ |
| 116 | + --dry-run=client -o yaml | kubectl apply -f - |
| 117 | + |
| 118 | +echo "==> Creating Secret flower-superlink-tls..." |
| 119 | +kubectl create secret generic flower-superlink-tls \ |
| 120 | + --namespace="${NAMESPACE}" \ |
| 121 | + --from-file=server.pem="${CERT_DIR}/server.pem" \ |
| 122 | + --from-file=server.key="${CERT_DIR}/server.key" \ |
| 123 | + --from-file=ca.crt="${CERT_DIR}/ca.crt" \ |
| 124 | + --dry-run=client -o yaml | kubectl apply -f - |
| 125 | + |
| 126 | +echo "" |
| 127 | +echo "TLS certificates created successfully!" |
| 128 | +echo " Namespace: ${NAMESPACE}" |
| 129 | +echo " CA Secret: flower-tls-ca" |
| 130 | +echo " TLS Secret: flower-superlink-tls" |
| 131 | +echo " SANs: ${SAN}" |
| 132 | +echo " Validity: ${DAYS} days" |
| 133 | +echo "" |
| 134 | +echo "Next: helm install flower-addon ./charts/flower-addon --set tls.enabled=true ..." |
0 commit comments