HAPROXY 1.5 : Configurando com SSL SNI (Passthrough)

Neste post será apresentado como configurar o HAPROXY SSL Passthrough .

HAPROXY é um serviço de balanceamento de serviços de rede. Nas versões antes do RHEL 7 existia um serviço chamado PIRANHA que realizava a mesma função , porém do RHEL7 em diante foi mantido o HAPROXY e Keepalived como ferramenta de balanceamento oficial.

Referência do fabricante :
http://blog.haproxy.com/2012/04/13/enhanced-ssl-load-balancing-with-server-name-indication-sni-tls-extension/

Referência da Redhat :
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/7.0_Release_Notes/sect-Red_Hat_Enterprise_Linux-7.0_Release_Notes-Clustering-Keepalived_and_HAProxy_Replace_Piranha_as_Load_Balancer.html

OBS : A versão do repositório do Redhat 7 está na versão 1.4 , caso tenha interesse em usar esta feature será necessário compilar a versão 1.5 , segue procedimento abaixo :

http://fajlinux.com.br/high-availability/haproxy-compilra-suporte-ssl/

sni_loadbalancing

1) Instação dos pacotes essenciais :

yum install pcre-devel -y 

2) Instalando o OpenSSL:

Como eu tive problemas com a versão instalada do repositório Redhat (openssl-devel-1.0.1e-42.el6.x86_64) estarei instalando na mão o openssl.

wget -v https://www.openssl.org/source/openssl-0.9.8zg.tar.gz
tar -xvf openssl-0.9.8zg.tar.gz 
cd openssl-0.9.8zg
./config --prefix=/var/tmp/ssl no-shared
make
make install_sw

3) Instalando o HAPROXY 1.5 :

Compilando

wget -v http://www.haproxy.org/download/1.5/src/haproxy-1.5.14.tar.gz
tar -xvf haproxy-1.5.14.tar.gz 
cd haproxy-1.5.14
make TARGET=linux2628 USE_OPENSSL=yes USE_STATIC_PCRE=1 USE_OPENSSL=1 SSL_INC=/var/tmp/ssl/include SSL_LIB=/var/tmp/ssl/lib ADDLIB=-ldl
make install

Copiando os binários e scripts

cp /usr/local/sbin/haproxy /usr/sbin/
cp examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy

4) Configurando o HAPROXY para SSLPassthrough :

Em meu exemplo estou usando configurações com ACL caso seja necessário ter mais de um domínio é só seguir o example dos templates.

mkdir /etc/haproxy/

vim /etc/haproxy/haproxy.cfg

##### GLOBAL CONFIG #####
global

        log 127.0.0.1   local0
        log 127.0.0.1   local1 debug
        maxconn   45000 # Total Max Connections.
        daemon
        nbproc      1 # Number of processing cores.

defaults
        timeout server 86400000
        timeout connect 86400000
        timeout client 86400000
        timeout queue   1000s

log     global
        mode    tcp
        option  tcplog
        option  dontlognull


#####  DASHBOARD #####
listen stats :4997
mode http
stats   enable
stats   hide-version
stats   refresh 30s
stats   show-node
stats realm Haproxy\ Statistics
stats auth admin:haproxy
stats uri       /haproxy


############ Configuracao dos Frontends #######################

###### HTTP HTTPS  FRONTEND #############
frontend http-in
    bind <IP DO HAPROXY>:80

 # Default Backend 
 default_backend template_http-lb

 # ACL  BACKEND HTTP TEMPLATE
    acl host_http-template.example.com hdr(host) -i template.example.com
    use_backend template_http-lb if host_http-template.example.com


##### HTTPS FRONTEND ############
#frontend https-in
    mode tcp
    bind <IP DO HAPROXY>:443


tcp-request inspect-delay 5s
tcp-request content accept if { req_ssl_hello_type 1 }


# ACL  BACKEND HTTPS  TEMPLATE
  acl host_https-temaplte req_ssl_sni -i template.example.com
  use_backend template_https-lb if host_https-template.example.com

 # Default Backend 
 default_backend template_https-lb


###########  Configuracao dos Backends #####################


#### BACKEND TEMPLATE HTTP-LB ####
backend template_http-lb
        mode http
        balance roundrobin
        option forwardfor
        option  http-server-close
        option  http-pretend-keepalive
        server  SERVER1 <IP DO SERVER 1>:80 check
        server  SERVER2 <IP DO SERVER 2>:80 check



#### BACKEND TEMPLATE HTTPS-LB ####
backend template_https-lb
        mode tcp
        balance source
        option  http-server-close

# maximum SSL session ID length is 32 bytes.
  stick-table type binary len 32 size 30k expire 30m
  acl clienthello req_ssl_hello_type 1
  acl serverhello rep_ssl_hello_type 2

  # use tcp content accepts to detects ssl client and server hello.
  tcp-request inspect-delay 5s
  tcp-request content accept if clienthello

  # no timeout on response inspect delay by default.
  tcp-response content accept if serverhello

  stick on payload_lv(43,1) if clienthello

  # Learn on response if server hello.
  stick store-response payload_lv(43,1) if serverhello

  option ssl-hello-chk
  #option  http-pretend-keepalive
        server  SERVER1 <IP DO SERVER 1>:443 check
        server  SERVER2 <IP DO SERVER 2>:443 check

Restarte o HAPROXY após aplicar o arquivo de configuração :

service haproxy restart
Translate »