Title: Use Haproxy with Let's Encrypt subtitle: [EN] Date: 2015-12-18 09:33 Modified: 2015-12-18 09:33 Category: Système Tags: haproxy, hébergement, reverse proxy, sécurité, système keywords: haproxy, hébergement, reverse proxy, sécurité, système Slug: use-letsencrypt-haproxy Authors: Victor Status: published
[TOC]
Note : Ceci est la version anglaise de l'article, pour la version française, voir ici.
Haproxy is a proxy software. It has many use, but here we will use its capacity to reverse proxying HTTP and HTTPS.
For this post, we will consider you have a working Haproxy server and a working configuration.
Let's Encrypt is an open source project sponsored by Mozilla foundation and Cisco.
Idea is to create an free certificate authority in order to create SSL certificate and doing so, allowing to most people possible to use HTTPS and secure websites.
Regarding usual cost of SSL certificate, this a very interesting project!
In addition to that, Let's Encrypt is really easy to use, mainly thank to an API allowing to create certificate very easily.
We will use this API in this post through Let's Encrypt official python client.
EDIT : 04/04/16 : add note about case when you use redirect HTTP to HTTPS in your HTTP haproxy frontend
EDIT : 09/16/16 : Update with new binary + add git repository with some scritps to automate LE management
Let's Encrypt offers many option to create and validate certificate via its client.
If your Haproxy is localised on the same server than your web server, you can use the --webroot option, which allow Let's Encrypt to store a special file directly in the root directory of your website, in order to allow Let's Encrypt server to request the file and validate that you are the real owner of the domain.
If you want to use this option, you can read this blog post about using webroot and haproxy, including using a dedicated apache webserver for validation.
For this post, we will use instead the --standalone option, which launch a mini-webserver. This webserver will be used for the validation process, as Let's Encrypt server will request it directly. Usually, this option has a big disadvantage: as the webserver bind on 80 network port, the real webserver needs to be temporally shutdown during the validation process.
In other word, your website(s) will be unavailable during all the process.
But we are going to use an option to change the default port, in coordination with haproxy frontend/backend capabilities, to allow validation without any downtime :-)
-- Many thanks to coolaj86 as his post give me this idea to use haproxy and Let's Encrypt together ;-) --
As LE is now officially released, a new official binary is available, more advanced and more easy to use than the previous letsencrypt-auto
All information are available on the certbot official website, but here are quick instructions how to use it.
cd /root/
mkdir letsencrypt
cd letsencrypt
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
You can next directly use the binary to get your new certificate (at least, once you have configured as this blog article describes it :-p)
./certbot-auto certonly --domains blog.victor-hery.com --renew-by-default --http-01-port 63443 --agree-tos
To avoid any downtime, we will use your existing frontend.
Let's Encrypt will request the IP address which resolve your website, so if you have many frontends or many IPs, you need to configure each frontend according to your needs.
Main idea is : during the process, Let's Encrypt request the base website URL, following by /.well-known/acme-challenge/a_unique_id.
So we are going to configure a haproxy ACL which match this path to redirect it to a specific backend!
frontend http-in acl app_letsencrypt path_beg /.well-known/acme-challenge/ [...] use_backend bk-letsencrypt if app_letsencrypt
Doing so, all Let's Encrypt requests will be redirected to the bk-letsencrypt backend.
Warning : if you are using redirect from HTTP to HTTPS for your website, haproxy will also redirect Let's Encrypt request to your HTTPS frontend.
You will then need to add the acl and use_backend lines to your HTTPS frontend as well, or let's encrypt will get 404 not found answer.
About the backend, we are going to configure it to redirect request to the server launched by Let's Encrypt client.
backend bk-letsencrypt log global mode http server srv_letsencrypt 127.0.0.1:63443
The local server will not always be up, only when the client is running. Rest of the time, haproxy will return a 503 error if someone try to get an URL matching the ACL.
Of course, you need to reload haproxy after doing these modifications.
systemctl reload haproxy.service
To simplify the command line usage, we use a configuration file for Let's Encrypt client.
By default, the client uses the file /etc/letsencrypt/cli.ini. So this is the file we are going to edit.
rsa-key-size = 4096 email = your_admin_email authenticator = standalone standalone-supported-challenges = http-01
Using HTTP for the verification process is safe, as only the verification request will be send in clear, and it has no secret inside.
The command line to use to generate your certificate is the following:
/root/letsencrypt/certbot-auto certonly --domains yourdomain.tld --renew-by-default --http-01-port 63443 --agree-tos
Certificates are created in a directory called /etc/letsencrypt/live/yourdomain.tld/ by the client during the generation process.
You will find multiple files inside:
In order to use with haproxy, you need to concatenate fullchain.pem and privkey.pem, and store it where haproxy read its certificates.
cat fullchain.pem privkey.pem > domain.tld.pem
For example for the following HTTPS frontend:
frontend https-in bind IP:443 ssl crt /etc/haproxy/cert/
Here we need to store the domain.tld.pem file in /etc/haproxy/cert/
Once your certificate is stored in the right place, reload haproxy for it to re-read certificate, and everything should be OK.
Of course do not forget to configure an HTTPS frontend with correct ACL for your website in haproxy!
Usually, simply copy/paste of your HTTP frontend changing port to 443 (and adding ssl and crt option) will do the job.
Even from the end of beta time, LE has taken decision to provide certificate for 90 days duration max. It allow them to avoid abuse usage and to make the project alive by forcing regular renew.
Note that unlilke the beta client, certbot-auto is capable to use all your available certificates (thanks to the directory/etc/letsencrypt/renewal/) to allow automatic renewal .
So remember to configure a cron job on your server to renew certificate:
crontab -e
#renew certificate 30 01 01,10,20,30 * * /root/letsencrypt/certbot-auto renew
This cron task will launch the renew command each 10 days to be sure your certificates will stay valids.
Do not forget that you still need to create the .pem from fullchain.pem and .key to give it to haproxy. See below for some automation about this task!
Because of its free use Let's Encrypt use a rate-limit sysytem to avoid generation of too many certificate for the same domain.
You will get an error if you try to generate too quickly or too often certificates for the same domain
Be careful: the limitation take into account subdomain as well! All certificates finishing with domain.tld will be count.
For the moment, Let's Encrypt does not allow certificate for international Domain Name.
This is impossible to generate a certificate for a domain or subdomain containing accent or special characters.
For example, impossible to generate certificate for https://blog.héry.com unfortunately.
There is currently a boulder on Let's Encrypt github, but it is still no available. If you are willing to help or need the function, does not hesitate to join :-)
In order to make the process easier, I have written some scripts to allo generation and renewal of certificate, and haproxy interactions, easier.
They are available in my git repository, so you can easily clone them in your server:
git clone https://git.lecygnenoir.info/LecygneNoir/letsencrypt-haproxy.git
README simply describes how to use them, it pretty simple.
create-certificate allow you to create a certificate for the domain you pass to the script, then it creates the .pem for haproxy, store it in the given directory and reload haproxy.
renew-certificates only renew all certificates that need to be renewed, creates as well haproxy pem files, en reload haproxy. You can use renew-certificate in our cron task as explained before if you want.
Do not forget to check path in scripts, mainly where to store certificates for haproxy, and path to certbot binary
And voila, with all of that, you should be able to create all certificates you need, and use them directly in haproxy, without any downtime! So, welcome in the HTTPS world :-)