Skip to content

Instantly share code, notes, and snippets.

@moea
Created January 3, 2022 14:22
Show Gist options
  • Save moea/149a0d03dd0d6f09ac442e17022fa0c1 to your computer and use it in GitHub Desktop.
Save moea/149a0d03dd0d6f09ac442e17022fa0c1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from oso import Oso, Relation
from dataclasses import dataclass
from functools import partial
policy = """
actor User {
relations = {groups: Group};
}
resource Group {
permissions = ["read"];
roles = ["host", "member"];
"read" if "member";
"member" if "host";
}
allow(actor, action, resource) if
has_permission(actor, action, resource);
has_role(u: User, _role: String, _: Group) if
g in u.groups and g.name = "testing";
"""
open('policy.polar', 'w').write(policy)
OSO = Oso()
args = {'exec_query': lambda q: q,
'combine_query': lambda a, b: (a, b)}
def build_query(table, filt):
print(table, filt)
class Something:
def __getattr__(self, x):
return self
def __iter__(self):
return iter([self])
return Something()
@dataclass
class User:
id: str
OSO.register_class(
User,
fields={'id': str,
'groups': Relation(
kind='many', my_field='id', other_field='user_id', other_type='Group')},
build_query=partial(build_query, 'users'),
**args)
class Group:
pass
OSO.register_class(
Group,
fields={'id': str},
build_query=partial(build_query, 'group'),
**args)
OSO.load_files(["policy.polar"])
print(OSO.authorized_query(User(id="xyz"), "read", Group))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment