Apache Authentication Proxy

Aus FHEMWiki
Zur Navigation springen Zur Suche springen
Info green.pngEine sicherere Variante eines Apache Authentication Proxy zusätzlich mit Zertifikaten ist hier dokumentiert

Konfiguration

Um den Zugriff auf FHEMWEB etwas sicherer zu machen, kann man den Webzugriff über einen Apache Webserver laufen lassen. Dies ist ein kurzes Rezept, um Zugriffe auf FHEMWEB über einen Apachen authentifizieren zu lassen. Erstellt wurde es auf Debian Squeeze], sollte aber auch mit Ubuntu funktionieren.

apt-get install apache2 libapache2-mod-proxy-html


Step 1) FHEMWEB sollte nur noch auf Anfragen vom selben Rechner lauschen, also kein 'global' Attribut in der Definition in fhem.cfg

define WEBS FHEMWEB 8084

Step 2) die folgenden Apache2 Module müssen aktiviert sein: mod_proxy + mod_proxy_http

a2enmod proxy
a2enmod proxy_http

Step 3) neue Datei /etc/apache2/conf.d/fhem anlegen:

Diese Konfiguration sorgt dafür, dass alle Anfragen unter /fhem weiter nach http://localhost/fhem geleitet werden. Zusätzlich wird eine Basic-Authentifizierung eingeschaltet. Die Benutzerdatenbank ist dann in /etc/fhem-htpasswd zu finden.

<Location /fhem>
  # ProxyPass/ProxyPassReverse leitet HTTP requests auf eine andere URL um
  ProxyPass http://localhost:8084/fhem
  ProxyPassReverse http://localhost:8084/fhem
  ProxyHTMLEnable On
  # ProxyHTMLURLMap passt Links im HTML/JavaScript Source an
  ProxyHTMLURLMap /        /fhem/
  ProxyHTMLURLMap /fhem/     /fhem/
  AuthType Basic
  AuthName &quot;Password Required&quot;
  AuthUserFile /etc/fhem-htpasswd
  Require valid-user
  Order deny,allow
  Allow from all
</Location>

Step 4) Benutzer-Datenbank in /etc/fhem-htpasswd anlegen

# -c -> create file
# -s SHA encryption
htpasswd -c -s /etc/fhem-htpasswd <username>
# add more users with
htpasswd -s /etc/fhem-htpasswd <username>

Step 5) Apache neu starten

invoke-rc.d apache2 reload

bzw.

service apache2 reload

Fertig. FHEM ist jetzt über http://server/fhem erreichbar. Alle Zugriffe müssen aber erst mit Benutzername + Passwort freigeschaltet werden.

SSL-Konfiguration

Um nicht die Passwörter unverschlüsselt übers Netz zu schicken, ist die Konfiguration eines SSL-Reverse-Proxys noch empfehlenswerter. Die untenstehende Konfiguration zeigt die Verwendung mittels Zertifikaten, die von Let's Encrypt ausgestellt wurden.

	<VirtualHost *:80>
  	   ServerName           $host
  	   RedirectPermanent    / https://$host/
	</VirtualHost>

	<VirtualHost *:443>
	    ServerName          $host

	    SSLEngine           on
	    SSLProtocol         all -SSLv2 -SSLv3
	    SSLCipherSuite      ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
	    SSLHonorCipherOrder on
	    SSLCompression      off
	    SSLOptions          +StrictRequire
	
	    SSLCertificateFile  /etc/letsencrypt/live/$host/fullchain.pem
	    SSLCertificateKeyFile /etc/letsencrypt/live/$host/privkey.pem

	    Use RootDir $dir  # hier sollte ein Verzeichnis unter /var/www stehen (kann leer sein, muss aber existieren)
	
            ProxyPass /.well-known !
            Alias /.well-known "/var/www/proxy/.well-known"
            <Directory "$dir/.well-known">
               order allow,deny
               allow from all
               AllowOverride All
               AddDefaultCharset Off
            </Directory>


	    ProxyRequests       Off
	    ProxyVia 		Off
	    ProxyPreserveHost   On
	    ProxyPass 		/fhem http://$fhemhost:$port/fhem
	    ProxyPassReverse 	/fhem http://$fhemhost:$port/fhem

   	    Header always set Strict-Transport-Security "max-age=31536000"

	    <Proxy *>
		Order deny,allow
		Allow from all
                AuthType Basic
                AuthName &quot;Password Required&quot;
                AuthUserFile /etc/fhem-htpasswd
                Require valid-user
	    </Proxy>
	</VirtualHost>

Die Proxy-Konfiguration ist so gewählt, dass auch automatische Zertifikats-Aktualisierung per webroot klappt (letsencrypt certonly --renew-by-default --webroot -w $dir -d $host). Initial muss man natürlich ohne Zertifikat einen normalen non-SSL-VHost aufmachen.

Websockets-Unterstützung

			RewriteEngine On
			RewriteCond %{HTTP:Upgrade} =websocket [NC]
			RewriteRule /fhem(.*)           ws://$url$1 [P,L]
			RewriteCond %{HTTP:Upgrade} !=websocket [NC]
			RewriteRule /fhem(.*)           http://$url$1 [P,L]

Hinweis zu Apache 2.4

Es muss zusätzlich das Modul proxy_html aktiviert werden (a2enmod proxy_html). Und die zu erstellende Config-Datei muss jetzt nach /etc/apache2/conf-available/fhem.conf (statt /etc/apache2/conf.d/fhem).

Mögliche Probleme

Invalid command 'ProxyHTMLURLMap'

Falls nach dem Neustart des Apache folgende Fehlermeldung kommt:

Invalid command 'ProxyHTMLURLMap', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.

fehlt das Paket libapache2-mod-proxy-html. Einfach mit

apt-get install libapache2-mod-proxy-html

nachinstallieren.

ProxyHTMLURLMap funktioniert nicht

Nicht bei allen Distributionen scheint die Konfigurationsdatei proxy_html.conf mitgeliefert zu werden. Allerdings ist seit einigen Versionen aus mod_proxy_ssl alle Information über zu ersetzende Links etc. entfernt worden. proxy_html.conf wird daher zwingend benötigt. Sie sollte in /etc/apache2/mods-available liegen und bei der Aktivierung des mods nach /etc/apache2/mods-enabled verlinkt werden. Wenn das nicht der Fall ist, kann man das nachholen.

Eine Beispielkonfiguration gibt es hier beim ursprünglichen Autor des mods. Abspeichern unter /etc/apache2/mods-available/proxy_html.conf und dann verlinken:

ln -s /etc/apache2/mods-available/proxy_html.conf /etc/apache2/mods-enabled/proxy_html.conf

Dann Apache neu starten/laden.

Content Encoding Fehler

Wenn Firefox und Chromium sich nach der Konfigurationsänderung über Content Encoding Fehler beschweren, könnte Folgendes helfen:

SetOutputFilter INFLATE;proxy-html;DEFLATE

Evtl. kann auch folgender Eintrag noch notwendig sein:

        # Enables outgoing compression for specific file types
        <IfModule mod_deflate.c>
                <FilesMatch ".*\.(html|htm|shtml|php|css|js|xml|log|txt|bmp|ttf|otf|eot|svg)$">
                   SetOutputFilter DEFLATE
                </FilesMatch>
            AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
            AddOutputFilterByType DEFLATE application/rss+xml
            AddOutputFilterByType DEFLATE text/xml application/xml application/xhtml+xml text/x-com$
            AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-java$
        </IfModule>
 

Links