Reference
API
Python

Python Client API

💡

Before going through this guide, make sure you follow the Oso Cloud Quickstart to get your Oso Cloud API Key properly set in your environment.

First, install the oso-cloud package on PyPI:

pip install oso-cloud

Instantiating an Oso Cloud client

The Oso Cloud library works by providing an Oso class which you configure with your Oso Cloud URL and API key:

from oso_cloud import Oso
 
oso = Oso(url="https://cloud.osohq.com", api_key=YOUR_API_KEY)
 
# Later:
oso.tell({"name": "has_role", "args": [user, role, resource]})
 
# Wherever authorization needs to be performed:
if oso.authorize(user, action, resource):
    # Action is allowed

Passing application entities into the client

Under the hood, Oso Cloud represents an entity in your application as a combination of a type and an ID, which together uniquely identify the entity. The Python client represents these entities as dictionaries with both type and id properties. For example:

alice = {"type": "User", "id": "alice"}
anvils_repository = {"type": "Repostory", "id": "anvils"}

You will pass dictionaries like these into nearly every function call you make to the Python client.

Management API

Add fact: oso.tell(fact)

Adds a fact named name with the provided arguments. Example:

oso.tell({
  "name": "has_role",
  "args": [{"type": "User": "id": "bob"}, "owner", {"type": "Organization": "id": "acme"}]
})
ℹ️

For Oso Cloud sandbox accounts, bulk_tell, bulk_delete, and bulk calls are limited to 20 facts. If you attempt to send more than 20 facts, these functions will throw an error.

Add many facts: oso.bulk_tell([*facts])

Adds many facts at once. Example:

oso.bulk_tell([
    {
      "name": "has_role",
      "args": [{"type": "User", "id": "bob"}, "owner", {"type": "Organization", "id": "acme"}]
    },
    {
      "has_role",
      "args": [{"type": "User", "id": "bob"}, "maintainer", {"type": "Repository", "id": "anvils"}]
    },
])

Delete fact: oso.delete(fact)

Deletes a fact. Does not throw an error if the fact is not found. Example:

oso.delete({
  "name": "has_role",
  "args": [{"type": "User": "id": "bob"}, "maintainer", {"type": "Repository": "id": "anvils"}]
})

Delete many facts: oso.bulk_delete([*facts])

Deletes many facts at once. Does not throw an error when some of the facts are not found. Example:

oso.bulk_delete([
    {
      "name": "has_role",
      "args": [{"type": "User", "id": "bob"}, "owner", {"type": "Organization", "id": "acme"}]
    },
    {
      "name": "has_role",
      "args": {"type": "User" "id": "bob"}, "maintainer", {"type": "Repository", "id": "anvils"}]
    },
])

Transactionally delete and add facts: oso.bulk(delete=[*facts], tell=[*facts])

Deletes and adds many facts in one atomic transaction. The deletions are performed before the adds. None can be used as a wildcard in facts in delete. Does not throw an error when the facts to delete are not found. Example:

oso.bulk(delete=
  [
    # All `has_role` facts linking User:bob and Repository:anvils will be deleted.
    {
      "name": "has_role",
      "args": [{"type": "User", "id": "bob"}, None, { "type": "Repository", "id": "anvils" }],
    }
  ], tell=
  [
    # This fact will be added.
    {
      "name": "has_role",
      "args": [{"type": "User", "id": "bob"}, "maintainer", { "type": "Repository", "id": "anvils" }]
    }
  ]
)

List facts: oso.get(fact)

ℹ️

For Oso Cloud sandbox accounts, Get calls are limited to 1000 results. If you have more than 1000 facts, the function will throw an error.

Lists facts that are stored in Oso Cloud. Can be used to check the existence of a particular fact, or used to fetch all facts that have a particular argument:

bob = {"type": "User", "id": "bob"}
anvils = {"type": "Repository", "id": "anvils"}
 
# Get one fact:
oso.get({"name": "has_role", "args": [bob, "admin", anvils])
# => [{
#   "name": "has_role",
#   "args": [
#     { "type": "User", "id": "bob" },
#     { "type": "String", "id": "admin" },
#     { "type": "Repository", "id": "anvils" }
#   ]
# }]
 
# List all role-related facts on the `anvils` repo
oso.get({"name": "has_role", "args": [None, None, anvils]})
# => [
#   {
#     "name": "has_role",
#     "args": [
#       { "type": "User", "id": "bob" },
#       { "type": "String", "id": "admin" },
#       { "type": "Repository", "id": "anvils" }
#     ]
#   },
#   ... other has_role facts
# ]

Note that None behaves like a wildcard: passing the fact arguments None, None, anvils means "find all facts where anvils is the third argument, regardless of other arguments".

Query for anything: oso.query(rule)

Query Oso Cloud for any predicate and any combination of concrete and wildcard arguments. Unlike oso.get, which only lists facts you've added, you can use oso.query to list derived information about any rule in your policy. Example:

# Query for all the repos `User:bob` can `read`
oso.query({
  "name": "allow",
  "args": [{"type": "User", "id": "bob"}, "read", {"type": "Repository"}]
})
# => [
#      {
#        "name": "allow",
#        "args": [{"type": "User", "id": "bob"}, "read", {"type": "Repository", "id": "acme"}],
#      },
#      {
#        "name": "allow",
#        "args": [{"type": "User", "id": "bob"}, "read", {"type": "Repository", "id": "anvils"}],
#      }
#    ]
 
# Query for all the objects `User:admin` can `read`
oso.query({
  "name": "allow",
  "args": [{"type": "User", "id": "admin"}, "read", None]
})
# => [
#      # `User:admin` can `read` anything
#      {
#        "name": "allow",
#        "args": [{"type": "User", "id": "admin"}, "read", None],
#      },
#    ]

Note that None behaves like a wildcard. Passing "allow", None, None, anvils means "find anyone who can do anything to anvils". None also behaves like a wildcard in return values from oso.query. Additionally, if you want to query over all instances of a particular type, pass a dictionary with a "type" key but no "id" key. For example, "allow", bob, "read", {"type": "Repository"} will query for all the objects of type "Repository" that bob can read.

Learn more about how to query Oso Cloud.

Check API

Context facts

ℹ️

For Oso Cloud sandbox accounts, the number of context facts per request is limited to 20.

You may provide an array of context facts as an optional argument to any of the Check API methods. When Oso Cloud performs a check, it will consider these context facts in addition to any other facts you've previously added. Context facts are only used in the API call in which they're provided— they do not persist across requests. Learn more about context facts.

Check a permission: oso.authorize(actor, action, resource)

Determines whether or not an action is allowed, based on a combination of authorization data and policy logic. Example:

alice = {"type": "User", "id": "alice"}
anvils_repository = {"type": "Repository", "id": "anvils"}
 
if not oso.authorize(alice, "read", anvils_repository):
    raise Exception("Action is not allowed")

You may provide a list of context facts as an optional fourth argument to this method. Example:

issue_on_anvils_repository = {"type": "Issue", "id": "anvils-1"}
 
oso.authorize(alice, "read", anvils_repository, [
  # a context fact
  {
    "name": "has_relation",
    "args": [issue_on_anvils_repository, "parent", anvils_repository]
  }
])

Check authorized resources: oso.authorize_resources(actor, action, resources)

Returns a subset of resources on which an actor can perform a particular action. Ordering and duplicates, if any exist, are preserved. Example:

alice = {"type": "User", "id": "alice"}
anvils_repository = {"type": "Repository", "id": "anvils"}
acme_repository = {"type": "Repository", "id": "acme"}
 
oso.authorize_resources(alice, "read", [anvils_repository, acme_repository])
# => [acme_respository]

You may provide a list of context facts as an optional fourth argument to this method. Example:

issue_on_acme_repository = {"type": "Issue", "id": "acme-1"}
issue_on_anvils_repository = {"type": "Issue", "id": "anvils-2"}
 
oso.authorize_resources(
  alice, "read", [anvils_repository, acme_repository],
  [ # context facts
    {
      "name": "has_relation",
      "args": [issue_on_anvils_repository, "parent", anvils_repository]
    },
    {
      "name": "has_relation",
      "args": [issue_on_acme_repository, "parent", acme_repository]
    },
  ]
)
# => [issue_on_acme_repository]

List authorized resources: oso.list(actor, action, resource_type)

Fetches a list of resources on which an actor can perform a particular action. Example:

alice = {"type": "User", "id": "alice"}
 
oso.list(alice, "read", "Repository")
# => ["acme"]

You may provide a list of context facts as an optional fourth argument to this method. Example:

anvils_repository = {"type": "Repository", "id": "anvils"}
acme_repository = {"type": "Repository", "id": "acme"}
 
issue_on_acme_repository = {"type": "Issue", "id": "acme-1"}
issue_on_anvils_repository = {"type": "Issue", "id": "anvils-2"}
 
oso.list(
  alice, "read", "Issue",
  [ # context facts
    {
      "name": "has_relation",
      "args": [issue_on_anvils_repository, "parent", anvils_repository]
    },
    {
      "name": "has_relation",
      "args": [issue_on_acme_repository, "parent", acme_repository]
    }
  ]
)
# => ["acme-1"]

List authorized actions: oso.actions(actor, resource)

Fetches a list of actions which an actor can perform on a particular resource. Example:

alice = {"type": "User", "id": "alice"}
acme_repository = {"type": "Repository", "id": "acme"}
 
oso.actions(alice, acme_repository)
# => ["read"]

You may provide a list of context facts as an optional third argument to this method. Example:

issue_on_acme_repository = {"type": "Issue", "id": "acme-1"}
 
actions = oso.actions(
  alice, issue_on_acme_repository,
  # a context fact
  [
    {
      "name": "has_relation",
      "args": [issue_on_acme_repository, "parent", acme_repository]
    }
  ]
)
# => ["read"]

Policy API

Update the active policy: oso.policy(policy)

Updates the policy in Oso Cloud. The string passed into this method should be written in Polar. Example:

oso.policy("actor User {}")

Talk to an Oso Engineer

Our team is happy to help you get started with Oso Cloud. If you'd like to learn more about using Oso Cloud in your app or have any questions about this guide, schedule a 1x1 with an Oso engineer.

Get started with Oso Cloud →

Last updated on September 30, 2022