This repository has been archived by the owner on Feb 13, 2025. It is now read-only.
forked from datawan-labs/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
wip: prototype data policies #8
Draft
srenatus
wants to merge
2
commits into
main
Choose a base branch
from
sr/data-policies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,8 @@ export interface Database { | |
|
||
query?: string; | ||
|
||
errors?: { message: string; row: number; col: number }[]; | ||
|
||
rego?: string; | ||
|
||
input?: Record<string, unknown>; | ||
|
@@ -121,13 +123,25 @@ interface State { | |
// 2. we need the rego.v1 import because the Preview API has no "v1" flag | ||
const defaultRego = ""; | ||
const rego = { | ||
orders: `package conditions | ||
orders: `package filters | ||
import rego.v1 | ||
|
||
filter["users.name"] := input.user | ||
filter["products.price"] := {"lte": 500} if input.budget == "low" | ||
user := input.user | ||
|
||
include if { | ||
input.users.name == user | ||
input.budget != "low" | ||
} | ||
|
||
include if { | ||
input.budget == "low" | ||
input.products.price < 500 | ||
input.users.name == user | ||
} | ||
|
||
conditions := data.convert.to_conditions(input, ["input.products", "input.users"], "data.filters.include") | ||
|
||
query := ucast.as_sql(filter, "postgres", {"users": {"$self": "u"}, "products": {"$self": "p"}}) | ||
query := ucast.as_sql(conditions, "postgres", {"users": {"$self": "u"}, "products": {"$self": "p"}}) | ||
`, | ||
schools: `package conditions | ||
import rego.v1 | ||
|
@@ -160,6 +174,80 @@ JOIN students s2 ON ss2.student_id = s2.student_id | |
`, | ||
}; | ||
|
||
const convertRego = ` | ||
package convert | ||
|
||
import rego.v1 | ||
|
||
# TODO(sr): this is just good enough, the actual API is TBD | ||
partial_eval(inp, unknowns, query) := http.send({ | ||
"method": "POST", | ||
"url": "http://127.0.0.1:8181/v1/schmompile", | ||
"body": { | ||
"query": query, | ||
"unknowns": unknowns, | ||
"input": inp, | ||
}, | ||
}).body | ||
|
||
to_conditions(inp, unknowns, query) := conds(partial_eval(inp, unknowns, query)) | ||
|
||
conds(pe) := pe if pe.code | ||
conds(pe) := res if { | ||
not pe.code | ||
not pe.result.support # "support modules" are not supported right now | ||
res := or_({query_to_condition(q) | some q in pe.result.queries}) | ||
} | ||
|
||
query_to_condition(q) := and_({expr_to_condition(e) | some e in q}) | ||
|
||
expr_to_condition(e) := op_(op(e), field(e), value(e)) | ||
|
||
op(e) := o if { | ||
e.terms[0].type == "ref" | ||
e.terms[0].value[0].type == "var" | ||
o := e.terms[0].value[0].value | ||
is_valid(o) | ||
} | ||
|
||
is_valid(o) if o in {"eq", "lt", "gt", "neq"} | ||
|
||
field(e) := f if { | ||
# find the operand with 'input.*' | ||
some t in array.slice(e.terms, 1, 3) | ||
is_input_ref(t) | ||
f := concat(".", [t.value[1].value, t.value[2].value]) | ||
} | ||
|
||
value(e) := v if { | ||
# find the operand without 'input.*' | ||
some t in array.slice(e.terms, 1, 3) | ||
not is_input_ref(t) | ||
v := value_from_term(t) | ||
} | ||
|
||
value_from_term(t) := t.value if t.type != "null" | ||
else := null | ||
|
||
is_input_ref(t) if { | ||
t.type == "ref" | ||
t.value[0].value == "input" | ||
} | ||
|
||
# conditions helper functions | ||
eq_(field, value) := op_("eq", field, value) | ||
|
||
lt_(f, v) := op_("lt", f, v) | ||
|
||
op_(o, f, v) := {"type": "field", "operator": o, "field": f, "value": v} | ||
|
||
and_(values) := compound("and", values) | ||
|
||
or_(values) := compound("or", values) | ||
|
||
compound(op, values) := {"type": "compound", "operator": op, "value": values} | ||
`; | ||
|
||
export const useDBStore = create<State>()( | ||
persist( | ||
immer((set, get) => ({ | ||
|
@@ -284,15 +372,21 @@ export const useDBStore = create<State>()( | |
const connection = get().active!; | ||
const { input, data } = get().databases[connection.name]; | ||
|
||
// EOPA Preview API | ||
const helper = await putPolicy("convert.rego", convertRego); | ||
if (!helper.ok) { | ||
throw new Error(`convert policy: ${helper.statusText}`); | ||
} | ||
|
||
const main = await putPolicy("main.rego", rego); | ||
if (!main.ok) { | ||
throw new Error(`data policy: ${main.statusText}`); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll just update the policies via the Policy API for now. It's good enough for prototyping, we don't need multiplayer support (i.e. the Preview API) |
||
|
||
const req = { | ||
input, | ||
data, | ||
rego_modules: { | ||
"main.rego": rego, | ||
}, | ||
}; | ||
const resp = await fetch("/v0/preview/conditions", { | ||
const resp = await fetch("/v1/data/filters", { | ||
method: "POST", | ||
body: JSON.stringify(req), | ||
}); | ||
|
@@ -305,6 +399,23 @@ export const useDBStore = create<State>()( | |
throw new Error(result?.message); | ||
} | ||
|
||
const { conditions } = result?.result; | ||
if ("code" in conditions) { | ||
set((state) => { | ||
const errors = conditions.errors.map( | ||
({ message, location: { row, col } }) => ({ message, row, col }) | ||
); | ||
console.dir({ errors }, { depth: null }); | ||
state.databases[connection.name].errors = errors; | ||
return result; | ||
}); | ||
throw new Error( | ||
`${conditions.message}: ${conditions.errors | ||
.map((e) => `${e.message} at ${e.location.row}`) | ||
.join("; ")}` | ||
); | ||
} | ||
|
||
set((state) => { | ||
state.databases[connection.name].rego = rego; | ||
state.databases[connection.name].evaluated = result?.result; | ||
|
@@ -398,3 +509,13 @@ function combine(existing: string, filter: string | undefined): string { | |
} | ||
return existing + "\nWHERE " + sansWhere; | ||
} | ||
|
||
async function putPolicy(id: string, code: string): Promise<Response> { | ||
return fetch(`/v1/policies/${id}`, { | ||
method: "PUT", | ||
body: code, | ||
headers: { | ||
"Content-Type": "text/plain", | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we're hitting the special endpoint of EOPA that'll end up doing PE post-analysis: https://github.com/StyraInc/enterprise-opa-private/pull/2142