Partie 3 : Mise en place de HTTPS dans un Cluster Unifi

Bonjour à tous ! Bienvenue dans la 3ème et dernière partie consacré à notre tutoriel sur les clusters Unifi ! Dans cette partie, nous allons nous intéresser à HTTPS : comment sécuriser les interfaces Web de notre cluster Unifi ? 

Pour mettre en place HTTPS, nous allons nous baser sur Let’s Encrypt, l’outil bien connu permettant de délivrer des certificats gratuitement. Et sur son utilitaire Certbot, proposé pour les serveurs Unix.

Ce tutoriel est fortement inspiré de celui proposé par “sblaisot” ici : https://blog.blaisot.org/letsencrypt-wildcard-part1.html#comment-4091551395. Je l’ai ensuite appliqué à notre cluster.

Ce tutoriel utilise aussi les outils proposés par Steve Jenkins, sur son Github, pour Unifi : https://github.com/stevejenkins/unifi-linux-utils. N’hésitez pas à y jeter un œil, il y a plein d’outils dédiés à Unifi !

0. Vérifications et Pré-requis

Avant de s’attaquer à la configuration, il va être nécessaire de réaliser des vérifications, notamment au niveau DNS. En effet, la mise en place de certificat demande une configuration DNS fonctionnelle et stable : Let’s Encrypt utilise les enregistrements DNS de votre domaine pour vérifier que vous en êtes bien le propriétaire.

On va commencer par préciser à nouveau le cadre de travail de ce tutoriel :

  • Deux serveurs dédiés, le serveur Unifi1 d’IP X.X.X.X et le serveur Unifi2 d’IP Y.Y.Y.Y
  • Une IP Failover Z.Z.Z.Z, qui peut être attribuée a un des deux serveurs selon le besoin.

Les IPs sont des IP Publiques, accessibles depuis Internet.
Nous avons aussi trois enregistrements DNS, sur le domaine de votre choix. Ici pour l’exemple, votredomaine.fr :

  • unifi1.votredomaine.fr pointant vers X.X.X.X,
  • unifi2.votredomaine.fr pointant vers Y.Y.Y.Y,
  • unifi.votredomaine.fr pointant vers Z.Z.Z.Z

On va donc vérifier que nos noms de domaines pointent bien aux bons endroits : l’outil “nslookup“, sur Windows ou Unix, vous permet de faire cette vérification :

root@unifi1:~# nslookup unifi1.votredomaine.fr
Server:         62.210.16.6
Address:        62.210.16.6#53

Non-authoritative answer:
Name:   unifi1.votredomaine.fr
Address: X.X.X.X

root@unifi1:~# nslookup unifi.votredomaine.fr
Server:         62.210.16.6
Address:        62.210.16.6#53

Non-authoritative answer:
Name:   unifi.votredomaine.fr
Address: Z.Z.Z.Z

root@unifi1:~# nslookup unifi2.votredomaine.fr
Server:         62.210.16.6
Address:        62.210.16.6#53

Non-authoritative answer:
Name:   unifi2.votredomaine.fr
Address: Y.Y.Y.Y

Ici tous les noms sont correctement configurés. On va ensuite vérifier que vos fichiers /etc/hosts sont bien configurés. Tout d’abord sur le serveur primaire :

root@unifi1:~# nano /etc/hosts
127.0.0.1       localhost
127.0.1.1       unifi1.votredomaine.fr unifi1

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Puis sur le serveur secondaire

root@unifi2:~# nano /etc/hosts
127.0.0.1       localhost
127.0.1.1       unifi2.votredomaine.fr unifi2

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

On voit ici que les deux serveurs sont bien déclarés, en nom court et et en nom long. Tout est prêt pour lancer le tutoriel !

1. Création du certificat

Dans un cas standard, on pourrait créer un certificat pour chaque serveur, avec l’utilitaire certbot, et ce serait à peu près tout. Toutefois, ici on est dans le cas d’un cluster : si on met en place un certificat pour le premier serveur unifi1.votredomaine.fr, et un autre pour le deuxième serveur unifi2.votredomaine.fr, il risque d’avoir un problème concernant unifi.votredomaine.fr. Cette entrée DNS, qui sert de ticket d’entrée au cluster, sera en erreur car l’interface Web pointera sur unifi1.votredomaine.fr ou unifi2.votredomaine.fr et le nom ne correspondra pas au certificat ! D’où une erreur SSL, ce qui va a l’encontre de l’intérêt du tutoriel.

Pour résoudre facilement ce problème, on va simplement utiliser l’option Wildcard de Let’s Encrypt : le wildcard en SSL permet de créer un certificat qui soit valable pour tous les sous-domaines d’un domaine. Exemple ici, nous allons créer le wildcard pour *.votredomaine.fr, et le certificat deviendra valable pour tous les sous-domaines xxxx.votredomaine.fr, ce qui règle notre problème !

Pour pouvoir créer ce certificat SSL sur le serveur primaire, nous allons nous appuyer sur l’utilitaire Certbot proposé par Let’s Encrypt :

root@unifi1:~# mkdir /opt/certbot
root@unifi1:~# cd /opt/certbot
root@unifi1:~# wget https://dl.eff.org/certbot-auto
root@unifi1:~# chmod a+x certbot-auto
root@unifi1:~# ./certbot-auto --install-only

On lance ensuite la création d’un certificat Wildcard pour votre domaine (remplacer le domaine dans la commande ci-dessous par le vôtre). Sur le serveur primaire toujours : 

root@unifi1:~# cd /opt/eff.org/certbot/venv/bin/
root@unifi1:/opt/eff.org/certbot/venv/bin# ./certbot certonly --server https://acme-v02.api.letsencrypt.org/directory \
                     --manual -d '*.votredomaine.fr'

Entrez votre adresse email, puis répondre “A” pour accepter les termes et conditions. Répondez “Y” ou “N” selon si vous acceptez de partager votre adresse mail avec EFF. Puis répondre à nouveau “Y” pour que votre IP soit logguée. Enfin, vous allez recevoir une ligne de texte avec des caractères aléatoires :

root@unifi1:/opt/eff.org/certbot/venv/bin# ./certbot certonly --server https://acme-v02.api.letsencrypt.org/directory \
>                      --manual -d '*.votredomaine.fr'
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): sebastien@votredomaine.fr

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for votredomaine.fr

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.votredomaine.fr with the following value:

TgomGu6_kOYJshtHhiotln5VOgRglrCmHLlhRhA38As

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

A ce moment là, la commande se mettra en pause. Il va falloir aller sur sur l’interface Web de votre fournisseur DNS, créer l’enregistrement TXT suivant (remplacer la clé par celle que Certbot vous donne durant la commande).

_acme-challenge        IN    TXT        6CmoURMbv3F14hJdzR8zqXrhcYJeKWJEhT8xZcz4gUY

Exemple dans mon cas, mon nom de domaine est hébergé chez OVH

Création d’une entrée TXT sur l’interface OVH

Une fois que votre entrée TXT est créée, patientez le temps nécessaire pour qu’il soit pris en compte, avant de continuer la commande : la propagation peut prendre un certain temps. Dans mon cas quelques minutes, mais selon votre fournisseur, ça peut-être beaucoup plus long. Puis taper sur “Entrée” une fois que votre entrée à été prise en compte, afin de laisser la commande se terminer :

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/votredomaine.fr/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/votredomaine.fr/privkey.pem
   Your cert will expire on 2019-02-19. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Si tout se passe comme prévu, votre certificat est créé ! Il est disponible dans “/etc/letsencrypt/live/votredomaine.fr/fullchain.pem“.

2. Mise en place des certificats pour Unifi

Le certificat est créé, toutefois il n’est pas encore utilisé par Unifi : il va falloir dire à Unifi d’utiliser le certificat ainsi créé pour son interface Web. Pour cela, nous allons exploiter le script “unifi_ssl_import.sh” disponible sur le Github de Steve Jenkins

Sur le serveur primaire, on récupère le script en question :

root@unifi1:~# wget https://raw.githubusercontent.com/stevejenkins/unifi-linux-utils/master/unifi_ssl_import.sh -O /usr/local/bin/unifi_ssl_import.sh

On le modifie pour notre utilisation : 

root@unifi1:~# nano /usr/local/bin/unifi_ssl_import.sh
# CONFIGURATION OPTIONS
UNIFI_HOSTNAME=unifi1.votredomaine.fr
UNIFI_SERVICE=unifi

# Uncomment following three lines for Fedora/RedHat/CentOS
#UNIFI_DIR=/opt/UniFi
#JAVA_DIR=${UNIFI_DIR}
#KEYSTORE=${UNIFI_DIR}/data/keystore

# Uncomment following three lines for Debian/Ubuntu
UNIFI_DIR=/var/lib/unifi
JAVA_DIR=/usr/lib/unifi
KEYSTORE=${UNIFI_DIR}/keystore

# Uncomment following three lines for CloudKey
#UNIFI_DIR=/var/lib/unifi
#JAVA_DIR=/usr/lib/unifi
#KEYSTORE=${JAVA_DIR}/data/keystore

# FOR LET'S ENCRYPT SSL CERTIFICATES ONLY
# Generate your Let's Encrtypt key & cert with certbot before running this script
LE_MODE=no
LE_LIVE_DIR=/etc/letsencrypt/live

# THE FOLLOWING OPTIONS NOT REQUIRED IF LE_MODE IS ENABLED
PRIV_KEY=/etc/letsencrypt/live/votredomaine.fr/privkey.pem
SIGNED_CRT=/etc/letsencrypt/live/votredomaine.fr/cert.pem
CHAIN_FILE=/etc/letsencrypt/live/votredomaine.fr/fullchain.pem

Modifiez les informations suivantes :

  • UNIFI_HOSTNAME=unifi1.votredomaine.fr : Remplacez votredomaine.fr par le nom de votre domaine
  • PRIV_KEYSIGNED_CRTCHAIN_FILE : Remplacez votredomaine.fr par le nom de votre domaine

Sur le serveur secondaire, on copie aussi le script. On créé le répertoire /etc/letsencrypt et on modifie le script :

root@unifi2:~# wget https://raw.githubusercontent.com/stevejenkins/unifi-linux-utils/master/unifi_ssl_import.sh -O /usr/local/bin/unifi_ssl_import.sh
root@unifi2:~# mkdir /etc/letsencrypt
root@unifi2:~# nano /usr/local/bin/unifi_ssl_import.sh
# CONFIGURATION OPTIONS
UNIFI_HOSTNAME=unifi2.votredomaine.fr
UNIFI_SERVICE=unifi

# Uncomment following three lines for Fedora/RedHat/CentOS
#UNIFI_DIR=/opt/UniFi
#JAVA_DIR=${UNIFI_DIR}
#KEYSTORE=${UNIFI_DIR}/data/keystore

# Uncomment following three lines for Debian/Ubuntu
UNIFI_DIR=/var/lib/unifi
JAVA_DIR=/usr/lib/unifi
KEYSTORE=${UNIFI_DIR}/keystore

# Uncomment following three lines for CloudKey
#UNIFI_DIR=/var/lib/unifi
#JAVA_DIR=/usr/lib/unifi
#KEYSTORE=${JAVA_DIR}/data/keystore

# FOR LET'S ENCRYPT SSL CERTIFICATES ONLY
# Generate your Let's Encrtypt key & cert with certbot before running this script
LE_MODE=no
LE_LIVE_DIR=/etc/letsencrypt/live

# THE FOLLOWING OPTIONS NOT REQUIRED IF LE_MODE IS ENABLED
PRIV_KEY=/etc/letsencrypt/live/votredomaine.fr/privkey.pem
SIGNED_CRT=/etc/letsencrypt/live/votredomaine.fr/cert.pem
CHAIN_FILE=/etc/letsencrypt/live/votredomaine.fr/fullchain.pem

Modifiez les informations suivantes :

  • UNIFI_HOSTNAME=unifi2.votredomaine.fr : Remplacez votredomaine.fr par le nom de votre domaine
  • PRIV_KEY & SIGNED_CRTCHAIN_FILE : Remplacez votredomaine.fr par le nom de votre domaine

Sur le serveur primaire, on va réutiliser le paquet Unison pour synchroniser les dossiers /etc/letsencrypt/ entre le serveur Unifi1 et Unifi2. Lancez la commande suivante en remplaçant votredomaine.fr par votre domaine : 

root@unifi1:~# /usr/bin/unison -batch /etc/letsencrypt/ ssh://root@unifi2.votredomaine.fr//etc/letsencrypt/

Contacting server...
Connected [//unifi1//etc/letsencrypt -> //unifi2//etc/letsencrypt]
Looking for changes
  Waiting for changes from server
Reconciling changes
new file <-?-> new file   csr/0000_csr-letsencrypt.pem
local        : new file           modified on 2018-11-20 at 15:02:35  size 972       rw-r--r--
unifi2       : new file           modified on 2018-11-20 at 15:31:12  size 972       rw-r--r--
new file <-?-> new file   keys/0000_key-letsencrypt.pem
local        : new file           modified on 2018-11-20 at 15:02:35  size 1704      rw-------
unifi2       : new file           modified on 2018-11-20 at 15:31:12  size 1704      rw-------
props    <-?-> props      live
local        : dir props changed  modified on 2018-11-20 at 15:56:36  size 1520      rwx------
unifi2       : dir props changed  modified on 2018-11-20 at 15:28:53  size 88        rwxr-xr-x
new dir  ---->            accounts/acme-v02.api.letsencrypt.org
local        : new dir            modified on 2018-11-20 at 15:54:23  size 0         rwx------
unifi2       : absent
new dir  ---->            archive/votredomaine.fr
local        : new dir            modified on 2018-11-20 at 15:56:36  size 8820      rwxr-xr-x
unifi2       : absent
new file ---->            csr/0000_csr-certbot.pem
local        : new file           modified on 2018-11-20 at 15:54:48  size 924       rw-r--r--
unifi2       : absent
new file ---->            keys/0000_key-certbot.pem
local        : new file           modified on 2018-11-20 at 15:54:48  size 1704      rw-------
unifi2       : absent
new file ---->            live/README
local        : new file           modified on 2018-11-20 at 15:56:36  size 740       rw-r--r--
unifi2       : absent
new dir  ---->            live/votredomaine.fr
local        : new dir            modified on 2018-11-20 at 15:56:36  size 692       rwxr-xr-x
unifi2       : absent
new dir  ---->            renewal-hooks
local        : new dir            modified on 2018-11-20 at 15:54:23  size 0         rwxr-xr-x
unifi2       : absent
new file ---->            renewal/votredomaine.fr.conf
local        : new file           modified on 2018-11-20 at 15:56:49  size 540       rw-r--r--
unifi2       : absent
Propagating updates
UNISON 2.48.3 started propagating changes at 16:09:26.92 on 20 Nov 2018
[CONFLICT] Skipping csr/0000_csr-letsencrypt.pem
  contents changed on both sides
[CONFLICT] Skipping keys/0000_key-letsencrypt.pem
  contents changed on both sides
[CONFLICT] Skipping live
  properties changed on both sides
[BGN] Copying accounts/acme-v02.api.letsencrypt.org from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying archive/votredomaine.fr from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying csr/0000_csr-certbot.pem from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying keys/0000_key-certbot.pem from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying live/README from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying live/votredomaine.fr from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying renewal-hooks from /etc/letsencrypt to //unifi2//etc/letsencrypt
[BGN] Copying renewal/votredomaine.fr.conf from /etc/letsencrypt to //unifi2//etc/letsencrypt
Shortcut: copied /etc/letsencrypt/archive/votredomaine.fr/chain1.pem from local file /etc/letsencrypt/archive/unifi.votredomaine.fr/chain1.pem
Shortcut: copied /etc/letsencrypt/archive/votredomaine.fr/privkey1.pem from local file /etc/letsencrypt/keys/.unison.000_key-certbot.pem.9b43691cb1ffe6e6e509719e19b61d5d.unison.tmp
[END] Copying accounts/acme-v02.api.letsencrypt.org
[END] Copying csr/0000_csr-certbot.pem
[END] Copying keys/0000_key-certbot.pem
[END] Copying live/README
[END] Copying renewal/votredomaine.fr.conf
[END] Copying renewal-hooks
[END] Copying archive/votredomaine.fr
[END] Copying live/votredomaine.fr
UNISON 2.48.3 finished propagating changes at 16:09:26.95 on 20 Nov 2018
Saving synchronizer state
Synchronization complete at 16:09:26  (8 items transferred, 3 skipped, 0 failed)
  skipped: csr/0000_csr-letsencrypt.pem (contents changed on both sides)
  skipped: keys/0000_key-letsencrypt.pem (contents changed on both sides)
  skipped: live (properties changed on both sides)

Si tout se passe bien, vous devriez voir apparaître le certificats et les fichiers associés sur le serveur Unifi2, dans le dossier /etc/letsencrypt/. Si la commande a bien fonctionné, lancez ensuite les opérations suivants sur les deux serveurs :

root@unifi1:~# chmod +x /usr/local/bin/unifi_ssl_import.sh
root@unifi1:~# /usr/local/bin/unifi_ssl_import.sh
root@unifi2:~# chmod +x /usr/local/bin/unifi_ssl_import.sh
root@unifi2:~# /usr/local/bin/unifi_ssl_import.sh

Si tout se passe comme voulu, le script remplace le certificat standard d’Unifi par votre certificat Wildcard /etc/letsencrypt/live/votredomaine.fr/privkey.pem. Redémarrez ensuite le service Unifi sur les deux serveurs :

root@unifi1:~# service unifi restart
root@unifi2:~# service unifi restart

Via votre navigateur Web préféré, accéder aux URL suivantes : 

https://unifi.votredomaine.fr:8443
https://unifi1.votredomaine.fr:8443
https://unifi2.votredomaine.fr:8443

Les 3 URLS doivent être fonctionnelles, et sécurisées via HTTPS, sans erreur. Sinon, vérifiez que les certificats sont bien créés et disponibles sur les deux serveurs, que les scripts d’import Unifi ne rendent pas d’erreur.

Une fois que nos serveurs sont sécurisées, on va simplement planifier le renouvellement des certificats, et ce sera bon !

3. Planification & renouvellement des certificats

Sur le serveur primaire, ajouter à la crontab le renouvellement du certificat et le transfert vers le serveur secondaire :

root@unifi1:~# crontab -e
# m h  dom mon dow   command
15 */12 * * * /opt/eff.org/certbot/venv/bin/certbot renew
20 */12 * * * /usr/local/bin/unifi_ssl_import.sh
21 */12 * * * /usr/bin/unison -batch /etc/letsencrypt/ ssh://root@unifi2.votredomaine.fr//etc/letsencrypt/

Sur le serveur secondaire, ajouter l’exécution du certificat : 

 root@unifi2:~# crontab -e
# m h  dom mon dow   command
25 */12 * * * /usr/local/bin/unifi_ssl_import.sh

Conclusion

Et voilà, votre sécurisation via HTTPS est fonctionnelle, et automatique ! Le renouvellement du certificat Wildcard se fera tout seul, et vos interfaces Web n’affichent plus d’erreur HTTPS.

Cela marque la fin de ce tutoriel pour la création d’un cluster Unifi ! Toutefois, d’autres chapitres verront probablement le jour, notamment sur la sécurisation de ces serveurs, et d’autres fonctionnalités possibles 🙂 

Si vous avez des idées pour améliorer ce tutoriel, si vous souhaitez réagir, n’hésitez pas à nous contacter ou à commenter ! Et on se dit à une prochaine fois sur Wireless.fr

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Commentaires en ligne
Afficher tous les commentaires