From 1f20d0c4d3b7127c0d56aaf70bc70814c763d723 Mon Sep 17 00:00:00 2001 From: Daniel Schep Date: Wed, 1 May 2019 15:36:39 -0400 Subject: [PATCH 1/4] PLAT-916 - changes for inclusion by default in sls frmwk! --- src/lib/plugin.js | 55 ++++++++++++++++++++++++++---------------- src/lib/plugin.test.js | 3 ++- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/lib/plugin.js b/src/lib/plugin.js index 38c669e1..e1e421e4 100644 --- a/src/lib/plugin.js +++ b/src/lib/plugin.js @@ -25,38 +25,45 @@ class ServerlessEnterprisePlugin { const user = getLoggedInUser() const currentCommand = sls.processedInput.commands[0] - // Skip everything if user is not logged in and not trying to log in or out... - if ( - !user && - (currentCommand !== 'login' && - currentCommand !== 'logout' && - !process.env.SERVERLESS_ACCESS_KEY) - ) { - const errorMessage = `You are not currently logged in. To log in, use: $ serverless login` - console.log('') // eslint-disable-line - sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line - throw new Error(errorMessage) // eslint-disable-line + // default hook, only applies if user isn't using SFE. gets overridden if they are + this.hooks = { + 'after:aws:deploy:finalize:cleanup': () => + sls.cli.log( + 'Run `serverless login` and deploy again to explore, monitor, secure your serverless project for free.', + 'Serverless Enterprise' + ) } - // Defaults - this.sls = sls - this.state = {} // Useful for storing data across hooks - this.state.secretsUsed = new Set() - this.provider = this.sls.getProvider('aws') - // Check if Enterprise is configured const missing = [] - if (!this.sls.service.tenant) { + if (!sls.service.tenant) { missing.push('tenant') } - if (!this.sls.service.app) { + if (!sls.service.app) { missing.push('app') } - if (!this.sls.service.service) { + if (!sls.service.service) { missing.push('service') } + + // Skip everything if user is not logged in and not trying to log in or out... + if ( + !user && + (currentCommand !== 'login' && + currentCommand !== 'logout' && + !process.env.SERVERLESS_ACCESS_KEY) + ) { + if (missing.includes('tenant') && missing.includes('app')) { + return // user isn't trying to use SFE + } + const errorMessage = `You are not currently logged in. To log in, use: $ serverless login` + console.log('') // eslint-disable-line + sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line + throw new Error(errorMessage) // eslint-disable-line + + } if (missing.length > 0) { - this.sls.cli.log( + sls.cli.log( `Warning: The Enterprise Plugin requires a ${missing .map((opt) => `"${opt}"`) .join(', ')} property in your "serverless.yml" and will not work without it.`, @@ -65,6 +72,12 @@ class ServerlessEnterprisePlugin { return } + // Defaults + this.sls = sls + this.state = {} // Useful for storing data across hooks + this.state.secretsUsed = new Set() + this.provider = this.sls.getProvider('aws') + // Add commands this.commands = { login: { diff --git a/src/lib/plugin.test.js b/src/lib/plugin.test.js index 10d5d02f..dc70930c 100644 --- a/src/lib/plugin.test.js +++ b/src/lib/plugin.test.js @@ -10,6 +10,7 @@ import { saveDeployment } from './deployment' import { hookIntoVariableGetter } from './variables' import { generate } from './generateEvent' import injectLogsIamRole from './injectLogsIamRole' +import _ from 'lodash' afterAll(() => jest.restoreAllMocks()) @@ -97,7 +98,7 @@ describe('plugin', () => { }) it('construct requires tenant', () => { - const slsClone = Object.assign({}, sls) + const slsClone = _.cloneDeep(sls) delete slsClone.service.tenant const instance = new ServerlessEnterprisePlugin(slsClone) // eslint-disable-line expect(slsClone.getProvider).toBeCalledWith('aws') From 23669d7617517acfeddc4241a2a14f16b48306ff Mon Sep 17 00:00:00 2001 From: Daniel Schep Date: Wed, 1 May 2019 16:21:20 -0400 Subject: [PATCH 2/4] load full plugin on login&logout even if missing config bits --- src/lib/plugin.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/plugin.js b/src/lib/plugin.js index e1e421e4..6c7b87fe 100644 --- a/src/lib/plugin.js +++ b/src/lib/plugin.js @@ -60,9 +60,8 @@ class ServerlessEnterprisePlugin { console.log('') // eslint-disable-line sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line throw new Error(errorMessage) // eslint-disable-line - } - if (missing.length > 0) { + if (currentCommand !== 'login' && currentCommand !== 'logout' && missing.length > 0) { sls.cli.log( `Warning: The Enterprise Plugin requires a ${missing .map((opt) => `"${opt}"`) From 0933b11b6011feb3c7ae7a1301caa2cf16e09b1b Mon Sep 17 00:00:00 2001 From: Daniel Schep Date: Fri, 3 May 2019 08:16:51 -0400 Subject: [PATCH 3/4] better post-deploy hook for un-configured services --- src/lib/plugin.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib/plugin.js b/src/lib/plugin.js index 6c7b87fe..277ef38b 100644 --- a/src/lib/plugin.js +++ b/src/lib/plugin.js @@ -68,6 +68,18 @@ class ServerlessEnterprisePlugin { .join(', ')} property in your "serverless.yml" and will not work without it.`, 'Serverless Enterprise' ) + // replace the default hook with a message about configuring sls enterprise + this.hooks = { + 'after:aws:deploy:finalize:cleanup': () => + sls.cli.log( + `Update your "serverless.yml" with ${missing + .map((opt) => `"${opt}"`) + .join( + ', ' + )} properties and deploy again to explore, monitor, secure your serverless project for free.`, + 'Serverless Enterprise' + ) + } return } From c524f9c3c6974687dda1647d8c8c3828e50d6ba6 Mon Sep 17 00:00:00 2001 From: Daniel Schep Date: Fri, 3 May 2019 11:05:24 -0400 Subject: [PATCH 4/4] add node version requirement to pkg json --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 25f43637..12bcf67e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { "name": "@serverless/enterprise-plugin", "version": "0.4.1", + "engines": { + "node": ">=6.0" + }, "description": "The Serverless Enterprise plugin", "main": "dist/index.js", "scripts": {