-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rego templating using handlebars
- Loading branch information
samuel
committed
Feb 6, 2024
1 parent
aa12b51
commit 5e7b232
Showing
9 changed files
with
370 additions
and
304 deletions.
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Criterion, PolicyCriterionBuilder, Then } from '@app/authz/shared/types/policy-builder.type' | ||
import { Action } from '@narval/authz-shared' | ||
import { Intents } from '@narval/transaction-request-intent' | ||
|
||
export const examplePermitPolicy: PolicyCriterionBuilder = { | ||
then: Then.PERMIT, | ||
name: 'examplePermitPolicy', | ||
when: [ | ||
{ | ||
criterion: Criterion.CHECK_TRANSFER_RESOURCE_INTEGRITY, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_NONCE_EXISTS, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_ACTION, | ||
args: [Action.SIGN_TRANSACTION] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_PRINCIPAL_ID, | ||
args: ['[email protected]'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_WALLET_ID, | ||
args: ['eip155:eoa:0x90d03a8971a2faa19a9d7ffdcbca28fe826a289b'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TYPE, | ||
args: [Intents.TRANSFER_NATIVE] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TOKEN, | ||
args: ['eip155:137/slip44:966'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_AMOUNT, | ||
args: { currency: '*', operator: 'lte', value: '1000000000000000000' } | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_APPROVALS, | ||
args: [ | ||
{ | ||
approvalCount: 2, | ||
countPrincipal: false, | ||
approvalEntityType: 'Narval::User', | ||
entityIds: ['[email protected]', '[email protected]'] | ||
}, | ||
{ | ||
approvalCount: 1, | ||
countPrincipal: false, | ||
approvalEntityType: 'Narval::UserRole', | ||
entityIds: ['admin'] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
export const exampleForbidPolicy: PolicyCriterionBuilder = { | ||
then: Then.FORBID, | ||
name: 'exampleForbidPolicy', | ||
when: [ | ||
{ | ||
criterion: Criterion.CHECK_TRANSFER_RESOURCE_INTEGRITY, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_NONCE_EXISTS, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_ACTION, | ||
args: [Action.SIGN_TRANSACTION] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_PRINCIPAL_ID, | ||
args: ['[email protected]'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_WALLET_ID, | ||
args: ['eip155:eoa:0x90d03a8971a2faa19a9d7ffdcbca28fe826a289b'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TYPE, | ||
args: [Intents.TRANSFER_NATIVE] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TOKEN, | ||
args: ['eip155:137/slip44:966'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_SPENDING_LIMIT, | ||
args: { | ||
limit: '1000000000000000000', | ||
timeWindow: { | ||
type: 'rolling', | ||
value: 12 * 60 * 60 | ||
}, | ||
filters: { | ||
tokens: ['eip155:137/slip44:966'], | ||
users: ['[email protected]'] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
|
||
export const policies = { | ||
policies: [examplePermitPolicy, exampleForbidPolicy] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Criterion, Then } from '@app/authz/shared/types/policy-builder.type' | ||
import { readFileSync, writeFileSync } from 'fs' | ||
import Handlebars from 'handlebars' | ||
import { isEmpty } from 'lodash' | ||
import { policies } from './mockData' | ||
|
||
Handlebars.registerHelper('criterion', function (item, options) { | ||
const criterion: Criterion = item.criterion | ||
const args = item.args | ||
|
||
if (args === null) { | ||
return `${criterion}` | ||
} | ||
|
||
if (!isEmpty(args)) { | ||
if (Array.isArray(args)) { | ||
if (typeof args[0] === 'string') { | ||
return `${criterion}({${args.map((el) => `"${el}"`).join(', ')}})` | ||
} | ||
|
||
if (criterion === Criterion.CHECK_APPROVALS) { | ||
return `approvals = ${criterion}([${args.map((el) => JSON.stringify(el)).join(', ')}])` | ||
} | ||
|
||
return `${criterion}([${args.map((el) => JSON.stringify(el)).join(', ')}])` | ||
} | ||
|
||
return `${criterion}(${JSON.stringify(args)})` | ||
} | ||
}) | ||
|
||
Handlebars.registerHelper('reason', function (item, options) { | ||
if (item.then === Then.PERMIT) { | ||
const reason = [ | ||
`"type": "${item.then}"`, | ||
`"policyId": "${item.name}"`, | ||
'"approvalsSatisfied": approvals.approvalsSatisfied', | ||
'"approvalsMissing": approvals.approvalsMissing' | ||
] | ||
return `reason = {${reason.join(', ')}}` | ||
} | ||
|
||
if (item.then === Then.FORBID) { | ||
const reason = { | ||
type: item.then, | ||
policyId: item.name, | ||
approvalsSatisfied: [], | ||
approvalsMissing: [] | ||
} | ||
return `reason = ${JSON.stringify(reason)}` | ||
} | ||
}) | ||
|
||
// Read the template file | ||
const templateSource = readFileSync( | ||
'/Users/samuel/Documents/narval/narval/apps/authz/src/opa/template/template.hbs', | ||
'utf-8' | ||
) | ||
|
||
// Compile the template | ||
const template = Handlebars.compile(templateSource) | ||
|
||
// Generate Rego file content | ||
const regoContent = template(policies) | ||
|
||
// Write the content to a Rego file | ||
writeFileSync( | ||
'/Users/samuel/Documents/narval/narval/apps/authz/src/opa/rego/policies/generatedPolicies.rego', | ||
regoContent, | ||
'utf-8' | ||
) | ||
|
||
console.log('Policy .rego file generated successfully.') |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
{{#each policies}} | ||
{{ then }}[{"policyId": "{{ name }}" }] = reason { | ||
{{#each when}} | ||
{{#criterion this}}{{/criterion}} | ||
{{/each}} | ||
{{#reason this}}{{/reason}} | ||
} | ||
|
||
{{/each}} |
Oops, something went wrong.