Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add support to expose functions on a public domain #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/domain/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ const functionItem = {
title: 'Hash',
readOnly: true,
},
exposed: {
type: 'bool',
title: 'Exposed on HTTP Invocator',
},
versionID: {
type: 'string',
title: 'Version ID',
Expand Down
7 changes: 7 additions & 0 deletions lib/domain/storage/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ class StorageRedis extends Storage {
data.env = JSON.stringify(code.env);
}

if (code.exposed === true) {
data.exposed = 'true';
} else if (code.exposed === false) {
data.exposed = 'false';
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


this.setNamespaceMember(namespace, id);
return this.client.hmset(key, data);
}
Expand All @@ -233,6 +239,7 @@ class StorageRedis extends Storage {
updated: data.updated,
namespaceSettings,
versionID: data.versionID || null,
exposed: data.exposed === 'true',
};

if (data.env) {
Expand Down
16 changes: 16 additions & 0 deletions lib/http/routers/FunctionsPublicRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Router = require('express').Router;
const bodyParser = require('body-parser');

const functionRunHandler = require('./functionRunHandler');

const router = new Router();
const { bodyParserLimit } = require('../../support/config');


router.all(
'/:namespace/:id',
bodyParser.json({ limit: bodyParserLimit }),
(req, res) => functionRunHandler(req, res, { exposed: true })
);

module.exports = router;
99 changes: 15 additions & 84 deletions lib/http/routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const Router = require('express').Router;
const bodyParser = require('body-parser');
const Validator = require('jsonschema').Validator;

const functionRunHandler = require('./functionRunHandler');
const FunctionsRequest = require('../FunctionsRequest');
const SchemaResponse = require('../SchemaResponse');

const log = require('../../support/log');
const schemas = require('../../domain/schemas');
const Pipeline = require('../../domain/Pipeline');
const ErrorTracker = require('../../domain/ErrorTracker');
const { StdoutLogStorage, DefaultLogStorage } = require('../../domain/LogStorage');
const FunctionsRequest = require('../FunctionsRequest');
const Metric = require('../../domain/Metric');
const SchemaResponse = require('../SchemaResponse');
const { StdoutLogStorage } = require('../../domain/LogStorage');

const router = new Router();
const { bodyParserLimit } = require('../../support/config');
Expand Down Expand Up @@ -119,6 +119,7 @@ router.put('/:namespace/:id', bodyParser.json({ limit: bodyParserLimit }), async
const {
code,
env,
exposed,
} = req.body;
const filename = codeFileName(namespace, id);
const sandbox = req.app.get('sandbox');
Expand All @@ -139,6 +140,10 @@ router.put('/:namespace/:id', bodyParser.json({ limit: bodyParserLimit }), async
data.env = env;
}

if (exposed !== undefined) {
data.exposed = exposed;
}

try {
await memoryStorage.putCode(namespace, id, data);
res.set({ ETag: data.hash });
Expand Down Expand Up @@ -241,85 +246,11 @@ router.delete('/:namespace/:id', async (req, res) => {
});


router.all('/:namespace/:id/run', bodyParser.json({ limit: bodyParserLimit }), async (req, res) => {
const { namespace, id } = req.params;
const memoryStorage = req.app.get('memoryStorage');
const sandbox = req.app.get('sandbox');
const filename = codeFileName(namespace, id);
const metric = new Metric('function-run');
const logStorage = new DefaultLogStorage(namespace, id, req);

let code;

try {
code = await memoryStorage.getCodeByCache(namespace, id, {
preCache: (preCode) => {
preCode.script = sandbox.compileCode(filename, preCode.code);
return preCode;
},
});

if (!code) {
const error = new Error(`Code '${namespace}/${id}' is not found`);
error.statusCode = 404;
throw error;
}
} catch (err) {
res.status(err.statusCode || 500).json({ error: err.message });
return;
}

try {
const options = {
console: logStorage.console,
env: code.env,
};
const result = await sandbox.runScript(code.script, req, options);

res.set(result.headers);
res.status(result.status);
res.json(result.body);

const spent = metric.finish({
filename,
status: result.status,
});

logStorage.flush({
status: result.status,
requestTime: spent,
});
} catch (err) {
logStorage.console.error(`Failed to run function: ${err}`);
logStorage.console.error(err.stack);
const status = err.statusCode || 500;
res.status(status).json({ error: err.message });

const spent = metric.finish({
filename,
status,
error: err.message,
});

const logResult = logStorage.flush({
status,
requestTime: spent,
});

const { namespaceSettings } = code;
const { sentryDSN } = namespaceSettings || {};

const extra = Object.assign({ body: req.body }, logResult || {});
const errTracker = new ErrorTracker({
sentryDSN,
filename,
extra,
tags: { codeHash: code.hash },
code: code.code,
});
errTracker.notify(err);
}
});
router.all(
'/:namespace/:id/run',
bodyParser.json({ limit: bodyParserLimit }),
(req, res) => functionRunHandler(req, res, { exposed: false })
);


router.put('/pipeline', bodyParser.json({ limit: bodyParserLimit }), async (req, res) => {
Expand Down
96 changes: 96 additions & 0 deletions lib/http/routers/functionRunHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const ErrorTracker = require('../../domain/ErrorTracker');
const Metric = require('../../domain/Metric');
const { DefaultLogStorage } = require('../../domain/LogStorage');


function codeFileName(namespace, codeId) {
return `${namespace}/${codeId}.js`;
}


async function functionRunHandler(req, res, { exposed }) {
const { namespace, id } = req.params;
const memoryStorage = req.app.get('memoryStorage');
const sandbox = req.app.get('sandbox');
const filename = codeFileName(namespace, id);
const metric = new Metric('function-run');
const logStorage = new DefaultLogStorage(namespace, id, req);

let code;

try {
code = await memoryStorage.getCodeByCache(namespace, id, {
preCache: (preCode) => {
preCode.script = sandbox.compileCode(filename, preCode.code);
return preCode;
},
});

if (!code) {
const error = new Error(`Code '${namespace}/${id}' is not found`);
error.statusCode = 404;
throw error;
}
if (exposed && !code.exposed) {
const error = new Error('Unauthorized');
error.statusCode = 403;
throw error;
}
} catch (err) {
res.status(err.statusCode || 500).json({ error: err.message });
return;
}

try {
const options = {
console: logStorage.console,
env: code.env,
};
const result = await sandbox.runScript(code.script, req, options);

res.set(result.headers);
res.status(result.status);
res.json(result.body);

const spent = metric.finish({
filename,
status: result.status,
});

logStorage.flush({
status: result.status,
requestTime: spent,
});
} catch (err) {
logStorage.console.error(`Failed to run function: ${err}`);
logStorage.console.error(err.stack);
const status = err.statusCode || 500;
res.status(status).json({ error: err.message });

const spent = metric.finish({
filename,
status,
error: err.message,
});

const logResult = logStorage.flush({
status,
requestTime: spent,
});

const { namespaceSettings } = code;
const { sentryDSN } = namespaceSettings || {};

const extra = Object.assign({ body: req.body }, logResult || {});
const errTracker = new ErrorTracker({
sentryDSN,
filename,
extra,
tags: { codeHash: code.hash },
code: code.code,
});
errTracker.notify(err);
}
}

module.exports = functionRunHandler;
36 changes: 27 additions & 9 deletions lib/http/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');
const vhost = require('vhost');
const morgan = require('morgan');

const Sandbox = require('@globocom/backstage-functions-sandbox');
Expand All @@ -10,6 +11,7 @@ const StatusRouter = require('./routers/StatusRouter');
const DebugRouter = require('./routers/DebugRouter');
const NamespacesRouter = require('./routers/NamespacesRouter');
const FunctionsRouter = require('./routers/FunctionsRouter');
const FunctionsPublicRouter = require('./routers/FunctionsPublicRouter');
const RedisStorage = require('../domain/storage/redis');
const parseExposeEnv = require('../support/parseExposeEnv');
const config = require('../support/config');
Expand All @@ -26,19 +28,24 @@ morgan.token('response-sectime', (req, res) => {
return secs.toFixed(3);
});

const app = express();

app.use(morgan(config.log.morganFormat));
app.disable('x-powered-by');
app.enable('trust proxy');

app.set('memoryStorage', new RedisStorage());
app.set('sandbox', new Sandbox({
const memoryStorage = new RedisStorage();
const sandbox = new Sandbox({
env: parseExposeEnv(),
globalModules: config.defaultGlobalModules,
asyncTimeout: config.asyncTimeout,
syncTimeout: config.syncTimeout,
}));
});

function setupApp(app) {
app.use(morgan(config.log.morganFormat));
app.disable('x-powered-by');
app.enable('trust proxy');
app.set('memoryStorage', memoryStorage);
app.set('sandbox', sandbox);
}

const app = express();
setupApp(app);

app.get('/', (req, res) => {
const backstageRequest = new FunctionsRequest(req);
Expand All @@ -49,6 +56,17 @@ app.get('/', (req, res) => {
});
});

if (config.exposed.host) {
const publicApp = express();
setupApp(publicApp);
publicApp.use(FunctionsPublicRouter);
publicApp.use((req, res) => {
res.send({ error: 'Not found' });
});

app.use(vhost(config.exposed.host, publicApp));
}

app.use('/healthcheck', HealthcheckRouter);
app.use('/status', StatusRouter);
app.use('/_debug', DebugRouter);
Expand Down
3 changes: 3 additions & 0 deletions lib/support/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ module.exports = {
useCertFile: ConfigDiscovery.getBool('FUNCTIONS_USE_SSL_CERT_FILE', false),
certFile: process.env.SSL_CERT_FILE,
},
exposed: {
host: process.env.EXPOSED_HOST,
},
};
5 changes: 5 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"request": "^2.81.0",
"stack-trace": "^0.0.9",
"uuid": "^3.0.1",
"vhost": "^3.0.2",
"winston": "^2.2.0"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/domain/storage/storage-redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('StorageRedis', () => {
CLIENT_ID: 'my client id',
MY_VAR: 'my var',
},
exposed: true,
};
const x = await storage.putCode('backstage', 'test', code);
expect(x).to.be.eql('OK');
Expand All @@ -53,6 +54,7 @@ describe('StorageRedis', () => {
expect(code2.versionID).to.match(UUID_REGEX);
expect(code2.env.CLIENT_ID).to.be.eql('my client id');
expect(code2.env.MY_VAR).to.be.eql('my var');
expect(code2.exposed).to.be.eql(true);
});

it('should have a created equal updated for new function', async () => {
Expand Down