Skip to content

Commit

Permalink
Remove v1 DF API from Python and Ruby (#1497)
Browse files Browse the repository at this point in the history
* rm most of it

* rm the rest from python

* py docs update

* sequel adapter

* rm node df test

* rm node add-to-your-app

* no node

* rm references to removed doc

* rm another node example

* restore node app

Co-authored-by: Stephen Olsen <[email protected]>
  • Loading branch information
gw and saolsen authored Jan 24, 2022
1 parent 318f073 commit 37c0941
Show file tree
Hide file tree
Showing 38 changed files with 184 additions and 3,187 deletions.

This file was deleted.

11 changes: 11 additions & 0 deletions docs/content/node/getting-started/application/enforce/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Enforce authorization
description: |
Add authorization enforcement throughout your application using the
authorize API to reject or accept requests that users make.
weight: 2
---

# Enforce authorization

{{% coming_soon %}}

This file was deleted.

19 changes: 19 additions & 0 deletions docs/content/node/getting-started/application/filter-data/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Filter collections of data
description: |
Many applications perform authorization over large collections of data
that cannot be loaded into memory. Often index pages showing users a
number of resources, like the repositories they can access, will need to
use data filtering. The data filtering API provides support for these
use cases.
weight: 4
---

# Filter collections of data

{{% coming_soon %}}

## What's next

This is the end of __Add to your app__! For more detail on using
Oso, see the [How to guides](/guides).
11 changes: 0 additions & 11 deletions docs/content/node/getting-started/application/model/data/data.md

This file was deleted.

15 changes: 15 additions & 0 deletions docs/content/node/getting-started/application/model/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Model your authorization logic
description: |
Authorization in Oso starts with the policy. Authorization policies
define the resources that you want to control access to, and rules that
specify when an actor (the user making the request) can perform an
action on a resource. Because Oso is a library, you can write
authorization policies directly over the same data types that are
already used in your application.
weight: 1
---

# Model your authorization logic

{{% coming_soon %}}
7 changes: 2 additions & 5 deletions docs/examples/add-to-your-application/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
# Gemfile defined in `docs/examples/Makefile`.
unexport BUNDLE_GEMFILE

test: test-python test-policy-extensions test-ruby test-node test-go
test: test-python test-policy-extensions test-ruby test-go

test-policy-extensions: venv
. venv/bin/activate; pytest test_policy_extensions.py

test-python:
$(MAKE) -C python test

test-node:
$(MAKE) -C node test

test-ruby:
$(MAKE) -C ruby test

Expand All @@ -26,4 +23,4 @@ venv/touchfile: requirements.txt
. venv/bin/activate; pip install -r requirements.txt
touch $@

.PHONY: test test-policy-extensions test-python test-node test-ruby test-go
.PHONY: test test-policy-extensions test-python test-ruby test-go
110 changes: 0 additions & 110 deletions docs/examples/add-to-your-application/node/dataFiltering.js

This file was deleted.

1 change: 0 additions & 1 deletion docs/examples/add-to-your-application/node/main.polar

This file was deleted.

21 changes: 21 additions & 0 deletions docs/examples/add-to-your-application/node/main.polar
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
actor User {}

resource Repository {
permissions = ["read", "push", "delete"];
roles = ["contributor", "maintainer", "admin"];

"read" if "contributor";
"push" if "maintainer";
"delete" if "admin";

"maintainer" if "admin";
"contributor" if "maintainer";
}

has_role(actor: User, role_name: String, repository: Repository) if
role in actor.roles and
role_name = role.name and
repository = role.repository;

allow(actor, action, resource) if
has_permission(actor, action, resource);
12 changes: 0 additions & 12 deletions docs/examples/add-to-your-application/node/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,9 @@ async function routeWorks() {
.expect(200);
}

async function dataFilteringWorks() {
const { app } = require('./dataFiltering');
await request(app)
.get('/repos')
.expect(200)
.then(response => {
console.log(response.text);
assert(response.text.length >= 3);
});
}

async function runTests() {
await osoInits();
await routeWorks();
await dataFilteringWorks();
}

(async () => {
Expand Down
32 changes: 19 additions & 13 deletions docs/examples/add-to-your-application/python/app/data_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from . import models

from oso import Oso
from polar.data.adapter.sqlalchemy_adapter import SqlAlchemyAdapter

from sqlalchemy import Column, String, Boolean
from sqlalchemy.ext.declarative import declarative_base
Expand All @@ -16,17 +17,19 @@

oso = Oso()

engine = create_engine('sqlite://')
engine = create_engine("sqlite://")
Session = sessionmaker(bind=engine)

Base = declarative_base(bind=engine)


class Repository(Base):
__tablename__ = 'repo'
__tablename__ = "repo"

name = Column(String(128), primary_key=True)
is_public = Column(Boolean)


Base.metadata.create_all()

# docs: begin-data-filtering
Expand Down Expand Up @@ -58,33 +61,36 @@ def get_repositories(filters):

return query


oso.register_class(models.User)
oso.register_class(
Repository,
fields={
# Tell Oso the types of fields you will use in your policy.
"is_public": bool
},
build_query=get_repositories,
exec_query=lambda q: q.all(),
combine_query=lambda q1, q2: q1.union(q2),
# Tell Oso the types of fields you will use in your policy.
"is_public": bool
},

This comment has been minimized.

Copy link
@lafrech

lafrech Jan 26, 2022

Contributor

I guess the get_repositories implementation above should have been deleted as well.

)

oso.set_data_filtering_adapter(SqlAlchemyAdapter(Session()))

oso.load_files([Path(__file__).parent / "main.polar"])
# docs: end-data-filtering


class User:
@staticmethod
def get_current_user():
return models.User(roles=[{"name": "admin", "repository": Repository(name="gmail")}])
return models.User(
roles=[{"name": "admin", "repository": Repository(name="gmail")}]
)


# docs: begin-list-route
@app.route("/repos")
def repo_list():
repositories = oso.authorized_resources(
User.get_current_user(),
"read",
Repository)
repositories = oso.authorized_resources(User.get_current_user(), "read", Repository)

return serialize(repositories)


# docs: end-list-route
11 changes: 6 additions & 5 deletions docs/examples/add-to-your-application/python/app/models.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
from dataclasses import dataclass
from typing import List


@dataclass
class Repository:
name: str

def get_by_name(name):
return repos_db[name]

repos_db = {
"gmail": Repository("gmail")
}

repos_db = {"gmail": Repository("gmail")}

# docs: start
@dataclass
class Role:
name: str
repository: Repository


@dataclass
class User:
roles: List[Role]


users_db = {
"larry": User([Role(name="admin",
repository=repos_db["gmail"])]),
"larry": User([Role(name="admin", repository=repos_db["gmail"])]),
}
# docs: end
Loading

1 comment on commit 37c0941

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 37c0941 Previous: 318f073 Ratio
rust_get_attribute 52276 ns/iter (± 2832) 41132 ns/iter (± 1776) 1.27
n_plus_one/100 2706361 ns/iter (± 129764) 1991947 ns/iter (± 3197) 1.36
n_plus_one/500 12509912 ns/iter (± 151151) 9789828 ns/iter (± 29320) 1.28
n_plus_one/1000 24895245 ns/iter (± 312801) 19508195 ns/iter (± 53189) 1.28
unify_once 1112 ns/iter (± 53) 893 ns/iter (± 145) 1.25
unify_twice 2971 ns/iter (± 227) 2384 ns/iter (± 339) 1.25
many_rules 71458 ns/iter (± 1373) 65042 ns/iter (± 1309) 1.10
fib/5 599967 ns/iter (± 11696) 472026 ns/iter (± 12820) 1.27
prime/3 20435 ns/iter (± 929) 16027 ns/iter (± 629) 1.28
prime/23 20391 ns/iter (± 1027) 15955 ns/iter (± 625) 1.28
prime/43 21035 ns/iter (± 814) 15916 ns/iter (± 633) 1.32
prime/83 20541 ns/iter (± 1088) 15871 ns/iter (± 600) 1.29
prime/255 18695 ns/iter (± 851) 14460 ns/iter (± 549) 1.29
indexed/100 6807 ns/iter (± 619) 5545 ns/iter (± 586) 1.23
indexed/500 8674 ns/iter (± 1725) 7701 ns/iter (± 1960) 1.13
indexed/1000 10585 ns/iter (± 503) 9487 ns/iter (± 704) 1.12
indexed/10000 28526 ns/iter (± 1653) 30550 ns/iter (± 1975) 0.93
not 6833 ns/iter (± 907) 5388 ns/iter (± 138) 1.27
double_not 14094 ns/iter (± 267) 11192 ns/iter (± 224) 1.26
De_Morgan_not 9394 ns/iter (± 316) 7272 ns/iter (± 176) 1.29
load_policy 1021876 ns/iter (± 17862) 807461 ns/iter (± 4539) 1.27
partial_and/1 36785 ns/iter (± 1723) 28509 ns/iter (± 1276) 1.29
partial_and/5 126328 ns/iter (± 5126) 97801 ns/iter (± 3237) 1.29
partial_and/10 241103 ns/iter (± 5733) 211437 ns/iter (± 4941) 1.14
partial_and/20 488279 ns/iter (± 10562) 428205 ns/iter (± 5604) 1.14
partial_and/40 1098849 ns/iter (± 57589) 928805 ns/iter (± 11340) 1.18
partial_and/80 2426045 ns/iter (± 30565) 2086813 ns/iter (± 6319) 1.16
partial_and/100 3196867 ns/iter (± 44657) 2778175 ns/iter (± 13978) 1.15
partial_rule_depth/1 118301 ns/iter (± 4410) 105007 ns/iter (± 4193) 1.13
partial_rule_depth/5 383515 ns/iter (± 9828) 300635 ns/iter (± 4654) 1.28
partial_rule_depth/10 838572 ns/iter (± 14651) 655815 ns/iter (± 33162) 1.28
partial_rule_depth/20 2336650 ns/iter (± 26499) 1840595 ns/iter (± 7830) 1.27
partial_rule_depth/40 8820208 ns/iter (± 320385) 7032133 ns/iter (± 134566) 1.25
partial_rule_depth/80 50319438 ns/iter (± 2216503) 41379555 ns/iter (± 258520) 1.22
partial_rule_depth/100 90565159 ns/iter (± 1120374) 76239674 ns/iter (± 462361) 1.19

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.