code-server/terraform/scripts/generate-vpn-certificates.sh
Claude 369f459203
Add AWS Client VPN support for secure private access to code-server
This commit adds comprehensive VPN infrastructure to enable secure,
certificate-based access to code-server deployments. VPN provides an
additional security layer by requiring network-level authentication
before accessing internal resources.

Features:
- AWS Client VPN endpoint with certificate-based authentication
- Split tunnel support (route only VPC traffic through VPN)
- CloudWatch logging for all VPN connections
- Multi-platform client support (Windows, macOS, Linux, iOS, Android)
- Automatic certificate generation and ACM upload
- Client configuration export scripts
- Integration with both EC2 and EKS deployments

New Terraform Module:
- modules/vpn: Complete AWS Client VPN infrastructure
  - VPN endpoint with configurable authentication
  - Network associations for HA across multiple AZs
  - Authorization rules for VPC access
  - Security groups for VPN traffic
  - CloudWatch log groups and streams
  - Support for SAML/federated authentication

Scripts:
- scripts/generate-vpn-certificates.sh: Generate and upload VPN certificates
  - Creates CA, server, and client certificates
  - Automatically uploads to AWS Certificate Manager
  - Outputs certificate ARNs for Terraform configuration
- scripts/export-vpn-config.sh: Export client VPN configuration
  - Downloads VPN config from AWS
  - Embeds client certificates
  - Creates platform-ready .ovpn files

Deployment Updates:
- EC2 and EKS deployments now support optional VPN
- New variables for VPN configuration
- Updated outputs to include VPN endpoint information
- Example configurations with VPN settings

Documentation:
- VPN-SETUP-GUIDE.md: Comprehensive VPN setup guide
  - Certificate generation process
  - Terraform configuration
  - Client setup for all major platforms
  - Testing and troubleshooting
  - Advanced configuration options
  - Cost considerations and optimization

Configuration Options:
- Certificate-based or SAML/SSO authentication
- Split tunnel (recommended) or full tunnel
- UDP (faster) or TCP (more reliable) transport
- Configurable session timeout (8-24 hours)
- Custom DNS servers
- Client login banner
- Multiple authorization rules

Security Features:
- X.509 certificate authentication
- Private subnet associations
- Network-level access control
- Session logging and audit trail
- Support for multi-factor (VPN cert + OAuth2/SAML)

Cost: ~$216/month base + ~$0.40/user/day for active connections
2025-11-15 17:40:23 +00:00

177 lines
5.2 KiB
Bash
Executable file

#!/bin/bash
# Generate certificates for AWS Client VPN
# This script creates server and client certificates required for VPN authentication
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
echo_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
echo_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
echo_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# Check prerequisites
check_prerequisites() {
echo_info "Checking prerequisites..."
if ! command -v openssl &> /dev/null; then
echo_error "OpenSSL is not installed. Please install OpenSSL first."
exit 1
fi
if ! command -v aws &> /dev/null; then
echo_error "AWS CLI is not installed. Please install AWS CLI first."
exit 1
fi
if ! aws sts get-caller-identity &> /dev/null; then
echo_error "AWS credentials are not configured. Please configure AWS credentials first."
exit 1
fi
echo_info "All prerequisites met!"
}
# Configuration
CERT_DIR="${1:-./vpn-certificates}"
REGION="${2:-us-east-1}"
COMMON_NAME="${3:-code-server-vpn}"
echo_info "Certificate Directory: ${CERT_DIR}"
echo_info "AWS Region: ${REGION}"
echo_info "Common Name: ${COMMON_NAME}"
echo ""
# Create certificate directory
mkdir -p "${CERT_DIR}"
cd "${CERT_DIR}"
echo_step "Step 1: Generate CA private key and certificate"
echo_info "Creating Certificate Authority (CA)..."
# Generate CA private key
openssl genrsa -out ca.key 2048
# Generate CA certificate
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=US/ST=State/L=City/O=Organization/OU=IT/CN=${COMMON_NAME}-ca"
echo_info "CA certificate created: ca.crt"
echo ""
echo_step "Step 2: Generate server private key and certificate"
echo_info "Creating server certificate..."
# Generate server private key
openssl genrsa -out server.key 2048
# Generate server certificate signing request (CSR)
openssl req -new -key server.key -out server.csr -subj "/C=US/ST=State/L=City/O=Organization/OU=IT/CN=${COMMON_NAME}-server"
# Sign server certificate with CA
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
echo_info "Server certificate created: server.crt"
echo ""
echo_step "Step 3: Generate client private key and certificate"
echo_info "Creating client certificate..."
# Generate client private key
openssl genrsa -out client.key 2048
# Generate client certificate signing request (CSR)
openssl req -new -key client.key -out client.csr -subj "/C=US/ST=State/L=City/O=Organization/OU=IT/CN=${COMMON_NAME}-client"
# Sign client certificate with CA
openssl x509 -req -days 3650 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt
echo_info "Client certificate created: client.crt"
echo ""
echo_step "Step 4: Upload certificates to AWS Certificate Manager"
echo_info "Uploading server certificate to ACM..."
# Upload server certificate
SERVER_CERT_ARN=$(aws acm import-certificate \
--certificate fileb://server.crt \
--private-key fileb://server.key \
--certificate-chain fileb://ca.crt \
--region ${REGION} \
--tags Key=Name,Value=${COMMON_NAME}-server Key=Purpose,Value=VPN-Server \
--query CertificateArn \
--output text)
echo_info "Server certificate uploaded: ${SERVER_CERT_ARN}"
# Upload client certificate (root CA)
echo_info "Uploading client root certificate to ACM..."
CLIENT_CERT_ARN=$(aws acm import-certificate \
--certificate fileb://ca.crt \
--private-key fileb://ca.key \
--region ${REGION} \
--tags Key=Name,Value=${COMMON_NAME}-client-root Key=Purpose,Value=VPN-Client-Root \
--query CertificateArn \
--output text)
echo_info "Client root certificate uploaded: ${CLIENT_CERT_ARN}"
echo ""
echo_step "Step 5: Save certificate ARNs to file"
cat > certificate-arns.txt <<EOF
Server Certificate ARN: ${SERVER_CERT_ARN}
Client Root Certificate ARN: ${CLIENT_CERT_ARN}
EOF
cat > terraform-vars.txt <<EOF
# Add these to your terraform.tfvars file:
enable_vpn = true
vpn_server_certificate_arn = "${SERVER_CERT_ARN}"
vpn_client_certificate_arn = "${CLIENT_CERT_ARN}"
EOF
echo_info "Certificate ARNs saved to certificate-arns.txt"
echo ""
echo_step "Summary of Generated Files:"
echo " ca.key - CA private key (keep secure!)"
echo " ca.crt - CA certificate"
echo " server.key - Server private key (keep secure!)"
echo " server.crt - Server certificate"
echo " client.key - Client private key (distribute to VPN users)"
echo " client.crt - Client certificate (distribute to VPN users)"
echo ""
echo_step "Important Notes:"
echo " 1. Store ca.key and server.key securely (never share these!)"
echo " 2. Distribute client.key and client.crt to VPN users"
echo " 3. Add the certificate ARNs to your terraform.tfvars:"
cat terraform-vars.txt
echo ""
echo_info "✅ Certificate generation complete!"
echo_warn "🔒 Please backup the ${CERT_DIR} directory securely"
echo ""
echo_info "Next steps:"
echo " 1. Add the certificate ARNs to terraform.tfvars"
echo " 2. Set enable_vpn = true in terraform.tfvars"
echo " 3. Run terraform apply"
echo " 4. Export VPN client configuration using export-vpn-config.sh"