ACL-based authorization in Kafka - Time & Space Complexity
When Kafka checks if a user can access a resource using ACLs, it needs to look through the list of rules. We want to understand how the time to check access changes as the number of ACL rules grows.
How does the number of ACL rules affect the time it takes to authorize a request?
Analyze the time complexity of the following code snippet.
// Simplified ACL check in Kafka
boolean isAuthorized(String user, String resource, String operation) {
for (AclRule rule : aclRules) {
if (rule.matches(user, resource, operation)) {
return rule.isAllowed();
}
}
return false; // deny if no match
}
This code checks each ACL rule one by one to see if it matches the user, resource, and operation. It returns as soon as it finds a matching rule.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of ACL rules.
- How many times: Up to the total number of ACL rules, until a match is found or all rules are checked.
As the number of ACL rules increases, the time to check authorization grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 rule checks |
| 100 | Up to 100 rule checks |
| 1000 | Up to 1000 rule checks |
Pattern observation: The number of checks grows linearly with the number of ACL rules.
Time Complexity: O(n)
This means the time to authorize grows linearly as the number of ACL rules increases.
[X] Wrong: "Authorization time stays the same no matter how many ACL rules exist."
[OK] Correct: Since the code checks rules one by one, more rules mean more checks, so time grows with the number of rules.
Understanding how authorization checks scale helps you design systems that stay fast as they grow. This skill shows you can think about real-world performance, not just code correctness.
"What if the ACL rules were stored in a hash map keyed by user and resource? How would the time complexity change?"
Practice
Solution
Step 1: Understand ACL-based authorization
ACL-based authorization is used to manage permissions for users or clients in Kafka.Step 2: Identify its main role
Its main role is to control access to Kafka topics and operations, ensuring security.Final Answer:
To control who can access Kafka topics and perform operations -> Option AQuick Check:
ACL controls access = B [OK]
- Confusing ACL with performance tuning
- Thinking ACL compresses data
- Assuming ACL monitors cluster health
alice to read from topic orders?Solution
Step 1: Identify the command to add ACL
The command to add an ACL uses--addand specifies the principal and operation.Step 2: Match user, operation, and topic
We want to add read permission for useraliceon topicorders, so the command must include these exactly.Final Answer:
kafka-acls --add --allow-principal User:alice --operation Read --topic orders -> Option AQuick Check:
Add ACL for alice to read orders = D [OK]
- Using --remove instead of --add
- Using deny instead of allow
- Specifying wrong user or operation
kafka-acls --add --allow-principal User:bob --operation Write --topic payments kafka-acls --add --allow-principal User:bob --operation Read --topic paymentsWhat will happen if user
bob tries to consume messages from topic payments?Solution
Step 1: Analyze the ACLs for user bob
Bob has both Write and Read permissions on the payments topic.Step 2: Understand consuming messages requires Read permission
Since Bob has Read permission, he can consume messages from the payments topic.Final Answer:
Bob will be able to consume messages from the payments topic -> Option DQuick Check:
Read permission allows consume = C [OK]
- Assuming Write permission alone allows consuming
- Confusing produce and consume permissions
- Ignoring the Read permission present
kafka-acls --add --allow-principal User:carol --operation Read --topic salesBut Carol still cannot read from the sales topic. What is the most likely cause?
Solution
Step 1: Check if ACL command syntax is correct
The command correctly adds Read permission for User:carol on topic sales.Step 2: Consider authentication and identity
If Carol cannot read despite ACL, likely her client is not authenticating as User:carol, so ACL does not apply.Final Answer:
Carol's client is not authenticating as User:carol -> Option CQuick Check:
ACL needs matching user identity = A [OK]
- Thinking Write permission is needed to read
- Assuming Kafka broker restart is required for ACL changes
- Confusing user names in ACL commands
logs but deny them from deleting the topic. Which ACL commands correctly achieve this?Solution
Step 1: Understand permission requirements
The users should be allowed to produce messages but denied from deleting the topic.Step 2: Use allow and deny ACLs appropriately
We add an allow ACL for Produce operation and explicitly add a deny ACL for Delete operation to prevent deletion.Final Answer:
Add allow ACL for Produce operation and add deny ACL for Delete operation for the user group -> Option BQuick Check:
Allow produce + deny delete = A [OK]
- Assuming deny is default without adding deny ACL
- Allowing delete by mistake
- Mixing up allow and deny commands
Ready to prove it?
Pass the test and this pattern moves unknown
