Bird
0
0

A microservice uses this RBAC check:

medium📝 Analysis Q14 of 15
Microservices - Authentication and Authorization
A microservice uses this RBAC check:
def check_access(role, permission):
    role_permissions = {
        "editor": ["read", "write"],
        "viewer": ["read"]
    }
    return permission in role_permissions[role]

print(check_access("admin", "write"))
What is the problem and how to fix it?
AKeyError occurs; fix by using role_permissions.get(role, [])
BSyntaxError due to missing colon; add colon after function
CReturns False incorrectly; add "admin" role to dictionary
DReturns True incorrectly; remove "write" from editor permissions
Step-by-Step Solution
Solution:
  1. Step 1: Identify error cause

    Accessing role_permissions["admin"] causes KeyError because "admin" key is missing.
  2. Step 2: Fix by safe dictionary access

    Use role_permissions.get(role, []) to return empty list if role missing, avoiding error.
  3. Final Answer:

    KeyError occurs; fix by using role_permissions.get(role, []) -> Option A
  4. Quick Check:

    Missing key causes KeyError; get() avoids it [OK]
Quick Trick: Use get() to avoid KeyError on missing roles [OK]
Common Mistakes:
MISTAKES
  • Adding roles without permissions
  • Confusing syntax errors with runtime errors
  • Ignoring missing key errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes