-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from 0xShield3/feat/validation-json-interfaces
Feat/validation json interfaces
- Loading branch information
Showing
8 changed files
with
714 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,130 @@ | ||
import node_pre_gyp from '@mapbox/node-pre-gyp'; | ||
const { find } = node_pre_gyp; | ||
import { resolve, join, dirname } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import node_pre_gyp from '@mapbox/node-pre-gyp' | ||
const { find } = node_pre_gyp | ||
import { resolve, join, dirname } from 'path' | ||
import { fileURLToPath } from 'url' | ||
import Ajv from 'ajv' | ||
|
||
import PolicyEngineResponse from './schemas/IPolicyEnginePolicyResponse.json' | ||
|
||
// Native modules are not currently supported with ES module imports. | ||
// https://nodejs.org/api/esm.html#esm_no_native_module_loading | ||
import { createRequire } from 'module'; | ||
const require = createRequire(import.meta.url); | ||
import { createRequire } from 'module' | ||
const require = createRequire(import.meta.url) | ||
|
||
// __dirname is not defined in ES module scope, so get it manaully. | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = dirname(__filename) | ||
|
||
const { | ||
hello, | ||
authorize, | ||
policy_to_json, | ||
validate_policy, | ||
// the file will be run in ./dist, so popd. | ||
} = require(find(resolve(join(__dirname, process.env.dev ? '' : '..', './package.json')))); | ||
|
||
} = require(find(resolve(join(__dirname, process.env.dev ? '' : '..', './package.json')))) | ||
|
||
export interface IBanyan {} | ||
|
||
export interface IPolicyEngineInvokePayload { | ||
principal: string | ||
action: string | ||
resource: string | ||
policy: string | ||
entities: string | ||
context: string | ||
schema: string | undefined | ||
principal: string | ||
action: string | ||
resource: string | ||
policy: string | ||
entities: string | ||
context: string | ||
schema: string | undefined | ||
} | ||
|
||
export interface IValidatePolicyPayload { | ||
policy: string | ||
additional_schema_fragments: string[] | ||
} | ||
|
||
export enum TriggeredPolicyAction { | ||
BLOCK = 'Block', | ||
MFA = 'MFA', | ||
NOTIFY = 'Notify', | ||
ALLOW = 'Allow' | ||
MFA = 'MFA', | ||
NOTIFY = 'Notify', | ||
} | ||
|
||
export enum PolicyDecision { | ||
ALLOW = 'allow', | ||
DENY = 'deny' | ||
ALLOW = 'Allow', | ||
DENY = 'Deny', | ||
} | ||
|
||
export interface IPolicyStatementResult { | ||
name?: string | ||
message?: string | ||
action?: TriggeredPolicyAction | ||
invoked: boolean | ||
} | ||
|
||
export interface IPolicyEnginePolicyResponse { | ||
action?: TriggeredPolicyAction | ||
name?: string | ||
decision: PolicyDecision | ||
reason: 'true' | 'false' | ||
message?: string | ||
reasons: IPolicyStatementResult[] | ||
decision: PolicyDecision | ||
errors: string[] | ||
} | ||
|
||
export interface IPolicyEngineInvokeResponse { | ||
result: { | ||
[key: string]: IPolicyEnginePolicyResponse | ||
} | ||
export interface IPolicyJSON { | ||
effect: 'forbid' | 'permit' | ||
principal: any | ||
action: any | ||
resource: any | ||
conditions: any[] | ||
annotations: { | ||
[key: string]: string | ||
} | ||
} | ||
|
||
export const Banyan = { | ||
test: 'test' | ||
}; | ||
export type PolicyToJSONResponse = IPolicyJSON[] | ||
|
||
export const invoke = () => { | ||
console.log('hello'); | ||
return hello(); | ||
console.log('hello') | ||
return hello() | ||
} | ||
|
||
export const isAuthorized = (request: string) => { | ||
console.log('authorize'); | ||
return authorize(request); | ||
console.log('authorize') | ||
return authorize(request) | ||
} | ||
|
||
export const parsePolicyEngineResponsePayload = (response: any): IPolicyEngineInvokeResponse | null => { | ||
try { | ||
const parsedResponse = JSON.parse(response) | ||
export function validatePolicyEngineResponse(maybePolicyEngineResponse: any) { | ||
const ajv = new Ajv() | ||
const validate = ajv.compile(PolicyEngineResponse) | ||
return validate(maybePolicyEngineResponse) | ||
} | ||
|
||
export const parsePolicyEngineResponsePayload = (response: any): IPolicyEnginePolicyResponse | null => { | ||
try { | ||
const parsedResponse = JSON.parse(response) | ||
const validationResult = validatePolicyEngineResponse(parsedResponse) | ||
console.log({ validationResult }) | ||
if (validationResult) return parsedResponse | ||
else { | ||
throw new Error('Failed to validate response') | ||
} | ||
} catch (error) { | ||
// TODO handle parse error by raising an alert | ||
console.error('Failed to parse response:', error) | ||
return null | ||
} | ||
} | ||
|
||
export const invokePolicyEngine = (request: IPolicyEngineInvokePayload): IPolicyEnginePolicyResponse => { | ||
console.log('invokePolicyEngine') | ||
const result = isAuthorized(JSON.stringify(request)) | ||
const parsed = parsePolicyEngineResponsePayload(result) | ||
return parsed | ||
} | ||
|
||
export const policyToJson = (policy: string): PolicyToJSONResponse => { | ||
console.log('policyToJson') | ||
const result = policy_to_json(policy) | ||
const parsedResponse: PolicyToJSONResponse = JSON.parse(result) // TODO validate | ||
return parsedResponse | ||
// const validationResult = validatePolicyEngineResponse(parsedResponse) | ||
// console.log({ validationResult }) | ||
// if (validationResult) return parsedResponse | ||
// else { | ||
// throw new Error('Failed to validate response') | ||
// } | ||
} catch (error) { | ||
// TODO handle parse error by raising an alert | ||
console.error('Failed to parse response:', error) | ||
return null | ||
} | ||
} | ||
|
||
export const invokePolicyEngine = (request: IPolicyEngineInvokePayload): IPolicyEngineInvokeResponse => { | ||
console.log('invokePolicyEngine'); | ||
const result = isAuthorized(JSON.stringify(request)); | ||
const parsed = parsePolicyEngineResponsePayload(result); | ||
return parsed | ||
} | ||
export const validatePolicy = (request: IValidatePolicyPayload): any => { | ||
console.log('validatePolicy') | ||
const result = validate_policy(JSON.stringify(request)) | ||
const parsedResponse = JSON.parse(result) // TODO validate | ||
return parsedResponse | ||
} |
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.