Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
ESD-3341: better objects handling for mappings (#43)
Browse files Browse the repository at this point in the history
* better objects handling for mappings

* updated versions
  • Loading branch information
zxan1285 authored and shawnmclean committed Jan 2, 2020
1 parent 205bdfa commit 91650c2
Show file tree
Hide file tree
Showing 9 changed files with 3,582 additions and 3,559 deletions.
7,053 changes: 3,537 additions & 3,516 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth0-deploy-extensions",
"version": "2.0.0",
"version": "3.0.1",
"description": "Auth0 Deployment Extensions",
"engines": {
"node": "5.9.0"
Expand Down
68 changes: 31 additions & 37 deletions server/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,20 @@ const getRulesFiles = (files) => {
return rules;
};

const extractFileContent = (item) => {
if (typeof item === 'string') {
try {
item = JSON.parse(item);
} catch (e) {
logger.info(`Cannot extract data from:\n${item}`);
}
const extractFileContent = (item = {}, mappings, asString) => {
const content = (typeof item === 'object') ? JSON.stringify(item) : item;
const converted = keywordReplace(content, mappings);

if (asString) {
return converted;
}

return item || {};
try {
return JSON.parse(converted);
} catch (e) {
logger.info(`Cannot extract data from:\n${item}\nError:${e}`);
return item;
}
};

const checkSessionLifetime = (data, property) => {
Expand All @@ -264,66 +268,56 @@ const checkSessionLifetime = (data, property) => {
}
};

const applyMappings = (item, mappings) => {
const result = {};
Object.keys(item).forEach((key) => {
if (Array.isArray(item[key])) {
result[key] = JSON.parse(keywordReplace(JSON.stringify(item[key]), mappings));
} else {
result[key] = typeof item[key] !== 'string' ? item[key] : keywordReplace(item[key], mappings);
}
});

return result;
};

const unifyItem = (item, type) => {
const unifyItem = (item, type, mappings) => {
switch (type) {
default:
case 'rules': {
const meta = extractFileContent(item.metadataFile);
const meta = extractFileContent(item.metadataFile, mappings);
const script = extractFileContent(item.scriptFile, mappings, true);
const { order = 0, enabled, stage = 'login_success' } = meta;

return ({ script: item.scriptFile, name: item.name, order, stage, enabled });
return ({ script, name: item.name, order, stage, enabled });
}
case 'pages': {
const meta = extractFileContent(item.metadataFile);
const meta = extractFileContent(item.metadataFile, mappings);
const html = extractFileContent(item.htmlFile, mappings, true);
const { enabled } = meta;

return ({ html: item.htmlFile, name: item.name, enabled });
return ({ html, name: item.name, enabled });
}

case 'emailTemplates': {
if (item.name === 'provider') return null;
const meta = extractFileContent(item.metadataFile);
const meta = extractFileContent(item.metadataFile, mappings);
const body = extractFileContent(item.htmlFile, mappings, true);

return ({ ...meta, body: item.htmlFile });
return ({ ...meta, body });
}
case 'roles':
case 'clientGrants':
case 'guardianFactors':
case 'guardianFactorTemplates':
case 'guardianFactorProviders':
case 'emailProvider': {
const data = extractFileContent(item.configFile);
const data = extractFileContent(item.configFile, mappings);

return ({ ...data });
}

case 'tenant': {
const data = extractFileContent(item.configFile);
const data = extractFileContent(item.configFile, mappings);
checkSessionLifetime(data, 'session_lifetime');
checkSessionLifetime(data, 'idle_session_lifetime');

return ({ ...data });
}

case 'databases': {
const settings = extractFileContent(item.settings);
const settings = extractFileContent(item.settings, mappings);
const customScripts = {};
const options = settings.options || {};

_.forEach(item.scripts, (script) => { customScripts[script.name] = script.scriptFile; });
_.forEach(item.scripts, (script) => { customScripts[script.name] = extractFileContent(script.scriptFile, mappings, true); });

if (item.scripts && item.scripts.length && options.enabledDatabaseCustomization !== false) {
options.customScripts = customScripts;
Expand All @@ -336,14 +330,14 @@ const unifyItem = (item, type) => {
case 'resourceServers':
case 'connections':
case 'clients': {
const meta = extractFileContent(item.metadataFile);
const data = extractFileContent(item.configFile);
const meta = extractFileContent(item.metadataFile, mappings);
const data = extractFileContent(item.configFile, mappings);

return ({ name: item.name, ...meta, ...data });
}

case 'rulesConfigs': {
const data = extractFileContent(item.configFile);
const data = extractFileContent(item.configFile, mappings);

return ({ key: item.name, value: data.value });
}
Expand All @@ -357,11 +351,11 @@ const unifyData = (assets, mappings) => {
result[type] = [];
if (Array.isArray(data)) {
_.forEach(data, (item) => {
const unified = unifyItem(applyMappings(item, mappings), type);
const unified = unifyItem(item, type, mappings);
if (unified) result[type].push(unified);
});
} else {
result[type] = unifyItem(applyMappings(data, mappings), type);
result[type] = unifyItem(data, type, mappings);
}
});

Expand Down
4 changes: 4 additions & 0 deletions tests/server/mocks/repo-data-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ module.exports = {
{
name: 'ClientTwo',
configFile: '{ "string":"Client Two Config", "array": [ ##ONE##, "##TWO##" ] }'
},
{
name: 'ClientThree',
configFile: { string: 'Client ##THREE## Config', array: [] }
}
],
clientGrants: [],
Expand Down
6 changes: 5 additions & 1 deletion tests/server/utils.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('unifyData', () => {
});

it('should unify mapped assets', (done) => {
utils.unifyData(assets.mapped, { MAP_ONE: 'first', MAP_TWO: 'second', ARR_MAP: [ 1, 2 ], ONE: 1, TWO: 'two' })
utils.unifyData(assets.mapped, { MAP_ONE: 'first', MAP_TWO: 'second', THREE: 'Three', ARR_MAP: [ 1, 2 ], ONE: 1, TWO: 'two' })
.then((unified) => {
expect(unified.rules[0].script).toEqual('Rule script with first');
expect(unified.rules[0].name).toEqual('Rule');
Expand All @@ -85,6 +85,10 @@ describe('unifyData', () => {
expect(unified.clients[1].string).toEqual('Client Two Config');
expect(unified.clients[1].array).toEqual([ 1, 'two' ]);

expect(unified.clients[2].name).toEqual('ClientThree');
expect(unified.clients[2].string).toEqual('Client Three Config');
expect(unified.clients[2].array).toEqual([]);

expect(unified.rulesConfigs).toNotExist();
expect(unified.emailProvider).toNotExist();
expect(unified.emailTemplates).toNotExist();
Expand Down
2 changes: 1 addition & 1 deletion webtask-templates/bitbucket.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Bitbucket Deployments",
"name": "auth0-bitbucket-deploy",
"version": "3.0.0",
"version": "3.0.1",
"preVersion": "2.10.0",
"author": "auth0",
"description": "This extension gives Auth0 customers the possibility to deploy Hosted Pages, Rules and Custom Database Connections from Bitbucket.",
Expand Down
2 changes: 1 addition & 1 deletion webtask-templates/github.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "GitHub Deployments",
"name": "auth0-github-deploy",
"version": "3.0.0",
"version": "3.0.1",
"preVersion": "2.10.0",
"author": "auth0",
"description": "This extension gives Auth0 customers the possibility to deploy Pages, Rules and Custom Database Connections from GitHub.",
Expand Down
2 changes: 1 addition & 1 deletion webtask-templates/gitlab.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "GitLab Deployments",
"name": "auth0-gitlab-deploy",
"version": "3.0.0",
"version": "3.0.1",
"preVersion": "2.11.0",
"author": "auth0",
"description": "This extension gives Auth0 customers the possibility to deploy Hosted Pages, Rules and Custom Database Connections from GitLab.",
Expand Down
2 changes: 1 addition & 1 deletion webtask-templates/visualstudio.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Visual Studio Team Services Deployments",
"name": "auth0-visualstudio-deploy",
"version": "3.0.0",
"version": "3.0.1",
"preVersion": "2.9.0",
"author": "auth0",
"description": "This extension gives Auth0 customers the possibility to deploy Hosted Pages, Rules and Custom Database Connections from Visual Studio Team Services.",
Expand Down

0 comments on commit 91650c2

Please sign in to comment.