-
-
Notifications
You must be signed in to change notification settings - Fork 110
feat: implementing permission checker #1411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
21a0a2d
WIP: permission checker
ymc9 58626fb
Merge remote-tracking branch 'origin/dev' into feat/permission-checker
ymc9 5c46728
Merge remote-tracking branch 'origin/dev' into feat/permission-checker
ymc9 5ab8a44
WIP: progress
ymc9 5e44c0b
Merge remote-tracking branch 'origin/dev' into feat/permission-checker
ymc9 19fea2c
WIP: progress
ymc9 2ef2da0
more tests
ymc9 b55740a
more fixes and tests
ymc9 ebae535
fix: error wording
ymc9 ae96914
change return type to a deferred promise
ymc9 7c0277d
fix: handle nullable `auth()` access
ymc9 3c6e5ee
more fixes
ymc9 8af5cdd
guard checker generation with an option flag
ymc9 3665391
multiple fixes and features
ymc9 a68e068
multiple fixes
ymc9 64e0c98
fix: add input check validation
ymc9 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 hidden or 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
219 changes: 219 additions & 0 deletions
219
packages/runtime/src/enhancements/policy/constraint-solver.ts
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,219 @@ | ||
import Logic from 'logic-solver'; | ||
import { match } from 'ts-pattern'; | ||
import type { | ||
CheckerConstraint, | ||
ComparisonConstraint, | ||
ComparisonTerm, | ||
LogicalConstraint, | ||
ValueConstraint, | ||
VariableConstraint, | ||
} from '../types'; | ||
|
||
/** | ||
* A boolean constraint solver based on `logic-solver`. Only boolean and integer types are supported. | ||
*/ | ||
export class ConstraintSolver { | ||
// a table for internalizing string literals | ||
private stringTable: string[] = []; | ||
|
||
// a map for storing variable names and their corresponding formulas | ||
private variables: Map<string, Logic.Formula> = new Map<string, Logic.Formula>(); | ||
|
||
/** | ||
* Check the satisfiability of the given constraint. | ||
*/ | ||
checkSat(constraint: CheckerConstraint): boolean { | ||
// reset state | ||
this.stringTable = []; | ||
this.variables = new Map<string, Logic.Formula>(); | ||
|
||
// convert the constraint to a "logic-solver" formula | ||
const formula = this.buildFormula(constraint); | ||
|
||
// solve the formula | ||
const solver = new Logic.Solver(); | ||
solver.require(formula); | ||
|
||
// DEBUG: | ||
// const solution = solver.solve(); | ||
// if (solution) { | ||
// console.log('Solution:'); | ||
// this.variables.forEach((v, k) => console.log(`\t${k}=${solution?.evaluate(v)}`)); | ||
// } else { | ||
// console.log('No solution'); | ||
// } | ||
|
||
return !!solver.solve(); | ||
} | ||
|
||
private buildFormula(constraint: CheckerConstraint): Logic.Formula { | ||
return match(constraint) | ||
.when( | ||
(c): c is ValueConstraint => c.kind === 'value', | ||
(c) => this.buildValueFormula(c) | ||
) | ||
.when( | ||
(c): c is VariableConstraint => c.kind === 'variable', | ||
(c) => this.buildVariableFormula(c) | ||
) | ||
.when( | ||
(c): c is ComparisonConstraint => ['eq', 'ne', 'gt', 'gte', 'lt', 'lte'].includes(c.kind), | ||
(c) => this.buildComparisonFormula(c) | ||
) | ||
.when( | ||
(c): c is LogicalConstraint => ['and', 'or', 'not'].includes(c.kind), | ||
(c) => this.buildLogicalFormula(c) | ||
) | ||
.otherwise(() => { | ||
throw new Error(`Unsupported constraint format: ${JSON.stringify(constraint)}`); | ||
}); | ||
} | ||
|
||
private buildLogicalFormula(constraint: LogicalConstraint) { | ||
return match(constraint.kind) | ||
.with('and', () => this.buildAndFormula(constraint)) | ||
.with('or', () => this.buildOrFormula(constraint)) | ||
.with('not', () => this.buildNotFormula(constraint)) | ||
.exhaustive(); | ||
} | ||
|
||
private buildAndFormula(constraint: LogicalConstraint): Logic.Formula { | ||
if (constraint.children.some((c) => this.isFalse(c))) { | ||
// short-circuit | ||
return Logic.FALSE; | ||
} | ||
return Logic.and(...constraint.children.map((c) => this.buildFormula(c))); | ||
} | ||
|
||
private buildOrFormula(constraint: LogicalConstraint): Logic.Formula { | ||
if (constraint.children.some((c) => this.isTrue(c))) { | ||
// short-circuit | ||
return Logic.TRUE; | ||
} | ||
return Logic.or(...constraint.children.map((c) => this.buildFormula(c))); | ||
} | ||
|
||
private buildNotFormula(constraint: LogicalConstraint) { | ||
if (constraint.children.length !== 1) { | ||
throw new Error('"not" constraint must have exactly one child'); | ||
} | ||
return Logic.not(this.buildFormula(constraint.children[0])); | ||
} | ||
|
||
private isTrue(constraint: CheckerConstraint): unknown { | ||
return constraint.kind === 'value' && constraint.value === true; | ||
} | ||
|
||
private isFalse(constraint: CheckerConstraint): unknown { | ||
return constraint.kind === 'value' && constraint.value === false; | ||
} | ||
|
||
private buildComparisonFormula(constraint: ComparisonConstraint) { | ||
if (constraint.left.kind === 'value' && constraint.right.kind === 'value') { | ||
// constant comparison | ||
const left: ValueConstraint = constraint.left; | ||
const right: ValueConstraint = constraint.right; | ||
return match(constraint.kind) | ||
.with('eq', () => (left.value === right.value ? Logic.TRUE : Logic.FALSE)) | ||
.with('ne', () => (left.value !== right.value ? Logic.TRUE : Logic.FALSE)) | ||
.with('gt', () => (left.value > right.value ? Logic.TRUE : Logic.FALSE)) | ||
.with('gte', () => (left.value >= right.value ? Logic.TRUE : Logic.FALSE)) | ||
.with('lt', () => (left.value < right.value ? Logic.TRUE : Logic.FALSE)) | ||
.with('lte', () => (left.value <= right.value ? Logic.TRUE : Logic.FALSE)) | ||
.exhaustive(); | ||
} | ||
|
||
return match(constraint.kind) | ||
.with('eq', () => this.transformEquality(constraint.left, constraint.right)) | ||
.with('ne', () => this.transformInequality(constraint.left, constraint.right)) | ||
.with('gt', () => | ||
this.transformComparison(constraint.left, constraint.right, (l, r) => Logic.greaterThan(l, r)) | ||
) | ||
.with('gte', () => | ||
this.transformComparison(constraint.left, constraint.right, (l, r) => Logic.greaterThanOrEqual(l, r)) | ||
) | ||
.with('lt', () => | ||
this.transformComparison(constraint.left, constraint.right, (l, r) => Logic.lessThan(l, r)) | ||
) | ||
.with('lte', () => | ||
this.transformComparison(constraint.left, constraint.right, (l, r) => Logic.lessThanOrEqual(l, r)) | ||
) | ||
.exhaustive(); | ||
} | ||
|
||
private buildVariableFormula(constraint: VariableConstraint) { | ||
return ( | ||
match(constraint.type) | ||
.with('boolean', () => this.booleanVariable(constraint.name)) | ||
.with('number', () => this.intVariable(constraint.name)) | ||
// strings are internalized and represented by their indices | ||
.with('string', () => this.intVariable(constraint.name)) | ||
.exhaustive() | ||
); | ||
} | ||
|
||
private buildValueFormula(constraint: ValueConstraint) { | ||
return match(constraint.value) | ||
.when( | ||
(v): v is boolean => typeof v === 'boolean', | ||
(v) => (v === true ? Logic.TRUE : Logic.FALSE) | ||
) | ||
.when( | ||
(v): v is number => typeof v === 'number', | ||
(v) => Logic.constantBits(v) | ||
) | ||
.when( | ||
(v): v is string => typeof v === 'string', | ||
(v) => { | ||
// internalize the string and use its index as formula representation | ||
const index = this.stringTable.indexOf(v); | ||
if (index === -1) { | ||
this.stringTable.push(v); | ||
return Logic.constantBits(this.stringTable.length - 1); | ||
} else { | ||
return Logic.constantBits(index); | ||
} | ||
} | ||
) | ||
.exhaustive(); | ||
} | ||
|
||
private booleanVariable(name: string) { | ||
this.variables.set(name, name); | ||
return name; | ||
} | ||
|
||
private intVariable(name: string) { | ||
const r = Logic.variableBits(name, 32); | ||
this.variables.set(name, r); | ||
return r; | ||
} | ||
|
||
private transformEquality(left: ComparisonTerm, right: ComparisonTerm) { | ||
if (left.type !== right.type) { | ||
throw new Error(`Type mismatch in equality constraint: ${JSON.stringify(left)}, ${JSON.stringify(right)}`); | ||
} | ||
|
||
const leftFormula = this.buildFormula(left); | ||
const rightFormula = this.buildFormula(right); | ||
if (left.type === 'boolean' && right.type === 'boolean') { | ||
// logical equivalence | ||
return Logic.equiv(leftFormula, rightFormula); | ||
} else { | ||
// integer equality | ||
return Logic.equalBits(leftFormula, rightFormula); | ||
} | ||
} | ||
|
||
private transformInequality(left: ComparisonTerm, right: ComparisonTerm) { | ||
return Logic.not(this.transformEquality(left, right)); | ||
} | ||
|
||
private transformComparison( | ||
left: ComparisonTerm, | ||
right: ComparisonTerm, | ||
func: (left: Logic.Formula, right: Logic.Formula) => Logic.Formula | ||
) { | ||
return func(this.buildFormula(left), this.buildFormula(right)); | ||
} | ||
} |
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.