Apache Authentication Proxy: Unterschied zwischen den Versionen
| K (Fehlermeldung und Behebung hinzugefügt) | Aps (Diskussion | Beiträge)  | ||
| (10 dazwischenliegende Versionen von 6 Benutzern werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
| {{Randnotiz|RNText=Eine sicherere Variante eines Apache Authentication Proxy zusätzlich mit Zertifikaten ist [https://gist.github.com/gbirke/8608543 hier dokumentiert]}} | |||
| == Konfiguration == | |||
| Um den Zugriff auf [[FHEMWEB]] etwas sicherer zu machen, kann man den Webzugriff über einen [https://httpd.apache.org/ Apache Webserver] laufen lassen. Dies ist ein kurzes Rezept, um Zugriffe auf FHEMWEB über einen Apachen authentifizieren zu lassen. Erstellt wurde es auf [https://www.debian.org Debian Squeeze]], sollte aber auch mit [https://www.ubuntu.com/ Ubuntu] funktionieren. | |||
| <syntaxhighlight lang="bash"> | |||
| apt-get install apache2 libapache2-mod-proxy-html | |||
| </syntaxhighlight> | |||
| Step  | Step 1) FHEMWEB sollte nur noch auf Anfragen vom selben Rechner lauschen, also kein 'global' Attribut in der Definition in fhem.cfg | ||
| <syntaxhighlight lang="perl"> | |||
| define WEBS FHEMWEB 8084 | |||
| </syntaxhighlight> | |||
| Step  | Step 2) die folgenden Apache2 Module müssen aktiviert sein: [https://httpd.apache.org/docs/current/mod/mod_proxy.html mod_proxy] + [http://httpd.apache.org/docs/2.4/mod/mod_proxy_http.html mod_proxy_http] | ||
| <syntaxhighlight lang="ini"> | |||
| a2enmod proxy | |||
| a2enmod proxy_http | |||
| </syntaxhighlight> | |||
| 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. | |||
| <syntaxhighlight lang="ini"> | |||
| <Location /fhem> | |||
|    # ProxyPass/ProxyPassReverse leitet HTTP requests auf eine andere URL um |    # ProxyPass/ProxyPassReverse leitet HTTP requests auf eine andere URL um | ||
|    ProxyPass http://localhost:8084/fhem |    ProxyPass http://localhost:8084/fhem | ||
|    ProxyPassReverse http://localhost:8084/fhem |    ProxyPassReverse http://localhost:8084/fhem | ||
|   ProxyHTMLEnable On | |||
|    # ProxyHTMLURLMap passt Links im HTML/JavaScript Source an |    # ProxyHTMLURLMap passt Links im HTML/JavaScript Source an | ||
|    ProxyHTMLURLMap /        /fhem/ |    ProxyHTMLURLMap /        /fhem/ | ||
| Zeile 28: | Zeile 41: | ||
|    Order deny,allow |    Order deny,allow | ||
|    Allow from all |    Allow from all | ||
| </Location> | |||
| </syntaxhighlight> | |||
| Step 4) Benutzer-Datenbank in /etc/fhem-htpasswd anlegen | Step 4) Benutzer-Datenbank in '''''/etc/fhem-htpasswd''''' anlegen | ||
| <syntaxhighlight lang="bash"> | |||
| # -c -> create file | |||
| # -s SHA encryption | # -s SHA encryption | ||
| htpasswd -c -s /etc/fhem-htpasswd  | htpasswd -c -s /etc/fhem-htpasswd <username> | ||
| # add more users with | # add more users with | ||
| htpasswd -s /etc/fhem-htpasswd  | htpasswd -s /etc/fhem-htpasswd <username> | ||
| </syntaxhighlight> | |||
| Step 5) Apache neu starten | Step 5) Apache neu starten | ||
| <syntaxhighlight lang="bash">invoke-rc.d apache2 reload</syntaxhighlight> | |||
| bzw. | |||
| <syntaxhighlight lang="bash">service apache2 reload</syntaxhighlight> | |||
| 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 [https://letsencrypt.org Let's Encrypt] ausgestellt wurden.  | |||
| <syntaxhighlight lang="ini"> | |||
| 	<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 "Password Required" | |||
|                 AuthUserFile /etc/fhem-htpasswd | |||
|                 Require valid-user | |||
| 	    </Proxy> | |||
| 	</VirtualHost> | |||
| </syntaxhighlight> | |||
| Die Proxy-Konfiguration ist so gewählt, dass auch automatische Zertifikats-Aktualisierung per webroot klappt (<code>letsencrypt certonly --renew-by-default --webroot -w $dir -d $host</code>). Initial muss man natürlich ohne Zertifikat einen normalen non-SSL-VHost aufmachen. | |||
| == Websockets-Unterstützung == | |||
| <syntaxhighlight lang="ini"> | |||
| 			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] | |||
| </syntaxhighlight> | |||
| == Hinweis zu Apache 2.4 == | |||
| Es muss zusätzlich das Modul '''proxy_html''' aktiviert werden (<code>a2enmod proxy_html</code>). | |||
| 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: | Falls nach dem Neustart des Apache folgende Fehlermeldung kommt: | ||
| Zeile 52: | Zeile 145: | ||
| fehlt das Paket libapache2-mod-proxy-html. Einfach mit   | fehlt das Paket libapache2-mod-proxy-html. Einfach mit   | ||
| <syntaxhighlight lang="bash">apt-get install libapache2-mod-proxy-html</syntaxhighlight>   | |||
| nachinstallieren. | 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 [http://apache.webthing.com/svn/apache/filters/proxy_html/proxy_html.conf hier beim ursprünglichen Autor des mods]. Abspeichern unter /etc/apache2/mods-available/proxy_html.conf und dann verlinken: | |||
|  <syntaxhighlight lang="bash">ln -s /etc/apache2/mods-available/proxy_html.conf /etc/apache2/mods-enabled/proxy_html.conf</syntaxhighlight> | |||
| 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: | |||
|  <pre>SetOutputFilter INFLATE;proxy-html;DEFLATE</pre> | |||
| Evtl. kann auch folgender Eintrag noch notwendig sein: | |||
|  <pre> | |||
|         # 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> | |||
|  </pre> | |||
| ==Links== | |||
| *[[HTTPS-Absicherung & Authentifizierung via nginx Webserver]] | |||
| [[Kategorie:HOWTOS]] | [[Kategorie:HOWTOS]] | ||
Aktuelle Version vom 17. Juni 2018, 18:29 Uhr
 Eine sicherere Variante eines Apache Authentication Proxy zusätzlich mit Zertifikaten ist hier dokumentiert
Eine sicherere Variante eines Apache Authentication Proxy zusätzlich mit Zertifikaten ist hier dokumentiertKonfiguration
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 "Password Required"
  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 "Password Required"
                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>