This repository has been archived on 2026-11-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Ephaitek-provisioning/scripts/admin/steps/03-pki.sh
T

89 lines
4.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ==============================================================================
# 03-pki.sh — Attente SSH de la VM + import CA Vault dans le trust-store système
# ==============================================================================
# Variables reçues de run-all.sh (export) :
# NAME, VM_ADMIN_IP, SSH_PORT, DRY_RUN, ADMIN_TOOLS
#
# La VM est joignable via son IP admin (VLAN 100) sur le port SSH dérivé du VLAN
# ==============================================================================
set -euo pipefail
VAULT_CA_CERT="${ADMIN_TOOLS}/pki/root.crt"
SSH_TIMEOUT=300 # secondes max d'attente avant abandon
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; BLUE='\033[0;34m'; NC='\033[0m'
info() { echo -e " ${BLUE}›${NC} $*"; }
ok() { echo -e " ${GREEN}✔${NC} $*"; }
warn() { echo -e " ${YELLOW}⚠${NC} $*"; }
die() { echo -e " ${RED}✖${NC} $*" >&2; exit 1; }
# ─── Attente SSH ──────────────────────────────────────────────────────────────
wait_for_ssh() {
local host="$1" port="$2" elapsed=0
info "Attente SSH sur ${host}:${port} (timeout ${SSH_TIMEOUT}s)..."
while ! nc -z -w 3 "${host}" "${port}" 2>/dev/null; do
sleep 5; elapsed=$((elapsed + 5))
if [[ ${elapsed} -ge ${SSH_TIMEOUT} ]]; then
die "VM ${NAME} (${host}:${port}) injoignable après ${SSH_TIMEOUT}s — vérifier le boot Cloud-Init"
fi
info " … ${elapsed}s / ${SSH_TIMEOUT}s"
done
ok "SSH joignable: ${host}:${port}"
}
# ─── SSH vers la VM ───────────────────────────────────────────────────────────
vm_ssh() {
ssh -p "${SSH_PORT}" \
-o StrictHostKeyChecking=no \
-o ConnectTimeout=10 \
-o BatchMode=yes \
"root@${VM_ADMIN_IP}" "$@"
}
# ─── Vérifications préalables ─────────────────────────────────────────────────
[[ ! -f "${VAULT_CA_CERT}" ]] && die "CA cert Vault absent: ${VAULT_CA_CERT}"
CERT_SHA=$(sha256sum "${VAULT_CA_CERT}" | awk '{print $1}')
info "CA cert : ${VAULT_CA_CERT}"
info "SHA256 : ${CERT_SHA:0:24}…"
info "VM cible : root@${VM_ADMIN_IP}:${SSH_PORT}"
if [[ "${DRY_RUN}" == true ]]; then
echo -e " ${YELLOW}[DRY-RUN]${NC} wait_for_ssh ${VM_ADMIN_IP}:${SSH_PORT}"
echo -e " ${YELLOW}[DRY-RUN]${NC} scp ${VAULT_CA_CERT} root@${VM_ADMIN_IP}:/etc/pki/ca-trust/source/anchors/ephaitek-root.crt"
echo -e " ${YELLOW}[DRY-RUN]${NC} update-ca-trust extract [sur ${VM_ADMIN_IP}]"
exit 0
fi
# ─── Attente démarrage VM ─────────────────────────────────────────────────────
wait_for_ssh "${VM_ADMIN_IP}" "${SSH_PORT}"
# Attente supplémentaire que Cloud-Init finisse (sshd peut répondre avant la fin)
info "Attente fin Cloud-Init (15s)..."
sleep 15
# ─── Push CA cert ─────────────────────────────────────────────────────────────
info "Copie du CA cert vers la VM..."
scp -P "${SSH_PORT}" \
-o StrictHostKeyChecking=no \
-o ConnectTimeout=10 \
"${VAULT_CA_CERT}" \
"root@${VM_ADMIN_IP}:/etc/pki/ca-trust/source/anchors/ephaitek-root.crt" \
|| die "SCP du CA cert échoué"
info "Import dans le trust-store système (update-ca-trust)..."
vm_ssh bash <<'REMOTE'
set -euo pipefail
chmod 644 /etc/pki/ca-trust/source/anchors/ephaitek-root.crt
update-ca-trust extract
# Vérification
if trust list 2>/dev/null | grep -i "ephaitek" >/dev/null; then
echo "[PKI] Trust anchor Ephaitek importé et confirmé"
else
echo "[PKI] WARN: anchor non visible dans 'trust list' — peut être normal selon la version"
fi
REMOTE
ok "CA cert Vault importé dans /etc/pki/ca-trust/ sur ${NAME}"