Ceci est une ancienne révision du document !
Déploiement automatique de certificat Let's Encrypt vers Java Keystore
Contexte
Cette documentation détaille le processus d'automatisation du déploiement d'un certificat Let's Encrypt vers un Java Keystore (utilisé par Tomcat ou d'autres applications Java).
Prérequis
* Un certificat Let's Encrypt généré via Certbot * Accès root sur les deux serveurs (source et destination) * OpenSSL et Keytool installés * SSH configuré entre les serveurs
Configuration SSH sans mot de passe
Sur le serveur source (où se trouve le certificat Let's Encrypt) : <syntaxhighlight lang=“bash”> # Générer la paire de clés ssh-keygen -t ed25519
# Copier la clé vers le serveur de destination ssh-copy-id root@serveur-destination </syntaxhighlight>
Script de déploiement
Créer le script suivant dans /usr/local/bin/deploy_cert_mdm.sh : <syntaxhighlight lang=“bash”> #!/bin/bash
# Configuration CERT_PATH=“/etc/letsencrypt/live/mdm.murcier.fun” MDM_SERVER=“mdm.murcier.fun” MDM_USER=“root” KEYSTORE_PASSWORD=“123456” REMOTE_PATH=“/var/lib/tomcat9/ssl”
# Vérification des droits root if [ “$EUID” -ne 0 ]; then
echo "Ce script doit être exécuté en tant que root" exit 1
fi
# Création d'un dossier temporaire TEMP_DIR=$(mktemp -d) cd “$TEMP_DIR” || exit 1
echo “Création du keystore…”
# Création du keystore PKCS12 openssl pkcs12 -export \
- in “$CERT_PATH/fullchain.pem” \
- inkey “$CERT_PATH/privkey.pem” \
- out mdm.murcier.fun.p12 \
- name tomcat \
- password pass:$KEYSTORE_PASSWORD
# Conversion en JKS keytool -importkeystore \
- srckeystore mdm.murcier.fun.p12 \
- srcstoretype PKCS12 \
- srcstorepass $KEYSTORE_PASSWORD \
- destkeystore mdm.murcier.fun.jks \
- deststoretype JKS \
- deststorepass $KEYSTORE_PASSWORD
echo “Copie du keystore vers le serveur MDM…”
# Copie et configuration sur le serveur distant scp mdm.murcier.fun.jks $MDM_USER@$MDM_SERVER:$REMOTE_PATH/ ssh $MDM_USER@$MDM_SERVER “chown tomcat:tomcat $REMOTE_PATH/mdm.murcier.fun.jks && chmod 600 $REMOTE_PATH/mdm.murcier.fun.jks && systemctl restart tomcat9”
# Nettoyage cd / rm -rf “$TEMP_DIR”
echo “Déploiement terminé avec succès!” </syntaxhighlight>
Installation du script
<syntaxhighlight lang=“bash”> chmod +x /usr/local/bin/deploy_cert_mdm.sh ln -s /usr/local/bin/deploy_cert_mdm.sh /etc/letsencrypt/renewal-hooks/deploy/deploy_cert_mdm </syntaxhighlight>
Configuration Tomcat
Le fichier server.xml doit contenir : <syntaxhighlight lang=“xml”> <Connector port=“8443” protocol=“org.apache.coyote.http11.Http11NioProtocol”
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="/var/lib/tomcat9/ssl/mdm.murcier.fun.jks"
certificateKeystorePassword="123456"
type="RSA" />
</SSLHostConfig>
</Connector> </syntaxhighlight>
Test manuel
Pour tester le renouvellement manuellement : <syntaxhighlight lang=“bash”> # Test à blanc certbot renew –dry-run
# Renouvellement forcé certbot certonly –force-renewal -d votre.domaine.com </syntaxhighlight>
Vérification
Pour vérifier l'installation : <syntaxhighlight lang=“bash”> # Vérifier le statut de Tomcat systemctl status tomcat9
# Vérifier le certificat curl -v https://votre.domaine.com:8443 </syntaxhighlight>
Maintenance
* Le renouvellement est automatique via Certbot * Le script s'exécute automatiquement après chaque renouvellement * Le certificat est renouvelé environ 30 jours avant expiration * Vérifier les logs dans /var/log/letsencrypt/ en cas de problème
Dépannage
Problèmes courants
* 'Erreur de permissions' : Vérifier les droits utilisateur tomcat
* 'Erreur SSH' : Vérifier la configuration SSH et les clés
* 'Tomcat ne démarre pas' : Vérifier les logs avec journalctl -u tomcat9
* 'Certificat non renouvelé' : Vérifier les logs Certbot
Commandes utiles
* Vérifier le contenu d'un keystore : <syntaxhighlight lang=“bash”> keytool -list -v -keystore keystore.jks </syntaxhighlight>
* Vérifier la validité du certificat : <syntaxhighlight lang=“bash”> openssl x509 -in fullchain.pem -text -noout </syntaxhighlight>
