Skip to content

Commit

Permalink
Merge branch 'master' of github.com:blueflag/serverless-plugin-cloudf…
Browse files Browse the repository at this point in the history
…ormation-cross-region-variables
  • Loading branch information
thepont committed Apr 2, 2020
2 parents 2275195 + c58a994 commit 8f6caae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 222 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
lib
*.log
.idea
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-plugin-cloudformation-cross-region-variables",
"version": "1.0.7",
"version": "1.0.8",
"url": "https://github.com/blueflag/serverless-plugin-cloudformation-cross-region-variables",
"description": "Serverless plugin to get varibles from cross region cloudformation templates",
"main": "lib/index.js",
Expand Down
27 changes: 13 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@

var Observable = require('rxjs/Rx').Observable
const Observable = require('rxjs/Rx').Observable;

import {getValueSSMCR, getValueFromCF} from './awsVars';

const CF_PREFIX = 'cfcr'
const CF_SPLIT = /(cfcr):(.*?):(.*?):([0-9a-zA-Z]*)/
const CF_SPLIT_DOT = /(cfcr):(.*?)\.(.*?):([0-9a-zA-Z]*)/
const CF_PREFIX = 'cfcr';
const CF_SPLIT = /(cfcr):(.*?):(.*?):([0-9a-zA-Z\-_]*)/;
const CF_SPLIT_DOT = /(cfcr):(.*?)\.(.*?):([0-9a-zA-Z\-_]*)/;

const SSM_PREFIX = 'ssmcr'
const SSM_SPLIT = /(ssmcr):(.*?):([a-zA-Z0-9_.\-/]+)[~]?(true|false)?/
const SSM_PREFIX = 'ssmcr';
const SSM_SPLIT = /(ssmcr):(.*?):([a-zA-Z0-9_.\-/]+)[~]?(true|false)?/;




export default class ServerlessCFCrossRegionVariables {
constructor(serverless, options) {
this.resolvedValues = {}
const delegate = serverless.variables.getValueFromSource.bind(serverless.variables)
this.resolvedValues = {};
const delegate = serverless.variables.getValueFromSource.bind(serverless.variables);
serverless.variables.getValueFromSource = (variableString) => {
if(this.resolvedValues[variableString]){
return Promise.resolve(this.resolvedValues[variableString])
Expand All @@ -26,11 +25,11 @@ export default class ServerlessCFCrossRegionVariables {
if(split === null){
throw new Error(`Invalid syntax, must be cfcr:region:service:output got "${variableString}"`)
}
var [string, cfcr, region, stack, variable] = split
var [string, cfcr, region, stack, variable] = split;
return this._getValueFromCF(region, stack, variable, variableString)
}
}
else if (variableString.startsWith(`${SSM_PREFIX}:`)) {
var split = SSM_SPLIT.exec(variableString)
var split = SSM_SPLIT.exec(variableString);
if(split === null){
throw new Error(`Invalid syntax, must be ssmcr:region:varlocation got "${variableString}"`)
}
Expand All @@ -43,7 +42,7 @@ export default class ServerlessCFCrossRegionVariables {

async _getValueSSMCR(region, variable, decrypt, variableString) {
try{
var value = await getValueSSMCR(region, variable, decrypt);
const value = await getValueSSMCR(region, variable, decrypt);
this.resolvedValues[variableString] = value;
return value;
} catch (e){
Expand All @@ -53,7 +52,7 @@ export default class ServerlessCFCrossRegionVariables {

async _getValueFromCF(region, stack, variable, variableString) {
try{
var value = await getValueFromCF(region, stack, variable);
const value = await getValueFromCF(region, stack, variable);
this.resolvedValues[variableString] = value;
return value;
} catch (e){
Expand Down
91 changes: 54 additions & 37 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,100 @@ import proxyquire from 'proxyquire'
import ServerlessCFVariables from '../src'

function buildSls(serverlessCFVariables) {
const sls = new Serverless()
sls.pluginManager.addPlugin(serverlessCFVariables)
sls.init()
const sls = new Serverless();
sls.pluginManager.addPlugin(serverlessCFVariables);
sls.init();
return sls

}

test('Variables are passed through', async t => {
const sls = buildSls(ServerlessCFVariables)
sls.service.custom.myVar = 'myVar'
sls.service.custom.myResoledVar = '${self:custom.myVar}' // eslint-disable-line
const sls = buildSls(ServerlessCFVariables);
sls.service.custom.myVar = 'myVar';
sls.service.custom.myResoledVar = '${self:custom.myVar}'; // eslint-disable-line

await sls.variables.populateService()
await sls.variables.populateService();
t.is(sls.service.custom.myResoledVar, 'myVar')
})
});

test('Correctly parses vars', async t => {
var serverlessCFVariables = proxyquire.noCallThru()('../src', {
const serverlessCFVariables = proxyquire.noCallThru()('../src', {
'./awsVars': {
getValueFromCF: (region, stack, variables) => {
return `region:${region},stack:${stack},variables:${variables}`
}
}
})
const sls = buildSls(serverlessCFVariables)
sls.service.custom.myResoledVar = '${cfcr:region:servicename:ServiceEndpoint}' // eslint-disable-lin
await sls.variables.populateService()
});
const sls = buildSls(serverlessCFVariables);
sls.service.custom.myResoledVar = '${cfcr:region:servicename:ServiceEndpoint}'; // eslint-disable-lin
await sls.variables.populateService();
t.is(sls.service.custom.myResoledVar, 'region:region,stack:servicename,variables:ServiceEndpoint')
})
});

test('Correctly throws an error on incorrect syntax', async t => {
const sls = buildSls(ServerlessCFVariables)
sls.service.custom.myResoledVar = '${cfcr:region:servicename}' // eslint-disable-lin
// await sls.variables.populateService()
await t.throws(() => sls.variables.populateService(), /Invalid syntax, must be cfcr:region:service:output got/)
})
test('Correctly parses vars with hyphen and underscore', async t => {
const serverlessCFVariables = proxyquire.noCallThru()('../src', {
'./awsVars': {
getValueFromCF: (region, stack, variables) => {
return `region:${region},stack:${stack},variables:${variables}`
}
}
});
const sls = buildSls(serverlessCFVariables);
sls.service.custom.myResoledVar = '${cfcr:region:servicename:ServiceEndpoint-stage_1}'; // eslint-disable-lin
await sls.variables.populateService();
t.is(sls.service.custom.myResoledVar, 'region:region,stack:servicename,variables:ServiceEndpoint-stage_1')
});

test.skip('Correctly throws an error on incorrect syntax', t => {
const sls = buildSls(ServerlessCFVariables);
sls.service.custom.myResoledVar = '${cfcr:region:servicename}'; // eslint-disable-lin

return sls.variables.populateService().then(
() => t.fail('Should have thrown'),
(res) => t.true(res.message.match(/Invalid syntax, must be cfcr:region:service:output got.*/))
);
});

test('Returns an error if var can`t be found', async t => {
var serverlessCFVariables = proxyquire.noCallThru()('../src', {
const serverlessCFVariables = proxyquire.noCallThru()('../src', {
'./awsVars': {
getValueFromCF: (region, stack, variables) => {
throw Error()
}
}
})
const sls = buildSls(serverlessCFVariables)
sls.service.custom.myResoledVar = '${cfcr:region:servicename:ServiceEndpointWillBeUndefined}' // eslint-disable-lin
});
const sls = buildSls(serverlessCFVariables);
sls.service.custom.myResoledVar = '${cfcr:region:servicename:ServiceEndpointWillBeUndefined}'; // eslint-disable-lin
let result = await sls.variables.populateService();
t.true(typeof sls.service.custom.myResoledVar === 'undefined');
})
});



test('Correctly parses ssm vars', async t => {
var serverlessCFVariables = proxyquire.noCallThru()('../src', {
const serverlessCFVariables = proxyquire.noCallThru()('../src', {
'./awsVars': {
getValueSSMCR: (region, variables) => {
return `region:${region},variables:${variables}`
}
}
})
const sls = buildSls(serverlessCFVariables)
sls.service.custom.myResoledVar = '${ssmcr:region:name}' // eslint-disable-lin
await sls.variables.populateService()
});
const sls = buildSls(serverlessCFVariables);
sls.service.custom.myResoledVar = '${ssmcr:region:name}'; // eslint-disable-lin
await sls.variables.populateService();
t.is(sls.service.custom.myResoledVar, 'region:region,variables:name')
})
});

test('Parses requests for decript', async t => {
var serverlessCFVariables = proxyquire.noCallThru()('../src', {
const serverlessCFVariables = proxyquire.noCallThru()('../src', {
'./awsVars': {
getValueSSMCR: (region, variables, decript) => {
return `region:${region},variables:${variables}:${decript}`
}
}
})
const sls = buildSls(serverlessCFVariables)
sls.service.custom.myResoledVar = '${ssmcr:region:name~true}' // eslint-disable-lin
await sls.variables.populateService()
});
const sls = buildSls(serverlessCFVariables);
sls.service.custom.myResoledVar = '${ssmcr:region:name~true}'; // eslint-disable-lin
await sls.variables.populateService();
t.is(sls.service.custom.myResoledVar, 'region:region,variables:name:true')
});
});
Loading

0 comments on commit 8f6caae

Please sign in to comment.