This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
146 lines (136 loc) · 4.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const fs = require('fs');
const snyk = require('snyk/lib');
const chalk = require('chalk');
const BbPromise = require('bluebird');
const execSync = require('child_process').execSync;
require('dotenv').config({silent: true});
class ServerlessSnyk {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.snyk = snyk;
this.snykCLI = 'node ./node_modules/snyk/cli';
/* Defaults to be overriden in serverless.yml file */
this.breakOnVuln = true;
this.snykAuth = false;
this.monitor = true;
this.authenticated = false;
/* Pull in any custom snyk related variables */
var customs = this.serverless.service.custom;
if (customs && customs.snyk) {
for (var prop in customs.snyk) {
this[prop] = customs.snyk[prop];
}
}
this.commands = {
snyk: {
usage: 'Checks dependencies for known vulnerabilities using Snyk.io.',
lifecycleEvents: [
'testVulnerabilities',
],
},
};
this.hooks = {
'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.auth)
.then(this.protect)
.then(this.test)
.then(this.takeSnapshot),
};
}
auth() {
if (process.env.snykAuth) {
var cmd = this.snykCLI + ' auth -api ' + process.env.snykAuth;
try {
var auth = execSync(cmd);
this.serverless.cli.log(
auth.toString().replace(new RegExp('\r?\n','g'), '')
);
this.authenticated = true;
} catch (error) {
if (error.stderr) {
throw new this.serverless.classes.Error(error.stdout.toString());
} else {
throw error;
}
}
}
}
takeSnapshot() {
if (this.monitor && this.authenticated) {
try {
var monitor = execSync(this.snykCLI + ' monitor');
var output = monitor.toString().split('\n\n');
for (var i = 0; i < output.length; i++) {
if (output[i] != '\n') {
this.serverless.cli.log(output[i].replace('\n', ' '));
}
}
} catch (error) {
if (error.stderr) {
throw new this.serverless.classes.Error(error.stdout.toString());
} else {
throw error;
}
}
}
}
test() {
this.serverless.cli.log('Querying vulnerabilities database...');
var self = this;
return this.snyk.test('./').then(function (res) {
if (res.ok) {
self.serverless.cli.log('Snyk tested ' + res.dependencyCount
+ ' dependencies for known vulnerabilities, '
+ ' found 0 vulnerabilities, 0 vulnerable paths.')
} else {
res.vulnerabilities.forEach(function (vuln) {
var res = '';
var name = vuln.name + '@' + vuln.version;
var severity = vuln.severity[0].toUpperCase()
+ vuln.severity.slice(1);
res += chalk.red('✗ ' + severity + ' severity vulnerability found on '
+ name);
self.serverless.cli.consoleLog(res);
});
var msg = 'Snyk tested ' + res.dependencyCount
+ ' dependencies for known vulnerabilities,'
+ ' found ' + res.uniqueCount + ' vulnerabilities, '
+ res.summary + '. ';
msg += ' Run `snyk wizard` to resolve these issues';
if (self.breakOnVuln) {
throw new self.serverless.classes.Error(msg);
} else {
self.serverless.cli.log(msg);
}
}
});
}
protect() {
var path = process.cwd();
var self = this;
fs.exists(path + '/.snyk', function (exists) {
if (exists) {
try {
var protect = execSync(self.snykCLI + ' protect');
self.serverless.cli.log(
protect.toString().replace(new RegExp('\r?\n','g'), '')
);
} catch (error) {
if (error.stderr) {
throw new self.serverless.classes.Error(error.stdout.toString());
} else {
throw error;
}
}
} else {
self.serverless.cli.log(
'No Snyk protect policy was found. Skipping updates and patches.');
self.serverless.cli.log(
'Try running `snyk wizard` to define a Snyk policy.');
}
});
}
}
module.exports = ServerlessSnyk;