|
| 1 | +import { IAccessCandidate, TAccessLevel } from '@sre/types/ACL.types'; |
| 2 | +import { ACL } from '@sre/Security/AccessControl/ACL.class'; |
| 3 | +import { CodeConfig, CodePreparationResult, CodeConnector, CodeInput, CodeDeployment, CodeExecutionResult } from '../CodeConnector'; |
| 4 | +import { AccessRequest } from '@sre/Security/AccessControl/AccessRequest.class'; |
| 5 | +import { Logger } from '@sre/helpers/Log.helper'; |
| 6 | +import axios from 'axios'; |
| 7 | +import { runJs } from '@sre/helpers/ECMASandbox.helper'; |
| 8 | + |
| 9 | +const console = Logger('ECMASandbox'); |
| 10 | +export class ECMASandbox extends CodeConnector { |
| 11 | + public name = 'ECMASandbox'; |
| 12 | + private sandboxUrl: string; |
| 13 | + |
| 14 | + constructor(config: { sandboxUrl: string }) { |
| 15 | + super(config); |
| 16 | + this.sandboxUrl = config.sandboxUrl; |
| 17 | + } |
| 18 | + public async prepare(acRequest: AccessRequest, codeUID: string, input: CodeInput, config: CodeConfig): Promise<CodePreparationResult> { |
| 19 | + return { |
| 20 | + prepared: true, |
| 21 | + errors: [], |
| 22 | + warnings: [], |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + public async deploy(acRequest: AccessRequest, codeUID: string, input: CodeInput, config: CodeConfig): Promise<CodeDeployment> { |
| 27 | + return { |
| 28 | + id: codeUID, |
| 29 | + runtime: config.runtime, |
| 30 | + createdAt: new Date(), |
| 31 | + status: 'Deployed', |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + public async execute(acRequest: AccessRequest, codeUID: string, inputs: Record<string, any>, config: CodeConfig): Promise<CodeExecutionResult> { |
| 36 | + try { |
| 37 | + if (!this.sandboxUrl) { |
| 38 | + // run js code in isolated vm |
| 39 | + console.debug('Running code in isolated vm'); |
| 40 | + const result = await runJs(inputs.code); |
| 41 | + console.debug(`Code result: ${result}`); |
| 42 | + return { |
| 43 | + output: result?.Output, |
| 44 | + executionTime: 0, |
| 45 | + success: true, |
| 46 | + errors: [], |
| 47 | + }; |
| 48 | + } else { |
| 49 | + console.debug('Running code in remote sandbox'); |
| 50 | + const result: any = await axios.post(this.sandboxUrl, { code: inputs.code }).catch((error) => ({ error })); |
| 51 | + if (result.error) { |
| 52 | + |
| 53 | + const error = result.error?.response?.data || result.error?.message || result.error.toString() || 'Unknown error'; |
| 54 | + console.error(`Error running code: ${JSON.stringify(error, null, 2)}`); |
| 55 | + return { |
| 56 | + output: undefined, |
| 57 | + executionTime: 0, |
| 58 | + success: false, |
| 59 | + errors: [error], |
| 60 | + }; |
| 61 | + } else { |
| 62 | + console.debug(`Code result: ${result?.data?.Output}`); |
| 63 | + return { |
| 64 | + output: result.data?.Output, |
| 65 | + executionTime: 0, |
| 66 | + success: true, |
| 67 | + errors: [], |
| 68 | + }; |
| 69 | + } |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + console.error(`Error running code: ${error}`); |
| 73 | + return { |
| 74 | + output: undefined, |
| 75 | + executionTime: 0, |
| 76 | + success: false, |
| 77 | + errors: [error], |
| 78 | + }; |
| 79 | + } |
| 80 | + } |
| 81 | + public async executeDeployment(acRequest: AccessRequest, codeUID: string, deploymentId: string, inputs: Record<string, any>, config: CodeConfig): Promise<CodeExecutionResult> { |
| 82 | + const result = await this.execute(acRequest, codeUID, inputs, config); |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + public async listDeployments(acRequest: AccessRequest, codeUID: string, config: CodeConfig): Promise<CodeDeployment[]> { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + public async getDeployment(acRequest: AccessRequest, codeUID: string, deploymentId: string, config: CodeConfig): Promise<CodeDeployment | null> { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + public async deleteDeployment(acRequest: AccessRequest, codeUID: string, deploymentId: string): Promise<void> { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + public async getResourceACL(resourceId: string, candidate: IAccessCandidate): Promise<ACL> { |
| 99 | + const acl = new ACL(); |
| 100 | + |
| 101 | + //give Read access everytime |
| 102 | + //FIXME: !!!!!! IMPORTANT !!!!!! this implementation have to be changed in order to reflect the security model of AWS Lambda |
| 103 | + acl.addAccess(candidate.role, candidate.id, TAccessLevel.Read); |
| 104 | + |
| 105 | + return acl; |
| 106 | + } |
| 107 | +} |
0 commit comments