Bash : Ajouter du texte derrière une balise
De www.yakakliker.org
Description
Voici un cas concret ou j'ai souhaité scripter la configuration de Haproxy :
- Le fichier de configuration Haproxy se trouve dans /etc/haproxy/haproxy.cfg
La complexité de l'affaire se situe dans le fait qu'il y a des informations à ajouter dans des endroits différents du fichier.
- Voici un exemple de fichier haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
tune.bufsize 65536
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend https
bind *:443 ssl crt /etc/haproxy/certs/wildcard.yakakliker.org.pem
mode http
option httplog
http-request redirect scheme https code 301 unless { ssl_fc }
acl mon_appli_acl hdr(host) mon_appli.yakakliker.org
use_backend mon_appli if mon_appli_acl
backend mon_appli
mode http
option httpchk GET /
option forwardfor
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server mon_appli 127.0.0.1:8008
Comme on peut le voir, la configuration se fait en deux parties :
- Définition de l'ACL dans le bloc Frontend :
acl mon_appli_acl hdr(host) mon_appli.yakakliker.org
use_backend mon_appli if mon_appli_acl
- Définition du Backend correspondant à la déclaration du Frontend :
backend mon_appli
mode http
option httpchk GET /
option forwardfor
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server mon_appli 127.0.0.1:8008
La solution est de placer une balise au début de chaque bloc :
- Exemple :
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
tune.bufsize 65536
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend https
bind *:443 ssl crt /etc/haproxy/certs/wildcard.yakakliker.org.pem
mode http
option httplog
http-request redirect scheme https code 301 unless { ssl_fc }
#################################################################
# INSERT_ACL
acl mon_appli_acl hdr(host) mon_appli.yakakliker.org
use_backend mon_appli if mon_appli_acl
##################################################################
# INSERT_BACKEND
backend mon_appli
mode http
option httpchk GET /
option forwardfor
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server mon_appli 127.0.0.1:8008
Le script s'appuiera sur ces balises pour y insérer les nouvelles configurations.
Le script
#!/bin/bash
# Variables de la nouvelle application
APP_NAME="mon-app"
APP_HOST="mon-app.yakakliker.org"
APP_PORT="8888"
# 1. Création du bloc ACL
ACL_ENTRY=" acl ${APP_NAME}_acl hdr(host) ${APP_HOST}\n use_backend ${APP_NAME} if ${APP_NAME}_acl"
# 2. Création du bloc BACKEND
BACKEND_ENTRY="backend ${APP_NAME}\n mode http\n option httpchk GET /\n option forwardfor\n http-request add-header X-Forwarded-Proto https if { ssl_fc }\n server ${APP_NAME} 127.0.0.1:${APP_PORT}\n"
# 3. Insertion dans le fichier haproxy.cfg
# On cherche la balise et on ajoute le texte après (commande 'a')
sed -i "/# INSERT_ACL/a $ACL_ENTRY" /etc/haproxy/haproxy.cfg
sed -i "/# INSERT_BACKEND/a $BACKEND_ENTRY" /etc/haproxy/haproxy.cfg
Nous pouvons enrichir ce script comme ceci :
#!/bin/bash
############ Les variables
export selection="(◕_◕)"
export var25042301="(°_°)"
export var25070203="°(◕_◕)°"
export ver="PRY.260112"
export var24051101
var24051101=$(date +%y%m%d) # Variable date année-mois-jour
export var26011203="/etc/haproxy/haproxy.cfg"
export var26011204="$var26011203.$var24051101"
export alert="root=,red;window=,white;border=black,white;textbox=black,white;button=red,white"
export info="root=,blue;window=,white;border=black,white;textbox=black,white;button=red,white"
#####################################################
## Le code ? C'est le code !
var25051201=$(NEWT_COLORS="$info" whiptail --inputbox "$var25070203 : Nom de l'application" --title "$ver" 10 60 3>&1 1>&2 2>&3)
var25110701=$(NEWT_COLORS="$info" whiptail --inputbox "$var25070203 : Port de l'application" --title "$ver" 10 60 3>&1 1>&2 2>&3)
var25120801=$(NEWT_COLORS="$info" whiptail --inputbox "$var25070203 : URL (sans https://)" --title "$ver" 10 60 3>&1 1>&2 2>&3)
# Contrôle de redondance
if grep -q "acl ${var25051201}_acl" "$var26011203"; then
echo "L'entrée pour $var25051201 existe déjà."
else
# Sauvegarde de sécurité
cp "$var26011203" "$var26011204"
# Création du bloc ACL
var26011201="acl ${var25051201}_acl hdr(host) ${var25120801}\n use_backend ${var25051201} if ${var25051201}_acl\n\n"
# Création du bloc BACKEND
var26011202="backend ${var25051201}\n mode http\n option httpchk GET /\n option forwardfor\n http-request add-header X-Forwarded-Proto https if { ssl_fc }\n server ${var25051201} 127.0.0.1:${var25110701}\n\n"
# Insertion dans le fichier haproxy.cfg
# On cherche la balise et on ajoute le texte après (commande 'a')
sed -i "/# INSERT_ACL/a $var26011201" "$var26011203"
sed -i "/# INSERT_BACKEND/a $var26011202" "$var26011203"
NEWT_COLORS="$info" whiptail --msgbox "$var25070203 : Configuration haproxy créée." --title "$ver" 10 60
export var25062701
var25062701=$(haproxy -f $var26011203 -c 2>&1)
exitstatus=$?
if [ $exitstatus = 0 ]; then
NEWT_COLORS="$info" whiptail --msgbox "(◕_◕) : Le fichier de configuration est valide !" --title "Fichier valide" 8 55
NEWT_COLORS="$info" whiptail --msgbox "(◕_◕) : Rechargement de la configuration !" --title "$ver" 8 55
systemctl reload haproxy;
clear;
else
NEWT_COLORS="$alert" whiptail --msgbox "$var25062701" --title "Configuration non valide" 20 100
fi
fi