Setting Up Basic Authentication
Basic authentication uses usernames and passwords to control access to the Elasticsearch API.
Step 1: Create a User
You can create users using the Kibana UI or the Elasticsearch REST API.
Using Kibana
- Open Kibana and go to Management > Security > Users.
- Click Create user.
- Fill in the username, password, and assign roles (e.g., superuser).
Using the REST API
Alternatively, you can create a user using the REST API:
curl -X POST "localhost:9200/_security/user/my_user" -H 'Content-Type: application/json' -d'
{
"password" : "mypassword",
"roles" : [ "superuser" ],
"full_name" : "John Doe",
"email" : "john.doe@example.com"
}'
Step 2: Authenticate API Requests
To authenticate API requests, include the username and password in the request header.
Example: Indexing a Document
curl -u my_user:mypassword -X POST "localhost:9200/myindex/_doc/1" -H 'Content-Type: application/json' -d'
{
"name": "John Doe",
"age": 30,
"city": "New York"
}'
Output
The response indicates that the document is indexed successfully:
{
"_index": "myindex",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
Elasticsearch API Authentication: How to Set Up with Examples
Elasticsearch is a powerful distributed search and analytics engine widely used for logging, monitoring, and data analysis. To protect your data and ensure secure access, setting up API authentication is essential.
This article will guide you through the process of configuring Elasticsearch API authentication with detailed examples and outputs. We will cover basic authentication, API keys, and role-based access control (RBAC).