Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit 478377b

Browse files
authored
Merge pull request #11 from Snyk/develop
Merge latest changes from develop
2 parents 6d53205 + d6a234d commit 478377b

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016 Snyk Ltd.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,21 @@
99

1010
Around 14% of npm packages carry a known vulnerability, and new vulnerabilities are being [discovered every day](https://snyk.io/vuln). The Serverless Snyk plugin helps you keep your application secure by allowing you to check the Node.js dependencies in your [Serverless](https://github.com/serverless/serverless) app for known vulnerabilities using [Snyk](https://snyk.io).
1111

12+
[Read more about Serverless security and how vulnerable open source packages affect it on the Snyk blog.](https://snyk.io/blog/Serverless-Security-Vulnerabilities/)
13+
1214
For Serverless v1 only.
1315

1416
## How do I use it?
1517

16-
1. Install Snyk using npm
17-
18-
`npm install -g snyk`
19-
20-
2. Run `snyk wizard` in your project
21-
22-
To get started, you'll need to run `snyk wizard`, which will create a Snyk policy file as well as prompt you to fix any discovered vulnerabilities.
18+
1. Fix any existing vulnerable packages using [Snyk's GitHub integration](https://snyk.io/docs/github/) or [Snyk wizard](https://snyk.io/docs/using-snyk/#wizard).
2319

24-
3. Install the Serverless Snyk plugin using npm
20+
2. Install the Serverless Snyk plugin using npm
2521

2622
`npm install serverless-snyk --save`
2723

2824
You should now have Serverless Snyk installed and ready to go. You can confirm that the plugin has been installed by running `serverless` from your command line. You should see the Snyk plugin in the list of installed plugins.
2925

30-
4. Add the plugin to your Serverless config
26+
3. Add the plugin to your Serverless config
3127

3228
Next, you'll need to add the plugin to your `serverless.yml` file:
3329

@@ -36,7 +32,7 @@ For Serverless v1 only.
3632
- serverless-snyk
3733
```
3834
39-
5. Optional: Get a Snyk API Key
35+
4. Optional: Get a Snyk API Key
4036
4137
To avoid running into API rate limits and to enable [continuous monitoring](#continuous-monitoring), you'll need to [sign up for a Snyk account](https://snyk.io/auth/github) (if you don't have one already) and copy the API token from your dashboard. Detailed instructions on how to include the API token in your configuration are included in the [setting an API key](#setting-an-api-key) section below.
4238
@@ -57,7 +53,7 @@ snykAuth=YOUR_API_TOKEN
5753
```
5854
5955
### Deploying even if vulnerabilities are discovered
60-
By default, Serverless Snyk will stop serverless from deplying if Snyk detects any vulnerabilities in your dependencies. Each vulnerability will also be outputted, and you'll be prompted to run `snyk wizard` to address the issues.
56+
By default, Serverless Snyk will stop serverless from deploying if Snyk detects any vulnerabilities in your dependencies. Each vulnerability will also be outputted, and you'll be prompted to run `snyk wizard` to address the issues.
6157
6258
If you would like serverless to deploy your application even if Snyk finds known vulnerabilities, you can accomplish this by using a custom variable in your `serverless.yml` file.
6359
@@ -84,4 +80,4 @@ custom:
8480
8581
### License
8682
87-
MIT
83+
[License: Apache License, Version 2.0](LICENSE)

index.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const fs = require('fs');
34
const snyk = require('snyk/lib');
45
const chalk = require('chalk');
56
const BbPromise = require('bluebird');
@@ -11,6 +12,7 @@ class ServerlessSnyk {
1112
this.serverless = serverless;
1213
this.options = options;
1314
this.snyk = snyk;
15+
this.snykCLI = 'node ./node_modules/snyk/cli';
1416

1517
/* Defaults to be overriden in serverless.yml file */
1618
this.breakOnVuln = true;
@@ -45,7 +47,7 @@ class ServerlessSnyk {
4547
}
4648
auth() {
4749
if (process.env.snykAuth) {
48-
var cmd = 'snyk auth ' + process.env.snykAuth;
50+
var cmd = this.snykCLI + ' auth -api ' + process.env.snykAuth;
4951
try {
5052
var auth = execSync(cmd);
5153
this.serverless.cli.log(
@@ -64,7 +66,7 @@ class ServerlessSnyk {
6466
takeSnapshot() {
6567
if (this.monitor && this.authenticated) {
6668
try {
67-
var monitor = execSync('snyk monitor');
69+
var monitor = execSync(this.snykCLI + ' monitor');
6870
var output = monitor.toString().split('\n\n');
6971
for (var i = 0; i < output.length; i++) {
7072
if (output[i] != '\n') {
@@ -115,19 +117,29 @@ class ServerlessSnyk {
115117

116118
protect() {
117119
var path = process.cwd();
118-
var that = this;
119-
try {
120-
var protect = execSync('snyk protect');
121-
that.serverless.cli.log(
122-
protect.toString().replace(new RegExp('\r?\n','g'), '')
123-
);
124-
} catch (error) {
125-
if (error.stderr) {
126-
throw new that.serverless.classes.Error(error.stdout.toString());
120+
var self = this;
121+
122+
fs.exists(path + '/.snyk', function (exists) {
123+
if (exists) {
124+
try {
125+
var protect = execSync(self.snykCLI + ' protect');
126+
self.serverless.cli.log(
127+
protect.toString().replace(new RegExp('\r?\n','g'), '')
128+
);
129+
} catch (error) {
130+
if (error.stderr) {
131+
throw new self.serverless.classes.Error(error.stdout.toString());
132+
} else {
133+
throw error;
134+
}
135+
}
127136
} else {
128-
throw error;
137+
self.serverless.cli.log(
138+
'No Snyk protect policy was found. Skipping updates and patches.');
139+
self.serverless.cli.log(
140+
'Try running `snyk wizard` to define a Snyk policy.');
129141
}
130-
}
142+
});
131143
}
132144
}
133145

0 commit comments

Comments
 (0)