/
docker-token.test.ts
255 lines (231 loc) · 6.04 KB
/
docker-token.test.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import * as tap from 'tap';
import * as cli from '../../src/cli/commands';
import { fakeServer } from './fake-server';
import * as sinon from 'sinon';
const { test } = tap;
const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
process.env.LOG_LEVEL = '0';
const apiKey = '123456789';
let oldkey;
let oldendpoint;
const server = fakeServer(BASE_API, apiKey);
// This import needs to come after the server init
// it causes the configured API url to be incorrect.
import * as plugins from '../../src/lib/plugins/index';
test('setup', async (t) => {
t.plan(3);
let key = await cli.config('get', 'api');
oldkey = key;
t.pass('existing user config captured: ' + oldkey);
key = await cli.config('get', 'endpoint');
oldendpoint = key;
t.pass('existing user endpoint captured: ' + oldendpoint);
await new Promise((resolve) => {
server.listen(port, resolve);
});
t.pass('started demo server');
t.end();
});
test('prime config', async (t) => {
await cli.config('unset', 'endpoint');
t.pass('endpoint removed');
await cli.config('unset', 'api');
t.pass('api key removed');
process.env.SNYK_DOCKER_TOKEN = 'docker-jwt-token';
t.pass('docker token set');
t.end();
});
test('`snyk test` with docker flag - docker token and no api key', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {},
},
t,
);
try {
await cli.test('foo:latest', {
docker: true,
});
const req = server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, 'docker-jwt/test-dep-graph', 'posts to correct url');
} catch (err) {
t.fail('did not expect exception to be thrown ' + err);
}
});
test('`snyk test` with docker flag - docker token and api key', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {},
},
t,
);
await cli.config('set', 'api=' + apiKey);
try {
await cli.test('foo:latest', {
docker: true,
});
const req = server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, 'test-dep-graph', 'posts to correct url');
} catch (err) {
t.fail('did not expect exception to be thrown ' + err);
}
await cli.config('unset', 'api');
t.end();
});
test('`snyk test` without docker flag - docker token and no api key', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {},
},
t,
);
try {
await cli.test('foo:latest', {
docker: false,
});
t.fail('expected MissingApiTokenError');
} catch (err) {
t.equal(err.name, 'MissingApiTokenError', 'should throw if not docker');
}
});
test('`snyk test` with docker flag - displays CTA', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {
name: 'docker-image',
dependencies: {
'apt/libapt-pkg5.0': {
version: '1.6.3ubuntu0.1',
dependencies: {
'bzip2/libbz2-1.0': {
version: '1.0.6-8.1',
},
},
},
'bzip2/libbz2-1.0': {
version: '1.0.6-8.1',
},
},
},
},
t,
);
const vulns = require('./fixtures/docker/find-result.json');
server.setNextResponse(vulns);
try {
await cli.test('foo:latest', {
docker: true,
});
} catch (err) {
const msg = err.message;
t.match(
msg,
'For more free scans that keep your images secure, sign up to Snyk at https://dockr.ly/3ePqVcp',
'displays docker CTA for scan with vulns',
);
}
});
test('`snyk test` with docker flag - does not display CTA', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {
name: 'docker-image',
dependencies: {
'apt/libapt-pkg5.0': {
version: '1.6.3ubuntu0.1',
dependencies: {
'bzip2/libbz2-1.0': {
version: '1.0.6-8.1',
},
},
},
'bzip2/libbz2-1.0': {
version: '1.0.6-8.1',
},
},
},
},
t,
);
const vulns = require('./fixtures/docker/find-result.json');
server.setNextResponse(vulns);
await cli.config('set', 'api=' + apiKey);
try {
await cli.test('foo:latest', {
docker: true,
});
} catch (err) {
const msg = err.message;
t.notMatch(
msg,
'For more free scans that keep your images secure, sign up to Snyk at https://dockr.ly/3ePqVcp',
'does not display docker CTA if API key was used',
);
}
await cli.config('unset', 'api');
t.end();
});
test('teardown', async (t) => {
t.plan(4);
delete process.env.SNYK_API;
delete process.env.SNYK_HOST;
delete process.env.SNYK_PORT;
delete process.env.SNYK_DOCKER_TOKEN;
t.notOk(process.env.SNYK_PORT, 'fake env values cleared');
await new Promise((resolve) => {
server.close(resolve);
});
t.pass('server shutdown');
if (!oldkey) {
await cli.config('unset', 'api');
} else {
await cli.config('set', 'api=' + oldkey);
}
if (oldendpoint) {
await cli.config('set', `endpoint=${oldendpoint}`);
t.pass('user endpoint restored');
} else {
t.pass('no endpoint');
}
t.pass('user config restored');
t.end();
});
function stubDockerPluginResponse(plugins, fixture: string | object, t) {
const plugin = {
async inspect() {
return typeof fixture === 'object' ? fixture : require(fixture);
},
};
const spyPlugin = sinon.spy(plugin, 'inspect');
const loadPlugin = sinon.stub(plugins, 'loadPlugin');
loadPlugin
.withArgs(sinon.match.any, sinon.match({ docker: true }))
.returns(plugin);
t.teardown(loadPlugin.restore);
return spyPlugin;
}