Skip to content

Commit

Permalink
JSON conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ipatka committed Nov 16, 2023
1 parent d01f266 commit 1b65f19
Show file tree
Hide file tree
Showing 14 changed files with 657 additions and 76 deletions.
102 changes: 50 additions & 52 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
hello,
authorize,
policy_to_json,
policy_from_json,
validate_policy,
// the file will be run in ./dist, so popd.
} = require(find(resolve(join(__dirname, process.env.dev ? '' : '..', './package.json'))))
Expand Down Expand Up @@ -51,7 +52,7 @@ export enum PolicyDecision {
export interface IPolicyStatementResult {
policy_id: string
invoked: boolean
annotations: {[key: string]: string}
annotations: { [key: string]: string }
}

export interface IPolicyEnginePolicyResponse {
Expand All @@ -73,6 +74,8 @@ export interface IPolicyJSON {

export type PolicyToJSONResponse = IPolicyJSON[]

export type PolicyFromJSONRequest = { policies: IPolicyJSON[] }

export const invoke = () => {
console.log('hello')
return hello()
Expand Down Expand Up @@ -112,6 +115,12 @@ export const invokePolicyEngine = (request: IPolicyEngineInvokePayload): IPolicy
return parsed
}

export const policyFromJson = (request: PolicyFromJSONRequest): string => {
console.log('policyFromJson')
const result = policy_from_json(JSON.stringify(request))
return result
}

export const policyToJson = (policy: string): PolicyToJSONResponse => {
console.log('policyToJson')
const result = policy_to_json(policy)
Expand All @@ -127,59 +136,48 @@ export const validatePolicy = (request: IValidatePolicyPayload): any => {
}

export const IPolicyEnginePolicyResponseSchema = {
"type": "object",
"properties": {
"reasons": {
"type": "array",
"items": {
"$ref": "#/definitions/IPolicyStatementResult"
}
},
"decision": {
"$ref": "#/definitions/PolicyDecision"
type: 'object',
properties: {
reasons: {
type: 'array',
items: {
$ref: '#/definitions/IPolicyStatementResult',
},
},
decision: {
$ref: '#/definitions/PolicyDecision',
},
errors: {
type: 'array',
items: {
type: 'string',
},
},
},
"errors": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"decision",
"errors",
"reasons"
],
"definitions": {
"IPolicyStatementResult": {
"type": "object",
"properties": {
"policy_id": {
"type": "string"
required: ['decision', 'errors', 'reasons'],
definitions: {
IPolicyStatementResult: {
type: 'object',
properties: {
policy_id: {
type: 'string',
},
invoked: {
type: 'boolean',
},
annotations: {
type: 'object',
additionalProperties: {
type: 'string',
},
},
},
required: ['annotations', 'invoked', 'policy_id'],
},
"invoked": {
"type": "boolean"
PolicyDecision: {
enum: ['Allow', 'Deny'],
type: 'string',
},
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": [
"annotations",
"invoked",
"policy_id"
]
},
"PolicyDecision": {
"enum": [
"Allow",
"Deny"
],
"type": "string"
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
$schema: 'http://json-schema.org/draft-07/schema#',
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shield3/banyan",
"version": "0.3.10",
"version": "0.3.11",
"description": "Banyan policy engine",
"main": "dist/index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 1b65f19

Please sign in to comment.