Skip to content

Commit

Permalink
backported SSM caching fix from 0.16.2 (middyjs#225)
Browse files Browse the repository at this point in the history
* backported SSM caching fix from 0.16.2
  • Loading branch information
theburningmonk authored and benjifs committed May 21, 2020
1 parent 452bc3a commit 4353922
Show file tree
Hide file tree
Showing 22 changed files with 55 additions and 36 deletions.
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
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

0 comments on commit 4353922

Please sign in to comment.