HAProxy funktioniert bei manueller Ausführung einwandfrei, kann aber als Systemd-Dienst die SSL-PEM-Datei nicht laden

HAProxy funktioniert bei manueller Ausführung einwandfrei, kann aber als Systemd-Dienst die SSL-PEM-Datei nicht laden

Ich versuche, SSL auf einem Fedora 22-Server mit HAProxy einzurichten. Hier ist die Konfiguration ( /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

Wenn ich es jetzt ausführe systemctl restart haproxy && journalctl -u haproxy.service -f, erhalte ich diesen Fehler:

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

Hier ist die Dienstkonfiguration:

# 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

Ich kann jedoch den Befehl kopieren, den der Dienst auszuführen versucht, und es funktioniert einwandfrei.

Erstens funktioniert dies, wenn ich es manuell ausführe (aus der Servicekonfiguration übernommen):

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

Das hier funktioniert auch (aus den Service-Logs entnommen, haproxy-systemd-wrapperläuft anscheinend so:

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

Zur Klarstellung: Wenn ich einen dieser Befehle manuell als Root ausführe, funktioniert er und ich kann über SSL auf meine Site zugreifen.

Daher gehe ich davon aus, dass HAProxy das Zertifikat nicht lesen kann, wenn es als Dienst ausgeführt wird.

Folgendes habe ich bisher versucht:

  • chown haproxy:haproxy /etc/haproxy/certificate.pem
  • Umstellung von Benutzer und Gruppe auf „Root“ inhaproxy.cfg
  • Hinzufügen von User=rootund Group=rootzur Servicekonfiguration unter[Service]
  • sudo -u haproxy /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Dsgibt einen anderen Fehler (bezüglich der Ports), daher wird der Benutzer haproxy höchstwahrscheinlich nicht vom Dienst verwendet, sonst würde es weitere

Ich kann als Benutzer haproxy auf die Datei zugreifen sudo -u haproxy cat /etc/haproxy/certificate.pem.

Bearbeiten:

Hier ist die aktualisierte Dienstkonfiguration basierend auf Ihrer Antwort:

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

Was passiert jetzt:

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.

So habe ich den Wert für erhalten 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

Antwort1

Sie verwenden Fedora, dasSELinux. Systemd-Dienste werden in einer sauberen Umgebung ausgeführt – sie sindnichtwerden direkt von systemctl erzeugt – daher starten sie unter anderem in einem anderen SELinux-Kontext (ich glaube, die Standardeinstellung ist system_u:system_r:init_t:s0?).

Stellen Sie sicher, dass der richtige SELinux-Kontext sowohl für die Zertifikatsdatei (mit ls -lZund chcon) als auch für den Haproxy-Prozess (ggf. mit SELinuxContext=in der systemd-Unit) festgelegt ist.

Versuchen Sie es mit der Ausführung, idum Ihren aktuellen Kontext (oder den des Dienstes) anzuzeigen.

verwandte Informationen