AuthN / authZ
Basic authentication
When your traffic is HTTP, you can use basic authentication to display a login prompt to users. Configuring it is easy, but it does have one drawback: credentials are transmitted in the clear over HTTP. You can mitigate this exposure by enabling TLS to encrypt the traffic. In our examples, we will do just that.
Enable basic authentication Jump to heading
Follow these steps to set up basic authentication:
-
Usernames and their associated passwords are stored in the load balancer’s running memory.
To define them, create a
userlist
section. Each entry in this section has auser
argument to indicate the username and aninsecure-password
argument to indicate the password.haproxyuserlist mycredentialsuser joe insecure-password joespassworduser alice insecure-password alicespassworduser mark insecure-password markspasswordhaproxyuserlist mycredentialsuser joe insecure-password joespassworduser alice insecure-password alicespassworduser mark insecure-password markspassword -
In your
frontend
section, enable TLS on yourbind
line so that credentials will be encrypted when transmitted between the client and load balancer.In this example, we also redirect HTTP requests to HTTPS. We use the
http-request auth
line to display the basic authentication login prompt to users. If a user has already logged in, then they will not see the prompt again.haproxyfrontend wwwbind :80bind :443 ssl crt /site.pemhttp-request redirect scheme https unless { ssl_fc }http-request auth unless { http_auth(mycredentials) }default_backend webservershaproxyfrontend wwwbind :80bind :443 ssl crt /site.pemhttp-request redirect scheme https unless { ssl_fc }http-request auth unless { http_auth(mycredentials) }default_backend webservers
Hash passwords in the userlist Jump to heading
You can store a hashed value for a password in the userlist
section instead of storing it as cleartext.
-
Install the
mkpasswd
tool:nix# mkpasswd is included in the whois packagesudo apt install whoisnix# mkpasswd is included in the whois packagesudo apt install whoisnixsudo yum install mkpasswdnixsudo yum install mkpasswd -
Call
mkpasswd
with the SHA-256 algorithm to hash your password:nixmkpasswd -m sha-256 mypassword123nixmkpasswd -m sha-256 mypassword123outputtext$5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/outputtext$5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/ -
Store the hashed password by using the
password
argument:haproxyuserlist mycredentialsuser joe password $5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/haproxyuserlist mycredentialsuser joe password $5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?