Apache Authentication Proxy: Unterschied zwischen den Versionen

Aus FHEMWiki
 
(5 dazwischenliegende Versionen von 4 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 ==
== Konfiguration ==


Um den Zugriff auf FHEMWEB etwas sicherer zu machen, kann man den Webzugriff über einen Apachen 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.
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.


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




Step 1) FHEMWEB sollte nur noch auf dem Loopback 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


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


Step 2) die folgenden Apache2 Module müssen aktiviert sein: mod_proxy + 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]


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


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


<nowiki>&lt;Location /fhem&gt;
<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
Zeile 33: Zeile 41:
   Order deny,allow
   Order deny,allow
   Allow from all
   Allow from all
&lt;/Location&gt;</nowiki>
</Location>
</syntaxhighlight>


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


<nowiki># -c -&gt; create file
<syntaxhighlight lang="bash">
# -c -> create file
# -s SHA encryption
# -s SHA encryption
htpasswd -c -s /etc/fhem-htpasswd &lt;username&gt;
htpasswd -c -s /etc/fhem-htpasswd <username>
# add more users with
# add more users with
htpasswd -s /etc/fhem-htpasswd &lt;username&gt;</nowiki>
htpasswd -s /etc/fhem-htpasswd <username>
</syntaxhighlight>


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


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


bzw.
bzw.


<nowiki>service apache2 reload</nowiki>
<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.


=== SSL-Konfiguration ===
=== 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 von letsencrypt.  
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.  


  <nowiki>
<syntaxhighlight lang="ini">
<VirtualHost *:80>
<VirtualHost *:80>
     ServerName $host
     ServerName           $host
     RedirectPermanent / https://$host/
     RedirectPermanent   / https://$host/
</VirtualHost>
</VirtualHost>


<VirtualHost *:443>
<VirtualHost *:443>
    ServerName $host
    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


    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
    SSLHonorCipherOrder on
    SSLCompression         off
    SSLCompression     off
 
    SSLOptions         +StrictRequire
    SSLOptions +StrictRequire
    SSLCertificateFile /etc/letsencrypt/live/$host/fullchain.pem
    SSLCertificateFile /etc/letsencrypt/live/$host/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/$host/privkey.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/$host/privkey.pem


Zeile 107: Zeile 116:
    </Proxy>
    </Proxy>
</VirtualHost>
</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.


  </nowiki>
== Websockets-Unterstützung ==


Die Proxy-Konfiguration ist so gewählt, dass auch automatische letsencrypt-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.
<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 127: Zeile 145:
fehlt das Paket libapache2-mod-proxy-html. Einfach mit  
fehlt das Paket libapache2-mod-proxy-html. Einfach mit  


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


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


  <pre>ln -s /etc/apache2/mods-available/proxy_html.conf /etc/apache2/mods-enabled/proxy_html.conf</pre>
  <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 146: 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==
*[[HTTPS-Absicherung & Authentifizierung via nginx Webserver]]


[[Kategorie:HOWTOS]]
[[Kategorie:HOWTOS]]

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