From 2033c3e66976de1855a5746f33c745bf76492b03 Mon Sep 17 00:00:00 2001 From: Erica Windisch <73207+ewindisch@users.noreply.github.com> Date: Wed, 14 Feb 2018 18:13:07 +0000 Subject: [PATCH] feat: Support sls default custom mapping template Adds support for the serverless framework legacy `lambda` integration which uses custom-mapping templates instead of the now-default lambda-proxy. This input type uses the ApiGateway event type fields as the meaning is similar, although only a subset of those fields are supported by this event type. Signed-off-by: Erica Windisch <73207+ewindisch@users.noreply.github.com> Co-authored-by: Corey Light --- src/__snapshots__/index.test.js.snap | 14 ++++++ src/eventSamples/index.js | 12 +++++- src/eventSamples/slsIntegrationLambda.js | 50 ++++++++++++++++++++++ src/plugins/index.js | 12 +++++- src/plugins/slsIntegrationLambda.js | 54 ++++++++++++++++++++++++ src/util/constants.js | 4 ++ src/util/eventType.js | 2 + src/util/logFromKeys.js | 2 +- 8 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/eventSamples/slsIntegrationLambda.js create mode 100644 src/plugins/slsIntegrationLambda.js create mode 100644 src/util/constants.js diff --git a/src/__snapshots__/index.test.js.snap b/src/__snapshots__/index.test.js.snap index bf6ab6b..68f0517 100644 --- a/src/__snapshots__/index.test.js.snap +++ b/src/__snapshots__/index.test.js.snap @@ -74,6 +74,20 @@ Object { } `; +exports[`slsIntegrationLambda reports snapshot 1`] = ` +Object { + "@iopipe/event-info.apiGateway.headers.X-Amz-Cf-Id": "l06CAg2QsrALeQcLAUSxGXbm8lgMoMIhR2AjKa4AiKuaVnnGsOFy5g==", + "@iopipe/event-info.apiGateway.headers.X-Amzn-Trace-Id": "Root=1-5970ef3e249c0321b2eef14aa513ae", + "@iopipe/event-info.apiGateway.httpMethod": "GET", + "@iopipe/event-info.apiGateway.requestContext.accountId": "1234", + "@iopipe/event-info.apiGateway.requestContext.httpMethod": "GET", + "@iopipe/event-info.apiGateway.requestContext.identity.userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", + "@iopipe/event-info.apiGateway.requestContext.stage": "dev", + "@iopipe/event-info.eventType": "apiGateway", + "@iopipe/event-info.eventType.source": "slsIntegrationLambda", +} +`; + exports[`sns reports snapshot 1`] = ` Object { "@iopipe/event-info.eventType": "sns", diff --git a/src/eventSamples/index.js b/src/eventSamples/index.js index 591b95a..2660ca6 100644 --- a/src/eventSamples/index.js +++ b/src/eventSamples/index.js @@ -1,4 +1,5 @@ import apiGateway from './apiGateway'; +import slsIntegrationLambda from './slsIntegrationLambda'; import cloudFront from './cloudFront'; import firehose from './firehose'; import kinesis from './kinesis'; @@ -6,4 +7,13 @@ import s3 from './s3'; import scheduled from './scheduled'; import sns from './sns'; -export { apiGateway, cloudFront, firehose, kinesis, s3, scheduled, sns }; +export { + apiGateway, + slsIntegrationLambda, + cloudFront, + firehose, + kinesis, + s3, + scheduled, + sns +}; diff --git a/src/eventSamples/slsIntegrationLambda.js b/src/eventSamples/slsIntegrationLambda.js new file mode 100644 index 0000000..799fb06 --- /dev/null +++ b/src/eventSamples/slsIntegrationLambda.js @@ -0,0 +1,50 @@ +export default { + body: {}, + method: 'GET', + principalId: '1234', + stage: 'dev', + cognitoPoolClaims: { + sub: '' + }, + enhancedAuthContext: {}, + headers: { + Accept: + 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6,zh-CN;q=0.4', + 'CloudFront-Forwarded-Proto': 'https', + 'CloudFront-Is-Desktop-Viewer': 'true', + 'CloudFront-Is-Mobile-Viewer': 'false', + 'CloudFront-Is-SmartTV-Viewer': 'false', + 'CloudFront-Is-Tablet-Viewer': 'false', + 'CloudFront-Viewer-Country': 'GB', + Host: 'ec5ycylws8.execute-api.us-east-1.amazonaws.com', + 'upgrade-insecure-requests': '1', + 'User-Agent': + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', + Via: '2.0 f165ce34daf8c0da182681179e863c24.cloudfront.net (CloudFront)', + 'X-Amz-Cf-Id': 'l06CAg2QsrALeQcLAUSxGXbm8lgMoMIhR2AjKa4AiKuaVnnGsOFy5g==', + 'X-Amzn-Trace-Id': 'Root=1-5970ef3e249c0321b2eef14aa513ae', + 'X-Forwarded-For': '94.117.120.169, 116.132.62.73', + 'X-Forwarded-Port': '443', + 'X-Forwarded-Proto': 'https' + }, + query: {}, + path: {}, + identity: { + cognitoIdentityPoolId: '', + accountId: '1234', + cognitoIdentityId: '', + caller: '', + apiKey: '', + sourceIp: '94.197.120.169', + accessKey: '', + cognitoAuthenticationType: '', + cognitoAuthenticationProvider: '', + userArn: '', + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', + user: '' + }, + stageVariables: {} +}; diff --git a/src/plugins/index.js b/src/plugins/index.js index 31f0b8d..d75c78c 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -1,4 +1,5 @@ import * as apiGateway from './apiGateway'; +import * as slsIntegrationLambda from './slsIntegrationLambda'; import * as cloudFront from './cloudFront'; import * as firehose from './firehose'; import * as kinesis from './kinesis'; @@ -6,4 +7,13 @@ import * as s3 from './s3'; import * as scheduled from './scheduled'; import * as sns from './sns'; -export { apiGateway, cloudFront, firehose, kinesis, s3, scheduled, sns }; +export { + apiGateway, + slsIntegrationLambda, + cloudFront, + firehose, + kinesis, + s3, + scheduled, + sns +}; diff --git a/src/plugins/slsIntegrationLambda.js b/src/plugins/slsIntegrationLambda.js new file mode 100644 index 0000000..dc25cad --- /dev/null +++ b/src/plugins/slsIntegrationLambda.js @@ -0,0 +1,54 @@ +import get from 'lodash.get'; + +import logFromKeys from '../util/logFromKeys'; +import { pluginName } from '../util/constants'; + +/* This module supports the "lambda" type integration + for the serverless framework which provides a + default custom mapping template. + + The sls mapping template is described here: + https://serverless.com/framework/docs/providers/aws/events/apigateway/#example-lambda-event-before-customization + */ + +const type = 'apiGateway'; +const source = 'slsIntegrationLambda'; + +const keys = ['body', 'method', 'principalId', 'stage']; + +const keysThatNeedValues = [ + 'identity.userAgent', + 'identity.sourceIp', + 'identity.accountId' +]; + +function eventType(event) { + if (typeof event === 'object') { + const keysArePresent = keys.every(s => s in event); + const valuesArePresent = + keysThatNeedValues + .map(k => { + return typeof get(event, k) !== 'undefined'; + }) + .filter(Boolean).length === keysThatNeedValues.length; + return keysArePresent && valuesArePresent ? source : false; + } + return false; +} + +const pluginKeyMapping = [ + ['method', 'httpMethod'], + ['identity.accountId', 'requestContext.accountId'], + ['method', 'requestContext.httpMethod'], + ['identity.userAgent', 'requestContext.identity.userAgent'], + ['stage', 'requestContext.stage'], + 'headers.X-Amz-Cf-Id', + 'headers.X-Amzn-Trace-Id' +]; + +function plugin(event, log) { + logFromKeys({ event, type, keys: pluginKeyMapping, log }); + log(`${pluginName}.eventType.source`, source); +} + +export { eventType, plugin }; diff --git a/src/util/constants.js b/src/util/constants.js new file mode 100644 index 0000000..dd28d36 --- /dev/null +++ b/src/util/constants.js @@ -0,0 +1,4 @@ +/*eslint-disable import/prefer-default-export*/ +const pluginName = '@iopipe/event-info'; + +export { pluginName }; diff --git a/src/util/eventType.js b/src/util/eventType.js index d3883f4..03d390c 100644 --- a/src/util/eventType.js +++ b/src/util/eventType.js @@ -4,6 +4,7 @@ import { eventType as firehose } from '../plugins/firehose'; import { eventType as kinesis } from '../plugins/kinesis'; import { eventType as s3 } from '../plugins/s3'; import { eventType as scheduled } from '../plugins/scheduled'; +import { eventType as slsIntegrationLambda } from '../plugins/slsIntegrationLambda'; import { eventType as sns } from '../plugins/sns'; // e === original lambda event @@ -15,6 +16,7 @@ export default function(e) { kinesis(e) || s3(e) || scheduled(e) || + slsIntegrationLambda(e) || sns(e) ); } diff --git a/src/util/logFromKeys.js b/src/util/logFromKeys.js index 0911d84..6f5e6b1 100644 --- a/src/util/logFromKeys.js +++ b/src/util/logFromKeys.js @@ -1,6 +1,6 @@ import get from 'lodash.get'; -const pluginName = '@iopipe/event-info'; +import { pluginName } from './constants'; export default function logFromWantedKeys({ event = {},