- Ajout de la gestion de certificats

This commit is contained in:
Frogg 2026-05-09 15:40:29 +02:00
parent 5f31fd1587
commit 657b22741d
4 changed files with 231 additions and 7 deletions

14
TODO.md
View File

@ -1,5 +1,17 @@
BUG SMTP PROXMOX
---------------------------
Sur mon serveur CA
step ca certificate "*.server.home" server-wildcard.crt server-wildcard.key \
--provisioner admin@frogg.home \
--not-after 876000h
876000h 100 ans ?
----------------------------
changer le pass
pct exec 105 -- passwd root
aller sur la vm
@ -33,6 +45,8 @@ git -c http.sslVerify=false clone https://192.168.0.100/Frogg/Linux_frogg-profil
----------------------------------------------------------
option pour desactiver firewall
--
Bash
nano /etc/pve/lxc/ID.conf

View File

@ -2,6 +2,10 @@
# shellcheck disable=SC2034
CONFIG_DEB_INSTALL_DEFAULT_CA_SERVER="192.168.0.110"
CONFIG_DEB_INSTALL_DEFAULT_CA_WILDCARD="*.server.home"
CONFIG_DEB_INSTALL_DEFAULT_CA_SERVICES="gitea nginx apache2 mysql postgresql"
CONFIG_DEB_INSTALL_JSON='[
{"id": "git_pkg", "label": "Installer Git", "type": "pkg", "params": "git"},
{"id": "jq_pkg", "label": "Installer JQ", "type": "pkg", "params": "jq"},
@ -16,6 +20,7 @@ CONFIG_DEB_INSTALL_JSON='[
{"id": "ipv6", "label": "Désactiver IPv6", "type": "sys", "params": "ipv6"},
{"id": "motd", "label": "Désactiver les messages MOTD", "type": "sys", "params": "motd"},
{"id": "mail", "label": "Configurer ADMIN_MAIL", "type": "sys", "params": "mail"},
{"id": "cert_install", "label": "Installer le certificat wildcard auto", "type": "sys", "params": "cert_install"},
{"id": "hostname", "label": "Changer le Hostname", "type": "sys", "params": "hostname"},
{"id": "static_ip", "label": "Configurer IP Statique (eth0)", "type": "sys", "params": "static_ip"},
{"id": "deploy_ssh_key", "label": "Autoriser la clé RSA sur un hôte distant", "type": "sys", "params": "deploy_ssh_key"},

View File

@ -126,6 +126,15 @@ check_deb_sys_clone_ssh_key(){
return 0
}
check_deb_sys_cert_install(){
local MARKER="# STEP-CA-RENEWAL"
if crontab -l 2>/dev/null | grep -q "$MARKER"; then
return 0
else
return 1
fi
}
do_deb_sys_motd(){
$SUDO chmod -x /etc/update-motd.d/*
msg_success "Les fichiers dans /etc/update-motd.d/ ne sont plus executable"
@ -195,14 +204,14 @@ do_deb_sys_clone_ssh_key() {
fi
# 2. Saisie des infos
read -p "Utilisateur distant : " remote_user
read -p "IP/Hostname distant : " remote_host
read -p "Port SSH (22) : " remote_port
read -rp "Utilisateur distant : " remote_user
read -rp "IP/Hostname distant : " remote_host
read -rp "Port SSH (22) : " remote_port
remote_port=${remote_port:-22}
# 3. Confirmation (car c'est sensible)
msg_warning "Ceci va copier votre clé PRIVÉE sur $remote_host."
read -p "Êtes-vous sûr ? (o/N) : " confirm
read -rp "Êtes-vous sûr ? (o/N) : " confirm
[[ "$confirm" != "o" && "$confirm" != "O" ]] && return
# 4. Envoi et configuration des droits en une seule commande
@ -262,7 +271,7 @@ do_deb_sys_ufw() {
# 2. Boucle de saisie (on remplit uniquement le tableau en mémoire)
while true; do
read -p "Entrez un port à ouvrir (ou 'f' pour terminer) : " input_port
read -rp "Entrez un port à ouvrir (ou 'f' pour terminer) : " input_port
[[ "$input_port" == "f" ]] && break
if [[ ! "$input_port" =~ ^[0-9]+$ ]]; then
@ -292,7 +301,7 @@ do_deb_sys_ufw() {
echo -e "Les ports suivants vont être ouverts : ${COLOR_LIGHT_BLUE}${ports_to_allow[*]}${NONE}"
msg_error "Toutes les autres règles existantes seront supprimées."
read -p "Appliquer ces changements maintenant ? (o/N) : " confirm
read -rp "Appliquer ces changements maintenant ? (o/N) : " confirm
if [[ "$confirm" != "o" && "$confirm" != "O" ]]; then
msg_info "Action annulée. Le firewall n'a pas été modifié."
return
@ -479,7 +488,6 @@ do_deb_sys_hostname() {
fi
}
do_deb_sys_msmtp() {
msg_info "Installation et configuration de MSMTP..."
@ -517,4 +525,191 @@ EOF"
$SUDO chown root:root /etc/msmtprc
msg_success "MSMTP configuré. Test d'envoi recommandé : echo 'Test' | mail -s 'Test sujet' ton@mail.com"
}
do_deb_sys_cert_install(){
local CA_IP WILDCARD_DOMAIN CA_URL CA_FINGERPRINT BASE_DOMAIN STEP_BIN MARKER VERSION URL ROOT_CRT SVC_USER
msg_info "=== Configuration Automatisée du Client PKI ==="
# 1. Saisie des informations de base
read -rp "IP du serveur CA [${CONFIG_DEB_INSTALL_DEFAULT_CA_SERVER}] : " INPUT_IP
CA_IP=${INPUT_IP:-${CONFIG_DEB_INSTALL_DEFAULT_CA_SERVER}}
read -rp "Domaine Wildcard [${CONFIG_DEB_INSTALL_DEFAULT_CA_WILDCARD}] : " INPUT_DOMAIN
WILDCARD_DOMAIN=${INPUT_DOMAIN:-${CONFIG_DEB_INSTALL_DEFAULT_CA_WILDCARD}}
# Petit bonus : on s'assure que le CA_URL est bien formé
CA_URL="https://$(echo "$CA_IP" | sed 's|https://||')"
echo -e "Configuration retenue : IP=$CA_IP | Domaine=$WILDCARD_DOMAIN"
CA_URL="https://$CA_IP"
# 2. Récupération automatique de la Fingerprint via HTTPS
msg_warning "[1/6] Récupération de l'empreinte via HTTPS..."
# Cette commande récupère le certificat du serveur et calcule son empreinte SHA256
CA_FINGERPRINT=$(openssl s_client -connect "${CA_IP}:443" </dev/null 2>/dev/null | openssl x509 -fingerprint -sha256 -noout | cut -d'=' -f2 | tr -d ':')
if [ -z "$CA_FINGERPRINT" ]; then
msg_error "Erreur : Impossible de contacter le serveur sur le port 443. Vérifie l'IP."
exit 1
fi
msg_info "Empreinte détectée : ${GREEN}$CA_FINGERPRINT${NONE}"
# 3. Installation de Step CLI (si besoin)
if ! command -v step &> /dev/null; then
msg_warning "[2/6] Installation du paquet Step CLI..."
# On télécharge une version stable spécifique
VERSION="0.24.4"
URL="https://github.com/smallstep/cli/releases/download/v${VERSION}/step-cli_${VERSION}_amd64.deb"
wget -q $URL -O /tmp/step.deb
if [ $? -eq 0 ]; then
dpkg -i /tmp/step.deb > /dev/null 2>&1
rm /tmp/step.deb
else
msg_error "Échec du téléchargement du paquet .deb"
exit 1
fi
fi
# On définit le binaire (maintenant il est forcément dans /usr/bin/step)
STEP_BIN="/usr/bin/step"
# 4. Configuration et Certificat
msg_warning "[3/6] Liaison et génération du certificat..."
# 1. On force un environnement de travail propre
export STEPPATH="/tmp/step-config"
rm -rf "$STEPPATH"
mkdir -p "$STEPPATH"
# 2. Utilisation de la Fingerprint confirmée
CA_FINGERPRINT="4873b9eaeb8a7643475939b4035221bd1bc3acd0db00e94df5a76d771459f439"
# 3. Liaison (Bootstrap)
step ca bootstrap --ca-url "$CA_URL" --fingerprint "$CA_FINGERPRINT" --force
# Chemin du certificat racine récupéré
ROOT_CRT="$STEPPATH/certs/root_ca.crt"
# 4. Génération du Wildcard
mkdir -p /etc/ssl/wildcard
BASE_DOMAIN=$(echo "$WILDCARD_DOMAIN" | sed 's/\*\.//')
step ca certificate "$WILDCARD_DOMAIN" /etc/ssl/wildcard/server.crt /etc/ssl/wildcard/server.key \
--ca-url "$CA_URL" \
--root "$ROOT_CRT" \
--san "$WILDCARD_DOMAIN" \
--san "$BASE_DOMAIN" \
--not-after=8760h \
--force
msg_success "Certificat généré avec succès dans /etc/ssl/wildcard/"
msg_warning "[4/6] Modification des droits de lecture du certificat..."
# S'assurer que le groupe existe avant la boucle
groupadd -f ssl-cert
for SVC in $CONFIG_DEB_INSTALL_DEFAULT_CA_SERVICES; do
if systemctl is-active --quiet "$SVC"; then
# On récupère l'utilisateur. On ajoute || true pour éviter que le script s'arrête si pgrep échoue
PID=$(pgrep -f "$SVC" | head -n1)
if [ -n "$PID" ]; then
SVC_USER=$(ps -o user= -p "$PID" | tr -d ' ')
if [ -n "$SVC_USER" ] && [ "$SVC_USER" != "root" ]; then
echo "🚀 Service détecté : $SVC (Utilisateur : $SVC_USER)"
# Ajout au groupe
usermod -aG ssl-cert "$SVC_USER"
# Application des droits sur la clé
chown root:ssl-cert /etc/ssl/wildcard/server.key
chmod 640 /etc/ssl/wildcard/server.key
# Le certificat doit être lisible (souvent déjà 644)
chmod 644 /etc/ssl/wildcard/server.crt
msg_success "Droit ajouté pour $SVC_USER sur $SVC"
# CRITIQUE : Redémarrage pour appliquer le changement de groupe
msg_warning "Redémarrage de $SVC pour appliquer les nouveaux droits..."
systemctl restart "$SVC"
fi
fi
fi
done
msg_warning "[5/6] Installation automatique du Root CA..."
# 1. Définition des chemins
# On s'assure de retrouver le fichier même si la variable a sauté
local TEMP_ROOT="/tmp/step-config/certs/root_ca.crt"
local SYSTEM_TARGET="/usr/local/share/ca-certificates/step-ca-frogg.crt"
if [ -f "$TEMP_ROOT" ]; then
# 2. On nettoie le certificat (format PEM pur) pour éviter l'erreur 'rehash'
# Cela extrait uniquement le certificat et ignore le texte inutile
openssl x509 -in "$TEMP_ROOT" -out "$SYSTEM_TARGET"
# 3. Mise à jour du magasin (sans --fresh pour éviter les warnings inutiles)
update-ca-certificates > /dev/null 2>&1
# 4. Vérification réelle
if [ $? -eq 0 ]; then
msg_success "Le système fait maintenant confiance au Root CA Frogg."
else
msg_error "Échec lors de la mise à jour des certificats système."
fi
else
msg_error "Source introuvable dans $TEMP_ROOT. Vérifie que l'étape 3 a réussi."
fi
# --- ÉTAPE 6 (Cron & Nettoyage final) ---
# C'est seulement MAINTENANT qu'on peut supprimer
rm -rf "/tmp/step-config"
# 5. Planification
msg_warning "[6/6] Activation du renouvellement automatique..."
# On récupère la liste des services à redémarrer (ceux définis dans ta config)
# On crée une commande de restart groupée : systemctl restart service1 service2
RESTART_CMD=""
for SVC in $CONFIG_DEB_INSTALL_DEFAULT_CA_SERVICES; do
if systemctl is-active --quiet "$SVC"; then
RESTART_CMD="$RESTART_CMD $SVC"
fi
done
# Construction de la ligne Cron avec le restart à la fin
# Le "&&" assure que le restart ne se fait QUE si le renew a réussi
STEP_BIN=$(which step)
CRON_COMMAND="$STEP_BIN certificate renew /etc/ssl/wildcard/server.crt /etc/ssl/wildcard/server.key --force >> /var/log/cert-renew.log 2>&1"
if [ -n "$RESTART_CMD" ]; then
CRON_COMMAND="$CRON_COMMAND && systemctl restart $RESTART_CMD"
fi
MARKER="# STEP-CA-RENEWAL"
(
crontab -l 2>/dev/null | sed "/$MARKER/,/$MARKER/d"
echo "$MARKER"
echo "0 0 1 * * $CRON_COMMAND"
echo "$MARKER"
) | crontab -
msg_success "Cron mis à jour avec restart automatique des services."
msg_success " "
msg_success " ################################"
msg_success " # Tout est prêt et configuré ! # "
msg_success " ###########(/etc/ssl/wildcard/)#"
msg_success " "
}

10
script/renew-cert.sh Normal file
View File

@ -0,0 +1,10 @@
#!/bin/bash
if [[ -z "${WELCOME_SCRIPT_PATH}" ]]; then
WELCOME_SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &>/dev/null && pwd)"
fi
. "${WELCOME_SCRIPT_PATH}/config/config_colors.sh"
. "${WELCOME_SCRIPT_PATH}/func/message.sh"