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

Bug fix: Allow multiple environment variables to be set to the same SSM parameter via name #326

Merged
merged 5 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 23 additions & 0 deletions src/middlewares/__tests__/ssm.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,27 @@ describe('🔒 SSM Middleware', () => {
done
})
})

test('It should allow multiple option names to point at the same SSM path', (done) => {
testScenario({
ssmMockResponses: [
{
Parameters: [{ Name: '/dev/service_name/key_name', Value: 'key-value' }]
}
],
middlewareOptions: {
names: {
KEY_NAME_1: '/dev/service_name/key_name',
KEY_NAME_2: '/dev/service_name/key_name'
}
},
callbacks: [
() => {
expect(process.env.KEY_NAME_1).toEqual('key-value')
expect(process.env.KEY_NAME_2).toEqual('key-value')
}
],
done
})
})
})
17 changes: 4 additions & 13 deletions src/middlewares/ssm.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ module.exports = opts => {
},
[]
)

const ssmParamNames = getSSMParamValues(options.names)
if (ssmParamNames.length) {
const ssmPromise = ssmInstance
Expand Down Expand Up @@ -149,7 +148,7 @@ const getTargetObjectToAssign = (handler, options) =>
options.setToContext ? handler.context : process.env

const getSSMParamValues = userParamsMap =>
Object.keys(userParamsMap).map(key => userParamsMap[key])
[...new Set(Object.keys(userParamsMap).map(key => userParamsMap[key]))]

/**
* Lazily load aws-sdk and initialize SSM constructor
Expand Down Expand Up @@ -191,11 +190,9 @@ const handleInvalidParams = ({ Parameters, InvalidParameters }) => {
* @return {Object} Merged object for assignment to target object
*/
const getParamsToAssignByName = (userParamsMap, ssmParams) => {
const ssmToUserParamsMap = invertObject(userParamsMap)

return ssmParams.reduce((aggregator, ssmParam) => {
aggregator[ssmToUserParamsMap[ssmParam.Name]] = ssmParam.Value
return aggregator
return Object.keys(userParamsMap).reduce((acc, key) => {
acc[key] = ssmParams.find(param => param.Name === userParamsMap[key]).Value
return acc
}, {})
}

Expand All @@ -218,9 +215,3 @@ const getParamsToAssignByPath = (
ssmParam.Value
return aggregator
}, {})

const invertObject = obj =>
Object.keys(obj).reduce((aggregator, key) => {
aggregator[obj[key]] = key
return aggregator
}, {})