Oslo Policy

5.0.0 · active · verified Thu Apr 16

Oslo Policy is a core OpenStack library providing a robust and flexible authorization framework. It allows developers to define fine-grained access control rules using a policy file (JSON or YAML) and enforce them within their applications. As part of the OpenStack Oslo project, it is actively maintained with releases tied to the OpenStack development cycle, currently at version 5.0.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define policy rules, initialize an `Enforcer`, and use its `authorize` method to check permissions based on user context and target data. It shows successful and failed authorization attempts for different user roles and resource ownership scenarios.

from oslo_policy import policy

# Define policy rules inline for simplicity.
# In a real application, these would typically be loaded from a policy file
# (e.g., policy.yaml or policy.json) configured via oslo.config.
# Example: enforcer = policy.Enforcer(policy_file='path/to/policy.yaml')
rules = {
    "admin_api": "role:admin",
    "member_api": "role:member",
    "owner_api": "project_id:%(project_id)s"
}

# Initialize the Policy Enforcer
enforcer = policy.Enforcer(rules=rules)

# Context for an admin user
admin_context = {
    "user_id": "admin_user",
    "roles": ["admin"],
    "project_id": "admin_project"
}

# Context for a regular member user
member_context = {
    "user_id": "member_user",
    "roles": ["member"],
    "project_id": "member_project"
}

# Target data for owner check (e.g., a resource's project_id)
target_data_owner = {"project_id": "member_project"}
target_data_other = {"project_id": "another_project"}


print("--- Admin User Checks ---")
try:
    enforcer.authorize("admin_api", admin_context)
    print("Admin can access admin_api: YES")
except policy.PolicyNotAuthorized as e:
    print(f"Admin can access admin_api: NO ({e})")

try:
    enforcer.authorize("member_api", admin_context)
    print("Admin can access member_api: YES")
except policy.PolicyNotAuthorized as e:
    print(f"Admin can access member_api: NO ({e})")

print("\n--- Member User Checks ---")
try:
    enforcer.authorize("admin_api", member_context)
    print("Member can access admin_api: YES")
except policy.PolicyNotAuthorized as e:
    print(f"Member can access admin_api: NO ({e})") # Expected: NO

try:
    enforcer.authorize("member_api", member_context)
    print("Member can access member_api: YES")
except policy.PolicyNotAuthorized as e:
    print(f"Member can access member_api: NO ({e})")

# Check owner_api (member accessing their own project)
try:
    enforcer.authorize("owner_api", member_context, target_data_owner)
    print("Member can access owner_api for their project: YES")
except policy.PolicyNotAuthorized as e:
    print(f"Member can access owner_api for their project: NO ({e})")

# Check owner_api (member accessing another project)
try:
    enforcer.authorize("owner_api", member_context, target_data_other)
    print("Member can access owner_api for another project: YES")
except policy.PolicyNotAuthorized as e:
    print(f"Member can access owner_api for another project: NO ({e})") # Expected: NO

view raw JSON →