Repository: dherault/serverless-offline
Pull request #608 (2019-02-25 14:38:43)

Add environment variables
I have some situations when I want to customize some of the properties in the event context. Like `cognitoIdentyPoolId` and others.

I don't like the option of passing these params in headers since it requires to change the actual app code while using environment variable allow us to keep app code untouched.


Patch:
diff --git a/README.md b/README.md
index 8bdf3e87..1924919b 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,7 @@ This plugin is updated by its users, I just do maintenance and ensure that PRs a
 * [Custom authorizers](#custom-authorizers)
 * [Remote authorizers](#remote-authorizers)
 * [Custom headers](#custom-headers)
+* [Environment variables](#environment-variables)
 * [AWS API Gateway features](#aws-api-gateway-features)
 * [Usage with Webpack](#usage-with-webpack)
 * [Velocity nuances](#velocity-nuances)
@@ -164,6 +165,21 @@ You are able to use some custom headers in your request to gain more control ove
 
 By doing this you are now able to change those values using a custom header. This can help you with easier authentication or retrieving the userId from a `cognitoAuthenticationProvider` value.
 
+## Environment variables
+You are able to use environmnet variables to customize identity params in event context.
+
+| Environment Variable | Event key |
+|----------------------|-----------|
+| SLS_COGNITO_IDENTITY_POOL_ID | event.requestContext.identity.cognitoIdentityPoolId |
+| SLS_ACCOUNT_ID | event.requestContext.identity.accountId |
+| SLS_COGNITO_IDENTITY_ID | event.requestContext.identity.cognitoIdentityId |
+| SLS_CALLER | event.requestContext.identity.caller |
+| SLS_API_KEY | event.requestContext.identity.apiKey |
+| SLS_COGNITO_AUTHENTICATION_TYPE | event.requestContext.identity.cognitoAuthenticationType |
+| SLS_COGNITO_AUTHENTICATION_PROVIDER | event.requestContext.identity.cognitoAuthenticationProvider |
+
+You can use [serverless-dotenv-plugin](https://github.com/colynb/serverless-dotenv-plugin) to load environment variables from your `.env` file.
+
 ## AWS API Gateway Features
 
 ### Velocity Templates
diff --git a/src/createLambdaProxyContext.js b/src/createLambdaProxyContext.js
index 90c17f2f..d2adb316 100644
--- a/src/createLambdaProxyContext.js
+++ b/src/createLambdaProxyContext.js
@@ -78,14 +78,14 @@ module.exports = function createLambdaProxyContext(request, options, stageVariab
       stage: options.stage,
       requestId: `offlineContext_requestId_${utils.randomId()}`,
       identity: {
-        cognitoIdentityPoolId: 'offlineContext_cognitoIdentityPoolId',
-        accountId: 'offlineContext_accountId',
-        cognitoIdentityId: request.headers['cognito-identity-id'] || 'offlineContext_cognitoIdentityId',
-        caller: 'offlineContext_caller',
-        apiKey: 'offlineContext_apiKey',
+        cognitoIdentityPoolId: process.env.SLS_COGNITO_IDENTITY_POOL_ID || 'offlineContext_cognitoIdentityPoolId',
+        accountId: process.env.SLS_ACCOUNT_ID || 'offlineContext_accountId',
+        cognitoIdentityId: request.headers['cognito-identity-id'] || process.env.SLS_COGNITO_IDENTITY_ID || 'offlineContext_cognitoIdentityId',
+        caller: process.env.SLS_CALLER || 'offlineContext_caller',
+        apiKey: process.env.SLS_API_KEY || 'offlineContext_apiKey',
         sourceIp: request.info.remoteAddress,
-        cognitoAuthenticationType: 'offlineContext_cognitoAuthenticationType',
-        cognitoAuthenticationProvider: request.headers['cognito-authentication-provider'] || 'offlineContext_cognitoAuthenticationProvider',
+        cognitoAuthenticationType: process.env.SLS_COGNITO_AUTHENTICATION_TYPE || 'offlineContext_cognitoAuthenticationType',
+        cognitoAuthenticationProvider: request.headers['cognito-authentication-provider'] || process.env.SLS_COGNITO_AUTHENTICATION_PROVIDER || 'offlineContext_cognitoAuthenticationProvider',
         userArn: 'offlineContext_userArn',
         userAgent: request.headers['user-agent'] || '',
         user: 'offlineContext_user',


Test patch:
diff --git a/test/unit/createLambdaProxyContextTest.js b/test/unit/createLambdaProxyContextTest.js
index d4cdae30..4b156cfb 100644
--- a/test/unit/createLambdaProxyContextTest.js
+++ b/test/unit/createLambdaProxyContextTest.js
@@ -451,4 +451,55 @@ describe('createLambdaProxyContext', () => {
       expect(lambdaProxyContext.headers['cognito-authentication-provider']).to.eq(testId);
     });
   });
+
+  context('with environment variables', () => {
+    const requestBuilder = new RequestBuilder('GET', '/fn1');
+    const request = requestBuilder.toObject();
+
+    let lambdaProxyContext;
+
+    before(() => {
+      process.env.SLS_COGNITO_IDENTITY_POOL_ID = 'customCognitoIdentityPoolId';
+      process.env.SLS_ACCOUNT_ID = 'customAccountId';
+      process.env.SLS_COGNITO_IDENTITY_ID = 'customCognitoIdentityId';
+      process.env.SLS_CALLER = 'customCaller';
+      process.env.SLS_API_KEY = 'customApiKey';
+      process.env.SLS_COGNITO_AUTHENTICATION_TYPE = 'customCognitoAuthenticationType';
+      process.env.SLS_COGNITO_AUTHENTICATION_PROVIDER = 'customCognitoAuthenticationProvider';
+      
+      lambdaProxyContext = createLambdaProxyContext(request, options, stageVariables);
+    });
+
+    it('should have the expected cognitoIdentityPoolId', () => {
+      expect(lambdaProxyContext.requestContext.identity.cognitoIdentityPoolId)
+        .to.eq('customCognitoIdentityPoolId');
+    });
+
+    it('should have the expected accountId', () => {
+      expect(lambdaProxyContext.requestContext.identity.accountId).to.eq('customAccountId');
+    });
+
+    it('should have the expected cognitoIdentityId', () => {
+      expect(lambdaProxyContext.requestContext.identity.cognitoIdentityId)
+        .to.eq('customCognitoIdentityId');
+    });
+
+    it('should have the expected caller', () => {
+      expect(lambdaProxyContext.requestContext.identity.caller).to.eq('customCaller');
+    });
+
+    it('should have the expected apiKey', () => {
+      expect(lambdaProxyContext.requestContext.identity.apiKey).to.eq('customApiKey');
+    });
+
+    it('should have the expected cognitoAuthenticationType', () => {
+      expect(lambdaProxyContext.requestContext.identity.cognitoAuthenticationType)
+        .to.eq('customCognitoAuthenticationType');
+    });
+
+    it('should have the expected cognitoAuthenticationProvider', () => {
+      expect(lambdaProxyContext.requestContext.identity.cognitoAuthenticationProvider)
+        .to.eq('customCognitoAuthenticationProvider');
+    });
+  });
 });