-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (47 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const child_process = require("child_process");
const util = require("util");
const fs = require("fs-extra");
const tmp = require("tmp-promise");
const path = require("path");
const LOADER_NAME = "loader";
const HANDLER_NAME = "handler";
const LOADER_TEMPLATE =
`const FunctionShield = require('@puresec/function-shield');
FunctionShield.configure({
policy: %j,
token: process.env.FUNCTION_SHIELD_TOKEN,
disable_analytics: %o
});
exports.${HANDLER_NAME} = require('%s').%s;
`;
const generateLoader = (inputs) => {
let handlerParts = inputs.handler.split('.');
let originalModulePath = handlerParts[0];
let originalHandlerName = handlerParts[1];
return util.format(LOADER_TEMPLATE, inputs.policy, inputs.disableAnalytics, originalModulePath, originalHandlerName);
};
const deploy = async (inputs, context) => {
let tmpDir = await tmp.dir({unsafeCleanup: true});
let originalRoot = inputs.root || ".";
inputs.root = tmpDir.path;
await fs.copy(originalRoot, inputs.root);
await child_process.execFile("npm", ["install", "--loglevel", "error", `@puresec/function-shield@${inputs.functionShieldVersion}`, "--no-save"], {cwd: inputs.root});
let loaderStr = generateLoader(inputs);
await fs.writeFile(path.join(inputs.root, `${LOADER_NAME}.js`), loaderStr);
inputs.handler = `${LOADER_NAME}.${HANDLER_NAME}`;
inputs.env = inputs.env || {};
inputs.env.FUNCTION_SHIELD_TOKEN = inputs.token;
let awsLambdaComponent = await context.load("aws-lambda", "secureFunction", inputs);
await awsLambdaComponent.deploy();
context.saveState({deployed: true});
};
const remove = async (inputs, context) => {
let awsLambdaComponent = await context.load("aws-lambda", "secureFunction", inputs);
await awsLambdaComponent.remove();
context.saveState();
};
module.exports = {
deploy,
remove
};