Role-based access control in Elasticsearch - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using role-based access control in Elasticsearch, it is important to understand how the system checks permissions as the number of roles and users grows.
We want to know how the time to verify access changes when more roles or permissions are added.
Analyze the time complexity of the following role check query.
POST /_security/user/_has_privileges
{
"username": "alice",
"privileges": {
"cluster": ["monitor"],
"index": [
{
"names": ["sales-data"],
"privileges": ["read"]
}
]
}
}
This request checks if user "alice" has the specified cluster and index privileges based on her assigned roles.
When Elasticsearch checks privileges, it:
- Primary operation: Iterates over all roles assigned to the user.
- How many times: Once per role, checking each role's permissions.
The dominant work is checking each role's permissions against requested privileges.
As the number of roles assigned to a user increases, the time to check privileges grows roughly in proportion.
| Input Size (number of roles) | Approx. Operations |
|---|---|
| 10 | 10 permission checks |
| 100 | 100 permission checks |
| 1000 | 1000 permission checks |
Pattern observation: The time grows linearly as more roles are checked.
Time Complexity: O(n)
This means the time to verify access grows linearly with the number of roles assigned to the user.
[X] Wrong: "Checking user privileges is always constant time regardless of roles."
[OK] Correct: The system must check each role's permissions, so more roles mean more checks and longer time.
Understanding how role checks scale helps you explain system performance and design better access control in real projects.
What if we changed from checking roles one by one to caching combined permissions? How would the time complexity change?
Practice
Solution
Step 1: Understand RBAC concept
RBAC is about managing permissions by assigning roles to users.Step 2: Identify RBAC purpose in Elasticsearch
It controls who can do what actions on the cluster or indexes.Final Answer:
To control who can perform specific actions by assigning roles -> Option AQuick Check:
RBAC = Control access by roles [OK]
- Confusing RBAC with data storage or backup
- Thinking RBAC speeds up queries
- Assuming RBAC changes data formats
logs-2024?Solution
Step 1: Check cluster privileges for read access
Read access to an index usually requires cluster privileges like 'monitor', not 'all' or 'read'.Step 2: Verify index privileges
The index privileges must include 'read' for the specified index.Final Answer:
{"cluster": ["monitor"], "indices": [{"names": ["logs-2024"], "privileges": ["read"]}]} -> Option DQuick Check:
Cluster 'monitor' + index 'read' = correct role [OK]
- Using 'all' cluster privilege unnecessarily
- Confusing 'write' with 'read' privileges
- Assigning 'read' cluster privilege which is invalid
sales-data index?
{
"cluster": ["monitor"],
"indices": [
{
"names": ["sales-data"],
"privileges": ["read", "write"]
}
]
}Solution
Step 1: Analyze cluster privileges
Cluster privilege 'monitor' allows monitoring but no write or admin cluster changes.Step 2: Analyze index privileges
Privileges 'read' and 'write' on 'sales-data' index allow reading and writing data there.Final Answer:
User can read and write data in sales-data index -> Option AQuick Check:
Index 'read' + 'write' = read/write access [OK]
- Ignoring 'write' privilege and assuming read-only
- Confusing cluster 'monitor' with admin rights
- Assuming full admin access without 'all' privilege
app-logs index. What is the error?
{
"cluster": ["monitor"],
"indices": [
{
"names": ["app-logs"],
"privileges": ["read"]
}
]
}Solution
Step 1: Check index privileges
The role only grants 'read' privilege on 'app-logs', so writing is not allowed.Step 2: Identify missing privilege
To write, the 'write' privilege must be added to the index privileges.Final Answer:
The index privilege should include 'write' to allow writing -> Option BQuick Check:
Write access needs 'write' privilege [OK]
- Assuming 'monitor' cluster privilege allows writing
- Overlooking missing 'write' privilege on index
- Thinking 'run_as' is required for write permission
prod- but only write to prod-logs. Which role definition is correct?Solution
Step 1: Understand the requirement
User needs read access on all 'prod-*' indexes and write only on 'prod-logs'.Step 2: Check each option
{ "cluster": ["monitor"], "indices": [ {"names": ["prod-*"], "privileges": ["read"]}, {"names": ["prod-logs"], "privileges": ["write"]} ] } correctly assigns 'read' to 'prod-*' and 'write' to 'prod-logs'. { "cluster": ["all"], "indices": [ {"names": ["prod-logs"], "privileges": ["read", "write"]} ] } gives full cluster 'all' which is too broad. { "cluster": ["monitor"], "indices": [ {"names": ["prod-logs"], "privileges": ["read", "write"]}, {"names": ["prod-*"], "privileges": ["read", "write"]} ] } incorrectly grants 'read' and 'write' to all 'prod-*' indexes. { "cluster": ["monitor"], "indices": [ {"names": ["prod-*"], "privileges": ["write"]} ] } wrongly gives 'write' to all 'prod-*' indexes.Final Answer:
{ "cluster": ["monitor"], "indices": [ {"names": ["prod-*"], "privileges": ["read"]}, {"names": ["prod-logs"], "privileges": ["write"]} ] } -> Option CQuick Check:
Read on prod-* + write on prod-logs = correct role [OK]
- Giving write privilege to all prod-* indexes
- Using cluster 'all' unnecessarily
- Mixing up index names and privileges
