2017-02-20 18:32:27 +00:00
|
|
|
import clone from 'clone';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../models';
|
2017-03-23 22:10:42 +00:00
|
|
|
import {setDefaultProtocol} from './misc';
|
2016-11-10 01:15:27 +00:00
|
|
|
import * as db from './database';
|
2017-02-20 18:32:27 +00:00
|
|
|
import * as templating from '../templating';
|
2016-04-15 02:13:49 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
export const KEEP_ON_ERROR = 'keep';
|
|
|
|
export const THROW_ON_ERROR = 'throw';
|
|
|
|
|
|
|
|
export async function buildRenderContext (ancestors, rootEnvironment, subEnvironment, baseContext = {}) {
|
2017-01-13 19:21:03 +00:00
|
|
|
if (!Array.isArray(ancestors)) {
|
|
|
|
ancestors = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const environments = [];
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
if (rootEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(rootEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (subEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(subEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 19:21:03 +00:00
|
|
|
for (const doc of ancestors.reverse()) {
|
2016-09-03 04:32:45 +00:00
|
|
|
if (!doc.environment) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(doc.environment);
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-01-24 18:51:25 +00:00
|
|
|
// At this point, environments is a list of environments ordered
|
|
|
|
// from top-most parent to bottom-most child
|
2017-05-15 20:55:05 +00:00
|
|
|
// Do an Object.assign, but render each property as it overwrites. This
|
|
|
|
// way we can keep same-name variables from the parent context.
|
2017-07-11 01:05:54 +00:00
|
|
|
const renderContext = baseContext;
|
2017-01-13 19:21:03 +00:00
|
|
|
for (const environment of environments) {
|
2017-05-15 20:55:05 +00:00
|
|
|
// Sort the keys that may have Nunjucks last, so that other keys get
|
|
|
|
// defined first. Very important if env variables defined in same obj
|
|
|
|
// (eg. {"foo": "{{ bar }}", "bar": "Hello World!"})
|
|
|
|
const keys = Object.keys(environment).sort((k1, k2) =>
|
|
|
|
environment[k1].match && environment[k1].match(/({{)/) ? 1 : -1
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
/*
|
|
|
|
* If we're overwriting a string, try to render it first with the base as
|
|
|
|
* a context. This allows for the following scenario:
|
|
|
|
*
|
|
|
|
* base: { base_url: 'google.com' }
|
|
|
|
* obj: { base_url: '{{ base_url }}/foo' }
|
|
|
|
* final: { base_url: 'google.com/foo' }
|
|
|
|
*
|
|
|
|
* A regular Object.assign would yield { base_url: '{{ base_url }}/foo' } and the
|
|
|
|
* original base_url of google.com would be lost.
|
|
|
|
*/
|
|
|
|
if (typeof renderContext[key] === 'string') {
|
2017-07-11 01:05:54 +00:00
|
|
|
renderContext[key] = await render(
|
|
|
|
environment[key],
|
|
|
|
renderContext,
|
|
|
|
null,
|
|
|
|
KEEP_ON_ERROR,
|
|
|
|
'Environment'
|
|
|
|
);
|
2017-05-15 20:55:05 +00:00
|
|
|
} else {
|
|
|
|
renderContext[key] = environment[key];
|
|
|
|
}
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:51:25 +00:00
|
|
|
// Render the context with itself to fill in the rest.
|
2017-02-07 16:09:12 +00:00
|
|
|
let finalRenderContext = renderContext;
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Render recursive references.
|
2017-02-07 16:09:12 +00:00
|
|
|
for (let i = 0; i < 3; i++) {
|
2017-07-11 01:05:54 +00:00
|
|
|
finalRenderContext = await render(
|
|
|
|
finalRenderContext,
|
|
|
|
finalRenderContext,
|
|
|
|
null,
|
|
|
|
KEEP_ON_ERROR,
|
|
|
|
'Environment'
|
|
|
|
);
|
2017-02-07 16:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return finalRenderContext;
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
/**
|
|
|
|
* Recursively render any JS object and return a new one
|
2017-05-15 20:55:05 +00:00
|
|
|
* @param {*} obj - object to render
|
2017-02-20 18:32:27 +00:00
|
|
|
* @param {object} context - context to render against
|
2017-03-28 22:45:23 +00:00
|
|
|
* @param blacklistPathRegex - don't render these paths
|
2017-07-11 01:05:54 +00:00
|
|
|
* @param errorMode - how to handle errors
|
|
|
|
* @param name - name to include in error message
|
2017-02-20 18:32:27 +00:00
|
|
|
* @return {Promise.<*>}
|
|
|
|
*/
|
2017-07-11 01:05:54 +00:00
|
|
|
export async function render (obj, context = {}, blacklistPathRegex = null, errorMode = THROW_ON_ERROR, name = '') {
|
2017-05-15 20:55:05 +00:00
|
|
|
// Make a deep copy so no one gets mad :)
|
|
|
|
const newObj = clone(obj);
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
async function next (x, path = name) {
|
2017-03-28 22:45:23 +00:00
|
|
|
if (blacklistPathRegex && path.match(blacklistPathRegex)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
const asStr = Object.prototype.toString.call(x);
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
// Leave these types alone
|
|
|
|
if (
|
2017-05-15 20:55:05 +00:00
|
|
|
asStr === '[object Date]' ||
|
|
|
|
asStr === '[object RegExp]' ||
|
|
|
|
asStr === '[object Error]' ||
|
|
|
|
asStr === '[object Boolean]' ||
|
|
|
|
asStr === '[object Number]' ||
|
|
|
|
asStr === '[object Null]' ||
|
|
|
|
asStr === '[object Undefined]'
|
2017-02-20 18:32:27 +00:00
|
|
|
) {
|
|
|
|
// Do nothing to these types
|
2017-05-15 20:55:05 +00:00
|
|
|
} else if (asStr === '[object String]') {
|
2017-02-20 18:32:27 +00:00
|
|
|
try {
|
2017-07-11 01:05:54 +00:00
|
|
|
x = await templating.render(x, {context, path});
|
2017-05-15 20:55:05 +00:00
|
|
|
|
|
|
|
// If the variable outputs a tag, render it again. This is a common use
|
|
|
|
// case for environment variables:
|
|
|
|
// {{ foo }} => {% uuid 'v4' %} => dd265685-16a3-4d76-a59c-e8264c16835a
|
|
|
|
if (x.includes('{%')) {
|
2017-07-11 01:05:54 +00:00
|
|
|
x = await templating.render(x, {context, path});
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
} catch (err) {
|
2017-07-11 01:05:54 +00:00
|
|
|
if (errorMode !== KEEP_ON_ERROR) {
|
|
|
|
throw err;
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
} else if (Array.isArray(x)) {
|
|
|
|
for (let i = 0; i < x.length; i++) {
|
2017-03-28 22:45:23 +00:00
|
|
|
x[i] = await next(x[i], `${path}[${i}]`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
} else if (typeof x === 'object') {
|
2017-03-28 22:45:23 +00:00
|
|
|
// Don't even try rendering disabled objects
|
2017-05-15 20:55:05 +00:00
|
|
|
// Note, this logic probably shouldn't be here, but w/e for now
|
2017-03-28 22:45:23 +00:00
|
|
|
if (x.disabled) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const keys = Object.keys(x);
|
|
|
|
for (const key of keys) {
|
2017-03-28 22:45:23 +00:00
|
|
|
const pathPrefix = path ? path + '.' : '';
|
|
|
|
x[key] = await next(x[key], `${pathPrefix}${key}`);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
2016-11-29 20:55:31 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
return next(newObj);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
export async function getRenderContext (request, environmentId, ancestors = null) {
|
2017-03-23 22:10:42 +00:00
|
|
|
if (!request) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-02-13 08:12:02 +00:00
|
|
|
if (!ancestors) {
|
2017-04-21 04:30:52 +00:00
|
|
|
ancestors = await db.withAncestors(request, [
|
|
|
|
models.requestGroup.type,
|
|
|
|
models.workspace.type
|
|
|
|
]);
|
2017-02-13 08:12:02 +00:00
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2017-02-13 08:12:02 +00:00
|
|
|
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
|
2016-11-10 01:15:27 +00:00
|
|
|
const rootEnvironment = await models.environment.getOrCreateForWorkspace(workspace);
|
|
|
|
const subEnvironment = await models.environment.getById(environmentId);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Add meta data helper function
|
|
|
|
const baseContext = {};
|
|
|
|
baseContext.getMeta = () => ({
|
2017-06-09 01:10:12 +00:00
|
|
|
requestId: request._id,
|
|
|
|
workspaceId: workspace._id
|
|
|
|
});
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Generate the context we need to render
|
|
|
|
const context = await buildRenderContext(
|
|
|
|
ancestors,
|
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment,
|
|
|
|
baseContext
|
|
|
|
);
|
|
|
|
|
2017-06-09 01:10:12 +00:00
|
|
|
return context;
|
2017-02-13 08:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRenderedRequest (request, environmentId) {
|
2017-04-21 04:30:52 +00:00
|
|
|
const ancestors = await db.withAncestors(request, [
|
|
|
|
models.requestGroup.type,
|
|
|
|
models.workspace.type
|
|
|
|
]);
|
2017-02-13 08:12:02 +00:00
|
|
|
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
|
|
|
|
const cookieJar = await models.cookieJar.getOrCreateForWorkspace(workspace);
|
|
|
|
|
|
|
|
const renderContext = await getRenderContext(request, environmentId, ancestors);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
|
|
|
// Render all request properties
|
2017-05-15 20:55:05 +00:00
|
|
|
const renderedRequest = await render(
|
2017-03-28 22:45:23 +00:00
|
|
|
request,
|
|
|
|
renderContext,
|
|
|
|
request.settingDisableRenderRequestBody ? /^body.*/ : null
|
|
|
|
);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
// Remove disabled params
|
|
|
|
renderedRequest.parameters = renderedRequest.parameters.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled headers
|
|
|
|
renderedRequest.headers = renderedRequest.headers.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled body params
|
|
|
|
if (renderedRequest.body && Array.isArray(renderedRequest.body.params)) {
|
|
|
|
renderedRequest.body.params = renderedRequest.body.params.filter(p => !p.disabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disabled authentication
|
|
|
|
if (renderedRequest.authentication && renderedRequest.authentication.disabled) {
|
2017-03-03 20:09:08 +00:00
|
|
|
renderedRequest.authentication = {};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Default the proto if it doesn't exist
|
|
|
|
renderedRequest.url = setDefaultProtocol(renderedRequest.url);
|
|
|
|
|
|
|
|
// Add the yummy cookies
|
2017-03-23 22:10:42 +00:00
|
|
|
// TODO: Don't deal with cookies in here
|
2016-10-02 20:57:00 +00:00
|
|
|
renderedRequest.cookieJar = cookieJar;
|
|
|
|
|
|
|
|
return renderedRequest;
|
|
|
|
}
|