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

backported SSM caching fix from 0.16.2 #225

Merged
merged 4 commits into from
Aug 23, 2018
Merged
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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"packages": [
"packages/*"
],
"version": "1.0.0-alpha.13"
"version": "1.0.0-alpha.14"
}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "middy-monorepo",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/cache",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Cache middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/core",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/do-not-wait-for-empty-event-loop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/do-not-wait-for-empty-event-loop",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Middleware for the middy framework that allows to easily disable the wait for empty event loop in a Lambda function",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/error-logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/error-logger",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Input and output logger middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-content-negotiation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-content-negotiation",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Http content negotiation middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-cors/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-cors",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-error-handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-error-handler",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Http error handler middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-event-normalizer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-event-normalizer",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Http event normalizer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-header-normalizer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-header-normalizer",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Http header normalizer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-json-body-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-json-body-parser",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Http JSON body parser middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-partial-response/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-partial-response",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Http partial response middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-urlencode-body-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-urlencode-body-parser",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Urlencode body parser middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/input-output-logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/input-output-logger",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Input and output logger middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/s3-key-normalizer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/s3-key-normalizer",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "S3 key normalizer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
35 changes: 22 additions & 13 deletions packages/ssm/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('🔒 SSM Middleware', () => {

function testScenario ({ssmMockResponse, ssmMockResponses, middlewareOptions, callbacks, done, delay = 0}) {
(ssmMockResponses || [ssmMockResponse]).forEach(ssmMockResponse => {
getParametersMock.mockReturnValueOnce({
getParametersMock.mockReturnValue({
promise: () => Promise.resolve(ssmMockResponse)
})

Expand Down Expand Up @@ -86,16 +86,18 @@ describe('🔒 SSM Middleware', () => {
},
middlewareOptions: {
names: {
KEY_NAME: '/dev/service_name/key-value'
KEY_NAME: '/dev/service_name/key_name'
},
cache: true
},
callbacks: [
() => {
expect(process.env.KEY_NAME).toEqual('key-value')
expect(getParametersMock).toBeCalled()
getParametersMock.mockReset()
getParametersMock.mockClear()
},
() => {
expect(process.env.KEY_NAME).toEqual('key-value')
expect(getParametersMock).not.toBeCalled()
}
],
Expand All @@ -120,11 +122,13 @@ describe('🔒 SSM Middleware', () => {
setToContext: true
},
callbacks: [
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).toBeCalledWith({'Names': ['/dev/service_name/secure_param'], 'WithDecryption': true})
getParametersMock.mockReset()
getParametersMock.mockClear()
},
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).not.toBeCalled()
}
],
Expand All @@ -146,7 +150,8 @@ describe('🔒 SSM Middleware', () => {
paramsLoaded: false
},
callbacks: [
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).toBeCalledWith({'Names': ['/dev/service_name/secure_param'], 'WithDecryption': true})
}
],
Expand All @@ -169,11 +174,13 @@ describe('🔒 SSM Middleware', () => {
paramsLoaded: false
},
callbacks: [
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).toBeCalledWith({'Names': ['/dev/service_name/secure_param'], 'WithDecryption': true})
getParametersMock.mockReset()
getParametersMock.mockClear()
},
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).toBeCalledWith({'Names': ['/dev/service_name/secure_param'], 'WithDecryption': true})
}
],
Expand All @@ -197,11 +204,13 @@ describe('🔒 SSM Middleware', () => {
paramsLoaded: false
},
callbacks: [
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).toBeCalledWith({'Names': ['/dev/service_name/secure_param'], 'WithDecryption': true})
getParametersMock.mockReset()
getParametersMock.mockClear()
},
() => {
(_, {context}) => {
expect(context.secureValue).toEqual('something-secure')
expect(getParametersMock).not.toBeCalled()
}
],
Expand Down
16 changes: 13 additions & 3 deletions packages/ssm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ module.exports = opts => {
cache: false,
cacheExpiryInMillis: undefined,
paramsLoaded: false,
paramsCache: undefined,
paramsLoadedAt: new Date(0)
}

const options = Object.assign({}, defaults, opts)

return {
before: (handler, next) => {
if (!shouldFetchFromParamStore(options)) return next()
if (!shouldFetchFromParamStore(options)) {
if (options.paramsCache) {
const targetParamsObject = getTargetObjectToAssign(handler, options)
options.paramsCache.forEach(object => {
Object.assign(targetParamsObject, object)
})
}
return next()
}

ssmInstance = ssmInstance || getSSMInstance(options.awsSdkOptions)

Expand All @@ -39,13 +48,13 @@ module.exports = opts => {

const ssmParamNames = getSSMParamValues(options.names)
if (ssmParamNames.length) {
ssmPromises.push(
const ssmPromise =
ssmInstance
.getParameters({ Names: ssmParamNames, WithDecryption: true })
.promise()
.then(handleInvalidParams)
.then(ssmResponse => getParamsToAssignByName(options.names, ssmResponse))
)
ssmPromises.push(ssmPromise)
}

return Promise.all(ssmPromises).then(objectsToMap => {
Expand All @@ -54,6 +63,7 @@ module.exports = opts => {
Object.assign(targetParamsObject, object)
})
options.paramsLoaded = true
options.paramsCache = objectsToMap
options.paramsLoadedAt = new Date()
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ssm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/ssm",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const createError = require('http-errors')
const Ajv = require('ajv')
const ajvKeywords = require('ajv-keywords')
const ajvLocalize = require('ajv-i18n')
const { deepEqual } = require('assert')
const { deepStrictEqual } = require('assert')

let ajv
let previousConstructorOptions
Expand Down Expand Up @@ -98,7 +98,7 @@ function shouldInitAjv (options) {

function areConstructorOptionsNew (options) {
try {
deepEqual(options, previousConstructorOptions)
deepStrictEqual(options, previousConstructorOptions)
} catch (e) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/validator",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Validator middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/warmup/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/warmup",
"version": "1.0.0-alpha.13",
"version": "1.0.0-alpha.14",
"description": "Warmup (cold start mitigation) middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down