![]() |
VOOZH | about |
Elasticsearch is a powerful distributed search and analytics engine commonly used for logging, monitoring, and data analysis. Security is paramount when dealing with sensitive data, and basic authentication is one of the fundamental methods to ensure that only authorized users can access your Elasticsearch cluster.
This article provides a detailed guide on setting up basic authentication for an Elasticsearch cluster, complete with examples and outputs. The guide is designed to be easy to understand and beginner-friendly.
Basic authentication helps in:
Prerequisites
Before setting up basic authentication, ensure you have the following:
By default, security features in Elasticsearch are disabled. To enable them, we need to modify the Elasticsearch configuration and restart the service.
Open the elasticsearch.yml configuration file and add the following settings:
xpack.security.enabled: trueElasticsearch requires transport and HTTP layer encryption. Use the elasticsearch-certutil tool to generate the necessary certificates.
bin/elasticsearch-certutil ca
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
Follow the prompts to generate the certificates. Typically, you would run these commands in your Elasticsearch directory.
Add the generated certificates to the Elasticsearch keystore:
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
Restart Elasticsearch to apply the changes:
bin/elasticsearchBasic authentication uses usernames and passwords to control access to the Elasticsearch API.
Users can be created using Kibana or the Elasticsearch REST API.
Using Kibana
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"
}'
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
}
}
Properly managing users and roles is crucial for securing an Elasticsearch cluster.
Roles define specific permissions for users. You can create and manage roles using Kibana or the REST API.
Using Kibana
Using the REST API
Create a role using the REST API:
curl -u my_user:mypassword -X PUT "localhost:9200/_security/role/my_role" -H 'Content-Type: application/json' -d'
{
"cluster": ["all"],
"indices": [
{
"names": ["myindex"],
"privileges": ["read"]
}
]
}'
Assign the created role to a user using Kibana or the REST API.
Using Kibana
Using the REST API
Assign a role to a user using the REST API:
curl -u my_user:mypassword -X POST "localhost:9200/_security/user/my_user/_roles" -H 'Content-Type: application/json' -d'
{
"roles": ["my_role"]
}'
Authenticated API requests will now have access based on the assigned roles.
Example: Querying an Index with Role-Based Permissions
curl -u my_user:mypassword -X GET "localhost:9200/myindex/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'
Output
The response will include documents from the myindex index:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "myindex",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "John Doe",
"age": 30,
"city": "New York"
}
}
]
}
}
Enforcing password policies ensures that users use strong passwords. This can be configured in the elasticsearch.yml file:
xpack.security.authc.password_hashing.algorithm: bcrypt
xpack.security.authc.password_min_length: 8
xpack.security.authc.password_complexity: high
Restrict access to your Elasticsearch cluster based on IP addresses. This can be configured using the xpack.security.http.filter settings in the elasticsearch.yml file:
xpack.security.http.filter.allow: ["192.168.1.0/24"]
xpack.security.http.filter.deny: ["0.0.0.0/0"]
Enabling auditing allows you to track security-related events. Configure auditing in the elasticsearch.yml file:
xpack.security.audit.enabled: true
xpack.security.audit.logfile.events.emit_request_body: true
Audit logs can help in monitoring and troubleshooting security-related incidents.
Setting up basic authentication in Elasticsearch is a fundamental step in securing your cluster. By enabling security features, creating users, managing roles, and configuring additional security measures, you can ensure that your data is protected and only accessible to authorized users.
This guide provided a comprehensive overview of the steps involved in setting up basic authentication, with examples and expected outputs to help you understand and implement the necessary configurations. With these practices, you can enhance the security of your Elasticsearch deployment and ensure that your data remains safe and secure.