From 5fbaa517a7dedd9e843be5a929e3ff68bddc12b0 Mon Sep 17 00:00:00 2001 From: Karl Snyder Date: Tue, 1 May 2018 12:34:55 -0400 Subject: [PATCH 1/6] Add SSL Support --- README.md | 3 +++ index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc8fe0b..244e630 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,9 @@ custom: host: 'localhost' port: 6379 db: 12 + endpointAddressSSL: true + keyPath: contrib/secure/tls-key.pem + certPath: contrib/secure/tls-cert.pem ``` ### Using with serverless-offline plugin diff --git a/index.js b/index.js index cbb430d..a776668 100644 --- a/index.js +++ b/index.js @@ -18,10 +18,15 @@ const VERBOSE = typeof process.env.SLS_DEBUG !== 'undefined' const defaultOpts = { host: 'localhost', location: '.', - port: 1883, - httpPort: 1884, + port: 1883, // HTTP + securePort: 8883, // HTTPS + httpPort: 1884, // WS + httpsPort: 1885, // WSS noStart: false, - skipCacheInvalidation: false + skipCacheInvalidation: false, + endpointAddressSSL: false, + keyPath: '', + certPath: '' } const ascoltatoreOpts = { @@ -63,6 +68,10 @@ class ServerlessIotLocal { usage: 'http port for client connections over WebSockets. Default: 1884', shortcut: 'h' }, + httpsPort: { + usage: 'https port for client connections over WebSockets. Default: 1885', + shortcut: 's' + }, noStart: { shortcut: 'n', usage: 'Do not start local MQTT broker (in case it is already running)', @@ -71,6 +80,15 @@ class ServerlessIotLocal { usage: 'Tells the plugin to skip require cache invalidation. A script reloading tool like Nodemon might then be needed', shortcut: 'c', }, + endpointAddressSSL: { + usage: 'Instructs this plugin to use the SSL port for the endpoint address' + }, + keyPath: { + usage: 'Path to the private key file' + }, + certPath: { + usage: 'Path to the certificate file' + }, } } } @@ -108,6 +126,7 @@ class ServerlessIotLocal { _createMQTTBroker() { const { host, port, httpPort } = this.options + const { securePort, httpsPort, keyPath, certPath, endpointAddressSSL } = this.options const mosca = { host, @@ -119,6 +138,30 @@ class ServerlessIotLocal { } } + if (httpsPort) { + _.merge(mosca, { + secure: { + port: securePort + }, + https: { + host, + port: httpsPort, + bundle: true + }, + onlyHttp: false + }) + + if(keyPath && certPath) { + _.merge(mosca, { + secure: { + keyPath, + certPath + }, + allowNonSecure: true + }) + } + } + // For now we'll only support redis backend. const redisConfigOpts = this.options.redis; @@ -126,7 +169,8 @@ class ServerlessIotLocal { this.mqttBroker = createMQTTBroker(ascoltatore, mosca) - const endpointAddress = `${IP.address()}:${httpPort}` + const endpointPort = (endpointAddressSSL) ? httpsPort : httpPort; + const endpointAddress = `${host}:${endpointPort}` // prime AWS IotData import // this is necessary for below mock to work From f505a9b5d3dbe5cc39de2fb42b35448fb8917682 Mon Sep 17 00:00:00 2001 From: Karl Snyder Date: Tue, 1 May 2018 12:41:39 -0400 Subject: [PATCH 2/6] Add instructions to create a self-signed certificate --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 244e630..67da001 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,14 @@ plugins: - serverless-offline ``` +### Setting up a Self Signed Certificate + +When using with the AWS IoT Device SDK create a self-signed certificate using OpenSSL. + +```$ openssl genrsa -out tls-key.pem 2048 +$ openssl req -new -sha256 -key tls-key.pem -out my-csr.pem +$ openssl x509 -req -in my-csr.pem -signkey tls-key.pem -out tls-cert.pem``` + ## Todo - Improve support of AWS Iot SQL syntax From 191f6f188ae0e99cfab49a04eb1053cc10cc6085 Mon Sep 17 00:00:00 2001 From: karlsnyder0 Date: Tue, 14 May 2019 16:37:21 -0400 Subject: [PATCH 3/6] Update from base and mosca. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 32f9106..871303a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "aws-sdk-mock": "^1.7.0", "ip": "^1.1.5", "lodash": "^4.17.4", - "mosca": "^2.6.0", + "mosca": "^2.8.3", "mqtt": "^2.13.1", "mqtt-match": "^1.0.3", "redis": "^2.8.0" From 74aaa2f7de081c3282d5ae9301d40e7433fbdedc Mon Sep 17 00:00:00 2001 From: "justin.kruse" Date: Fri, 6 Dec 2019 09:02:31 -0600 Subject: [PATCH 4/6] update referenced file name due to change with serverless offline upgrade (#2) --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9d102fe..76e9512 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,19 @@ const evalInContext = require('./eval') const createMQTTBroker = require('./broker') // TODO: send PR to serverless-offline to export this const functionHelper = require('serverless-offline/src/functionHelper') -const createLambdaContext = require('serverless-offline/src/createLambdaContext') +let createLambdaContext; + +try { + createLambdaContext = require('serverless-offline/src/createLambdaContext'); +} catch (e) { + try { + // latest serverless-offline changed the file name + createLambdaContext = require('serverless-offline/src/LambdaContext'); + } catch (e2) { + throw new Error('Unable to find LambdaContext file'); + } +} + const VERBOSE = typeof process.env.SLS_DEBUG !== 'undefined' const defaultOpts = { host: 'localhost', From 207eb6e81719d9d2c9eefbc79d19b7ea3e8bc82f Mon Sep 17 00:00:00 2001 From: bryglen Date: Mon, 30 Dec 2019 22:22:41 +0800 Subject: [PATCH 5/6] added runtime --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 76e9512..9b63867 100644 --- a/index.js +++ b/index.js @@ -238,7 +238,7 @@ class ServerlessIotLocal { const fun = this._getFunction(key) const funName = key const servicePath = path.join(this.serverless.config.servicePath, location) - const funOptions = functionHelper.getFunctionOptions(fun, key, servicePath) + const funOptions = functionHelper.getFunctionOptions(fun, key, servicePath, runtime) this.debug(`funOptions ${JSON.stringify(funOptions, null, 2)} `) if (!fun.environment) { From f645a71d60a04e0c39d6c67ea1bd14a8c87e4734 Mon Sep 17 00:00:00 2001 From: bryglen Date: Wed, 1 Jan 2020 22:50:33 +0800 Subject: [PATCH 6/6] simplify lambda context --- index.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 9b63867..687feda 100644 --- a/index.js +++ b/index.js @@ -13,18 +13,7 @@ const evalInContext = require('./eval') const createMQTTBroker = require('./broker') // TODO: send PR to serverless-offline to export this const functionHelper = require('serverless-offline/src/functionHelper') -let createLambdaContext; - -try { - createLambdaContext = require('serverless-offline/src/createLambdaContext'); -} catch (e) { - try { - // latest serverless-offline changed the file name - createLambdaContext = require('serverless-offline/src/LambdaContext'); - } catch (e2) { - throw new Error('Unable to find LambdaContext file'); - } -} +const LambdaContext = require('serverless-offline/src/LambdaContext'); const VERBOSE = typeof process.env.SLS_DEBUG !== 'undefined' const defaultOpts = { @@ -349,7 +338,7 @@ class ServerlessIotLocal { return } - const lambdaContext = createLambdaContext(fn) + const lambdaContext = new LambdaContext(fn, {}) try { handler(event, lambdaContext, lambdaContext.done) } catch (error) {