HAProxy funciona bien y se ejecuta manualmente, pero como servicio Systemd no puede cargar el archivo pem SSL

HAProxy funciona bien y se ejecuta manualmente, pero como servicio Systemd no puede cargar el archivo pem SSL

Estoy intentando configurar SSL en un servidor Fedora 22 que ejecuta HAProxy. Aquí está la configuración ( /etc/haproxy/haproxy.cfg):

global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    tune.ssl.default-dh-param 2048

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    option  http-server-close
    option  forwardfor
    option  redispatch
    retries 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s

frontend main
    bind *:80
    bind *:443 ssl crt /etc/haproxy/certificate.pem
    redirect scheme https if !{ ssl_fc }
    default_backend app

backend app
    balance roundrobin
    server app1 127.0.0.1:8000 check

Ahora, cuando ejecuto systemctl restart haproxy && journalctl -u haproxy.service -f, aparece este error:

Sep 13 15:39:31 fedora-server systemd[1]: Started HAProxy Load Balancer.
Sep 13 15:39:31 fedora-server systemd[1]: Starting HAProxy Load Balancer...
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : parsing [/etc/haproxy/haproxy.cfg:30] : 'bind *:443' : unable to load SSL private key from PEM file '/etc/haproxy/certificate.pem'.
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : Proxy 'main': no SSL certificate specified for bind '*:443' at [/etc/haproxy/haproxy.cfg:30] (use 'crt').
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : Fatal errors found in configuration.
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: haproxy-systemd-wrapper: exit, haproxy RC=256

Aquí está la configuración del servicio:

# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
EnvironmentFile=/etc/sysconfig/haproxy
ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

Sin embargo, puedo copiar el comando que el servicio intenta ejecutar y funciona bien.

Primero, esto funciona cuando lo ejecuto manualmente (tomado de la configuración del servicio):

# whoami
root
# /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid

Esto también funciona (tomado de los registros de servicio, aparentemente haproxy-systemd-wrapperejecuta esto:

# whoami
root
# /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Para aclarar: cuando ejecuto cualquiera de estos comandos manualmente como root, funcionan y puedo acceder a mi sitio a través de SSL.

Entonces, supongo que cuando se ejecuta como servicio, HAProxy no puede leer el certificado.

Esto es lo que he probado hasta ahora:

  • chown haproxy:haproxy /etc/haproxy/certificate.pem
  • Cambiar de usuario y grupo a roothaproxy.cfg
  • Agregar User=rooty Group=rootconfigurar el servicio en[Service]
  • sudo -u haproxy /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Dsda un error diferente (con respecto a los puertos), por lo que lo más probable es que el servicio no utilice el usuario haproxy; de lo contrario, llegaría más lejos

Puedo acceder al archivo como usuario haproxy, sudo -u haproxy cat /etc/haproxy/certificate.pem.

Editar:

Aquí está la configuración del servicio actualizada según su respuesta:

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
SELinuxContext=unconfined_u:object_r:var_t:s0

Que pasa ahora:

Sep 13 17:51:21 ServiceName systemd[1]: Starting HAProxy Load Balancer...
Sep 13 17:51:21 ServiceName systemd[1]: haproxy.service: main process exited, code=exited, status=203/EXEC
Sep 13 17:51:21 ServiceName systemd[1]: Unit haproxy.service entered failed state.
Sep 13 17:51:21 ServiceName systemd[1]: haproxy.service failed.

Cómo obtuve el valor de SELinuxContext:

# ls -lZ /etc/haproxy/certificate.pem
-rw-r--r--. 1 root root unconfined_u:object_r:var_t:s0 9245 Sep 13 17:43 /etc/haproxy/certificate.pem

Respuesta1

Estás en Fedora, que usaSELinux. Los servicios de Systemd se ejecutan en un entorno limpio: sonnogenerado por systemctl directamente, por lo que, entre otras cosas, comienzan en un contexto SELinux diferente (creo que el valor predeterminado es system_u:system_r:init_t:s0?).

Asegúrese de que esté configurado el contexto SELinux correcto, tanto para el archivo de certificado (usando ls -lZy chcon) como para el proceso haproxy (posiblemente usando SELinuxContext=en la unidad systemd).

Intente ejecutar idpara ver su contexto actual (o el del servicio).

información relacionada