API key management helps you control who can access your Elasticsearch data safely. It lets you create, use, and delete keys that act like secret passwords for apps.
API key management in Elasticsearch
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Elasticsearch
POST /_security/api_key
{
"name": "my-api-key",
"role_descriptors": {
"my-role": {
"cluster": ["all"],
"index": [
{
"names": ["my-index"],
"privileges": ["read"]
}
]
}
}
}The name is a friendly label for your API key.
role_descriptors define what the key can do, like read or write on certain indexes.
Examples
Elasticsearch
POST /_security/api_key
{
"name": "read-only-key",
"role_descriptors": {
"read_role": {
"cluster": ["monitor"],
"index": [
{
"names": ["logs-*"],
"privileges": ["read"]
}
]
}
}
}Elasticsearch
DELETE /_security/api_key
{
"ids": ["api_key_id_here"]
}Elasticsearch
GET /_security/api_key?name=my-api-key
Sample Program
This request creates an API key named 'example-key' that can monitor the cluster and read/write to 'example-index'.
Elasticsearch
POST /_security/api_key
{
"name": "example-key",
"role_descriptors": {
"example-role": {
"cluster": ["monitor"],
"index": [
{
"names": ["example-index"],
"privileges": ["read", "write"]
}
]
}
}
}Important Notes
Always keep your API keys secret like passwords.
You can create keys with very specific permissions to keep your data safe.
Deleting an API key immediately stops its access.
Summary
API key management lets you create and control secret keys for apps to access Elasticsearch.
You define what each key can do using roles and privileges.
Keys can be created, viewed, and deleted using Elasticsearch security API calls.
Practice
1. What is the primary purpose of an API key in Elasticsearch?
easy
Solution
Step 1: Understand API key role
API keys are secret tokens used to authenticate and authorize applications.Step 2: Identify purpose in Elasticsearch
They grant controlled access to Elasticsearch resources based on assigned roles.Final Answer:
To allow applications to securely access Elasticsearch with specific permissions -> Option CQuick Check:
API key = secure app access [OK]
Hint: API keys control app access permissions [OK]
Common Mistakes:
- Confusing API keys with data storage
- Thinking API keys monitor cluster health
- Assuming API keys handle backups
2. Which of the following is the correct Elasticsearch API call to create an API key?
easy
Solution
Step 1: Recall API key creation syntax
Elasticsearch uses POST method to create resources like API keys.Step 2: Match correct endpoint
The correct endpoint for creating an API key is POST /_security/api_key.Final Answer:
POST /_security/api_key -> Option DQuick Check:
POST + /_security/api_key = create key [OK]
Hint: Use POST to create API keys in Elasticsearch [OK]
Common Mistakes:
- Using GET or DELETE for creation
- Confusing endpoint paths
- Using PUT instead of POST
3. Given this API key creation request body, what will be the name of the created API key?
{
"name": "my-app-key",
"role_descriptors": {
"my-role": {
"cluster": ["all"],
"index": [{"names": ["logs-*"], "privileges": ["read"]}]
}
}
}medium
Solution
Step 1: Identify the API key name field
The "name" field in the request body sets the API key's name.Step 2: Read the value of the "name" field
The value is "my-app-key", which becomes the API key's name.Final Answer:
my-app-key -> Option AQuick Check:
API key name = "name" field value [OK]
Hint: API key name is in the "name" field [OK]
Common Mistakes:
- Confusing role name with API key name
- Using index pattern as key name
- Mistaking privileges for name
4. You try to delete an API key using this request:
DELETE /_security/api_key?id=12345 but get an error. What is the likely cause?medium
Solution
Step 1: Check API key deletion syntax
Elasticsearch requires the API key ID in the request body JSON, not as a URL query parameter.Step 2: Understand method support
DELETE method is supported, but parameters must be correctly passed in the body.Final Answer:
API key ID must be passed in the request body, not as a query parameter -> Option BQuick Check:
Delete API key ID in body, not URL [OK]
Hint: Pass API key ID in JSON body for deletion [OK]
Common Mistakes:
- Passing ID as URL query parameter
- Using wrong HTTP method
- Confusing API key name with ID
5. You want to create an API key that only allows reading from indices starting with "sales-" and no cluster privileges. Which role descriptor is correct in the request body?
hard
Solution
Step 1: Identify required privileges
The API key should have no cluster privileges and only read privileges on indices starting with "sales-".Step 2: Match role descriptor to requirements
{ "role_descriptors": { "read_sales": { "cluster": [], "index": [{ "names": ["sales-*"], "privileges": ["read"] }] } } } has empty cluster privileges and read privilege on "sales-*" indices, matching the requirement.Final Answer:
{ "role_descriptors": { "read_sales": { "cluster": [], "index": [{ "names": ["sales-*"], "privileges": ["read"] }] } } } -> Option AQuick Check:
No cluster + read sales-* = { "role_descriptors": { "read_sales": { "cluster": [], "index": [{ "names": ["sales-*"], "privileges": ["read"] }] } } } [OK]
Hint: Empty cluster array means no cluster privileges [OK]
Common Mistakes:
- Giving cluster all privileges by mistake
- Using write or all privileges instead of read
- Applying privileges to wrong index patterns
