Oso Cloud Python Client
Oso Cloud is an authorization-as-a-service platform. The Python client (`oso-cloud`) provides a convenient wrapper around the Oso Cloud HTTP API, enabling applications to interact with the service for modeling, storing, enforcing, querying, and testing authorization logic using the declarative Polar policy language. The library is currently at version 2.6.0 and demonstrates a healthy release cadence, with the last version released less than a year ago.
Warnings
- breaking Migration from `oso-cloud` v1 to v2 involved significant breaking changes. The Fact Management API now uses tuples instead of dictionaries for facts, and methods like `tell` were replaced by `insert`. The Query API was also replaced by a more powerful `build_query()` API, and `authorize_resources` was removed.
- gotcha When updating facts or policies in Oso Cloud, direct changes can lead to inconsistent authorization if not handled carefully during the transition period.
- deprecated The standalone `oso` open-source library is deprecated. New projects and existing users are encouraged to migrate to Oso Cloud and its client libraries (`oso-cloud`).
- gotcha The Oso Dev Server (a local tool often used with `oso-cloud`) had a breaking change in v1.2 related to its data schema. Existing local data might become incompatible.
- gotcha The `*` literal as a resource identifier was disallowed in Oso Dev Server v1.15.0 to align its behavior with Oso Cloud. Policies using `*` might break.
- gotcha For high-availability production systems, relying solely on the Oso Cloud service might not be sufficient for all failure scenarios.
Install
-
pip install oso-cloud
Imports
- Oso
from oso import Oso
from oso_cloud import Oso
- Value
from oso_cloud import Value
- IntoValue
from oso_cloud import IntoValue
- IntoFact
from oso_cloud import IntoFact
Quickstart
import os
from oso_cloud import Oso, Value
# Initialize Oso Cloud client with an API key
# Ensure OSO_AUTH environment variable is set or pass it directly:
# oso = Oso(api_key=os.environ.get('OSO_AUTH', ''))
oso = Oso()
# Define example data
user = Value("User", "alice")
repository = Value("Repository", "my-repo")
organization = Value("Organization", "acme")
async def run_auth_checks():
try:
# Insert a fact: Alice has the role 'owner' on 'my-repo'
await oso.insert(("has_role", user, "owner", repository))
print(f"Inserted fact: Alice is owner of my-repo.")
# Check if Alice can 'read' the 'my-repo' repository
# This assumes a policy has been uploaded to Oso Cloud, e.g.,
# allow(user: User, "read", repo: Repository) if has_role(user, "owner", repo);
is_allowed = await oso.check(user, "read", repository)
print(f"Can Alice 'read' my-repo? {is_allowed}")
# Query for all repositories Alice can 'read'
# Assumes a policy defining 'allow' rules.
readable_repos = []
async for result in oso.query(user, "read", Value("Repository")):
if result.resource:
readable_repos.append(result.resource.id)
print(f"Repositories Alice can read: {readable_repos}")
# Delete the previously inserted fact
await oso.delete(("has_role", user, "owner", repository))
print(f"Deleted fact: Alice is owner of my-repo.")
except Exception as e:
print(f"An error occurred: {e}")
print("Make sure OSO_AUTH environment variable is set with your Oso Cloud API key.")
import asyncio
asyncio.run(run_auth_checks())