Security Essentials

8 min read

Welcome to the Security Essentials guide for Elasticsearch. In the early days of data engineering, many developers left their databases completely open to the internet. This resulted in incidents like the infamous "Meow" attacks of 2020, where automated bots scanned the web for unsecured Elasticsearch clusters and wiped thousands of databases, replacing their indices with random strings ending in "-meow".

We do not want your data to go "meow". In this guide, we will progressively break down the core components of Elasticsearch security, moving from fundamental access control to application-level API keys. We will focus strictly on modern industry standards (Elasticsearch 8.x) and skip the outdated configurations you no longer need.


1. Enabling Basic Security Features 🛡️

Definition

Basic security in Elasticsearch refers to the enforcement of user authentication (verifying identity via usernames and passwords) and node authorization (verifying if a node belongs to the cluster) before any operation can be executed.

The Problem

If basic security is disabled, Elasticsearch operates in a "trust everyone" mode. Anyone who knows your server's IP address and port (default 9200) can send a simple HTTP DELETE /_all command and instantly erase your entire database.

The Solution

Enabling the built-in security features (formerly known as X-Pack Security) puts a strict authentication barrier in front of your cluster. Every request must be accompanied by valid credentials, and under the hood, Elasticsearch will check these credentials against its internal user registry before executing the query.

Industry Practice & Setup (Elasticsearch 8.x)

In modern industry practice, you rarely start from scratch with security turned off. As of Elasticsearch 8.0, security is enabled by default when you install it.

When you start an Elasticsearch 8.x node for the first time, it automatically auto-configures security. It generates a password for the built-in superuser (elastic) and prints it to the console.

However, as an engineer, you must know what is happening in the elasticsearch.yml configuration file. Here is the standard boilerplate that enables basic security:

yaml
# Inside config/elasticsearch.yml

# 1. Enable fundamental security features
xpack.security.enabled: true

# 2. Enable security auto-configuration (8.x specific)
xpack.security.autoconfiguration.enabled: true

If you ever lose the auto-generated password for the built-in elastic user, you do not need to panic or reinstall. You can reset it using the built-in command-line tool:

bash
# Run this from your Elasticsearch installation directory
bin/elasticsearch-reset-password -u elastic

2. Setting up Transport and HTTP TLS/SSL 🔒

Definition

TLS (Transport Layer Security) is a cryptographic protocol designed to provide communication security over a computer network.

  • Transport TLS encrypts the internal node-to-node communication within an Elasticsearch cluster.
  • HTTP TLS encrypts the communication between the Elasticsearch cluster and external clients (like Kibana, your backend applications, or curl commands).

The Problem

Even if you require a password, sending that password over standard HTTP means it travels across the network in plain text. A malicious actor could use network sniffing tools to read the password as it travels. Furthermore, without Transport TLS, a rogue server could pretend to be a valid Elasticsearch node, join your cluster, and silently replicate all your data.

The Solution

TLS solves this by wrapping the data in an encrypted tunnel and requiring cryptographic certificates to verify identities.

Imagine you are sending a highly confidential letter to a friend. Sending it over standard HTTP is like writing it on a postcard—the mail carrier, the sorting facility, and anyone else can read it. Sending it over TLS is like putting the letter inside an indestructible safe, locking it, and only giving your friend the combination. If someone intercepts the safe, they just see a heavy metal box.

Hands-On Setup

In Elasticsearch 8.x, the first startup auto-generates these certificates for you. However, managing custom certificates is a frequent industry task (e.g., when adding new nodes manually). Here is the standard workflow using the elasticsearch-certutil tool.

Step 1: Generate a Certificate Authority (CA)
The CA is the "master entity" that signs the certificates for your nodes.

bash
bin/elasticsearch-certutil ca

(This generates a file named elastic-stack-ca.p12)

Step 2: Generate Node Certificates
Use the CA to generate certificates for your cluster nodes.

bash
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

(This generates elastic-certificates.p12)

Step 3: Configure elasticsearch.yml
Move the generated .p12 file to your config/certs directory and update your configuration:

yaml
# Enable and configure Transport (Node-to-Node) TLS
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

# Enable and configure HTTP (Client-to-Node) TLS
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/http.p12

3. Role-Based Access Control (RBAC): Users and Roles 🧑‍🍳

Definition

Role-Based Access Control (RBAC) is an authorization model where system access is granted based on assigned roles, rather than assigning permissions directly to individual users.

The Problem

In a company, you might have 50 developers, 10 data scientists, and 5 external auditors accessing Elasticsearch. If you give everyone the elastic superuser password, an auditor might accidentally delete your production indices. If you try to manage permissions for every individual user, you will spend your entire week updating permissions when someone changes departments.

The Solution

RBAC solves this by creating an intermediary layer called a "Role". You assign permissions to the Role, and then you assign the Role to the User.

Imagine you are running a restaurant. You have cooking equipment, a cash register, and a safe. You do not give every new employee a custom list of things they can touch. Instead, you create a "Chef" role (access to cooking equipment) and a "Cashier" role (access to the register). When John is hired as a cook, you simply give him the Chef role. If John is promoted to Manager, you swap his role. The system is clean and scalable.

Industry Standard Implementation

In Elasticsearch, privileges are divided into Cluster Privileges (e.g., managing nodes) and Index Privileges (e.g., reading/writing data to specific indices).

Step 1: Create a Role
Let's create a role named log_reader that can only read data from indices starting with logs-.

json
POST /_security/role/log_reader
{
  "cluster": ["monitor"],
  "indices": [
    {
      "names": [ "logs-*" ],
      "privileges": [ "read" ]
    }
  ]
}

Step 2: Create a User and Assign the Role
Now, we create a user named john_doe and attach the log_reader role to him.

json
POST /_security/user/john_doe
{
  "password" : "SuperSecretPassword123!",
  "roles" : [ "log_reader" ],
  "full_name" : "John Doe",
  "email" : "[email protected]"
}

Now, if John tries to write data or read from an index named financial-data, Elasticsearch will block him with an HTTP 403 Forbidden error.


4. Securing API Keys for Application Access 🔑

Definition

An API Key in Elasticsearch is a unique, base64-encoded string generated by the cluster that allows external applications to authenticate and interact with Elasticsearch APIs without requiring a traditional username and password.

The Problem

Imagine you build a web application that searches your Elasticsearch data. To connect the app to the database, you hardcode John's username and password into the application's backend.

  1. If John leaves the company and his account is deleted, the application breaks.
  2. If the application is hacked, the attackers now have John's password, which he might have reused elsewhere.
  3. Passwords often require periodic rotation, causing unnecessary application downtime.

The Solution

Instead of user credentials, we issue an API Key specifically for that application.

Think of it like a hotel valet system. When you arrive at a hotel, you don't give the valet the master keys to your house, your car's glovebox, and your trunk. You give them a "valet key" that only turns on the ignition and opens the driver's door. API keys are the valet keys of Elasticsearch. They are scoped strictly to what the application needs, they don't depend on a human user's lifecycle, and if compromised, you simply invalidate the key without affecting human users.

Industry Standard Implementation

Step 1: Create the API Key
You can create an API key via Kibana (Stack Management > API Keys) or via the API. Best practice is to restrict the API key's privileges upon creation using role_descriptors.

json
POST /_security/api_key
{
  "name": "frontend-search-app-key",
  "expiration": "30d", 
  "role_descriptors": {
    "search_app_role": {
      "indices": [
        {
          "names": ["products"],
          "privileges": ["read"]
        }
      ]
    }
  }
}

Note: If you don't provide role_descriptors, the API key inherits the permissions of the user who created it. It is an industry best practice to always strictly define permissions for API keys.

Response from Elasticsearch:

json
{
  "id": "V1a2B3c4D5e6F7g8H9i0",
  "name": "frontend-search-app-key",
  "api_key": "xYz123...",
  "encoded": "V1a2B3c4D5e6F7g8H9i0:xYz123..." 
}

Step 2: Using the API Key
The application will use the encoded string provided in the response. It sends this string in the HTTP headers of every request.

bash
curl -X GET "https://localhost:9200/products/_search" \
     -H "Authorization: ApiKey V1a2B3c4D5e6F7g8H9i0:xYz123..."

If the application gets compromised, you can instantly revoke this specific key via the API:

json
DELETE /_security/api_key
{
  "id": "V1a2B3c4D5e6F7g8H9i0"
}

Final Thoughts 🎯

Security is not an afterthought; it is the foundation of your cluster. By relying on Elasticsearch 8.x defaults, leveraging TLS to encrypt data in motion, using RBAC to organize human access, and restricting application access via API keys, you ensure that your cluster is highly secure, easily manageable, and totally un-meow-able.


References & Further Reading

Challenge

You are the security administrator for an e-commerce platform using Elasticsearch. A new frontend application, product-search-app, needs to query the products index to display search results to users.

To adhere to the principle of least privilege, you must create a dedicated API key for this application. The key must meet the following strict requirements:

  1. The key should be named product_search_key.
  2. It must be non-expiring.
  3. Its permissions must be defined directly within the key's definition using role_descriptors, not inherited from the creator's user permissions.
  4. The permissions must grant only the ability to read and query data from the products index. No other privileges should be granted.

Your task is to write the JSON request body for a POST /_security/api_key request that will create this specific API key.

Expected result

Your solution should be a valid JSON object that serves as the request body for creating an Elasticsearch API key.

The JSON object must contain three top-level keys:

  • "name": with the string value "product_search_key".
  • "expiration": with the value null to indicate the key never expires.
  • "role_descriptors": an object that defines the specific, limited permissions for the key. Inside this object, there should be a key (e.g., "product_reader_role") whose value is an object defining the index permissions. This permissions object must specify the "names" of the index as ["products"] and the "privileges" as ["read", "query"].
json

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.