Apache Authentication Proxy: Unterschied zwischen den Versionen

Aus FHEMWiki
K (Link zur Seite "HTTPS-Absicherung & Authentifizierung via nginx Webserver" hinzugefügt)
 
(2 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 4: Zeile 4:
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.
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.


<source lang="bash">
<syntaxhighlight lang="bash">
apt-get install apache2 libapache2-mod-proxy-html
apt-get install apache2 libapache2-mod-proxy-html
</source>
</syntaxhighlight>




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


<source lang="perl">
<syntaxhighlight lang="perl">
define WEBS FHEMWEB 8084
define WEBS FHEMWEB 8084
</source>
</syntaxhighlight>


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]
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]


<source lang="ini">
<syntaxhighlight lang="ini">
a2enmod proxy
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_http
</source>
</syntaxhighlight>


Step 3) neue Datei '''''/etc/apache2/conf.d/fhem''''' anlegen:
Step 3) neue Datei '''''/etc/apache2/conf.d/fhem''''' anlegen:
Zeile 26: Zeile 26:
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.
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.


<source lang="ini">
<syntaxhighlight lang="ini">
<Location /fhem>
<Location /fhem>
   # ProxyPass/ProxyPassReverse leitet HTTP requests auf eine andere URL um
   # ProxyPass/ProxyPassReverse leitet HTTP requests auf eine andere URL um
Zeile 42: Zeile 42:
   Allow from all
   Allow from all
</Location>
</Location>
</source>
</syntaxhighlight>


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


<source lang="bash">
<syntaxhighlight lang="bash">
# -c -> create file
# -c -> create file
# -s SHA encryption
# -s SHA encryption
Zeile 52: Zeile 52:
# add more users with
# add more users with
htpasswd -s /etc/fhem-htpasswd <username>
htpasswd -s /etc/fhem-htpasswd <username>
</source>
</syntaxhighlight>


Step 5) Apache neu starten
Step 5) Apache neu starten


<source lang="bash">invoke-rc.d apache2 reload</source>
<syntaxhighlight lang="bash">invoke-rc.d apache2 reload</syntaxhighlight>


bzw.
bzw.


<source lang="bash">service apache2 reload</source>
<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.
Fertig. FHEM ist jetzt über '''''http://server/fhem''''' erreichbar. Alle Zugriffe müssen aber erst mit Benutzername + Passwort freigeschaltet werden.
Zeile 68: Zeile 68:
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.  
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.  


<source lang="ini">
<syntaxhighlight lang="ini">
<VirtualHost *:80>
<VirtualHost *:80>
     ServerName          $host
     ServerName          $host
Zeile 116: Zeile 116:
    </Proxy>
    </Proxy>
</VirtualHost>
</VirtualHost>
</source>
</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.
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 ==
== Hinweis zu Apache 2.4 ==
Zeile 135: Zeile 145:
fehlt das Paket libapache2-mod-proxy-html. Einfach mit  
fehlt das Paket libapache2-mod-proxy-html. Einfach mit  


<source lang="bash">apt-get install libapache2-mod-proxy-html</source>  
<syntaxhighlight lang="bash">apt-get install libapache2-mod-proxy-html</syntaxhighlight>  


nachinstallieren.
nachinstallieren.
Zeile 145: Zeile 155:
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:
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:


  <source lang="bash">ln -s /etc/apache2/mods-available/proxy_html.conf /etc/apache2/mods-enabled/proxy_html.conf</source>
  <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.
Dann Apache neu starten/laden.
Zeile 154: Zeile 164:


  <pre>SetOutputFilter INFLATE;proxy-html;DEFLATE</pre>
  <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==
==Links==

Aktuelle Version vom 17. Juni 2018, 19:29 Uhr

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