#!/bin/bash # Script pour exporter un certificat TLS depuis Kubernetes vers un format utilisable par des équipements externes set -e SECRET_NAME="${1}" NAMESPACE="${2:-certificates-ops}" CONTEXT="${3:-cluster-ops}" OUTPUT_DIR="${4:-./certificates-export}" FORMAT="${5:-pem}" # pem, pkcs12, pfx if [ -z "$SECRET_NAME" ]; then echo "Usage: $0 [namespace] [context] [output-dir] [format]" echo "" echo "Exemples:" echo " $0 wildcard-prd-tls certificates-ops cluster-ops ./certs pem" echo " $0 wildcard-prd-tls certificates-ops cluster-ops ./certs pkcs12" echo "" echo "Formats disponibles:" echo " - pem: Certificat et clé en format PEM (par défaut)" echo " - pkcs12: Format PKCS12 (.p12) pour pfSense, etc." echo " - pfx: Format PFX (identique à PKCS12)" exit 1 fi echo "=== Export du certificat TLS ===" echo "Secret: $SECRET_NAME" echo "Namespace: $NAMESPACE" echo "Context: $CONTEXT" echo "Format: $FORMAT" echo "" # Créer le répertoire de sortie mkdir -p "$OUTPUT_DIR" # Vérifier que le secret existe if ! kubectl get secret "$SECRET_NAME" -n "$NAMESPACE" --context="$CONTEXT" &>/dev/null; then echo "❌ Erreur: Le secret '$SECRET_NAME' n'existe pas dans '$NAMESPACE'" exit 1 fi # Extraire le certificat et la clé echo "📥 Extraction du certificat depuis Kubernetes..." kubectl get secret "$SECRET_NAME" -n "$NAMESPACE" --context="$CONTEXT" -o jsonpath='{.data.tls\.crt}' | base64 -d > "$OUTPUT_DIR/$SECRET_NAME.crt" kubectl get secret "$SECRET_NAME" -n "$NAMESPACE" --context="$CONTEXT" -o jsonpath='{.data.tls\.key}' | base64 -d > "$OUTPUT_DIR/$SECRET_NAME.key" # Extraire la chaîne de certificats complète (certificat + CA) kubectl get secret "$SECRET_NAME" -n "$NAMESPACE" --context="$CONTEXT" -o jsonpath='{.data.ca\.crt}' | base64 -d > "$OUTPUT_DIR/$SECRET_NAME-ca.crt" 2>/dev/null || true echo "✅ Certificat et clé extraits" echo "" # Convertir selon le format demandé case "$FORMAT" in pem) echo "📄 Format PEM (déjà généré)" echo " Certificat: $OUTPUT_DIR/$SECRET_NAME.crt" echo " Clé privée: $OUTPUT_DIR/$SECRET_NAME.key" if [ -f "$OUTPUT_DIR/$SECRET_NAME-ca.crt" ]; then echo " CA: $OUTPUT_DIR/$SECRET_NAME-ca.crt" fi ;; pkcs12|pfx) echo "📦 Conversion en format PKCS12..." # Demander un mot de passe pour le fichier PKCS12 read -sp "Mot de passe pour le fichier PKCS12 (laissez vide pour aucun): " PASSWORD echo "" # Créer le fichier PKCS12 if [ -z "$PASSWORD" ]; then openssl pkcs12 -export \ -out "$OUTPUT_DIR/$SECRET_NAME.p12" \ -inkey "$OUTPUT_DIR/$SECRET_NAME.key" \ -in "$OUTPUT_DIR/$SECRET_NAME.crt" \ -name "$SECRET_NAME" \ -passout pass: else openssl pkcs12 -export \ -out "$OUTPUT_DIR/$SECRET_NAME.p12" \ -inkey "$OUTPUT_DIR/$SECRET_NAME.key" \ -in "$OUTPUT_DIR/$SECRET_NAME.crt" \ -name "$SECRET_NAME" \ -passout pass:"$PASSWORD" fi echo "✅ Fichier PKCS12 créé: $OUTPUT_DIR/$SECRET_NAME.p12" ;; *) echo "❌ Format '$FORMAT' non supporté" echo " Formats disponibles: pem, pkcs12, pfx" exit 1 ;; esac # Afficher les informations du certificat echo "" echo "📋 Informations du certificat:" openssl x509 -in "$OUTPUT_DIR/$SECRET_NAME.crt" -noout -subject -issuer -dates 2>/dev/null || echo " (openssl non disponible pour afficher les détails)" echo "" echo "✅ Export terminé dans: $OUTPUT_DIR" echo "" echo "📝 Prochaines étapes:" echo "" case "$FORMAT" in pem) echo "Pour pfSense:" echo " 1. Allez dans System > Certificates" echo " 2. Cliquez sur 'Import'" echo " 3. Importez le certificat ($SECRET_NAME.crt)" echo " 4. Importez la clé privée ($SECRET_NAME.key)" echo "" echo "Pour OpenMediaVault:" echo " 1. Allez dans System > Certificates > SSL" echo " 2. Cliquez sur 'Import'" echo " 3. Sélectionnez le certificat et la clé" ;; pkcs12|pfx) echo "Pour pfSense:" echo " 1. Allez dans System > Certificates" echo " 2. Cliquez sur 'Import'" echo " 3. Sélectionnez le fichier PKCS12 ($SECRET_NAME.p12)" echo " 4. Entrez le mot de passe si vous en avez défini un" ;; esac