Files
promptfoo--promptfoo/examples/provider-http/tls/generate-test-certs.sh
T
wehub-resource-sync 0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

130 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# Generate self-signed certificates for testing TLS configurations
# WARNING: These certificates are for testing only, not for production!
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}🔐 Generating test certificates for TLS example${NC}"
echo -e "${YELLOW}⚠️ WARNING: These certificates are for testing only!${NC}\n"
# Create certs directory
mkdir -p certs
cd certs
# Certificate configuration
DAYS_VALID=365
KEY_SIZE=2048
COUNTRY="US"
STATE="California"
LOCALITY="San Francisco"
ORG="Promptfoo Test"
OU="Development"
# Generate CA private key
echo -e "${GREEN}1. Generating CA private key...${NC}"
openssl genrsa -out ca-key.pem $KEY_SIZE
# Generate CA certificate
echo -e "${GREEN}2. Generating CA certificate...${NC}"
openssl req -new -x509 -key ca-key.pem -out ca-cert.pem -days $DAYS_VALID \
-subj "/C=$COUNTRY/ST=$STATE/L=$LOCALITY/O=$ORG/OU=$OU/CN=Promptfoo Test CA"
# Generate server private key
echo -e "${GREEN}3. Generating server private key...${NC}"
openssl genrsa -out server-key.pem $KEY_SIZE
# Generate server certificate signing request
echo -e "${GREEN}4. Generating server CSR...${NC}"
cat >server.cnf <<EOF
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[req_distinguished_name]
C = $COUNTRY
ST = $STATE
L = $LOCALITY
O = $ORG
OU = $OU
CN = localhost
[v3_req]
keyUsage = keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = *.localhost
IP.1 = 127.0.0.1
IP.2 = ::1
EOF
openssl req -new -key server-key.pem -out server.csr -config server.cnf
# Sign server certificate with CA
echo -e "${GREEN}5. Signing server certificate with CA...${NC}"
openssl x509 -req -in server.csr -CA ca-cert.pem -CAkey ca-key.pem \
-CAcreateserial -out server-cert.pem -days $DAYS_VALID \
-extensions v3_req -extfile server.cnf
# Generate client private key
echo -e "${GREEN}6. Generating client private key...${NC}"
openssl genrsa -out client-key.pem $KEY_SIZE
# Generate client certificate signing request
echo -e "${GREEN}7. Generating client CSR...${NC}"
openssl req -new -key client-key.pem -out client.csr \
-subj "/C=$COUNTRY/ST=$STATE/L=$LOCALITY/O=$ORG/OU=$OU/CN=Test Client"
# Sign client certificate with CA
echo -e "${GREEN}8. Signing client certificate with CA...${NC}"
openssl x509 -req -in client.csr -CA ca-cert.pem -CAkey ca-key.pem \
-CAcreateserial -out client-cert.pem -days $DAYS_VALID
# Generate PFX bundle from client cert and key
echo -e "${GREEN}9. Creating PFX bundle...${NC}"
openssl pkcs12 -export -out client.pfx \
-inkey client-key.pem -in client-cert.pem \
-certfile ca-cert.pem -password pass:testpassword
# Generate encrypted client private key (for testing passphrase with PEM)
echo -e "${GREEN}10. Creating encrypted client private key...${NC}"
openssl rsa -aes256 -in client-key.pem -out client-key-encrypted.pem \
-passout pass:testpassword
# Clean up temporary files
rm -f *.csr *.cnf *.srl
# Display certificate information
echo -e "\n${GREEN}✅ Certificates generated successfully!${NC}"
echo -e "\nGenerated files:"
echo " 📄 ca-cert.pem - Certificate Authority certificate"
echo " 🔑 ca-key.pem - CA private key (keep secure!)"
echo " 📄 server-cert.pem - Server certificate"
echo " 🔑 server-key.pem - Server private key"
echo " 📄 client-cert.pem - Client certificate"
echo " 🔑 client-key.pem - Client private key (unencrypted)"
echo " 🔑 client-key-encrypted.pem - Client private key (encrypted, password: testpassword)"
echo " 📦 client.pfx - Client certificate bundle (password: testpassword)"
echo -e "\n${YELLOW}Certificate Details:${NC}"
echo "CA Certificate:"
openssl x509 -in ca-cert.pem -noout -subject -dates | sed 's/^/ /'
echo -e "\nServer Certificate:"
openssl x509 -in server-cert.pem -noout -subject -dates | sed 's/^/ /'
echo -e "\nClient Certificate:"
openssl x509 -in client-cert.pem -noout -subject -dates | sed 's/^/ /'
echo -e "\n${GREEN}You can now test the TLS configuration with:${NC}"
echo " 1. Start the mock server: node ../mock-server.js"
echo " 2. Run the evaluation: npm run local -- eval -c ../promptfooconfig-mock.yaml"