Basics

There are plenty of plugins for elasticsearch to enable authentication for HTTP API. All of them seem to be a bit beta. I decided to use httpd with mod_proxy to add authentication using PAM, htpasswd file or host-based rules. This satisfies my needs for different use-cases:

  • PAM – human interaction on API or Plugins
  • htpasswd – API-KEY stype basic auth for scripted interaction from remote hosts
  • host-based – scripted interaction from localhost

Elasticsearch Configuration

You only need to configure elasticsearch to bind http api on localhost, port 9100. Edit your /etc/elasticsearch/elasticsearch.yml to have the following in it:

http.host: 127.0.0.1
http.port: 9100

Httpd Configuration

Just bind your elasticsearch to a different port (9100 f.e.) and only on loopback interface. Configure httpd as follows:

/etc/httpd/conf.d/elasticsearch-api.conf

Listen 9200

# load httpd modules if not already done
<IfModule !proxy_module>
        LoadModule proxy_module modules/mod_proxy.so
</IfModule>

<IfModule !mod_proxy_http.c>
        LoadModule proxy_http_module modules/mod_proxy_http.so
</IfModule>

<VirtualHost *:9200>
        # reverse proxy to localhost:9100
        # please ensure that elasticsearch http is bind to 127.0.0.1 on port 9100
        ProxyRequests           off
        ProxyPreserveHost       On
        ProxyPass        / http://localhost:9100/
        ProxyPassReverse / http://localhost:9100/

        # define acl's for http api access
        # if this file doesn't exist, there is no authentication at all
        Include /etc/httpd/conf.d/elasticsearch-api-authorization.con[f]

        # define custom log files
        ErrorLog /data/logs/www/elasticsearch-api-error.log
        CustomLog /data/logs/www/elasticsearch-api-access.log combined
</VirtualHost>
  • IfModule loads modules only if they aren’t available
  • Include <Path> includes a separated authorization config file, shown below
  • <filename>.con[f] is a regex, loading the file only if it exists, otherwise there will be no error

/etc/httpd/conf.d/elasticsearch-api-authorization.conf

# load relevant modules
<IfModule !auth_pam_module>
        LoadModule auth_pam_module modules/mod_auth_pam.so
</IfModule>

<IfModule !auth_basic_module>
        LoadModule auth_basic_module modules/mod_auth_basic.so
</IfModule>

<IfModule !authn_file_module>
        LoadModule authn_file_module modules/mod_authn_file.so
</IfModule>

<IfModule !authz_user_module>
        LoadModule authz_user_module modules/mod_authz_user.so
</IfModule>

<Location />
        # block everything by default
        Order deny,allow
        Deny from all

        # allow from localhost without authentication
        Allow from 127.0.0.1

        AuthType Basic
        AuthBasicProvider file
        AuthName "Datacenter Account needed for elasticsearch administration"

        # Allow pam auth but failover to userfile auth if the user doesn't exist as systemuser
        AuthPAM_Enabled on
        AuthPAM_FallThrough on

        # also use htpasswd file for api key auth
        AuthUserFile conf.d/elasticsearch_api_authorization.htpasswd

        Require valid-user

        # accept basic auth or "Allow from" directives
        Satisfy Any
</Location>
  • „Deny from all“ means whitelisting, everything is blocked if it is not explicitly allowed
  • „AuthType Basic“ defines, that mod_auth_basic is used
  • „AuthPAM_Enabled on“ defines, that mod_auth_pam is used
  • „AuthPAM_FallThrough on“ defines, that httpd asks other auth modules if auth_pam failed (nice if you wan’t to mix up htpasswd file based auth with pam auth)
  • „AuthUserFile“ defines the htpasswd file
  • „Satisfy Any“ defines, that only one of the given providers needs to confirm the credentials, instead of all of them (Allows auth by host with „Allow from …“ OR basic auth)

API-Key Authentication

Create htpasswd file for api-key user „token“ by:

htpasswd -c /etc/httpd/conf.d/elasticsearch_api_authorization.htpasswd token

Authenticate via curl from remote host with:

curl -u token:mypassword http://my-elasticsearch-node:9200/_cluster/health

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden .