reecriture validate_config_js
This commit is contained in:
parent
cb43cb75b7
commit
2da84289f1
99
update.sh
99
update.sh
@ -222,77 +222,46 @@ check_conf() {
|
|||||||
check_conf "$CONF_SH"
|
check_conf "$CONF_SH"
|
||||||
|
|
||||||
# ────────── VALIDATION config.js (avec Node) ──────────
|
# ────────── VALIDATION config.js (avec Node) ──────────
|
||||||
|
# remplace validate_config_js par :
|
||||||
validate_config_js() {
|
validate_config_js() {
|
||||||
[[ -f "$CFG_JS" ]] || { err "Manquant: $CFG_JS"; errors=$((errors+1)); return; }
|
[[ -f "$CFG_JS" ]] || { err "Manquant: $CFG_JS"; errors=$((errors+1)); return; }
|
||||||
|
# récupère des paires clé: valeur basiques (sans exécuter)
|
||||||
|
parse() { grep -E "^\s*$1\s*:" "$CFG_JS" | head -n1 | sed -E "s/.*:\s*//; s/[,'\"]//g; s,//.*,,"; }
|
||||||
|
|
||||||
log "Validation de $CFG_JS…"
|
dbtype="$(parse dbtype)"
|
||||||
local CHECK="$TMP_DIR/check_config.js"
|
port="$(parse port)"
|
||||||
cat > "$CHECK" <<'JS'
|
name="$(parse name)"
|
||||||
const fs = require('fs');
|
sessionSecret="$(parse sessionSecret)"
|
||||||
const path = process.argv[2];
|
DB_TABLE="$(parse DB_TABLE)"
|
||||||
function isEmptyOrPlaceholder(v){
|
finishdirectory="$(parse finishdirectory)"
|
||||||
if (v == null) return true;
|
logdirectory="$(parse logdirectory)"
|
||||||
if (typeof v === 'string') {
|
infodirectory="$(parse infodirectory)"
|
||||||
const s = v.trim();
|
|
||||||
if (!s) return true;
|
|
||||||
const P = [/^voir/i, /^change/i, /^changeme/i, /^todo/i, /^example/i, /^your/i, /^\/path\/to/i];
|
|
||||||
if (P.some(rx => rx.test(s))) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const cfg = require(path);
|
|
||||||
let errs = [];
|
|
||||||
|
|
||||||
// requis généraux
|
# checks minimaux
|
||||||
if (!Number.isInteger(cfg.port) || cfg.port < 1 || cfg.port > 65535)
|
[[ "$port" =~ ^[0-9]+$ ]] && [ "$port" -ge 1 ] && [ "$port" -le 65535 ] || { err "config.js: port invalide"; errors=$((errors+1)); }
|
||||||
errs.push("config.js: 'port' doit être un entier 1-65535");
|
[ -n "$name" ] || { err "config.js: name vide"; errors=$((errors+1)); }
|
||||||
if (isEmptyOrPlaceholder(cfg.name)) errs.push("config.js: 'name' non renseigné");
|
[[ "$sessionSecret" =~ ^(Voir|change|CHANGE|todo|TODO|example|your|/path/to|)$ ]] && { err "config.js: sessionSecret non renseigné"; errors=$((errors+1)); }
|
||||||
if (isEmptyOrPlaceholder(cfg.sessionSecret)) errs.push("config.js: 'sessionSecret' non renseigné");
|
[ -n "$DB_TABLE" ] || { err "config.js: DB_TABLE vide"; errors=$((errors+1)); }
|
||||||
if (typeof cfg.trustProxy === 'undefined') errs.push("config.js: 'trustProxy' manquant");
|
|
||||||
if (typeof cfg.cookieSecure === 'undefined') errs.push("config.js: 'cookieSecure' manquant");
|
|
||||||
if (isEmptyOrPlaceholder(cfg.DB_TABLE)) errs.push("config.js: 'DB_TABLE' non renseigné");
|
|
||||||
|
|
||||||
// chemins
|
for d in "$finishdirectory" "$logdirectory" "$infodirectory"; do
|
||||||
const pathKeys = ['finishdirectory', 'logdirectory', 'infodirectory', 'sessionStorePath'];
|
[ -d "$d" ] || { err "config.js: dossier manquant: $d"; errors=$((errors+1)); }
|
||||||
for (const k of pathKeys) {
|
done
|
||||||
if (typeof cfg[k] === 'string' && cfg[k].trim()) {
|
|
||||||
try { if (!fs.existsSync(cfg[k])) errs.push(`config.js: chemin inexistant pour '${k}': ${cfg[k]}`); }
|
|
||||||
catch {}
|
|
||||||
} else if (k !== 'sessionStorePath') {
|
|
||||||
errs.push(`config.js: '${k}' non renseigné`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// règles conditionnelles dbtype
|
case "$dbtype" in
|
||||||
if (!cfg.dbtype || !['sqlite','mysql'].includes(cfg.dbtype)) {
|
sqlite)
|
||||||
errs.push("config.js: 'dbtype' doit être 'sqlite' ou 'mysql'");
|
dbFile="$(parse dbFile)"
|
||||||
} else if (cfg.dbtype === 'sqlite') {
|
[ -n "$dbFile" ] && [ -d "$(dirname "$dbFile")" ] || { err "config.js: dbFile requis (sqlite)"; errors=$((errors+1)); }
|
||||||
if (isEmptyOrPlaceholder(cfg.dbFile)) {
|
;;
|
||||||
errs.push("config.js: 'dbFile' requis en mode sqlite");
|
mysql)
|
||||||
} else {
|
DB_HOST="$(parse DB_HOST)"; DB_PORT="$(parse DB_PORT)"; DB_USER="$(parse DB_USER)"; DB_PASSWORD="$(parse DB_PASSWORD)"; DB_DATABASE="$(parse DB_DATABASE)"
|
||||||
const dir = require('path').dirname(cfg.dbFile);
|
[ -n "$DB_HOST" ] || { err "config.js: DB_HOST requis (mysql)"; errors=$((errors+1)); }
|
||||||
if (!fs.existsSync(dir)) errs.push(`config.js: dossier de 'dbFile' inexistant: ${dir}`);
|
[[ "$DB_PORT" =~ ^[0-9]+$ ]] || { err "config.js: DB_PORT entier requis (mysql)"; errors=$((errors+1)); }
|
||||||
}
|
[ -n "$DB_USER" ] || { err "config.js: DB_USER requis (mysql)"; errors=$((errors+1)); }
|
||||||
// champs MySQL facultatifs → ne rien exiger
|
[ -n "$DB_PASSWORD" ] || { err "config.js: DB_PASSWORD requis (mysql)"; errors=$((errors+1)); }
|
||||||
} else if (cfg.dbtype === 'mysql') {
|
[ -n "$DB_DATABASE" ] || { err "config.js: DB_DATABASE requis (mysql)"; errors=$((errors+1)); }
|
||||||
// dbFile facultatif → ne rien exiger
|
;;
|
||||||
if (isEmptyOrPlaceholder(cfg.DB_HOST)) errs.push("config.js: 'DB_HOST' requis en mode mysql");
|
*) err "config.js: dbtype doit être sqlite ou mysql"; errors=$((errors+1));;
|
||||||
if (!Number.isInteger(cfg.DB_PORT)) errs.push("config.js: 'DB_PORT' entier requis en mode mysql");
|
esac
|
||||||
if (isEmptyOrPlaceholder(cfg.DB_USER)) errs.push("config.js: 'DB_USER' requis en mode mysql");
|
|
||||||
if (isEmptyOrPlaceholder(cfg.DB_PASSWORD)) errs.push("config.js: 'DB_PASSWORD' requis en mode mysql");
|
|
||||||
if (isEmptyOrPlaceholder(cfg.DB_DATABASE)) errs.push("config.js: 'DB_DATABASE' requis en mode mysql");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errs.length) { console.error(errs.join('\n')); process.exit(2); }
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`Impossible de charger ${path}: ${e.message}`);
|
|
||||||
process.exit(2);
|
|
||||||
}
|
|
||||||
JS
|
|
||||||
if ! node "$CHECK" "$CFG_JS"; then
|
|
||||||
errors=$((errors+1))
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
validate_config_js
|
validate_config_js
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user